函数调用

Portkey 的 AI 网关支持许多基础模型提供商提供的函数调用功能。在 API 调用中,您可以描述函数,模型可以选择输出文本或该函数名称及其参数。

函数使用

Portkey 支持 OpenAI 签名以将函数定义为 API 请求的一部分。tools 参数接受可以专门发送给支持函数/工具调用的模型的函数。

import Portkey from 'portkey-ai';

// Initialize the Portkey client
const portkey = new Portkey({
    apiKey: "PORTKEY_API_KEY",  // Replace with your Portkey API key
    virtualKey: "VIRTUAL_KEY"   // Add your provider's virtual key
});

// Generate a chat completion with streaming
async function getChatCompletionFunctions(){
  const messages = [{"role": "user", "content": "What's the weather like in Boston today?"}];
  const tools = [
      {
        "type": "function",
        "function": {
          "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"],
          },
        }
      }
  ];

  const response = await portkey.chat.completions.create({
    model: "gpt-3.5-turbo",
    messages: messages,
    tools: tools,
    tool_choice: "auto",
  });
  
  console.log(response)

}
await getChatCompletionFunctions();

完成后,请求将记录在日志 UI 中,可以查看工具和函数。Portkey 将自动格式化输入和输出中的 JSON 块,从而提供良好的调试体验。

在提示中管理功能和工具

Portkey 的提示库支持创建带有功能/工具定义的提示模板,并允许您设置 tool choice 参数。Portkey 还会实时验证您的工具定义,消除语法错误。

支持的提供商和模型

以下提供商支持函数调用,更多提供商将很快添加。请提交请求PR以将模型或提供商添加到AI网关。

提供商
模型

gpt-4 系列模型 gpt-3.5-turbo 系列模型

gpt-4 系列模型 gpt-3.5-turbo 系列模型

mistralai/Mistral-7B-Instruct-v0.1 mistralai/Mixtral-8x7B-Instruct-v0.1

mistralai/Mixtral-8x7B-Instruct-v0.1 mistralai/Mistral-7B-Instruct-v0.1 togethercomputer/CodeLlama-34b-Instruct

firefunction-v1

fw-function-call-34b-v0

gemini-1.0-pro

gemini-1.0-pro-001

gemini-1.5-pro-latest

食谱

这是一个关于使用 Portkey 进行函数调用的详细食谱。

Last updated