2import { schemasWeather } from "https://esm.town/v/webup/schemasWeather";
3
4export const chatSampleFunctionSingle = (async () => {
5 // Example dummy function hard coded to return the same weather
6 // In production, this could be your backend API or an external API
7 const getCurrentWeather = (location, unit = "fahrenheit") => ({
11 forecast: ["sunny", "windy"],
12 });
13 // Step 1: send the conversation and available functions to GPT
14 const messages = [{
15 "role": "user",
17 "What's the weather like in Boston, and what's the weather in Huston?",
18 }];
19 const functions = [schemasWeather[0]];
20 const response = await chat(messages, {
21 functions,
22 function_call: "auto", // auto is default, but we'll be explicit
23 });
24 console.log(response);
25 // Step 2: Check if GPT wanted to call a function
26 if (typeof response !== "object")
27 return;
28 // Step 3: Call the function
29 // Note: The JSON response may not always be valid; be sure to handle errors
30 const { name } = response;
34 args?.unit,
35 );
36 if (!functions)
37 return;
38 // Step 4: Send the info on the function call and function response to GPT
39 // Extend conversation with assistant's reply
40 messages.push({
41 role: "assistant",
42 function_call: response,
43 content: "",
44 });
45 // Extend conversation with function response
46 messages.push({
47 role: "function",
48 name,
49 content: JSON.stringify(funcResponse),
50 });
51 // Get a new response from GPT where it can see the function response
52 return await chat(messages);
53})();