Sure, here's the translation: ```html

如何使用OpenAI和FAISS索引来优化检索增强生成(RAG)中的令牌使用

``` This HTML code maintains the structure while providing the text in simplified Chinese.

在我的上一篇博客《使用RAG架构彻底改变数据查询:将您的业务提示转换为SQL查询》中,我解释了如何使用RAG架构在不对模型进行微调的情况下,利用OpenAI来解决任何业务问题。

问题:OpenAI的GPT模型有令牌限制,过度使用令牌可能导致成本增加和响应时间变慢。高效的令牌管理确保模型在不必要的支出下以最佳状态运行。

可以通过下面的代码片段来复习如何使用FAISS并创建索引。

#for efficient similarity search and clustering of dense vectors. 
#Use openai embedding model to crete vecotr embeddings for both data and query.
# to generate embedding we can use OpenAI lib's embeedings function:

def generate_embeddings(text, model=azure_openai_deployment_name): # model = "deployment_name"
return client.embeddings.create(input = [text], model=model).data[0].embedding

# generate query embeddings:
question_embedding = np.array(generate_embeddings(query))
# generate data embeddings:

embeddings_dataset = dataset.map(
lambda x: {"embeddings": np.array(generate_embeddings(x["text"]))}

# install faiss library
pip install faiss-cpu

# use any python IDE and paste the below code
import faiss
import numpy as np

# Assuming `data` is a 2D numpy array where each row is a vector representation of a document
d = data.shape[1]
index = faiss.IndexFlatL2(d)
index.add(data)

# in order to do data retrieval using a query use index search:
query_vector = np.array([your_query_vector])
D, I = index.search(query_vector, k) # `k` is the number of nearest neighbors to retrieve

#D is distaince and I is index fo datapoint

To translate "index.Search returns the most relevant documents based on KNN where `k` is the number of nearest neighbors to retrieve" into simplified Chinese while keeping the HTML structure intact, you can use the following: ```html index.搜索根据KNN返回最相关的文档,其中 `k` 是要检索的最近邻居的数量。 ``` In this translation: - "index.Search" is translated as "index.搜索" to maintain the context of searching or retrieving. - "returns" is translated as "返回", indicating what is being provided or returned. - "most relevant documents" is translated as "最相关的文档", referring to the documents that are most closely related or pertinent. - "based on KNN" remains "根据KNN", which stands for "K Nearest Neighbors". - "`k` is the number of nearest neighbors to retrieve" is translated as "`k` 是要检索的最近邻居的数量", specifically noting `k` as the variable representing the number of neighbors to be retrieved. This translation maintains clarity and accurately conveys the original meaning in simplified Chinese.

Sure, here's the translated text in simplified Chinese while maintaining the HTML structure: ```html

上述代码展示了大多数RAG应用程序如何使用Faiss实现,这引发了以下问题:

``` This HTML snippet retains the structure of the original text while presenting the translation in simplified Chinese.
  • To translate the given English text into simplified Chinese while maintaining HTML structure, you can use the following: ```html

    选择K的值:选择正确的K值对KNN的性能至关重要。较小的K可能导致过拟合,而较大的K可能导致欠拟合。例如,如果您提供较小的K值,则可能会丢失一些重要的数据点,而较大的K值可能会导致令牌使用过多。

    ``` In this HTML snippet: - `

    ` and `

    ` enclose the translated text to denote it as a paragraph. - Chinese characters are used for the translation of the text content. Make sure to include this HTML snippet in your document where you want the translated text to appear. Adjust the formatting as needed to fit your specific HTML structure.
  • Sure, here is the text translated to simplified Chinese, while keeping the HTML structure intact: ```html

    距离度量:距离度量的选择(欧氏距离、曼哈顿距离等)显著影响KNN的性能。最佳的度量标准因数据的性质而异。

    ``` In this HTML snippet: - `

    ` represents a paragraph tag in HTML. - The Chinese text provided is a direct translation of the English sentence into simplified Chinese.

  • To translate the given English text to simplified Chinese while maintaining the HTML structure, you can use the following: ```html

    长文档的可扩展性:在大型数据集上使用索引搜索,如果不对数据进行汇总,可能会因关键字数量庞大而导致大量的标记使用。

    ``` This HTML snippet preserves the structure and content of the English sentence while providing its simplified Chinese translation.

Sure, here's the translation: ```html

