Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { initOperationSdk } from '@yandex-cloud/nodejs-sdk/sdk/operation';

import { readFile } from 'fs/promises';
import { ExpirationConfig_ExpirationPolicy } from '@yandex-cloud/nodejs-sdk/proto/ai/common/common';
import { Message } from '@yandex-cloud/nodejs-sdk/ai-assistants-v1/threads/message';

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

Expand All @@ -41,8 +42,9 @@ const session = new Session({ iamToken });

const createFile = async () => {
const fileSdk = initFileSdk(session);
const filePath = 'examples/assistants/SomeFileToSave.pdf';

const fileToSaveContent = await readFile('./SomeFileToSave.pdf', {
const fileToSaveContent = await readFile(filePath, {
encoding: 'binary',
});

Expand All @@ -53,6 +55,8 @@ const createFile = async () => {
expirationConfig,
mimeType: 'application/pdf',
content,
name: 'SomeFileToSave from example',
labels: { fileName: 'SomeFileToSave', filePath },
});

return file;
Expand All @@ -64,6 +68,11 @@ const createSearchIndex = async (fileId: string) => {
const createSearchIndexOperation = await searchIndexSdk.create({
folderId,
fileIds: [fileId],
textSearchIndex: {
chunkingStrategy: {
staticStrategy: { chunkOverlapTokens: 300, maxChunkSizeTokens: 700 },
},
},
expirationConfig,
});

Expand All @@ -79,32 +88,54 @@ const createAssistantWithSearchIndex = async (searchIndexId: string) => {
const assistant = await assistantSdk.create({
folderId,
modelId: 'yandexgpt/latest',
tools: [{ searchIndex: { searchIndexIds: [searchIndexId] } }],
name: 'Assistant from examples',
instruction: `Ты — помощник по внутренней документации компании.
Отвечай вежливо.
Если информация не содержится в документах ниже, не придумывай ответ.
`,
tools: [
{
searchIndex: {
searchIndexIds: [searchIndexId],
// https://yandex.cloud/ru/docs/foundation-models/concepts/assistant/rephraser
rephraserOptions: {
rephraserUri: `gpt://${folderId}/rephraser/latest`,
},
},
},
],
});

return assistant;
};

(async function () {
const file = await createFile();
console.log(file);
console.log('File:\n');
console.log(JSON.stringify(file, undefined, 4));
console.log('\n');

const searchIndex = await createSearchIndex(file.id);

const assistant = await createAssistantWithSearchIndex(searchIndex.id);
console.log(assistant);
console.log('Assistant:\n');
console.log(JSON.stringify(assistant, undefined, 4));
console.log('\n');

const threadSdk = initThreadSdk(session);
const thread = await threadSdk.create({ folderId, name: '' }).withSdk();
const thread = await threadSdk.create({ folderId, name: 'Thread from example' }).withSdk();

console.log(thread);
console.log('Thread:\n');
console.log(JSON.stringify(await thread.data, undefined, 4));
console.log('\n');

const asyncIterableStreamEvent = await thread
.sendMessage({
content: MessageSdk.getMessageContent('О чем был текст файла ?'),
})
.getAssistantResponse(assistant);

let completedMessage: Message | undefined;
for await (const streamEvent of asyncIterableStreamEvent) {
console.log('\n---------------------\n');

Expand All @@ -115,9 +146,14 @@ const createAssistantWithSearchIndex = async (searchIndexId: string) => {
}

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

console.log('Completed Message:\n');
console.log(JSON.stringify(completedMessage, undefined, 4));
console.log('\n');
})();
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { writeFile } from 'fs';
import { Session } from '@yandex-cloud/nodejs-sdk/dist/session';

import { initOperationSdk } from '@yandex-cloud/nodejs-sdk/sdk/operation';
import { imageGeneration } from '@yandex-cloud/nodejs-sdk/ai-foundation_models-v1';
import { initImageGenerationSdk } from '@yandex-cloud/nodejs-sdk/sdk/ai-foundation_models-v1';

dotenv.config({ path: path.resolve(__dirname, '.env') });
Expand Down Expand Up @@ -34,6 +33,7 @@ const folderId = getEnv('YC_FOLDER_ID');
folderId,
modelId: 'yandex-art',
generationOptions: {
seed: Math.random(),
mimeType: 'image/jpeg',
},
messages: [{ text: 'Three cats', weight: 1 }],
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../tsconfig.json",
"include": ["./*.ts", "stream-stt/index.ts"]
"include": ["./**/*.ts", "stream-stt/index.ts"]
}
2 changes: 1 addition & 1 deletion src/sdk/ai-assistants-v1/messageSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class MessageSdk {
this.messageClient = session.client(MessageServiceClient, endpoint);
}

static getMessageContent(...args: string[]): TypeFromProtoc<MessageContent> {
static getMessageContent(...args: string[]): TypeFromProtoc<MessageContent, 'content'> {
return { content: args.map((content) => ({ text: { content } })) };
}

Expand Down