-
Notifications
You must be signed in to change notification settings - Fork 14
/
chat-with-assistant.ts
86 lines (67 loc) · 2.41 KB
/
chat-with-assistant.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
import path from 'path';
import dotenv from 'dotenv';
import {
assistantService,
messageService,
threadService,
runService,
} from '@yandex-cloud/nodejs-sdk/ai-assistants-v1';
import { Session } from '@yandex-cloud/nodejs-sdk/dist/session';
dotenv.config({ path: path.resolve(__dirname, '.env') });
const getEnv = (envName: string, defaultValue?: string): string => {
const envValue = process.env[envName] || defaultValue;
if (!envValue) {
throw new Error(`Env variable ${envName} is not defined`);
}
return envValue;
};
const iamToken = getEnv('YC_IAM_TOKEN');
const folderId = getEnv('YC_FOLDER_ID');
(async function () {
const session = new Session({ });
const assistantClient = session.client(assistantService.AssistantServiceClient);
const messageClient = session.client(messageService.MessageServiceClient);
const threadClient = session.client(threadService.ThreadServiceClient);
const runClient = session.client(runService.RunServiceClient);
const threadP = threadClient.create(
threadService.CreateThreadRequest.fromPartial({
name: 'Thread Name',
folderId,
}),
);
const assistantP = assistantClient.create(
assistantService.CreateAssistantRequest.fromPartial({
name: 'Assistant Name',
folderId,
modelUri: `gpt://${folderId}/yandexgpt/latest`,
}),
);
const [thread, assistant] = await Promise.all([threadP, assistantP]);
const assistantId = assistant.id;
const threadId = thread.id;
await messageClient.create(
messageService.CreateMessageRequest.fromPartial({
threadId,
content: {
content: [{ text: { content: 'What is it "qwerty"?' } }],
},
}),
);
const run = await runClient.create(
runService.CreateRunRequest.fromPartial({
threadId,
assistantId,
}),
);
const asyncIterableForStreamEvent = runClient.listen(
runService.ListenRunRequest.fromPartial({ runId: run.id }),
);
let lastStreamEvent: runService.StreamEvent | null = null;
for await (const streamEvent of asyncIterableForStreamEvent) {
lastStreamEvent = streamEvent;
}
console.dir({ lastStreamEvent });
if (lastStreamEvent?.completedMessage?.content) {
console.dir(lastStreamEvent.completedMessage.content.content);
}
})();