langchain-ai / langchain

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

Streaming response comes with extra data. #27182

Open hizbul-inument opened 1 week ago

hizbul-inument commented 1 week ago

Checked other resources

Example Code

async def generate_stream_response(message: str, user: dict, prompt_dict: dict):
    session_data["chat_history_lc"].extend([HumanMessage(content=message)])
    prompt = ChatPromptTemplate.from_messages(session_data["chat_history_lc"])
    agent = create_cohere_react_agent(
                llm=llm,
                tools=[search_for_1, search_for_2],
                prompt=prompt,
            )
    agent_executor = AgentExecutor(agent=agent, tools=[search_for_1, search_for_2], verbose=True)

    # Get Response
    async for event in agent_executor.astream_events({
                "preamble": session_data["initial_prompt"],
        }, version="v2"):
        if event["event"] == "on_chat_model_stream":
            chunk_content = serialize_aimessagechunk(event["data"]["chunk"])
            chunk_content_html = chunk_content.replace("\n", "<br>")
            yield f"data: {json.dumps({'event': 'stream', 'data': chunk_content_html})}\n\n"

Error Message and Stack Trace (if applicable)

Following is the response message:

Plan: I will greet the user and ask for their name and location, then tailor my response accordingly. Action: ```json [ { "tool_name": "directly_answer", "parameters": {} } ]


Relevant Documents: None
Cited Documents: None
Answer: Hi! It's lovely to hear from you. What's your name, and are you based in Australia or New Zealand?
Grounded answer: Hi! It's lovely to hear from you. What's your name, and are you based in Australia or New Zealand?

Following is the event logs (sample):

{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content='Plan', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=':', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' I', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' will', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' greet', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' the', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' user', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' and', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' ask', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' them', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' for', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' their', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' name', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' and', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=' location', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=',', additional_kwargs={}, response_metadata={}, id='run-dfedab08-8416-4f6c-9a81-b7b254b45e6f')}, 'run_id': 'dfedab08-8416-4f6c-9a81-b7b254b45e6f', 'name': 'ChatCohere', 'tags': ['seq:step:2'], 'metadata': {'ls_provider': 'cohere', 'ls_model_name': 'command-r', 'ls_model_type': 'chat', 'ls_temperature': 0.3, 'ls_stop': ['\nObservation:']}, 'parent_ids': ['d0727885-7c6f-4c73-ade5-bebd71ee7944', '73a65464-a75a-40e0-96a0-7b3668a36923', 'ff035de4-e28e-4c77-a9cb-1ffb64697325', '39af241c-9bc9-4ca9-94fc-a35f3a8a5975']}
{

Description

I using Langchain and Cohere.

It works fine whenever i used invoke with agent executor, but whenever i moved for streaming and using astream_events i got extra data along with the response. I'd like to get only Hi! It's lovely to hear from you. What's your name, and are you based in Australia or New Zealand? as response.

System Info

Django==5.1 cohere==5.11.0 uuid st-supabase-connection==1.2.0 requests==2.31.0 langchain==0.3.1 langchain-cohere==0.3.0 langchain-experimental==0.3.2 faiss-cpu sentence_transformers supabase==2.6.0 supafunc==0.5.1 python-dotenv==1.0.1 authlib==1.2.1 django-cors-headers==4.4.0 python-docx==1.1.2 uvicorn

OS Linux / Ubuntu

xnnxnyx commented 1 week ago

Hello! We're a group of students from the University of Toronto Scarborough, and we're excited to contribute to LangChain. We'd love the opportunity to investigate this bug further.