创建函数调用 (流式)
POST/v1/messages 创建函数调用(流式)
使用流式请求让模型在生成回复的过程中调用工具/函数,并逐步返回事件数据。
接口地址
POST https://www.vortapapi.com/v1/messages
说明
向 Vortap Messages 接口发送提示词、消息上下文以及工具定义后,模型会根据对话内容决定是否调用指定工具。启用流式输出时,响应会以增量事件的形式返回,便于客户端实时处理模型文本、工具调用参数以及结束状态。
请求头
| 名称 | 必填 | 说明 |
|---|---|---|
Content-Type | 是 | 请求体格式,通常为 application/json |
Accept | 是 | 如需流式响应,建议使用 text/event-stream |
Authorization | 否 | API Key 鉴权,例如 Bearer sk-*** |
请求示例
curl https://www.vortapapi.com/v1/messages \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-H "Authorization: Bearer sk-***" \
-d '{
"model": "claude-3-5-sonnet-latest",
"max_tokens": 1024,
"stream": true,
"messages": [
{
"role": "user",
"content": "查询北京今天的天气。"
}
],
"tools": [
{
"name": "get_weather",
"description": "获取指定城市的实时天气信息",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
]
}'
响应示例
非流式响应结构示例:
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
相关说明
stream: true表示启用流式返回。- 当模型判断需要调用工具时,会在响应事件中返回工具名称和对应输入参数。
- 客户端执行工具后,可将工具结果追加到后续请求中,让模型基于结果继续生成回答。