LangChain构建大语言模型应用7

一、搜索问答

需要说明,LLMRequestsChain、LLMChain在新版本中已经弃用,但新版本还不完善。

import os
os.environ['OPENAI_BASE_URL']=''
os.environ['OPENAI_API_KEY']=''

from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model='gpt-3.5-turbo-1106')

from langchain.chains import LLMRequestsChain
from langchain.chains import LLMChain   
from langchain.prompts import PromptTemplate

prompt = PromptTemplate.from_template('''
请根据如下搜索结果,回答用户问题。
搜索结果:
-----------
{requests_result}
-----------
问题:{question}
回答:
''')

llm_chain = LLMChain(llm=llm, prompt=prompt, verbose=True)

chain = LLMRequestsChain(llm_chain=llm_chain,verbose=True) # 这个链可以调用浏览器
question = '刀郎最近发布了什么新专辑'

inputs = {
    'question':question,
    'url':'https://www.baidu.com/s?wd=' + question.replace(' ', '+')
}
result = chain.invoke(inputs)
print(result)

# {'question': '刀郎最近发布了什么新专辑', 'url': 'https://www.baidu.com/s?wd=刀郎最近发布了什么新专辑', 'output': '刀郎最近发布了新专辑《山歌寥哉》,其中包含了新歌《罗刹海市》等歌曲。'}

二、多工具问答

谷歌浏览器用不了可以换成其他浏览器。

import os
os.environ['OPENAI_BASE_URL']=''
os.environ['OPENAI_API_KEY']=''
import warnings
warnings.filterwarnings('ignore')

from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
from langchain.tools import Tool
from langchain.chains import LLMRequestsChain, LLMChain
from langchain.prompts import Prompt
from langchain.memory import ConversationBufferMemory
from langchain.agents import create_react_agent, AgentExecutor,ZeroShotAgent
from langchain import hub

load_dotenv()

llm = ChatOpenAI(model='gpt-3.5-turbo-1106', temperature=0)
def generic_func(query):
    prompt = Prompt.from_template('回答问题:{query}')
    llm_chain = LLMChain(llm=llm, prompt=prompt)
    return llm_chain.invoke(query)

def search_func(query):
    prompt = Prompt.from_template('''
    请根据以下搜索结果,回答用户问题。
    搜索结果:
    {requests_result}
    问题:{query}
    ''')
    llm_chain = LLMChain(llm=llm, prompt=prompt)
    llm_request_chain = LLMRequestsChain(llm_chain=llm_chain)
    input = {
        'query': query,
        'url': 'https://www.google.com/search?q='+query.replace(' ', '+')
    }
    return llm_request_chain.invoke(input)

tools = [
    Tool(
        name='通用大模型',
        func=generic_func,
        description='利用大模型自身能力,回答问题。'
    ),
    Tool(
        name='搜索引擎',
        func=search_func,
        description='其他模型没有正确答案时,使用该工具。'
    ),
]

suffix = """Begin!

{chat_history}
Question: {input}
{agent_scratchpad}"""

agent_prompt = ZeroShotAgent.create_prompt(
    tools=tools,
    prefix='请用中文回答以下问题,可以使用以下工具:',
    suffix=suffix,
    input_variables=['chat_history', 'input', 'agent_scratchpad']
)

llm_chain = LLMChain(llm=llm, prompt=agent_prompt, verbose=True)
memory = ConversationBufferMemory(memory_key='chat_history')
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)
agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, memory=memory, verbose=True)

# prompt = hub.pull("hwchase17/openai-tools-agent")
# agent = create_react_agent(llm=llm,tools=tools,prompt=prompt)
# llm_chain = LLMChain(llm=llm, prompt=agent_prompt, verbose=True)
# memory = ConversationBufferMemory(memory_key='chat_history')
# agent_chain = AgentExecutor(agent=agent, tools=tools, memory=memory, verbose=True)

# 百日咳是一种什么病? 一般会有哪些症状?
while True:
    human_input = input('问题: ')
    result = agent_chain.invoke({'input':human_input})
    print('答案: ', result, '\n')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木珊数据挖掘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值