(1)使用curl 发送http请求
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": "Say this is a test!"}], "temperature": 0.7 }'
请求查询gpt-3.5-turbo模型。
(2)返回结果
{ "id":"chatcmpl-abc123", "object":"chat.completion", "created":1677858242, "model":"gpt-3.5-turbo-0301", "usage":{ "prompt_tokens":13, "completion_tokens":7, "total_tokens":20 }, "choices":[ { "message":{ "role":"assistant", "content":"\n\nThis is a test!" }, "finish_reason":"stop", "index":0 } ] }
prompt_tokens:问题token数,completion_tokens:回复token数,total_tokens:总token数=问题token数+回复token数。
finish_reason:stop表示模型产生的完整回复。在上述示例中只产生了一个message,可以设置参数n来产生多个message choices.
模型
列出所有可用模型
GET https://api.openai.com/v1/models
curl https://api.openai.com/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY"
返回结果
{ "data": [ { "id": "model-id-0", "object": "model", "owned_by": "organization-owner", "permission": [...] }, { "id": "model-id-1", "object": "model", "owned_by": "organization-owner", "permission": [...] }, { "id": "model-id-2", "object": "model", "owned_by": "openai", "permission": [...] }, ], "object": "list" }
某个模型
GET https://api.openai.com/v1/models/{model}
参数model:模型ID
curl https://api.openai.com/v1/models/text-davinci-003 \ -H "Authorization: Bearer $OPENAI_API_KEY"
返回结果
{ "id": "text-davinci-003", "object": "model", "owned_by": "openai", "permission": [...] }
会话
创建会话(create completion)
POST https://api.openai.com/v1/completions
请求主体
请求
curl https://api.openai.com/v1/completions \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "text-davinci-003", "prompt": "Say this is a test", "max_tokens": 7, "temperature": 0 }'
参数
{ "model": "text-davinci-003", "prompt": "Say this is a test", "max_tokens": 7, "temperature": 0, "top_p": 1, "n": 1, "stream": false, "logprobs": null, "stop": "\n" }
响应
{ "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7", "object": "text_completion", "created": 1589478378, "model": "text-davinci-003", "choices": [ { "text": "\n\nThis is indeed a test", "index": 0, "logprobs": null, "finish_reason": "length" } ], "usage": { "prompt_tokens": 5, "completion_tokens": 7, "total_tokens": 12 } }
聊天会话
创建聊天会话(Create chat completion)
POST https://api.openai.com/v1/chat/completions
价格表
标签:接口,id,tokens,api,openai,chatgpt,com,model From: https://www.cnblogs.com/readypalyerone/p/17312183.html