注册

Function Calling 调用

Function Calling

POST https://api.quickrouter.ai/v1/chat/completions 在线调试 →
Authorization

在 Header 添加参数 Authorization,其值为 Bearer 之后拼接 Token

示例: Authorization: Bearer ********************

请求参数

Header 参数
Content-Type string
必需
示例: application/json
Authorization string
必需
示例: Bearer $OPENAI_API_KEY
Body 参数 application/json
model string
必需
要使用的模型的 ID,如 gpt-4o。
messages array [object]
必需
至今为止对话所包含的消息列表。
tools array [object]
必需
模型可以调用的一组工具列表,目前只支持函数工具。
tool_choice object
可选
控制模型调用哪个函数。none 表示不调用,auto 表示自动选择。
temperature number
可选
使用什么采样温度,介于 0 和 2 之间。
max_tokens integer
可选
生成的最大标记数。
stream boolean
可选
设置为 true 启用流式输出。
示例
{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
}

请求示例代码

curl --location --request POST 'https://api.quickrouter.ai/v1/chat/completions' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data-raw '{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
}'
var myHeaders = new Headers();
myHeaders.append("Accept", "application/json");
myHeaders.append("Authorization", "Bearer YOUR_API_KEY");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
});

var requestOptions = {
   method: 'POST',
   headers: myHeaders,
   body: raw,
   redirect: 'follow'
};

fetch("https://api.quickrouter.ai/v1/chat/completions", requestOptions)
   .then(response => response.text())
   .then(result => console.log(result))
   .catch(error => console.log('error', error));
import java.io.*;
import java.net.*;
import java.util.*;

URL url = new URL("https://api.quickrouter.ai/v1/chat/completions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer YOUR_API_KEY");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
String jsonInputString = "{
    \"stream\": false,
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": \"北京今天天气怎么样\"
        }
    ],
    \"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\"]
                }
            }
        }
    ],
    \"max_tokens\": 1000,
    \"temperature\": 0.8,
    \"model\": \"gpt-4o\"
}";
try(OutputStream os = conn.getOutputStream()) {
    byte[] input = jsonInputString.getBytes("utf-8");
    os.write(input, 0, input.length);
}
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
import Foundation

let urlString = "https://api.quickrouter.ai/v1/chat/completions"
guard let url = URL(string: urlString) else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("Bearer YOUR_API_KEY", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let httpBody = "{
    \"stream\": false,
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": \"北京今天天气怎么样\"
        }
    ],
    \"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\"]
                }
            }
        }
    ],
    \"max_tokens\": 1000,
    \"temperature\": 0.8,
    \"model\": \"gpt-4o\"
}"
request.httpBody = httpBody.data(using: .utf8)

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    if let data = data {
        print(String(data: data, encoding: .utf8)!)
    }
}
task.resume()
package main

import (
    "fmt"
    "io"
    "net/http"
)

func main() {
    body := strings.NewReader(`{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
}`)
    req, _ := http.NewRequest("POST", "https://api.quickrouter.ai/v1/chat/completions", body)
    req.Header.Set("Accept", "application/json")
    req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
    req.Header.Set("Content-Type", "application/json")
    client := &http.Client{}
    resp, _ := client.Do(req)
    defer resp.Body.Close()
    bodyBytes, _ := io.ReadAll(resp.Body)
    fmt.Println(string(bodyBytes))
}
<?php

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.quickrouter.ai/v1/chat/completions',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => '{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
}',
  CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Authorization: Bearer YOUR_API_KEY",
    "Content-Type: application/json",
  ),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
import http.client
import json

conn = http.client.HTTPSConnection("api.quickrouter.ai")
payload = json.dumps({
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
})
headers = {
   'Accept': 'application/json',
   'Authorization': 'Bearer YOUR_API_KEY',
   'Content-Type': 'application/json',
}
conn.request("POST", "/v1/chat/completions", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
POST https://api.quickrouter.ai/v1/chat/completions HTTP/1.1
Accept: application/json
Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
}
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.quickrouter.ai/v1/chat/completions");

struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer YOUR_API_KEY");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{
    \"stream\": false,
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": \"北京今天天气怎么样\"
        }
    ],
    \"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\"]
                }
            }
        }
    ],
    \"max_tokens\": 1000,
    \"temperature\": 0.8,
    \"model\": \"gpt-4o\"
}");
CURLcode ret = curl_easy_perform(hnd);
var client = new RestClient("https://api.quickrouter.ai/v1/chat/completions");
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Authorization", "Bearer YOUR_API_KEY");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", @"{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
#import <Foundation/Foundation.h>

NSURL *url = [NSURL URLWithString:@"https://api.quickrouter.ai/v1/chat/completions"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"Bearer YOUR_API_KEY" forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[@"{
    \"stream\": false,
    \"messages\": [
        {
            \"role\": \"user\",
            \"content\": \"北京今天天气怎么样\"
        }
    ],
    \"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\"]
                }
            }
        }
    ],
    \"max_tokens\": 1000,
    \"temperature\": 0.8,
    \"model\": \"gpt-4o\"
}" dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume];
require "uri"
require "net/http"
require "json"

url = URI("https://api.quickrouter.ai/v1/chat/completions")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer YOUR_API_KEY"
request["Content-Type"] = "application/json"
request.body = '{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
}'
response = http.request(request)
puts response.read_body
(* Requires cohttp and lwt *)

let url = "https://api.quickrouter.ai/v1/chat/completions" in
let headers = Cohttp.Header.of_list [
  ("Accept", "application/json");
  ("Authorization", "Bearer YOUR_API_KEY");
  ("Content-Type", "application/json");
] in
let body = Cohttp_lwt.Body.of_string '{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
}' in
Lwt_main.run (
  Cohttp_lwt_unix.Client.request ?body:(Some body) ~method_:`POST ~headers (Uri.of_string url)
  >>= fun (resp, body) ->
  Cohttp_lwt.Body.to_string body >|= fun s -> print_endline s
)
import 'package:http/http.dart' as http;
import 'dart:convert';

var headers = {
  "Accept": "application/json",
  "Authorization": "Bearer YOUR_API_KEY",
  "Content-Type": "application/json",
};
var body = json.encode({
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
});
var response = await http.post(Uri.parse("https://api.quickrouter.ai/v1/chat/completions"), headers: headers, body: body);
print(response.body);
library(httr)

url <- "https://api.quickrouter.ai/v1/chat/completions"
body <- '{
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "北京今天天气怎么样"
        }
    ],
    "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"]
                }
            }
        }
    ],
    "max_tokens": 1000,
    "temperature": 0.8,
    "model": "gpt-4o"
}'
response <- post(url, body = body, add_headers("Accept" = "application/json", "Authorization" = "Bearer YOUR_API_KEY", "Content-Type" = "application/json"))
content(response, "text", encoding = "UTF-8")

返回响应

响应参数 🟢 200 OK · application/json
id string
必需
object string
必需
choices array [object]
必需
usage object
必需
示例
{
    "id": "chatcmpl-123",
    "object": "chat.completion",
    "created": 1677652288,
    "model": "gpt-4o",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": null,
                "tool_calls": [
                    {
                        "id": "call_abc123",
                        "type": "function",
                        "function": {
                            "name": "get_current_weather",
                            "arguments": "{\"location\": \"北京\"}"
                        }
                    }
                ]
            },
            "finish_reason": "tool_calls"
        }
    ],
    "usage": {
        "prompt_tokens": 82,
        "completion_tokens": 18,
        "total_tokens": 100
    }
}