函数调用
让 LLM 与外部 API 互动!
import Portkey from "portkey-ai";
const portkey = new Portkey({
apiKey: "PORTKEY_API_KEY",
virtualKey: "ANYSCALE_VIRTUAL_KEY",
});
// 描述天气 API 的功能和期望
let tools = [
{
"type": "function",
"function": {
"name": "getWeather",
"description": "获取给定地点的当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市和州",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
];
let response = await portkey.chat.completions.create({
model: "mistralai/Mixtral-8x7B-Instruct-v0.1",
messages: [
{"role": "system", "content": "您是一个有帮助的助手。"},
{"role": "user", "content": "德里的天气怎么样 - 请以 JSON 格式回复"}
],
tools,
tool_choice: "auto", // auto 是默认值,但可以显式设置
});
console.log(response.choices[0].finish_reason) 函数调用工作流程

支持的模型
模型/提供者
标准函数调用
并行函数调用
Last updated