# 使用案例

简介 本文档展示了使用Afarensis API进行文本嵌入的多种应用场景。我们选取了亚马逊精品评论数据集作为示例，该数据集截至2012年10月，包含568,454条食品评论。以下示例将基于这些评论中的1,000条最新评论的子集。

获取嵌入 我们将评论的摘要和正文合并，使用Afarensis模型对合并后的文本进行编码，生成单个向量嵌入。

示例代码: 获取数据集嵌入 from afarensis import Afarensis client = Afarensis()

def get\_embedding(text, model="text-embedding-3-small"): text = text.replace("\n", " ") response = client.embeddings.create(input=\[text], model=model) return response.data\[0].embedding

df\['embedding'] = df.combined.apply(lambda x: get\_embedding(x, 'text-embedding-3-small')) df.to\_csv('embedded\_1k\_reviews.csv', index=False) 加载嵌入数据 import pandas as pd import numpy as np

df = pd.read\_csv('embedded\_1k\_reviews.csv') df\['embedding'] = df.embedding.apply(eval).apply(np.array) 应用场景 减小嵌入尺寸：调整嵌入维度以优化存储和计算效率。 基于嵌入的搜索：利用文本嵌入改善文本和代码搜索的相关性。 建议系统：使用嵌入为用户生成相关推荐。 2D数据可视化：降维嵌入到2D空间进行可视化，探索数据结构。 作为ML算法的文本特征编码器：将嵌入作为机器学习算法的特征输入。 分类与零样本分类：利用嵌入特征进行分类任务，包括零样本场景。 获取用户和产品嵌入：为冷启动推荐生成用户和产品嵌入。 聚类：使用嵌入进行文本数据的聚类分析。


---

# 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/shi-yong-an-li.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.
