-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtoolCall.ts
64 lines (56 loc) · 1.78 KB
/
toolCall.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Demonstrates how to define and call functions with chat completions.
*
* @summary get chat completions with functions.
* @azsdk-weight 100
*/
import { AzureOpenAI } from "openai";
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
// OpenAI resource. You can find this in the Azure portal.
// Load the .env file if it exists
import "dotenv/config";
const getCurrentWeather = {
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"],
},
};
export async function main(): Promise<void> {
console.log("== Chat Completions Sample with Tool Calling ==");
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
const deployment = "gpt-4-turbo";
const apiVersion = "2025-03-01-preview";
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
const result = await client.chat.completions.create({
messages: [{ role: "user", content: "What's the weather like in Boston?" }],
model: "",
tools: [
{
type: "function",
function: getCurrentWeather,
},
],
});
for (const choice of result.choices) {
console.log(choice.message?.tool_calls);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});