Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
import { schemasWeather } from "https://esm.town/v/webup/schemasWeather";
import { chat } from "https://esm.town/v/webup/chat";
export const chatSampleFunctionMultiple = (async () => {
// Helper function to call and print assistant response
const callAssistant = async (messages) => {
const response = await chat(messages, {
functions: schemasWeather,
});
typeof response === "object"
? console.log(`Assistant: ${JSON.stringify(response)}`)
: console.log(`Assistant: ${response}`);
return response;
};
// Prompt the model about the current weather, it will respond with some clarifying questions
const messages = [
{
role: "system",
content:
"Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous.",
},
{ role: "user", content: "What's the weather like today" },
];
let response = await callAssistant(messages);
// Once we provide the missing info, it will generate the appropriate function arguments
messages.push({ role: "assistant", content: response });
messages.push({ role: "user", content: "I'm in Glasgow, Scotland." });
response = await callAssistant(messages);
// By prompting it differently, we can get it to target the other function we've told
messages.length = 1;
messages.push({
role: "user",
content:
"what is the weather going to be like in Glasgow, Scotland over the next x days",
});
response = await callAssistant(messages);
// Let's provide the num of days, and model will generate the call to the other function
messages.push({ role: "assistant", content: response });
messages.push({ role: "user", content: "5 days" });
response = await callAssistant(messages);
})();
October 23, 2023