import OpenAI from 'openai';
import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'
const openai = new OpenAI({
apiKey: "OPENAI_API_KEY",
baseURL: PORTKEY_GATEWAY_URL,
defaultHeaders: createHeaders({
provider: "openai",
apiKey: "PORTKEY_API_KEY"
})
});
// Define tools (for function calling example)
const tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
];
async function Examples() {
// Example : Function calling with caching
completion = await openai.chat.completions.create({
messages: [
{"role": "system", "content": "You are a helpful assistant that can check the weather."},
{"role": "user", "content": "What's the weather like in San Francisco?"}
],
model: "gpt-4",
tools: tools,
tool_choice: "auto"
});
console.log(JSON.stringify(completion, null, 2));
}
Examples();