langchain-ai / langchain-cohere

MIT License
31 stars 20 forks source link

ChatCohere always returns an AIMessage with empty content when bound to a tool #38

Closed SalamanderXing closed 2 months ago

SalamanderXing commented 5 months ago

Below the example to reproduce it. Happens the same with command-r or command-r-plus. Also it happens regardless of the structure of the tool.

from langchain_cohere import ChatCohere
from langchain_core.messages import HumanMessage, AIMessage, AnyMessage, SystemMessage
from pydantic.v1 import BaseModel

class SelectIceCream(BaseModel):
    """Use this function only when the user wants to buy ice cream"""

    ice_cream_type: str

cohere_api_key = "<...>"
llm = ChatCohere(model="command-r", temperature=0.6, cohere_api_key=cohere_api_key)
llm_with_tools = llm.bind_tools([SelectIceCream])

chat = [
    SystemMessage(content="You are a helpful assistant"),
    HumanMessage(content="Where is the city of Paris? Be concise."),
]

def main():
    response = llm.invoke(chat)
    print(f"Without tools:\t{response=} \n\n---\n\n")

    response = llm_with_tools.invoke(chat)
    print(f"With tools:\t{response=}")

main()

Outputs:

Without tools:  response=AIMessage(content='The city of Paris is in the north of France, on the River Seine.', additional_kwargs={'documents': None, 'citations': None, 'search_results': None, 'search_queries': None, 'is_search_required': None, 'generation_id': '86f2f8b4-a99d-4246-8d72-6b7843ff55d0'}, response_metadata={'documents': None, 'citations': None, 'search_results': None, 'search_queries': None, 'is_search_required': None, 'generation_id': '86f2f8b4-a99d-4246-8d72-6b7843ff55d0'}, id='run-ede8f9b2-9e9a-4086-af6a-0274c02c315e-0') 

---

With tools:     response=AIMessage(content='', additional_kwargs={'documents': None, 'citations': None, 'search_results': None, 'search_queries': None, 'is_search_required': None, 'generation_id': 'f4ed5db6-7ea7-4495-a06a-f364cd53b417'}, response_metadata={'documents': None, 'citations': None, 'search_results': None, 'search_queries': None, 'is_search_required': None, 'generation_id': 'f4ed5db6-7ea7-4495-a06a-f364cd53b417'}, id='run-ef87eae8-7408-4f36-9f07-778ebd49f612-0')

So without tools gives a normal reply but the response content of the llm bound to tools is always an empty string.

I'm using langchain-core==0.1.16 and langchain-cohere==0.1.3

harry-cohere commented 5 months ago

Thanks for reporting @SalamanderXing - we'll look into this issue this week

alwaysqzj commented 4 months ago

The same problem

Anirudh31415926535 commented 2 months ago

Hi @SalamanderXing @alwaysqzj This issue should be resolved with the latest versions of langchain-cohere and langchain-core! I've run the same code on my end, and see that it's working fine! langchain-cohere >= 0.1.9 and langchain-cohere==0.2.24

Closing the issue since it works on my end now, but do feel free to reopen/raise another one in case the problem hasn't been solved!