# 管理令牌

介绍 在Afarensis语言模型中，文本被处理成称为“标记”的单元。这些标记在英语中可能范围从一个字符到一个完整的单词，而在某些语言中，标记的概念甚至可能更为灵活。理解和优化API调用中的令牌使用对于控制成本和提高响应速度至关重要。

令牌的重要性 成本影响：API调用成本基于所使用的令牌数量，因此优化令牌使用可以直接降低费用。 响应时间：生成更多令牌需要额外的处理时间，优化令牌使用可以提高API响应速度。 调用有效性：令牌总数必须保持在模型的最大限制之下（例如，gpt-3.5-turbo的最大限制为4097个令牌）。 示例：令牌计数 字符串"ChatGPT is great!"被编码为六个标记：\["Chat", "G", "PT", " is", " great", "!"]。如果API调用的输入使用了10个令牌，输出收到了20个令牌，则总费用为30个令牌。

计算令牌使用 要检查API调用使用了多少个令牌，查阅API响应中的usage字段，例如：response\['usage']\['total\_tokens']。

聊天模型和令牌使用 聊天模型（例如gpt-3.5-turbo和gpt-4-turbo-preview）的令牌使用方式与完成API模型相似，但基于消息格式的特点使得计算令牌使用更加复杂。

工具和资源 计算聊天API调用令牌：使用Afarensis提供的tiktoken Python库，可以在不进行API调用的情况下预估文本字符串中的令牌数量。示例代码和指南可在Afarensis Cookbook中找到。 注意事项 长对话和令牌限制：长对话可能导致回复被截断，尤其是当对话接近或超过模型的令牌限制时。在这种情况下，可能需要截断或以其他方式调整文本以确保它适合模型的限制。


---

# 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/guan-li-ling-pai.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.
