Skip to content
Open
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
68 changes: 40 additions & 28 deletions src/agents/openai/OpenAIAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,51 @@ export class OpenAIAgent implements AIAgent {
if (!apiKey) {
throw new Error('OpenAI API key is required');
}

this.openai = new OpenAI({ apiKey });
this.assistant = await this.openai.beta.assistants.create({
name: 'Stream AI Assistant',
instructions: 'You are an AI assistant. Help users with their questions.',
tools: [
{ type: 'code_interpreter' },
{
type: 'function',
function: {
name: 'getCurrentTemperature',
description: 'Get the current temperature for a specific location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city and state, e.g., San Francisco, CA',
},
unit: {
type: 'string',
enum: ['Celsius', 'Fahrenheit'],
description:
"The temperature unit to use. Infer this from the user's location.",

const assitantId = process.env.OPENAI_ASSISTANT_ID as string | undefined;
if (assitantId) {
this.assistant = await this.openai.beta.assistants.retrieve(assitantId);
} else {
this.assistant = await this.openai.beta.assistants.create({
name: 'Stream AI Assistant',
instructions:
'You are an AI assistant. Help users with their questions.',
tools: [
{ type: 'code_interpreter' },
{
type: 'function',
function: {
name: 'getCurrentTemperature',
description:
'Get the current temperature for a specific location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city and state, e.g., San Francisco, CA',
},
unit: {
type: 'string',
enum: ['Celsius', 'Fahrenheit'],
description:
"The temperature unit to use. Infer this from the user's location.",
},
},
required: ['location', 'unit'],
},
required: ['location', 'unit'],
},
},
},
],
model: 'gpt-4o',
});
],
model: 'gpt-4o',
});
}

if (!this.assistant) {
throw new Error('Could not create or retrieve OpenAI assistant');
}

this.openAiThread = await this.openai.beta.threads.create();

this.chatClient.on('message.new', this.handleMessage);
Expand Down