解决方案
```
  1. 在发送到OpenAI之前,将大型文档分解为较小的块,并使用总结技术来减少需要处理的文本量。
  2. def summarize_text(text):
    prompt = f"Summarize the following text:\n\n{text}"
    response = openai.Completion.create(engine="text-davinci-003", prompt=prompt, max_tokens=100)
    return response.choices[0].text.strip()

    summary = summarize_text(long_document)

To translate the given English text to simplified Chinese while keeping the HTML structure intact, you can use the following: ```html 2. 使用范围搜索:我发现它在平衡查询的最近邻居数量和令牌使用方面非常有用。FAISS 索引提供了一种称为范围搜索的方法,作为 index.search 的替代方法。您可以通过基本数学方法调整其使用,并根据令牌限制优化令牌的数量。 ``` This translation maintains the original structure and content while converting it into simplified Chinese.

Sure, here's the translated text in simplified Chinese while maintaining HTML structure: ```html

这个想法是使用范围搜索返回的距离均值来确定一个阈值(可以理解为邻居的数量),并且反复重新计算该阈值,直到返回的数据点数量小于或等于您设置的令牌限制。

``` This HTML snippet translates the provided English text into simplified Chinese, keeping it formatted as a paragraph (`

` tag).

Sure, here's the HTML structure with the translated text in simplified Chinese: ```html

这里是一个代码片段来实现这个功能。例如,如果您需要令牌的数量为25,000个,只需重复区间搜索,直到令牌数量少于25,000个为止。
``` In simplified Chinese, the text translates to: "这里是一个代码片段来实现这个功能。例如,如果您需要令牌的数量为25,000个,只需重复区间搜索,直到令牌数量少于25,000个为止。"
# range search works with threshold value and returns all the neighbors within that limit
# for the first time we can use threshold = 0.95 so that all the points can be returned

limits, distances, indices = faiss_index.range_search(x=question_embedding.reshape(1, -1), thresh=0.95)
# once all the data points related to a query are returned then take the mean of all distances
threshold = float(statistics.mean(distances))
threshold = round(threshold,2)

tokens = num_tokens_from_string(concatenated_data, "cl100k_base")
# concatenated_data is the data retrived from range search indices

```html

结论

```

Sure, here is the translated text in simplified Chinese while keeping the HTML structure: ```html 优化在使用OpenAI的RAG模型中的token使用,涉及使用FAISS索引进行高效检索,精简查询构建,有效分块和摘要生成。通过遵循这些步骤,您可以确保您的RAG系统既具有成本效益又具有高性能,提供准确和相关的响应,避免不必要的token消耗。 ``` In HTML format: ```html

优化在使用OpenAI的RAG模型中的token使用,涉及使用FAISS索引进行高效检索,精简查询构建,有效分块和摘要生成。通过遵循这些步骤,您可以确保您的RAG系统既具有成本效益又具有高性能,提供准确和相关的响应,避免不必要的token消耗。

```

Sure, here is the HTML structure with the translated text in simplified Chinese: ```html

通过集成FAISS并应用这些优化技术,您可以充分利用与OpenAI的RAG的潜力,使您的应用程序更加高效和可扩展。

``` In simplified Chinese: ```html

通过集成FAISS并应用这些优化技术,您可以充分利用与OpenAI的RAG的潜力,使您的应用程序更加高效和可扩展。

``` This HTML snippet preserves the structure while displaying the translated text clearly.

2024-06-18 04:33:45 AI中文站翻译自原文