mrjunaid commited on
Commit
cf99d45
·
1 Parent(s): 366dc51

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
Files changed (1) hide show
  1. main.py +67 -62
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
- 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
  @cl.on_message
47
- async def main(message: cl.Message):
48
- """Process incoming messages and generate responses."""
49
- # Send a thinking message
50
- 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_output
70
 
71
- # Update the thinking message with the actual response
72
- msg.content = response_content
73
- 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 interaction
79
- 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)}")
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