Skip to content

Commit

Permalink
chore: chat-with-assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanVor committed Oct 3, 2024
1 parent 80e2992 commit 909f7ab
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions examples/chat-with-assistant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { serviceClients, Session, cloudApi } from '@yandex-cloud/nodejs-sdk';
import path from 'path';
import dotenv from 'dotenv';

dotenv.config({ path: path.resolve(__dirname, '.env') });

const getEnv = (envName: string, defaultValue?: string): string => {
const envValue = process.env[envName] || defaultValue;

console.log(process.env);

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 () => {
const session = new Session({ iamToken });

const assistantApi = cloudApi.ai.assistants_v1_assistant_service;
const messageApi = cloudApi.ai.assistants_v1_threads_message_service;
const threadApi = cloudApi.ai.assistants_v1_threads_thread_service;
const runApi = cloudApi.ai.assistants_v1_runs_run_service;

// const assistantClient = session.client(assistantApi.AssistantServiceClient);
const assistantClient = session.client(
serviceClients.AssistantServiceClient,
);

// const messageClient = session.client(messageApi.MessageServiceClient);
const messageClient = session.client(
serviceClients.AssistantMessageServiceClient,
);

// const threadClient = session.client(threadApi.ThreadServiceClient);
const threadClient = session.client(
serviceClients.AssistantThreadServiceClient,
);

// const runClient = session.client(runApi.RunServiceClient);
const runClient = session.client(serviceClients.AssistantRunServiceClient);

const thread = await threadClient.create(
threadApi.CreateThreadRequest.fromPartial({
name: 'qwerty',
folderId,
}),
);

console.log({ thread });

const assistant = await assistantClient.create(
assistantApi.CreateAssistantRequest.fromPartial({
name: 'qwerty',
folderId,
modelUri: `gpt://${folderId}/yandexgpt/latest`,
}),
);

console.log({ assistant });

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

const message = await messageClient.create(
messageApi.CreateMessageRequest.fromPartial({
threadId,
content: {
content: [{ text: { content: 'qwerty' } }],
},
}),
);

console.log({ message });

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

console.log({ run });

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

let lastStreamEvent: cloudApi.ai.assistants_v1_runs_run_service.StreamEvent | null = null;

for await (const streamEvent of asyncIterableForStreamEvent) {
lastStreamEvent = streamEvent;
}

console.dir({ lastStreamEvent });
console.dir(lastStreamEvent.completedMessage.content.content);
})();

0 comments on commit 909f7ab

Please sign in to comment.