You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I added a very descriptive title to this question.
I searched the LangChain documentation with the integrated search.
I used the GitHub search to find a similar question and didn't find it.
Commit to Help
I commit to help with one of those options 👆
Example Code
tool.pyforchunkinself._stream_response(response_str):
yieldchunkdef_stream_response(self, response_str):
"""Helper function to stream response in chunks. Args: response_str (str): The response string to be streamed. Yields: str: A chunk of the response string. """response_json=json.loads(response_str)
answer_content=response_json['tool_output']['answer']
chunk_size=100# Define your chunk size (# of characters per chunk)# Stream the answer content in chunksforiinrange(0, len(answer_content), chunk_size):
yieldanswer_content[i:i+chunk_size]
# Stream the rest of the response in chunksresponse_json['tool_output']['answer'] =""remaining_response_str=json.dumps(response_json)
foriinrange(0, len(remaining_response_str), chunk_size):
yieldremaining_response_str[i:i+chunk_size]
chat.py->theerrorisoccurringonastream_events```pythontry:
events= []
asyncforeventinagent_executor.astream_events(agent_input, version="v1"):
print(f"Event from astream log: {event} ",end="|", flush=True)
events.append(event)
exceptExceptionase:
logger.error(f"Error during agent execution: {e}")
yield"Sorry, there was an error processing your request."
### Description
My goal is to stream the response I get from a tool and then stream the final output.
I am using a custom tool that is seemingly working as intended, I am able to stream the response to my chat.py, however when I use astream_log or astream_event (not really sure which to use), when it gets to the actual llm answer portion it throws an error "Cannot pickle a generator"
I am using v1 of langchain
### System Info
root@e12630058745:/workspaces# pip freeze | grep langchain
langchain==0.1.9
langchain-aws==0.1.6
langchain-community==0.0.29
langchain-core==0.1.52
langchain-openai==0.1.1
macosx
python 3.11
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Checked other resources
Commit to Help
Example Code
Beta Was this translation helpful? Give feedback.
All reactions