import os from openai import AsyncOpenAI from fastapi.responses import JSONResponse from chainlit.auth import create_jwt from chainlit.server import app import chainlit as cl client = AsyncOpenAI(api_key=os.environ["OPENAI_API_KEY"]) settings = { "model": "gpt-3.5-turbo", "temperature": 0.7, "max_tokens": 500, "top_p": 1, "frequency_penalty": 0, "presence_penalty": 0, } @app.get("/custom-auth") async def custom_auth(): # Verify the user's identity with custom logic. token = create_jwt(cl.User(identifier="Test User")) return JSONResponse({"token": token}) @cl.on_chat_start async def on_chat_start(): cl.user_session.set( "message_history", [{"role": "system", "content": "You are a helpful assistant."}], ) await cl.Message(content="Connected to Chainlit!").send() @cl.on_message async def on_message(message: cl.Message): message_history = cl.user_session.get("message_history") message_history.append({"role": "user", "content": message.content}) msg = cl.Message(content="") await msg.send() stream = await client.chat.completions.create( messages=message_history, stream=True, **settings ) async for part in stream: if token := part.choices[0].delta.content or "": await msg.stream_token(token) message_history.append({"role": "assistant", "content": msg.content}) await msg.update()