一、搜索问答
需要说明,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')