Skip to content

Commit

Permalink
chore: chat with assistant example
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanVor committed Dec 8, 2024
1 parent 419da5b commit 5bb772f
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions examples/chat-with-assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ const getEnv = (envName: string, defaultValue?: string): string => {
const iamToken = getEnv('YC_IAM_TOKEN');
const folderId = getEnv('YC_FOLDER_ID');

const Sleep = (ms?: number) => new Promise<void>((res) => setTimeout(() => res(), ms));

(async function () {
const session = new Session({ iamToken });

Expand All @@ -35,46 +33,42 @@ const Sleep = (ms?: number) => new Promise<void>((res) => setTimeout(() => res()
const threadClient = session.client(threadService.ThreadServiceClient);
const runClient = session.client(runService.RunServiceClient);

const thread = await threadClient.create(
const threadP = threadClient.create(
threadService.CreateThreadRequest.fromPartial({
name: 'qwerty',
name: 'Thread Name',
folderId,
}),
);
console.log({ thread });

const assistant = await assistantClient.create(
const assistantP = assistantClient.create(
assistantService.CreateAssistantRequest.fromPartial({
name: 'qwerty',
name: 'Assistant Name',
folderId,
modelUri: `gpt://${folderId}/yandexgpt/latest`,
}),
);
console.log({ assistant });

const [thread, assistant] = await Promise.all([threadP, assistantP]);

const assistantId = assistant.id;
const threadId = thread.id;

const message = await messageClient.create(
await messageClient.create(
messageService.CreateMessageRequest.fromPartial({
threadId,
content: {
content: [{ text: { content: 'qwerty' } }],
content: [{ text: { content: 'What is it "qwerty"?' } }],
},
}),
);

console.log({ message });

const run = await runClient.create(
runService.CreateRunRequest.fromPartial({
threadId,
assistantId,
}),
);

console.log({ run });

const asyncIterableForStreamEvent = runClient.listen(
runService.ListenRunRequest.fromPartial({ runId: run.id }),
);
Expand Down

0 comments on commit 5bb772f

Please sign in to comment.