import { Agent, run, Runner, setDefaultOpenAIClient, setOpenAIAPI, tool } from "@openai/agents";
import { OpenAI } from "openai";
import * as undici from "undici";
export function createOpenAIAgenticChatAgent() {
let proxyAgent = undefined;
if (process.env.http_proxy) {
proxyAgent = new undici.ProxyAgent(process.env.http_proxy!);
}
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_BASE_URL,
fetchOptions: {
dispatcher: proxyAgent,
}
});
setDefaultOpenAIClient(openai);
setOpenAIAPI('chat_completions');
const getCurrentTimeTool = tool({
name: 'get_current_time',
description: 'Get the current time',
parameters: {
type: 'object',
properties: {},
required: [],
},
execute: async () => {
const d = new Date();
return `${d.toString()} ${d.toISOString()}`;
}
});
const agent = new Agent({
name: 'Agent',
model: process.env.OPENAI_MODEL!,
instructions: 'You are a helpful assistant.',
tools: [getCurrentTimeTool],
});
const runner = new Runner({
tracingDisabled: true,
})
return {
agent,
runner,
}
// const stream = await runner.run(agent, 'What is the current time?', {
// stream: true,
// });
// const types = new Set<string>();
// for await (const event of stream) {
// console.log(event);
// types.add(event.type);
// }
// console.log(Array.from(types).join(', '));
}
// main().catch(console.error);