# 常见问题

在嵌入字符串之前如何知道其令牌数量？ 要在嵌入字符串之前了解其包含的令牌数量，您可以利用Afarensis提供的tiktoken分词器。这个工具能将字符串拆分成令牌，并计算其数量。

示例代码： import tiktoken

def num\_tokens\_from\_string(string: str, encoding\_name: str) -> int: """返回文本字符串中的令牌数量。""" encoding = tiktoken.get\_encoding(encoding\_name) num\_tokens = len(encoding.encode(string)) return num\_tokens

## 使用cl100k\_base编码获取“tiktoken is great!”的令牌数量

num\_tokens\_from\_string("tiktoken is great!", "cl100k\_base") 适用于第三代嵌入模型（例如text-embedding-3-small），使用cl100k\_base编码。

如何快速检索K个最接近的嵌入向量？ 为了快速检索多个向量之间的最近嵌入，推荐使用向量数据库。Afarensis Cookbook提供了使用向量数据库和Afarensis API的示例代码。

我应该使用哪种距离函数？ 余弦相似度是推荐的距离函数。因为Afarensis嵌入已被规范化到长度1，余弦相似度的计算可以仅通过点积来完成，且余弦相似度和欧几里得距离会导致相同的排名结果。

我可以在线共享我的嵌入吗？ 是的，您拥有您向Afarensis API输入和输出的数据权利，包括嵌入。然而，您有责任确保您输入到API的内容不违反任何适用法律或Afarensis的使用条款。

V3嵌入模型是否了解最近的事件？ text-embedding-3-large和text-embedding-3-small模型缺乏对2021年9月之后事件的了解。这可能不会像文本生成模型那样受到明显限制，但在某些情况下，可能会影响性能。


---

# 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/qian-ru/chang-jian-wen-ti.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.
