heyibad/chainlit-basicbot
0
1import os2from dotenv import load_dotenv3from typing import cast4import chainlit as cl5from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel6from agents.run import RunConfig7 8# Load the environment variables from the .env file9load_dotenv()10 11gemini_api_key = os.getenv("GEMINI_API_KEY")12 13# Check if the API key is present; if not, raise an error14if not gemini_api_key:15 raise ValueError("GEMINI_API_KEY is not set. Please ensure it is defined in your .env file.")16 17 18@cl.on_chat_start19async def start():20 #Reference: https://ai.google.dev/gemini-api/docs/openai21 external_client = AsyncOpenAI(22 api_key=gemini_api_key,23 base_url="https://generativelanguage.googleapis.com/v1beta/openai/",24 )25 26 model = OpenAIChatCompletionsModel(27 model="gemini-2.0-flash",28 openai_client=external_client29 )30 31 config = RunConfig(32 model=model,33 model_provider=external_client,34 tracing_disabled=True35 )36 """Set up the chat session when a user connects."""37 # Initialize an empty chat history in the session.38 cl.user_session.set("chat_history", [])39 40 cl.user_session.set("config", config)41 agent: Agent = Agent(name="Assistant", instructions="You are a helpful assistant", model=model)42 cl.user_session.set("agent", agent)43 44 await cl.Message(content="Welcome to the Panaversity AI Assistant! How can I help you today?").send()45 46@cl.on_message47async def main(message: cl.Message):48 """Process incoming messages and generate responses."""49 # Send a thinking message50 msg = cl.Message(content="Thinking...")51 await msg.send()52 53 agent: Agent = cast(Agent, cl.user_session.get("agent"))54 config: RunConfig = cast(RunConfig, cl.user_session.get("config"))55 56 # Retrieve the chat history from the session.57 history = cl.user_session.get("chat_history") or []58 59 # Append the user's message to the history.60 history.append({"role": "user", "content": message.content})61 62 63 try:64 print("\n[CALLING_AGENT_WITH_CONTEXT]\n", history, "\n")65 result = Runner.run_sync(starting_agent = agent,66 input=history,67 run_config=config)68 69 response_content = result.final_output70 71 # Update the thinking message with the actual response72 msg.content = response_content73 await msg.update()74 75 # Update the session with the new history.76 cl.user_session.set("chat_history", result.to_input_list())77 78 # Optional: Log the interaction79 print(f"User: {message.content}")80 print(f"Assistant: {response_content}")81 82 except Exception as e:83 msg.content = f"Error: {str(e)}"84 await msg.update()85 print(f"Error: {str(e)}")