Skip to content

常见问题解答(FAQ)#

提示

如果您还没有安装 LlamaIndex 并完成 入门教程,请先进行安装。如果遇到不认识的术语,请查看 高级概念

在这一部分,我们将从您为 入门示例 编写的代码开始,展示您可能想要根据自己的用例对其进行自定义的最常见方式:

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("作者在成长过程中做了什么?")
print(response)

“我想将我的文档解析成更小的块”#

# 全局设置
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 进行安装:

pip install llama-index-vector-stores-chroma

要了解所有可用的集成,请查看 LlamaHub

然后,您可以在您的代码中使用它:

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)

StorageContext 定义了文档、嵌入和索引存储的后端。您可以了解更多关于 存储 以及 如何自定义它

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("作者在成长过程中做了什么?")
print(response)

“我想在查询时检索更多上下文”#

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("作者在成长过程中做了什么?")
print(response)

as_query_engine 在索引之上构建了默认的 检索器查询引擎。您可以通过传递关键字参数来配置检索器和查询引擎。在这里,我们配置检索器以返回最相似的前 5 个文档(而不是默认的 2 个)。您可以了解更多关于 检索器查询引擎


“我想使用不同的 LLM”#

# 全局设置
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))

您可以了解更多关于 自定义 LLMs


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

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("作者在成长过程中做了什么?")
print(response)

您可以了解更多关于 查询引擎响应模式


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

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("作者在成长过程中做了什么?")
response.print_response_stream()

您可以了解更多关于 流式响应


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

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("作者在成长过程中做了什么?")
print(response)

response = query_engine.chat("哦,有趣,告诉我更多。")
print(response)
了解更多关于聊天引擎的信息。


下一步#

  • 想要详细了解(几乎)所有可配置的内容吗?从了解 LlamaIndex开始。
  • 想要更深入地了解特定模块吗?查看组件指南