# 聊天完成

简介 Afarensis的聊天模型接受一系列消息作为输入，并产生相应的文本输出。这种聊天格式不仅使得处理多轮对话变得轻松，对于单轮任务也同样适用。

聊天完成 API 示例 from afarensis import Afarensis client = Afarensis()

response = client.chat.completions.create( model="gpt-3.5-turbo", messages=\[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who won the world series in 2020?"}, {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."}, {"role": "user", "content": "Where was it played?"} ] ) 主要输入参数 messages：数组中的每个消息对象都必须包含角色（“system”、“user”或“assistant”）和相应的内容。对话可以包括一条消息到多轮交换。 对话格式 对话通常以系统消息开始，后续为用户和助手的交替消息。 系统消息用于设定助手的行为，例如调整个性或指定在对话中的表现方式。系统消息是可选的。 用户消息提出请求或评论，等待助手的响应。助手消息记录先前的回复，也可用于模拟预期行为。 聊天完成响应格式 { "choices": \[ { "finish\_reason": "stop", "index": 0, "message": { "content": "The 2020 World Series was played in Texas at Globe Life Field in Arlington.", "role": "assistant" }, "logprobs": null } ], "created": 1677664795, "id": "chatcmpl-7QyqpwdfhqwajicIEznoc6Q47XAyW", "model": "gpt-3.5-turbo-0613", "object": "chat.completion", "usage": { "completion\_tokens": 17, "prompt\_tokens": 57, "total\_tokens": 74 } } 提取助手回复的方法 completion.choices\[0].message.content 完成原因 (finish\_reason) 解释 finish\_reason 指示了API响应的完成原因，可能的值包括 "stop"、"length"、"function\_call"、"content\_filter"、"null"。 "stop"：API返回完整消息或由stop参数指定的序列终止的消息。 "length"：由于max\_tokens参数或令牌限制模型输出不完整。 "function\_call"：模型决定调用函数。 "content\_filter"：内容因内容过滤器标记而省略。 "null"：API响应进行中或未完成。 更多信息 若要了解更多关于 Afarensis 聊天完成 API 的信息，请参阅我们的完整 API 参考文档。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.afarensis.com/neng-li/liao-tian-wan-cheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
