LONGYKING commited on
Commit ·
1ab6263
1
Parent(s): b953228
update
Browse files- app.py +74 -16
- giino-assistant.py → assistant.py +25 -0
- run.py +1 -1
app.py
CHANGED
|
@@ -1,23 +1,24 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from starlette.middleware.cors import CORSMiddleware
|
| 4 |
-
|
| 5 |
from chainlit.auth import create_jwt
|
| 6 |
from chainlit.user import User
|
| 7 |
from chainlit.utils import mount_chainlit
|
| 8 |
from chainlit.context import init_http_context, init_ws_context
|
| 9 |
-
from chainlit.session import WebsocketSession
|
| 10 |
import chainlit as cl
|
|
|
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
-
app.add_middleware(
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
)
|
| 21 |
|
| 22 |
|
| 23 |
@app.get("/custom-auth")
|
|
@@ -29,17 +30,74 @@ async def custom_auth():
|
|
| 29 |
@app.get("/app")
|
| 30 |
async def read_main():
|
| 31 |
init_http_context()
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
@app.get("/
|
| 36 |
-
async def
|
| 37 |
request: Request,
|
| 38 |
session_id: str,
|
|
|
|
| 39 |
):
|
|
|
|
|
|
|
| 40 |
ws_session = WebsocketSession.get_by_id(session_id=session_id)
|
| 41 |
init_ws_context(ws_session)
|
| 42 |
-
await cl.Message(content=
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from starlette.middleware.cors import CORSMiddleware
|
|
|
|
| 4 |
from chainlit.auth import create_jwt
|
| 5 |
from chainlit.user import User
|
| 6 |
from chainlit.utils import mount_chainlit
|
| 7 |
from chainlit.context import init_http_context, init_ws_context
|
| 8 |
+
from chainlit.session import WebsocketSession, HTTPSession
|
| 9 |
import chainlit as cl
|
| 10 |
+
import assistant as giino
|
| 11 |
+
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
+
# app.add_middleware(
|
| 16 |
+
# CORSMiddleware,
|
| 17 |
+
# allow_origins=["*"],
|
| 18 |
+
# allow_credentials=True,
|
| 19 |
+
# allow_methods=["*"],
|
| 20 |
+
# allow_headers=["*"],
|
| 21 |
+
# )
|
| 22 |
|
| 23 |
|
| 24 |
@app.get("/custom-auth")
|
|
|
|
| 30 |
@app.get("/app")
|
| 31 |
async def read_main():
|
| 32 |
init_http_context()
|
| 33 |
+
sid = await giino.start()
|
| 34 |
+
# await cl.Message(content="Hello, I am a chatbot!").send()
|
| 35 |
+
print(print("Session id from app:", cl.user_session.get("id")))
|
| 36 |
+
return {
|
| 37 |
+
"message": "Hello World from main app",
|
| 38 |
+
"sessionId": sid
|
| 39 |
+
}
|
| 40 |
|
| 41 |
+
@app.get("/chat/{session_id}/{message}")
|
| 42 |
+
async def chat(
|
| 43 |
request: Request,
|
| 44 |
session_id: str,
|
| 45 |
+
message: str
|
| 46 |
):
|
| 47 |
+
# http_session = HTTPSession(session_id=)
|
| 48 |
+
init_http_context()
|
| 49 |
ws_session = WebsocketSession.get_by_id(session_id=session_id)
|
| 50 |
init_ws_context(ws_session)
|
| 51 |
+
# await cl.Message(content=message).send()
|
| 52 |
+
msg = await giino.chat(message)
|
| 53 |
+
|
| 54 |
+
return msg
|
| 55 |
+
|
| 56 |
+
mount_chainlit(app=app, target="assistant.py", path="/telzho")
|
| 57 |
|
| 58 |
+
# import httpx
|
| 59 |
+
# from fastapi import FastAPI, HTTPException
|
| 60 |
+
# from pydantic import BaseModel
|
| 61 |
+
# import subprocess
|
| 62 |
+
# import asyncio
|
| 63 |
+
# import requests
|
| 64 |
+
#
|
| 65 |
+
# app = FastAPI()
|
| 66 |
+
#
|
| 67 |
+
#
|
| 68 |
+
# class Message(BaseModel):
|
| 69 |
+
# message: str
|
| 70 |
+
#
|
| 71 |
+
#
|
| 72 |
+
# @app.post("/send")
|
| 73 |
+
# async def send_message(message: Message):
|
| 74 |
+
# try:
|
| 75 |
+
# # Send the message to the Chainlit app
|
| 76 |
+
# response = await forward_to_chainlit(message.message)
|
| 77 |
+
# return {"response": response}
|
| 78 |
+
# except Exception as e:
|
| 79 |
+
# raise HTTPException(status_code=500, detail=str(e))
|
| 80 |
+
#
|
| 81 |
+
# async def forward_to_chainlit(message: str) -> str:
|
| 82 |
+
# chainlit_url = "http://0.0.0.0:8000/api/chainlit" # Adjust as needed
|
| 83 |
+
# payload = {"message": message}
|
| 84 |
+
#
|
| 85 |
+
# async with httpx.AsyncClient() as client:
|
| 86 |
+
# response = await client.post(chainlit_url, json=payload)
|
| 87 |
+
# response.raise_for_status() # Ensure the request was successful
|
| 88 |
+
# response_data = response.json()
|
| 89 |
+
# return response_data.get("response", "No response from Chainlit")
|
| 90 |
+
#
|
| 91 |
+
#
|
| 92 |
+
#
|
| 93 |
+
# # Existing code for Chainlit app setup...
|
| 94 |
+
#
|
| 95 |
+
# @app.on_event("startup")
|
| 96 |
+
# async def startup_event():
|
| 97 |
+
# # Start the Chainlit app in a separate thread
|
| 98 |
+
# loop = asyncio.get_event_loop()
|
| 99 |
+
# loop.run_in_executor(None, start_chainlit)
|
| 100 |
+
#
|
| 101 |
+
#
|
| 102 |
+
# def start_chainlit():
|
| 103 |
+
# subprocess.run(["chainlit", "run", "chainlit_app.py", "-h", "0.0.0.0", "-p", "8000"])
|
giino-assistant.py → assistant.py
RENAMED
|
@@ -184,6 +184,7 @@ async def set_starters():
|
|
| 184 |
|
| 185 |
@cl.on_chat_start
|
| 186 |
async def start():
|
|
|
|
| 187 |
is_dev_mode = True if os.getenv("DEV_MODE") else False
|
| 188 |
|
| 189 |
cl.user_session.set(
|
|
@@ -328,6 +329,7 @@ async def start():
|
|
| 328 |
|
| 329 |
# Set the assistant in the user session
|
| 330 |
cl.user_session.set("agent", telzho_assistant)
|
|
|
|
| 331 |
|
| 332 |
|
| 333 |
@cl.on_audio_chunk
|
|
@@ -385,6 +387,28 @@ async def on_audio_end(elements: list[ElementBased]):
|
|
| 385 |
answer_message.elements = [output_audio_el]
|
| 386 |
await answer_message.update()
|
| 387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
@cl.on_message
|
| 390 |
async def main(message: cl.Message):
|
|
@@ -407,6 +431,7 @@ async def main(message: cl.Message):
|
|
| 407 |
|
| 408 |
message_history.append({"role": "assistant", "content": msg.content})
|
| 409 |
await msg.update()
|
|
|
|
| 410 |
|
| 411 |
# Run the Chainlit application
|
| 412 |
if __name__ == "__main__":
|
|
|
|
| 184 |
|
| 185 |
@cl.on_chat_start
|
| 186 |
async def start():
|
| 187 |
+
print("Session id:", cl.user_session.get("id"))
|
| 188 |
is_dev_mode = True if os.getenv("DEV_MODE") else False
|
| 189 |
|
| 190 |
cl.user_session.set(
|
|
|
|
| 329 |
|
| 330 |
# Set the assistant in the user session
|
| 331 |
cl.user_session.set("agent", telzho_assistant)
|
| 332 |
+
return cl.user_session.get("id")
|
| 333 |
|
| 334 |
|
| 335 |
@cl.on_audio_chunk
|
|
|
|
| 387 |
answer_message.elements = [output_audio_el]
|
| 388 |
await answer_message.update()
|
| 389 |
|
| 390 |
+
@cl.on_message
|
| 391 |
+
async def chat(message: str):
|
| 392 |
+
|
| 393 |
+
message_history = cl.user_session.get("message_history")
|
| 394 |
+
message_history.append({"role": "user", "content": message})
|
| 395 |
+
|
| 396 |
+
msg = cl.Message(content="")
|
| 397 |
+
await msg.send()
|
| 398 |
+
|
| 399 |
+
# Retrieve the assistant from the user sessions
|
| 400 |
+
agent = cl.user_session.get("agent")
|
| 401 |
+
|
| 402 |
+
# Process the user message using the assistant
|
| 403 |
+
for delta in agent.run(message, stream=True):
|
| 404 |
+
for part in delta:
|
| 405 |
+
if token := part or "":
|
| 406 |
+
# Send the response back to the user
|
| 407 |
+
await msg.stream_token(token)
|
| 408 |
+
|
| 409 |
+
message_history.append({"role": "assistant", "content": msg.content})
|
| 410 |
+
await msg.update()
|
| 411 |
+
return msg.content
|
| 412 |
|
| 413 |
@cl.on_message
|
| 414 |
async def main(message: cl.Message):
|
|
|
|
| 431 |
|
| 432 |
message_history.append({"role": "assistant", "content": msg.content})
|
| 433 |
await msg.update()
|
| 434 |
+
return msg.content
|
| 435 |
|
| 436 |
# Run the Chainlit application
|
| 437 |
if __name__ == "__main__":
|
run.py
CHANGED
|
@@ -34,4 +34,4 @@ async def main():
|
|
| 34 |
|
| 35 |
asyncio.run(main())
|
| 36 |
# web: docker-compose up --build
|
| 37 |
-
# web: python -m chainlit run
|
|
|
|
| 34 |
|
| 35 |
asyncio.run(main())
|
| 36 |
# web: docker-compose up --build
|
| 37 |
+
# web: python -m chainlit run assistant.py -h --port 8080
|