使用LangGraph构建自主RAG(3)

===

设置WebSearch API:

我们使用serperAPI进行网络搜索(谷歌搜索)。其免费层提供2500次免费网络搜索API。它能在1 - 2秒内为给定查询提供谷歌搜索结果。

你可以在这里找到更多详细信息并获取你的 API 密钥此处

ini 复制代码
from langchain_community.utilities import GoogleSerperAPIWrapper
from dotenv import load_dotenv
load_dotenv()
SERPER_API_KEY = os.getenv("SERPER_API_KEY")

search = GoogleSerperAPIWrapper()
# testing the google search API
search.run(query="What are the various vaccines of COVID-19")

输出:

arduino 复制代码
"Different types of COVID-19 vaccines: How they work · mRNA vaccine · mRNA vaccine · Messenger RNA (mRNA) vaccine · Viral vector vaccine · Viral vector vaccine ... First introduced in December 2020, the original COVID mRNA vaccines from both Pfizer and Moderna protected against the original SARS-CoV-2 virus ... Currently, there are two types of COVID-19 vaccines for use in the United States: mRNA, and protein subunit vaccines. None of these vaccines can ... On this page, you will find infographics to explain how different types of vaccines work, including the Pfizer/BioNTech vaccine, the Moderna vaccine and the ... Types of COVID-19 vaccines. Two types of COVID-19 vaccines are recommended for use in the United States: mRNA vaccines. Moderna COVID-19 Vaccine ... List of COVID-19 vaccine authorizations · Contents · Overview maps · Oxford--AstraZeneca · Pfizer--BioNTech · Janssen · Moderna · Sinopharm BIBP · Sputnik V. WHO recommends a simplified single-dose regime for primary immunization for most COVID-19 vaccines which would improve acceptance and uptake and provide ... Find information and answers to your questions about the COVID-19 vaccine, including scheduling, kid's shots, boosters, additional doses, records and more. The California Department of Public Health is dedicated to optimizing the health and well-being of Californians. Find out how the COVID-19 vaccines work, how many doses are needed, possible side effects and who shouldn't get the vaccine."

设置OpenAI API客户端:

我们将使用Open AI API的gpt-5-nano模型进行本次实验。

我选择这个模型是因为我的实验规模很小且复杂度较低。我的提示非常简单。在现实场景中,你可能需要使用更高级的模型(gpt-5-mini / gpt-5)。

参考以下代码:

python 复制代码
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
openai_api_key = os.getenv("OPEN_AI_KEY")
def get_llm_response(prompt: str) -> str:
    """Function to get response from LLM"""
    from openai import OpenAI
    client_llm = OpenAI(api_key=openai_api_key)
    response = client_llm.chat.completions.create(
        model="gpt-5-nano",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

## testing the LLM response
prompt = "Explain the theory of relativity in simple terms in 30 words"
response = get_llm_response(prompt)
print(response)

相对论认为,物理定律适用于所有观察者,不存在优先参考系,且没有任何物体的运动速度能超过光速。时间和空间会随运动和引力而弯曲,从根本上使质量和能量等价。

现在场地已经布置好了。让我们开始实施RAG。首先,我们将使用第一个来源(医疗问答)构建一个传统RAG,然后再转向智能体RAG。

相关推荐
PaperRed ai写作降重助手1 天前
高性价比 AI 论文写作软件推荐:2026 年预算友好型
人工智能·aigc·论文·写作·ai写作·智能降重
梁辰兴1 天前
百亿美元赌注变数,AI军备竞赛迎来转折点?
人工智能·ai·大模型·openai·英伟达·梁辰兴·ai军备竞赛
PaperRed ai写作降重助手1 天前
智能写作ai论文生成软件推荐
人工智能·aigc·ai写作·智能降重·paperred
IT·小灰灰1 天前
30行PHP,利用硅基流动API,网页客服瞬间上线
开发语言·人工智能·aigc·php
阿杰学AI2 天前
AI核心知识75——大语言模型之MAS (简洁且通俗易懂版)
人工智能·ai·语言模型·自然语言处理·agent·多智能体协作·mas
小程故事多_802 天前
深度搜索Agent架构全解析:从入门到进阶,解锁复杂问题求解密码
人工智能·架构·aigc
PaperRed ai写作降重助手2 天前
如何选择适合自己的AI智能降重写作软件
人工智能·深度学习·aigc·ai写作·论文降重·论文查重·智能降重
laplace01232 天前
Clawdbot 部署到飞书(飞连)使用教程(完整版)
人工智能·笔记·agent·rag·clawdbot
AI原生应用开发2 天前
AIGC领域Bard在通信领域的内容创作
ai·aigc·bard
老蒋每日coding2 天前
AIGC领域多模态大模型的知识图谱构建:技术框架与实践路径
人工智能·aigc·知识图谱