为什么我无法使用 create_react_agent() 创建反应代理?

问题描述 投票:0回答:1

我在这里做错了什么?

import streamlit as st 
from langchain_groq import ChatGroq 
from langchain_community.tools import DuckDuckGoSearchRun 
from langchain.agents import AgentExecutor, create_react_agent 
from langchain_community.callbacks.streamlit import StreamlitCallbackHandler 
from langchain import hub 
import os  
from dotenv import load_dotenv 
load_dotenv() 

os.environ['GROQ_API_KEY'] =  os.getenv("GROQ_API_KEY") 
 
search=DuckDuckGoSearchRun(name="Search") 
prompt=hub.pull("hwchase17/react") 
 
 
st.title("🔎 LangChain - Chat with search") 

 
if "messages" not in st.session_state: 
    st.session_state["messages"]=[ 
        {"role":"assisstant","content":"Hi,I'm a chatbot who can search the web. How can I help you?"} 
    ] 
 
for msg in st.session_state.messages: 
    st.chat_message(msg["role"]).write(msg['content']) 
 
if prompt:=st.chat_input(placeholder="What is machine learning?"): 
    st.session_state.messages.append({"role":"user","content":prompt}) 
    st.chat_message("user").write(prompt) 
 
    model=ChatGroq(model_name="Llama3-8b-8192",streaming=True) 
    tools=[search] 
    agent = create_react_agent(model, tools, prompt) 
 
    search_agent = AgentExecutor(agent=agent, tools=tools) 
 
    with st.chat_message("assistant"): 
        st_cb=StreamlitCallbackHandler(st.container(),expand_new_thoughts=False) 
        response=search_agent.invoke(st.session_state.messages,callbacks=[st_cb]) 
        st.session_state.messages.append({'role':'assistant',"content":response}) 
        st.write(response) 

错误是

AttributeError: 'str' object has no attribute 'input_variables'
File "/path/to/langchain/agents/react/agent.py", line 114, in create_react_agent

prompt.input_variables + list(prompt.partial_variables)

我试图创建一个简单的代理,通过

create_react_agent

python artificial-intelligence streamlit langchain agent
1个回答
0
投票

if 条件中的

prompt
变量:

if prompt:=st.chat_input(placeholder="What is machine learning?"):

与您从集线器中提取的提示相冲突:

prompt = hub.pull(...)

请通过更改其中任何一个的名称来解决此问题。

该错误清楚地表明您正在向

agent = create_react_agent(model, tools, prompt)
发送带有错误数据类型的提示。

© www.soinside.com 2019 - 2024. All rights reserved.