Refactor main.py by commenting out existing code for environment variable loading, agent initialization, and message processing. This prepares the file for further development and testing without executing the current logic.
Browse files
main.py
CHANGED
|
@@ -1,87 +1,92 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
-
from typing import cast
|
| 4 |
import chainlit as cl
|
| 5 |
-
from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel
|
| 6 |
-
from agents.run import RunConfig
|
| 7 |
|
| 8 |
# Load the environment variables from the .env file
|
| 9 |
-
load_dotenv()
|
| 10 |
|
| 11 |
-
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
| 12 |
|
| 13 |
-
# Check if the API key is present; if not, raise an error
|
| 14 |
-
if not gemini_api_key:
|
| 15 |
-
|
| 16 |
|
| 17 |
|
| 18 |
-
@cl.on_chat_start
|
| 19 |
-
async def start():
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
await cl.Message(content="Welcome to the Panaversity AI Assistant! How can I help you today?").send()
|
| 45 |
|
| 46 |
@cl.on_message
|
| 47 |
-
async def main(
|
| 48 |
-
"
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# from dotenv import load_dotenv
|
| 3 |
+
# from typing import cast
|
| 4 |
import chainlit as cl
|
| 5 |
+
# from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel
|
| 6 |
+
# from agents.run import RunConfig
|
| 7 |
|
| 8 |
# Load the environment variables from the .env file
|
| 9 |
+
# load_dotenv()
|
| 10 |
|
| 11 |
+
# gemini_api_key = os.getenv("GEMINI_API_KEY")
|
| 12 |
|
| 13 |
+
# # Check if the API key is present; if not, raise an error
|
| 14 |
+
# if 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_start
|
| 19 |
+
# async def start():
|
| 20 |
+
# #Reference: https://ai.google.dev/gemini-api/docs/openai
|
| 21 |
+
# 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_client
|
| 29 |
+
# )
|
| 30 |
|
| 31 |
+
# config = RunConfig(
|
| 32 |
+
# model=model,
|
| 33 |
+
# model_provider=external_client,
|
| 34 |
+
# tracing_disabled=True
|
| 35 |
+
# )
|
| 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 |
|
| 47 |
@cl.on_message
|
| 48 |
+
async def main(msg: cl.Message):
|
| 49 |
+
await cl.Message(content=f"You said: {msg.content}").send()
|
| 50 |
+
|
| 51 |
+
# @cl.on_message
|
| 52 |
+
# async def main(message: cl.Message):
|
| 53 |
+
# """Process incoming messages and generate responses."""
|
| 54 |
+
# # Send a thinking message
|
| 55 |
+
# msg = cl.Message(content="Thinking...")
|
| 56 |
+
# await msg.send()
|
| 57 |
|
| 58 |
+
# agent: Agent = cast(Agent, cl.user_session.get("agent"))
|
| 59 |
+
# config: RunConfig = cast(RunConfig, cl.user_session.get("config"))
|
| 60 |
|
| 61 |
+
# # Retrieve the chat history from the session.
|
| 62 |
+
# history = cl.user_session.get("chat_history") or []
|
| 63 |
|
| 64 |
+
# # Append the user's message to the history.
|
| 65 |
+
# history.append({"role": "user", "content": message.content})
|
| 66 |
|
| 67 |
|
| 68 |
+
# try:
|
| 69 |
+
# print("\n[CALLING_AGENT_WITH_CONTEXT]\n", history, "\n")
|
| 70 |
+
# result = Runner.run_sync(starting_agent = agent,
|
| 71 |
+
# input=history,
|
| 72 |
+
# run_config=config)
|
| 73 |
|
| 74 |
+
# response_content = result.final_output
|
| 75 |
|
| 76 |
+
# # Update the thinking message with the actual response
|
| 77 |
+
# msg.content = response_content
|
| 78 |
+
# await msg.update()
|
| 79 |
|
| 80 |
+
# # Update the session with the new history.
|
| 81 |
+
# cl.user_session.set("chat_history", result.to_input_list())
|
| 82 |
|
| 83 |
+
# # Optional: Log the interaction
|
| 84 |
+
# print(f"User: {message.content}")
|
| 85 |
+
# print(f"Assistant: {response_content}")
|
| 86 |
|
| 87 |
+
# except Exception as e:
|
| 88 |
+
# msg.content = f"Error: {str(e)}"
|
| 89 |
+
# await msg.update()
|
| 90 |
+
# print(f"Error: {str(e)}")
|
| 91 |
|
| 92 |
|