-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
})(); |