Tham khảo API OpenAI
kiểm tra / kiểm tra / kiểm tra
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
]
}'
Giao diện proxy (Giao diện proxy)
Sử dụng CloudFlare
Đặt Base URL
https://api.openai.com/v1
thay thế bằng
https://gateway.ai.cloudflare.com/v1/[xxx]/[yyy]/openai
Ví dụ như
curl https://gateway.ai.cloudflare.com/v1/45a93dd9034ce3d27b263a074d05f09a/ververv/openai/chat/completions \
Định nghĩa giao diện Chat
Định dạng các phản hồi
response_format (đáp ứng)
Phải là một trong text
hoặc json_object
.
Cài đặt thành { "type": "json_object" }
cho phép chế độ JSON, đảm bảo rằng thông điệp mà mô hình tạo ra là JSON hợp lệ.
** Quan trọng: ** khi sử dụng chế độ JSON, bạn ** phải ** cũng hướng dẫn mô hình tự tạo JSON thông qua một thông điệp hệ thống hoặc người dùng. Nếu không có điều này, mô hình có thể tạo ra một luồng không gian trắng không kết thúc cho đến khi thế hệ đạt đến giới hạn token, dẫn đến một yêu cầu dài và dường như bị mắc kẹt. Cũng lưu ý rằng nội dung tin nhắn có thể bị cắt một phần nếu finish_reason="length"
, chỉ ra rằng thế hệ vượt quá max_tokens
hoặc cuộc hội thoại vượt quá chiều dài ngữ cảnh tối đa.
Function Call / Công cụ
Tools: danh sách các công cụ
Mỗi công cụ là một đối tượng và mỗi đối tượng có một
type:Currently, only 'function' is supported.
Chức năng: tên, mô tả, thông số
Định nghĩa của mỗi parameter:
Tên, kiểu tham số, mô tả, giá trị tùy chọn, tham số nào là bắt buộc
các ví dụ điển hình
** Yêu cầu **
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "What is the weather like in Boston?"
}
],
"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"]
}
}
}
],
"tool_choice": "auto"
}'
** Phản hồi **
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1699896916,
"model": "gpt-3.5-turbo-0125",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": "{\n\"location\": \"Boston, MA\"\n}"
}
}
]
},
"logprobs": null,
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 82,
"completion_tokens": 17,
"total_tokens": 99
}
}
khung hình lớn / khung hình lớn
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "What is the weather like in Boston?"
}
],
"tools": [ // tool列表,与model同级
{ // 具体某个tool
"type": "function", // 类型:暂时只支持function
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
// 更具体定义
}
}
}
],
"tool_choice": "auto" // 选择哪个tool,与model同级
}
các thông số
{
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": ["location"],
}