import { Agent, setDefaultOpenAIClient } from "@openai/agents";import OpenAI from "openai";import dotenv from "dotenv";dotenv.config();const client = new OpenAI({ baseURL: "https://ai-gateway.helicone.ai/v1", apiKey: process.env.HELICONE_API_KEY});// Set the client globally for all agentssetDefaultOpenAIClient(client);
4
Use OpenAI Agents normally
Your existing OpenAI Agents code continues to work without any changes:
import { Agent, run, tool } from "@openai/agents";import { z } from "zod";// Define toolsconst calculator = tool({ name: "calculator", description: "Perform basic arithmetic operations", parameters: z.object({ operation: z.enum(["add", "subtract", "multiply", "divide"]), a: z.number(), b: z.number() }), async execute({ operation, a, b }) { switch (operation) { case "add": return a + b; case "subtract": return a - b; case "multiply": return a * b; case "divide": if (b === 0) return "Error: Division by zero"; return a / b; } }});// Create an agent with toolsconst agent = new Agent({ name: "Assistant", instructions: "You are a helpful assistant.", tools: [calculator], model: "gpt-4o-mini",});// Run the agentconst result = await run(agent, "Multiply 2 by 2");console.log(result.finalOutput);