Spaces:
Sleeping
Sleeping
| import chainlit as cl | |
| import asyncio | |
| import os | |
| from dotenv import load_dotenv, find_dotenv | |
| from agents import ( | |
| Agent, | |
| Runner, | |
| AsyncOpenAI, | |
| set_default_openai_api, | |
| set_default_openai_client, | |
| ) | |
| from openai.types.responses import ResponseTextDeltaEvent | |
| load_dotenv(find_dotenv()) | |
| base_url = 'https://generativelanguage.googleapis.com/v1beta/openai/' | |
| api_key = os.getenv('gemini_api_key') | |
| model = 'gemini-2.0-flash' | |
| client = AsyncOpenAI(base_url=base_url, api_key=api_key) | |
| set_default_openai_client(client,use_for_tracing=False) | |
| set_default_openai_api('chat_completions') | |
| agent = Agent( | |
| name='Teacher', | |
| instructions='You are a teacher that teaches any topic in detail', | |
| model=model, | |
| #tool=[function_tool], | |
| ) | |
| async def on_chat_start(): | |
| cl.user_session.set("history", []) | |
| await cl.Message("Hi! This your Teacher. Please provide a brief description of the topic you would like to learn about.").send() | |
| async def handle(message: cl.Message): | |
| history = cl.user_session.get("history") | |
| history.append({ | |
| "role":"user", | |
| "content":message.content | |
| }) | |
| msg = cl.Message(content="") | |
| result = Runner.run_streamed(agent,f"{history}") | |
| async for event in result.stream_events(): | |
| if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent): | |
| response = event.data.delta | |
| await msg.stream_token(response) | |
| history.append({ | |
| "role":"teacher", | |
| "content":msg.content | |
| }) | |
| cl.user_session.set("history", history) | |
| await msg.update() | |