import Together from 'together-ai';
const together = new Together();
const weatherTool = {
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',
},
},
required: ['location'],
},
};
const toolPrompt = `You have access to the following functions:
Use the function '${weatherTool.name}' to '${weatherTool.description}':
${JSON.stringify(weatherTool)}
If you choose to call a function ONLY reply in the following format with no prefix or suffix:
<function=example_function_name>{"example_name": "example_value"}</function>
Reminder:
- Function calls MUST follow the specified format, start with <function= and end with </function>
- Required parameters MUST be specified
- Only call one function at a time
- Put the entire function call reply on one line
- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls
`;
let messages = [
{
role: 'system',
content: toolPrompt,
},
{
role: 'user',
content: 'What is the weather in Casablanca?',
},
];
async function main() {
const response = await together.chat.completions.create({
model: 'meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo',
messages: messages,
max_tokens: 1024,
temperature: 0,
});
if (response.choices?.[0]?.message) {
messages.push({
role: response.choices[0].message.role,
content: response.choices[0].message.content!,
});
console.log(response.choices?.[0]?.message?.content);
}
}
main();