Streaming Langchain response to React Native front end. #95
Replies: 1 comment
-
Sorry, posted this in the wrong place. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Really hoping someone can help me with this. And bare with me as I'm fairly new to all of this. I have created a simple Python backend which creates a Langchain chain with streaming enabled. I then want to stream this response, word by word, back to a react native frontend. However, I can't get it to work properly. I can get the response to print word by word to my terminal, but its not sending anything to the front end. Here is my code:
`import json
import os
import httpx
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI, ChatAnthropic
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.schema import HumanMessage
from langchain.callbacks.base import BaseCallbackHandler
from dotenv import load_dotenv
load_dotenv()
openai_api_key = os.environ.get("OPENAI_API_KEY")
app = FastAPI()
class MyCustomHandler(BaseCallbackHandler):
def on_llm_new_token(self, token: str, **kwargs) -> None:
print(f"My custom handler, token: {token}")
return token
async def gpt4_completion_stream(prompt):
chat = ChatOpenAI(streaming=True, callbacks=[MyCustomHandler()], temperature=0)
response = chat([HumanMessage(content=prompt)])
@app.get("/generate")
async def gpt4_completion_route(prompt: str):
response = await gpt4_completion_stream(prompt)
return StreamingResponse(response, media_type="text/event-stream")`
The output to the terminal looks like this with the test pompt "hello"
My custom handler, token:
My custom handler, token: Hello
My custom handler, token: !
My custom handler, token: How
My custom handler, token: can
My custom handler, token: I
My custom handler, token: assist
My custom handler, token: you
My custom handler, token: today
My custom handler, token: ?
My custom handler, token:
On the front end side, the message recieved looks like this:
Received event: Object {
"message": "cannot parse response",
"type": "error",
"xhrState": 4,
"xhrStatus": 0,
}
If anyone has any ideas, please let me know!! Thanks so much!
Beta Was this translation helpful? Give feedback.
All reactions