Created
January 23, 2026 17:41
-
-
Save wilyJ80/629077c49a20751e3d41099ad8706142 to your computer and use it in GitHub Desktop.
chainlit-langchain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Any, cast | |
| import chainlit as cl | |
| from langchain_core.runnables import Runnable, RunnableConfig | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| @cl.on_chat_start | |
| async def on_chat_start(): | |
| model = 'gemini-2.5-flash-lite' | |
| llm = ChatGoogleGenerativeAI(model=model) | |
| cl.user_session.set("runnable", llm) | |
| @cl.on_message | |
| async def on_message(message: cl.Message): | |
| runnable = cast(Runnable[Any, Any], cl.user_session.get("runnable")) | |
| msg = cl.Message(content="") | |
| async for chunk in runnable.astream( | |
| message.content, | |
| config = RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]) | |
| ): | |
| await msg.stream_token(chunk.content) | |
| await msg.send() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment