Skip to main content

การอ้างอิง API ของ OpenAI

การทดสอบ

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!"
}
]
}'

อินเทอร์เฟซพร็อกซี

การใช้ CloudFlare

官方教程

จะ Base URL

https://api.openai.com/v1

แทนที่ ด้วย

https://gateway.ai.cloudflare.com/v1/[xxx]/[yyy]/openai

อย่างเช่น

curl https://gateway.ai.cloudflare.com/v1/45a93dd9034ce3d27b263a074d05f09a/ververv/openai/chat/completions \

คําจํากัดความของอินเทอร์เฟซแชท

官网

การจัดรูปแบบการตอบสนอง

官网

การตอบสนอง format

จะต้องเป็นหนึ่งใน text หรือ json object

การตั้งค่าเป็น { "type": "json object" } เปิดใช้งานโหมด JSON ซึ่งรับประกันได้ว่าข้อความที่โมเดลสร้างขึ้นเป็น JSON ที่ถูกต้อง

** สําคัญ: ** เมื่อใช้โหมด JSON คุณ ** ต้อง ** ยังสั่งให้โมเดลผลิต JSON ด้วยตัวเองผ่านระบบหรือข้อความผู้ใช้หากปราศจากสิ่งนี้โมเดลอาจสร้างสตรีมที่ไม่มีที่สิ้นสุดของพื้นที่สีขาวจนกว่าการสร้างจะถึงขีด จํากัด ของโทเค็นส่งผลให้มีการร้องขอที่ยาวนานและดูเหมือนจะ "ติดอยู่"โปรดทราบด้วยว่าเนื้อหาข้อความอาจถูกตัดออกบางส่วนหาก finish reason="length" ซึ่งบ่งชี้ว่าการสร้างเกิน max tokens หรือการสนทนาเกินความยาวบริบทสูงสุด

ฟังก์ชั่นเรียก / เครื่องมือ

官网

เครื่องมือ: รายการของเครื่องมือ

เครื่องมือแต่ละตัวเป็นวัตถุและวัตถุแต่ละตัวมี

type: ปัจจุบันมีเพียง ฟังก์ชั่น เท่านั้นที่ได้รับการสนับสนุน

ฟังก์ชัน: ชื่อ, คําอธิบาย, พารามิเตอร์

คําจํากัดความของพารามิเตอร์แต่ละตัว:

ชื่อ, ชนิดของพารามิเตอร์, คําอธิบาย, ค่าตัวเลือก, พารามิเตอร์ใดที่จําเป็น

ตัวอย่าง

** ขอ **

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"
}'

** ตอบสนอง **

{
"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
}
}

กรอบขนาดใหญ่

{
"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同级
}

พารามิเตอร์

{
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
]
}
},
"required": ["location"],
}