Chat接入

发布时间 2023-12-20 09:29:26作者: 梅丹隆

一、文档

https://platform.openai.com/docs/api-reference/chat

二、调用

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

# 添加代理
curl -x http://116.198.46.47:10089 https://api.openai.com/v1/chat/completions \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer 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' \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system", 
      "content": "You are a helpful assistant."
    }, 
    {
      "role": "user", 
      "content": "Hello!"
    }
  ]
}

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

三、调用

1、流式调用

HTTP事件流 text/event-stream