Skip to content

Instantly share code, notes, and snippets.

@pathikrit
Created May 10, 2024 16:09
Show Gist options
  • Save pathikrit/2d642af0308174727a74181ff3f6c9fb to your computer and use it in GitHub Desktop.
Save pathikrit/2d642af0308174727a74181ff3f6c9fb to your computer and use it in GitHub Desktop.
OpenAI function calling example
#!/usr/bin/env node
import { Configuration as OpenAIConfig, OpenAIApi, ChatCompletionRequestMessageRoleEnum as Role } from 'openai'
const OPENAI_API_KEY = '?????'
const openai = new OpenAIApi(new OpenAIConfig({apiKey: OPENAI_API_KEY}))
const task = openai.createChatCompletion({
model: 'gpt-3.5-turbo-0613',
max_tokens: 2048,
temperature: 0.5,
messages: [
{role: Role.User, content: 'Get location of this address: 23 Academy Ln, Demarest NJ 07627'},
],
functions: [
{
name: "address_to_location",
description: "Convert the address to lat,long",
parameters: {
type: "object",
properties: {
line1: {type: "string", description: "Line1 of address"},
line2: {type: "string", description: "Line2 of address"},
city: {type: "string", description: "city"},
state: {type: "string", description: "state"},
zip: {type: "string", description: "zip"},
},
required: ["line1", "city", "state", "zip"]
}
}
],
})
task.then(res => console.log(JSON.stringify(res.data.choices)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment