import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: "PORTKEY_API_KEY",
baseURL: "https://aigw.portkey.ai/v1",
});
// 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: "@openai/gpt-4",
tools: tools,
tool_choice: "auto"
});
console.log(JSON.stringify(completion, null, 2));
}
Examples();