主题
提示
如果你还没有做过,请先安装 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("What did the author do growing up?")
print(response)“我想把文档切分成更小的块”
# Global settings
from llama_index.core import Settings
Settings.chunk_size = 512
# Local settings
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想了解全部可用集成,请看集成页面。
然后,就可以在代码里使用它:
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("What did the author do growing up?")
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("What did the author do growing up?")
print(response)as_query_engine 会在索引之上构建默认的 retriever 和 query engine。你可以通过关键字参数来配置检索器和查询引擎。这里我们把检索器配置为返回最相似的前 5 个文档(默认是 2 个)。你可以进一步了解检索器和查询引擎。
”我想换一个 LLM”
# Global settings
from llama_index.core import Settings
from llama_index.llms.ollama import Ollama
Settings.llm = Ollama(
model="mistral",
request_timeout=60.0,
# Manually set the context window to limit memory usage
context_window=8000,
)
# Local settings
index.as_query_engine(
llm=Ollama(
model="mistral",
request_timeout=60.0,
# Manually set the context window to limit memory usage
context_window=8000,
)
)你可以进一步了解如何定制 LLM。
”我想换一种响应模式”
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)”我想让回答流式返回”
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()你可以进一步了解流式响应。
”我想要聊天机器人,而不是 Q&A”
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 开始。
- 想更深入地理解某个具体模块?请看组件指南。