File size: 871 Bytes
2e5aca2
 
 
 
 
42cef40
2e5aca2
 
 
 
 
 
42cef40
2e5aca2
 
 
 
 
 
 
 
 
 
 
42cef40
2e5aca2
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import chainlit as cl
from dotenv import load_dotenv
from graph import create_graph
from langchain_core.runnables import RunnableConfig
from starters import set_starters

load_dotenv()

OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
graph = create_graph()


@cl.on_message
async def main(message: cl.Message):
    """
    This function will be called every time a message is recieved from a session.
    """

    msg = cl.Message(content="")

    async for event in graph.astream_events(
        {"question": message.content},
        config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
        version="v2",
    ):
        if (
            event["event"] == "on_chat_model_stream"
            and event["metadata"]["langgraph_node"] == "generate"
        ):
            await msg.stream_token(event["data"]["chunk"].content)

    await msg.send()