cohere-python icon indicating copy to clipboard operation
cohere-python copied to clipboard

Cannot stream response from cohere

Open hizbul-inument opened this issue 1 year ago • 1 comments

I am using langgraph stream_events and inside tools i am using cohere.

from langgraph.prebuilt import create_react_agent

async def generate_stream_response(message: str, user: dict, prompt_dict: dict):
    tools = [search_for_australia, search_for_newzealand]
    langgraph_agent_executor = create_react_agent(
        llm,
        tools=tools,
        state_modifier=None,
        checkpointer=memory
    )

    config = {"configurable": {"thread_id": prompt_dict['group_id']}}
    
    prompt = {"messages": [{"role": "user", "content": message}]}
    async for event in langgraph_agent_executor.astream_events(prompt, config, stream_mode="values", version="v2"):
         print(event)

I can not yeild response from tools.


@tool
def search_for_australia(query: str):
    """Searches Australia for relevant information"""
    documents = query_faiss(faiss_index, query, model, texts)
    llm_response_stream = co.chat_stream(
        messages=[{"role": "user", "content": query}],
        documents=documents,
        model="command-r-plus-08-2024",
        temperature=0.4
    )
    for event in llm_response_stream:
        if event:
            if event.type == "content-delta":
                print(event.delta.message.content.text)

But, can print like above. And If I co.chat and return response the data the it works. But, it's not a solution, cause it's return all data at a time and langgraph_agent_executor.astream_events is waiting until get the response.

Note: I can not show the citations also.

hizbul-inument avatar Oct 15 '24 07:10 hizbul-inument

Hi @hizbul-inument Thanks for reporting this issue... Could you please try using the tool_calling_agent and see if this issue persists? There is a known mismatch between the create_react_agent implementation and Cohere integration in langgraph...

Another alternative could be to try the create_cohere_react_agent from langchain-cohere package. Do let me know if this helps!

Anirudh31415926535 avatar Nov 18 '24 09:11 Anirudh31415926535