Skip to content

Commit

Permalink
chore: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanVor committed Dec 17, 2024
1 parent 263d666 commit ab8097f
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions examples/chat-with-assistant-sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import path from 'path';
import dotenv from 'dotenv';

import { Session } from '@yandex-cloud/nodejs-sdk/dist/session';

import {
initAssistantSdk,
initThreadSdk,
MessageSdk,
} from '@yandex-cloud/nodejs-sdk/ai-assistants-v1/sdk';

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({ iamToken });

const threadSdk = initThreadSdk(session);
const assistantSdk = initAssistantSdk(session);

const assistant = await assistantSdk.create({
folderId,
modelId: 'yandexgpt/latest',
});

const thread = await threadSdk
.create({ name: "Thread name", folderId })
.withSdk();

const asyncIterableForStreamEvent = await thread
.sendMessage({
content: MessageSdk.getMessageContent(
'Hi, how are you today ?',
'Explain me please what is it qwerty ?',
),
})
.getAssistantResponse(assistant);

for await (const streamEvent of asyncIterableForStreamEvent) {
console.log('\n---------------------\n');

if (streamEvent.partialMessage) {
console.log('Partial message:\n');
console.log(MessageSdk.messageContentToString(streamEvent.partialMessage));
continue;
}

if (streamEvent.completedMessage) {
console.log('Completed message:\n');
console.log(MessageSdk.messageContentToString(streamEvent.completedMessage.content));
}
}
})();

0 comments on commit ab8097f

Please sign in to comment.