-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzep.ts
177 lines (155 loc) · 4.96 KB
/
zep.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Zep example to test if zep is working in the stack
*
* Usage:
* ```bash
* ZEP_API_SECRET=your-zep-api-key deno examples/zep/zep.ts
* ```
*
* See https://github.com/getzep/zep-js/blob/oss/examples/memory/memory_example.ts
*
* Zep Community Edition SDK examples are a mess with frequently broken or
* incorrect code online. For best source of truth, check the actual code in
* `node_modules/@getzep/zep-js/examples`
*
* For deno on macOS, open `~/Library/Caches/deno/npm/registry.npmjs.org/@getzep/zep-js`
* and look at the `examples` folder.
*/
import { ZepClient } from 'npm:@getzep/zep-js'
import type { CreateUserRequest, Message } from 'npm:@getzep/zep-js/api'
import { v4 as uuidv4 } from 'npm:uuid'
import { history } from './history.ts'
const API_KEY = Deno.env.get('ZEP_API_SECRET')
const BASE_URL = 'http://localhost:8010'
console.log('API_KEY', API_KEY)
if (!API_KEY) {
throw new Error('ZEP_API_SECRET is not set')
}
function sleep(ms: number) {
const date = Date.now()
let currentDate = 0
do {
currentDate = Date.now()
} while (currentDate - date < ms)
}
async function main() {
// Create a zep client
const client = new ZepClient({ apiKey: API_KEY as string, baseUrl: BASE_URL })
// Create a user
const userId = uuidv4()
const userRequest: CreateUserRequest = {
userId: `amy${userId}`,
metadata: { role: 'admin' },
email: '[email protected]',
firstName: 'Amy',
lastName: 'Wu',
}
const user = await client.user.add(userRequest)
console.debug('Created user ', user)
// Example session ID
const sessionID = uuidv4()
// Add session associated with the above user
try {
await client.memory.addSession({
sessionId: sessionID,
metadata: { foo: 'bar' },
userId: user.userId,
})
console.debug('Adding new session ', sessionID)
} catch (error) {
console.debug('Got error:', error)
}
// Get session
try {
const session = await client.memory.getSession(sessionID)
console.debug('Retrieved session ', session)
} catch (error) {
console.debug('Got error:', error)
}
// Add memory. We could do this in a batch, but we'll do it one by one rather to
// ensure that summaries and other artifacts are generated correctly.
try {
for (const { role, role_type, content } of history) {
await client.memory.add(sessionID, {
messages: [{ role, roleType: role_type, content }],
})
}
console.debug('Added new memory for session ', sessionID)
} catch (error) {
console.debug('Got error:', error)
}
console.log('Sleeping for 30 seconds to let background tasks complete...')
sleep(30000) // Sleep for 30 seconds
console.log('Done sleeping!')
// Get newly added memory
try {
console.debug('Getting memory for newly added memory with sessionid ', sessionID)
const memory = await client.memory.get(sessionID)
console.log('Memory: ', JSON.stringify(memory))
if (memory?.messages) {
memory.messages.forEach((message) => {
console.debug(JSON.stringify(message))
})
}
} catch (error) {
console.error('Got error:', error)
}
// get session messages
let sessionMessages: Message[] = []
try {
const sessionMessagesResult = await client.memory.getSessionMessages(sessionID, {
limit: 10,
cursor: 1,
})
console.debug('Session messages: ', JSON.stringify(sessionMessagesResult))
if (sessionMessagesResult?.messages) {
sessionMessages = sessionMessagesResult.messages
}
} catch (error) {
console.error('Got error:', error)
}
const firstSessionsMessageId = sessionMessages[0].uuid
console.log('\n\n????? trying to update message metadata')
// Update session message metadata
try {
const metadata = { metadata: { foo: 'bar' } }
if (firstSessionsMessageId) {
const updatedMessage = await client.memory.updateMessageMetadata(
sessionID,
firstSessionsMessageId,
{
metadata: metadata,
},
)
console.debug('Updated message: ', JSON.stringify(updatedMessage))
}
} catch (error) {
console.error('Got error:', error)
}
// Get session message
console.log('\n\n????? trying to get session message')
try {
if (firstSessionsMessageId) {
const message = await client.memory.getSessionMessage(sessionID, firstSessionsMessageId)
console.debug('Session message: ', JSON.stringify(message))
}
} catch (error) {
console.error('Got error:', error)
}
console.log('\n\n????? trying to get search message')
// Search messages in memory
try {
const searchText = 'Name some books that are about dystopian futures.'
console.debug('Searching memory...', searchText)
const { results: searchResults } = await client.memory.searchSessions({
userId: user.userId,
text: searchText,
})
searchResults?.forEach((searchResult) => {
console.debug('Search Result: ', JSON.stringify(searchResult.fact))
})
} catch (error) {
console.error('Got error:', error)
}
}
main()