Skip to content

Commit

Permalink
feat: add function-calling example code
Browse files Browse the repository at this point in the history
  • Loading branch information
anandaroop committed Apr 13, 2024
1 parent 061dfb4 commit a36b49b
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/01-function-calling/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Sample code from https://platform.openai.com/docs/api-reference/chat/create, lightly TS-ified
*/

import OpenAI from "openai"

const openai = new OpenAI()

const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
{ role: "user", content: "What's the weather like in Boston today?" },
]

const tools: OpenAI.Chat.Completions.ChatCompletionTool[] = [
{
type: "function",
function: {
name: "get_current_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"],
},
},
},
]

const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages,
tools,
tool_choice: "auto",
})

console.log(response.choices[0])

0 comments on commit a36b49b

Please sign in to comment.