LlamaIndex 常见问题解答(FAQ)

 

提示:如果您尚未完成,请安装 LlamaIndex 并完成起步教程。遇到不熟悉的术语时,请参考高层次概念部分。

在这个章节中,我们将从您为起步示例编写的代码开始,展示您可能希望针对不同应用场景对其进行的常见定制方法:

python
 
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader documents = SimpleDirectoryReader("data").load_data() index = VectorStoreIndex.from_documents(documents) query_engine = index.as_query_engine() response = query_engine.query("What did the author do growing up?") print(response)

“我想将文档拆分为更小的片段”

python
 
# 全局设置 from llama_index.core import Settings Settings.chunk_size = 512 # 局部设置 from llama_index.core.node_parser import SentenceSplitter index = VectorStoreIndex.from_documents( documents, transformations=[SentenceSplitter(chunk_size=512)] )

“我想使用不同的向量存储”

首先,您可以安装您想使用的向量存储。例如,要使用 Chroma 作为向量存储,您可以使用 pip 进行安装:

bash
 
pip install llama-index-vector-stores-chroma

要了解所有可用集成,请访问 LlamaHub。

然后,在代码中使用它:

python
 
import chromadb from llama_index.vector_stores.chroma import ChromaVectorStore from llama_index.core import StorageContext chroma_client = chromadb.PersistentClient() chroma_collection = chroma_client.create_collection("quickstart") vector_store = ChromaVectorStore(chroma_collection=chroma_collection) storage_context = StorageContext.from_defaults(vector_store=vector_store) # 存储上下文定义了文档、嵌入和索引的存储后端。您可以了解更多关于存储及其定制方法。 from llama_index.core import VectorStoreIndex, SimpleDirectoryReader documents = SimpleDirectoryReader("data").load_data() index = VectorStoreIndex.from_documents( documents, storage_context=storage_context ) query_engine = index.as_query_engine() response = query_engine.query("What did the author do growing up?") print(response)

“查询时我想获取更多上下文”

python
 
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader documents = SimpleDirectoryReader("data").load_data() index = VectorStoreIndex.from_documents(documents) query_engine = index.as_query_engine(similarity_top_k=5) response = query_engine.query("What did the author do growing up?") print(response) # as_query_engine 在索引之上构建了一个默认的检索器和查询引擎。您可以通过传入关键字参数来配置检索器和查询引擎。这里我们配置检索器返回最相似的前5个文档(而非默认的2个)。您可以了解更多关于检索器和查询引擎的信息。

“我想使用不同的 LLM”

python
 
# 全局设置 from llama_index.core import Settings from llama_index.llms.ollama import Ollama Settings.llm = Ollama(model="mistral", request_timeout=60.0) # 局部设置 index.as_query_engine(llm=Ollama(model="mistral", request_timeout=60.0)) # 您可以了解更多关于定制 LLM 的信息。

“我想使用不同的响应模式”

python
 
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader documents = SimpleDirectoryReader("data").load_data() index = VectorStoreIndex.from_documents(documents) query_engine = index.as_query_engine(response_mode="tree_summarize") response = query_engine.query("What did the author do growing up?") print(response) # 您可以了解更多关于查询引擎和响应模式的信息。

“我想以流式方式返回响应”

python
 
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader documents = SimpleDirectoryReader("data").load_data() index = VectorStoreIndex.from_documents(documents) query_engine = index.as_query_engine(streaming=True) response = query_engine.query("What did the author do growing up?") response.print_response_stream() # 您可以了解更多关于流式响应的信息。

“我想要一个聊天机器人而非问答系统”

python
 
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader documents = SimpleDirectoryReader("data").load_data() index = VectorStoreIndex.from_documents(documents) query_engine = index.as_chat_engine() response = query_engine.chat("What did the author do growing up?") print(response) response = query_engine.chat("Oh interesting, tell me more.") print(response) # 了解更多关于聊天引擎的信息。

后续步骤

  • 全面了解几乎所有的可配置项:从“理解 LlamaIndex”开始深入学习。
  • 深入了解特定模块:查阅组件指南。

 

Video:AI 新世代

Tool:Llama3 在线Gemma在线ChatAIonline

Ref:https://docs.llamaindex.ai/en/stable/getting_started/customization/

Link:https://www.cnblogs.com/farwish/p/18133698

posted on 2024-04-14 23:04  ercom  阅读(228)  评论(0编辑  收藏  举报