About ChatAPI
What is ChatAPI
Why We Support ChatAPI
How To Design ChatAPI
1.1 Communication protocol
HTTPS
1.2 Request method
Only the POST method is supported to initiate requests.
1.3 Character encoding
The BASE64 encoding of HTTP communication and message adopts UTF-8 character set encoding format.
1.4 Context-Type
application/json
1.5 Parameter
Request Body
name | type | description | mark |
---|---|---|---|
messages | array | video conversations | required |
message
name | type | description | mark |
---|---|---|---|
role | string | Chat message role defined by the OpenAI API. | required The role of the author of this message. One of system , user , or assistant |
content | string | The contents of the message. | required |
name | string | This property isn't in the official documentation, but it's in the documentation for the official library for python: |
How_to_count_tokens_with_tiktoken.ipynb
example
{
"messages": [
{
"role": "user",
"content": "hello"
}
]
}
1.6 Return Value
The return value must be returned as a stream
Accept: text/event-stream
example
data:{"content":"I"}
data:{"content":" am"}
data:{"content":" an"}
data:{"content":" AI"}
data:{"content":" language"}
data:{"content":" model"}
data:{"content":" created"}
data:{"content":" by"}
data:{"content":" Open"}
data:{"content":"AI"}
data:{"content":"."}
data:{"content":" I"}
data:{"content":" am"}
data:{"content":" designed"}
data:{"content":" to"}
data:{"content":" assist"}
data:{"content":" and"}
data:{"content":" communicate"}
data:{"content":" with"}
data:{"content":" human"}
data:{"content":" beings"}
data:{"content":" using"}
data:{"content":" natural"}
data:{"content":" language"}
data:{"content":" processing"}
data:{"content":"."}
data:[DONE]
Attention: the end must be 'data:[DONE]'
data:[DONE]
1.7 Curl Test
Write to the end
openai api docs
// ChatCompletionRequest represents a request structure for chat completion API.
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []ChatCompletionMessage `json:"messages"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
N int `json:"n,omitempty"`
Stream bool `json:"stream,omitempty"`
Stop []string `json:"stop,omitempty"`
PresencePenalty float32 `json:"presence_penalty,omitempty"`
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
LogitBias map[string]int `json:"logit_bias,omitempty"`
User string `json:"user,omitempty"`
}
Modified at 2023-06-07 06:21:55