simonguest commited on
Commit
4f295e6
·
verified ·
1 Parent(s): 7420b2a

Initial commit

Browse files
Files changed (3) hide show
  1. Dockerfile +28 -0
  2. main.py +171 -0
  3. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.13.1
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ # Set up a new user named "user" with user ID 1000
10
+ RUN useradd -m -u 1000 user
11
+
12
+ # Switch to the "user" user
13
+ USER user
14
+
15
+ # Set home to the user's home directory
16
+ ENV HOME=/home/user \
17
+ PATH=/home/user/.local/bin:$PATH
18
+
19
+ # Set the working directory to the user's home directory
20
+ WORKDIR $HOME/app
21
+
22
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
23
+ COPY --chown=user . $HOME/app
24
+
25
+ # Expose port for local testing
26
+ EXPOSE 7860
27
+
28
+ CMD ["python", "main.py"]
main.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio import ChatMessage
3
+ from agents import Agent, Runner, function_tool, FileSearchTool
4
+
5
+ VECTOR_STORE_ID = "vs_6896d8c959008191981d645850b42313"
6
+
7
+
8
+ @function_tool
9
+ def get_bytes_cafe_menu(date: str) -> any:
10
+ return {
11
+ f"{date}": {
12
+ "daily byte": {
13
+ "name": "Steak Quesadilla",
14
+ "price": 12,
15
+ "description": "Flank steak, mixed cheese in a flour tortilla served with air fried potatoes, sour cream and salsa",
16
+ },
17
+ "vegetarian": {
18
+ "name": "Impossible Quesadilla",
19
+ "price": 12,
20
+ "description": "Impossible plant based product, mixed cheese in a flour tortilla served with air fried potatoes, sour cream and salsa",
21
+ },
22
+ "international": {
23
+ "name": "Chicken Curry",
24
+ "price": 12,
25
+ "description": "Chicken thighs, onion, carrot, potato, curry sauce served over rice",
26
+ },
27
+ }
28
+ }
29
+
30
+
31
+ cafe_agent = Agent(
32
+ name="Cafe Agent",
33
+ instructions="You help students locate and provide information about the Bytes Cafe.",
34
+ tools=[
35
+ get_bytes_cafe_menu,
36
+ ],
37
+ )
38
+
39
+
40
+ building_agent = Agent(
41
+ name="Building Agent",
42
+ instructions="You help students locate and provide information about buildings and rooms on campus. Be descriptive when giving locations.",
43
+ tools=[
44
+ FileSearchTool(
45
+ max_num_results=3,
46
+ vector_store_ids=[VECTOR_STORE_ID],
47
+ include_search_results=True,
48
+ )
49
+ ],
50
+ )
51
+
52
+ course_agent = Agent(
53
+ name="Course Agent",
54
+ instructions="You help students find out information about courses held at DigiPen.",
55
+ tools=[
56
+ FileSearchTool(
57
+ max_num_results=5,
58
+ vector_store_ids=[VECTOR_STORE_ID],
59
+ include_search_results=True,
60
+ )
61
+ ],
62
+ )
63
+
64
+ handbook_agent = Agent(
65
+ name="Handbook Agent",
66
+ instructions="You help students navigate the school handbook, providing information about campus policies and student conduct.",
67
+ tools=[
68
+ FileSearchTool(
69
+ max_num_results=5,
70
+ vector_store_ids=[VECTOR_STORE_ID],
71
+ include_search_results=True,
72
+ )
73
+ ],
74
+ )
75
+
76
+ agent = Agent(
77
+ name="DigiPen Campus Assistant",
78
+ instructions="You are a helpful campus assistant that can plan and execute tasks for students at DigiPen. Please be concise and accurate in handing off tasks to other agents as needed.",
79
+ handoffs=[building_agent, course_agent, handbook_agent, cafe_agent],
80
+ )
81
+
82
+
83
+ async def chat_with_agent(user_msg: str, history: list):
84
+ messages = [{"role": msg["role"], "content": msg["content"]} for msg in history]
85
+ messages.append({"role": "user", "content": user_msg})
86
+ responses = []
87
+ reply_created = False
88
+ active_agent = None
89
+
90
+ result = Runner.run_streamed(agent, messages)
91
+ async for event in result.stream_events():
92
+ if event.type == "raw_response_event":
93
+ if event.data.type == "response.output_text.delta":
94
+ if not reply_created:
95
+ responses.append(ChatMessage(role="assistant", content=""))
96
+ reply_created = True
97
+ responses[-1].content += event.data.delta
98
+ elif event.type == "agent_updated_stream_event":
99
+ active_agent = event.new_agent.name
100
+ responses.append(
101
+ ChatMessage(
102
+ content=event.new_agent.name,
103
+ metadata={"title": "Agent Now Running", "id": active_agent},
104
+ )
105
+ )
106
+ elif event.type == "run_item_stream_event":
107
+ if event.item.type == "tool_call_item":
108
+ if event.item.raw_item.type == "file_search_call":
109
+ responses.append(
110
+ ChatMessage(
111
+ content=f"Query used: {event.item.raw_item.queries}",
112
+ metadata={
113
+ "title": "File Search Completed",
114
+ "parent_id": active_agent,
115
+ },
116
+ )
117
+ )
118
+ else:
119
+ tool_name = getattr(event.item.raw_item, "name", "unknown_tool")
120
+ tool_args = getattr(event.item.raw_item, "arguments", {})
121
+ responses.append(
122
+ ChatMessage(
123
+ content=f"Calling tool {tool_name} with arguments {tool_args}",
124
+ metadata={"title": "Tool Call", "parent_id": active_agent},
125
+ )
126
+ )
127
+ if event.item.type == "tool_call_output_item":
128
+ responses.append(
129
+ ChatMessage(
130
+ content=f"Tool output: '{event.item.raw_item['output']}'",
131
+ metadata={"title": "Tool Output", "parent_id": active_agent},
132
+ )
133
+ )
134
+ if event.item.type == "handoff_call_item":
135
+ responses.append(
136
+ ChatMessage(
137
+ content=f"Name: {event.item.raw_item.name}",
138
+ metadata={
139
+ "title": "Handing Off Request",
140
+ "parent_id": active_agent,
141
+ },
142
+ )
143
+ )
144
+ yield responses
145
+
146
+
147
+ demo = gr.ChatInterface(
148
+ chat_with_agent,
149
+ title="DigiPen Campus Assistant",
150
+ theme=gr.themes.Soft(
151
+ primary_hue="red", secondary_hue="slate", font=[gr.themes.GoogleFont("Inter")]
152
+ ),
153
+ examples=[
154
+ "Where is the 'Hopper' room located?",
155
+ "I'm trying to find the WANIC classrooms. Can you help?",
156
+ "What's the policy for eating in auditoriums?",
157
+ "Where do I pickup my parking pass?",
158
+ "What's today's vegetarian dish at the Bytes Cafe?",
159
+ "Tell me more about CS 205...",
160
+ "What are the prerequisites for FLM201?",
161
+ "Which courses should I consider if I'm interested in audio mixing techniques?",
162
+ ],
163
+ submit_btn=True,
164
+ flagging_mode="manual",
165
+ flagging_options=["Like", "Spam", "Inappropriate", "Other"],
166
+ type="messages",
167
+ save_history=False,
168
+ )
169
+
170
+ if __name__ == "__main__":
171
+ demo.launch(server_name="0.0.0.0", server_port=7860)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio>=5.41.1
2
+ openai-agents>=0.2.5