langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.69k stars 15.32k forks source link

langchain.chat_models.ChatOpenAI does not returns a response while langchain.OpeAI does rteturn results #6968

Closed plaban1981 closed 1 year ago

plaban1981 commented 1 year ago

System Info

langchain==0.0.208 penai==0.27.8 python==3.10.6

Who can help?

@hwchase17 Could you please help me in resolving the error whenver I run an gaent with the LLM as ChatOpenAI() i get the response as ""Do I need to use a tool? No.""

While the agent returns the response perfectly fine with llm= OpenAI().

The irony is two days prior the ChatOpenAI() function was working fine and returned the results perfectly fine.

Information

Related Components

Reproduction

`import langchain from langchain.prompts.base import StringPromptTemplate from langchain.prompts import PromptTemplate,StringPromptTemplate from langchain.agents import Tool, AgentExecutor, AgentOutputParser,LLMSingleActionAgent,initialize_agent from langchain import OpenAI,LLMChain from langchain.chat_models import ChatOpenAI from langchain.tools import DuckDuckGoSearchRun from langchain.schema import AgentAction,AgentFinish import re from typing import List,Union import os from getpass import getpass

os.environ["OPENAI_API_KEY"] = getpass() # search = DuckDuckGoSearchRun()

3640 def duck_wrapper(input_text):

search_results = search.run(f"site:webmd.com {input_text}")
return search_results

tools = [ Tool( name = "Search WebMD", func=duck_wrapper, description="useful for when you need to answer medical and pharmalogical questions" ) ]

Set up the base template

template = """Answer the following questions as best you can, but speaking as compasionate medical professional. You have access to the following tools:

{tools}

Use the following format:

Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question

Begin! Remember to answer as a compansionate medical professional when giving your final answer.

Previous conversation history: {history}

Question: {input} """ tool_names = [tool.name for tool in tools] history = None prompt = PromptTemplate(template=template,input_variables=["tools","tool_names","history","input"])

from langchain.agents.agent_types import AgentType agent = initialize_agent(tools=tools, llm=ChatOpenAI(), agent = AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=False, return_intermediate_steps=True )

try: response = agent({"input":"how to treat acid reflux?", "tools":tools, "tool_names":tool_names, "history":None, "chat_history":None}, return_only_outputs=True) except ValueError as e: response = str(e) if not response.startswith("Could not parse LLM output: "): raise e response = response.removeprefix("Could not parse LLM output:").removesuffix("`")

print(response)

`

Expected behavior

{'output': 'There are a few lifestyle changes and home remedies that can help treat acid reflux, such as avoiding food triggers, eating smaller and more frequent meals, elevating the head of the bed while sleeping, and avoiding lying down after eating. Over-the-counter medications such as antacids and proton pump inhibitors can also be effective in treating acid reflux. However, it is always best to consult with a doctor if symptoms persist or worsen.', 'intermediate_steps': [(AgentAction(tool='Search WebMD', tool_input='how to treat acid reflux', log='Thought: Do I need to use a tool? Yes\nAction: Search WebMD\nAction Input: how to treat acid reflux'), 'Eat Earlier. Going to bed on a full stomach makes nighttime heartburn more likely. A full stomach puts pressure on the valve at the top of the stomach, which is supposed to keep stomach acid out ... Trim the fat off of meat and poultry, and cut the skin off chicken. Tweaks like these might be enough to tame your heartburn. Tomatoes (including foods like salsa and marinara sauce) and citrus ... Abdominal bloating. Abdominal pain. Vomiting. Indigestion. Burning or gnawing feeling in the stomach between meals or at night. Hiccups. Loss of appetite. Vomiting blood or coffee ground-like ... Esophagitis Symptoms. Symptoms of esophagitis include: Difficult or painful swallowing. Acid reflux. Heartburn. A feeling of something of being stuck in the throat. Chest pain. Nausea. Vomiting. This backflow (acid reflux) can irritate the lining of the esophagus. People with GERD may have heartburn, a burning sensation in the back of the throat, chronic cough, laryngitis, and nausea.')]}

plaban1981 commented 1 year ago

attaching the notebook Notebook.zip

amanmibra commented 1 year ago

commenting to follow

I am noticing that the chat model sometimes returns parseable json with action and action_input, and other times not

dosubot[bot] commented 1 year ago

Hi, @plaban1981! I'm Dosu, and I'm here to help the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you reported an issue with the ChatOpenAI function in the langchain.chat_models module not returning a response, while the OpenAI function is returning results. You attached a notebook for reference, and another user, amanmibra, noticed that the chat model sometimes returns parseable JSON with action and action_input, and other times not.

Before we close this issue, we wanted to check with you if this issue is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your understanding and contribution to the LangChain project!