Skip to content

Commit 79ef699

Browse files
committed
Instructions for lab 13
1 parent 9b0b86a commit 79ef699

File tree

3 files changed

+168
-76
lines changed

3 files changed

+168
-76
lines changed

agent.py

Lines changed: 19 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from langchain_openai import ChatOpenAI
44
from langchain_core.prompts import ChatPromptTemplate
55
from config import settings
6+
from agents import Agent, Runner, SQLiteSession, function_tool, set_default_openai_key
67

78
db = {
89
"job_descriptions": {
@@ -16,7 +17,7 @@
1617
}
1718
}
1819

19-
20+
@function_tool
2021
def extract_skills(session_id: str, job_id: int) -> list[str]:
2122
"""Given a job_id, lookup job descriptiona and extract the skills for that job description"""
2223
job_id = int(job_id)
@@ -25,7 +26,8 @@ def extract_skills(session_id: str, job_id: int) -> list[str]:
2526
db["state"][session_id]["skills"] = skills
2627
print(f"\n📋 Extracted skills: {', '.join(skills)}")
2728
return skills
28-
29+
30+
@function_tool
2931
def update_evaluation(session_id: str, skill: str, evaluation_result: bool) -> bool:
3032
"""This function takes the session_id, skill, and the evaluation result and saves it to the database. Returns success or failure (bool)"""
3133
try:
@@ -37,18 +39,13 @@ def update_evaluation(session_id: str, skill: str, evaluation_result: bool) -> b
3739
except KeyError:
3840
return False
3941

42+
@function_tool
4043
def transfer_to_skill_evaluator(session_id: str, skill: str) -> bool:
4144
"""This function takes a skill, evaluates it and returns the evaluation result for the skill as a boolean pass / fail"""
4245
result = True
4346
print(f"Evaluating skill: {skill}. Result {result}")
4447
return result
4548

46-
tools_mapping = {
47-
"extract_skills": extract_skills,
48-
"update_evaluation": update_evaluation,
49-
"transfer_to_skill_evaluator": transfer_to_skill_evaluator
50-
}
51-
5249
ORCHESTRATOR_SYSTEM_PROMPT = """
5350
You are an interview orchestrator. Your goal is to evaluate the candidate on the required skills.
5451
@@ -61,48 +58,6 @@ def transfer_to_skill_evaluator(session_id: str, skill: str) -> bool:
6158
3. Then, for EACH skill in the list, use transfer_to_skill_evaluator tool to delegate evaluation
6259
4. Once you get the response, use the update_evaluation tool to save the evaluation result into the database
6360
5. Once all skills are evaluated, mention that the screening is complete and thank the candidate for their time
64-
65-
# OUTPUT FORMAT
66-
67-
Output as a JSON following the JSON schema below:
68-
69-
```
70-
{{
71-
"type": "object",
72-
"properties": {{
73-
"response": {{ "type": "string" }}
74-
"tool_name": {{ "type": "string"}},
75-
"tool_params": {{
76-
"type": "array",
77-
"items": {{
78-
"type": "object",
79-
"properties": {{
80-
"param": {{ "type": "string" }},
81-
"value": {{ "type": "string" }}
82-
}}
83-
}}
84-
}}
85-
}},
86-
"required": []
87-
}}
88-
89-
Use the "tool_name" and "tool_params" properties to execute a tool and use the "response" property to reply to the user without a tool call.
90-
91-
# TOOLS
92-
93-
You have access to the following tools
94-
95-
1. `extract_skills(session_id: str, job_id: int) -> list[str]`
96-
97-
Given a job_id, lookup job descriptiona and extract the skills for that job description
98-
99-
2. `update_evaluation(session_id: str, skill: str, evaluation_result: bool) -> bool`
100-
101-
This function takes the session_id, skill, and the evaluation result and saves it to the database. Returns success or failure (bool)
102-
103-
3. `transfer_to_skill_evaluator(session_id, skill: str) -> bool`
104-
105-
This function takes a skill, evaluates it and returns the evaluation result for the skill as a boolean pass / fail
10661
"""
10762

10863
ORCHESTRATOR_USER_PROMPT = """
@@ -115,33 +70,22 @@ def transfer_to_skill_evaluator(session_id: str, skill: str) -> bool:
11570
"""
11671

11772
def run_orchestrator_agent(session_id, job_id):
118-
llm = ChatOpenAI(model="gpt-5.1", temperature=0, api_key=settings.OPENAI_API_KEY)
119-
messages = [
120-
("system", ORCHESTRATOR_SYSTEM_PROMPT),
121-
("human", ORCHESTRATOR_USER_PROMPT),
122-
]
123-
user_reply = ""
124-
while user_reply != "bye":
125-
orchestrator_prompt = ChatPromptTemplate.from_messages(messages)
126-
orchestrator_chain = orchestrator_prompt | llm
127-
output = orchestrator_chain.invoke({"job_id": job_id, "session_id": session_id})
128-
data = json.loads(output.content)
129-
print(f"Output by LLM: {data}")
130-
if "response" in data:
131-
print(data["response"])
132-
if "tool_name" in data and data["tool_name"] != "":
133-
tool_name = data["tool_name"]
134-
params = {param["param"]: param["value"] for param in data["tool_params"]}
135-
tools = tools_mapping[tool_name]
136-
tool_output = tools(**params)
137-
print(f"TOOL OUTPUT = {tool_output}")
138-
messages.append(("assistant", output.content.replace("{", "{{").replace("}", "}}")))
139-
messages.append(("ai", str(tool_output)))
140-
else:
141-
user_reply = input("User: ")
142-
messages.append(("human", user_reply))
73+
session = SQLiteSession(f"screening-{session_id}")
74+
agent = Agent(
75+
name="Interview Orchestrator Agent",
76+
instructions=ORCHESTRATOR_SYSTEM_PROMPT,
77+
model="gpt-5.1",
78+
tools=[extract_skills, transfer_to_skill_evaluator, update_evaluation]
79+
)
80+
user_input = ORCHESTRATOR_USER_PROMPT.format(job_id=job_id, session_id=session_id)
81+
while user_input != 'bye':
82+
result = Runner.run_sync(agent, user_input, session=session)
83+
print(result.final_output)
84+
user_input = input("User: ")
85+
return
14386

14487
def main():
88+
set_default_openai_key(settings.OPENAI_API_KEY)
14589
job_id = 1
14690
session_id = "session123"
14791
run_orchestrator_agent(session_id, job_id)

labs/13-openai-sdk.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Lab 13: Open AI Agents SDK
2+
3+
In this lab, we will convert our previous code to use Open AI SDK interface.
4+
5+
First, update `requirements.txt` and install the dependency
6+
7+
```
8+
openai-agents==0.6.2
9+
```
10+
11+
## High Level Overview
12+
13+
1. At the beginning the OpenAI API key using `set_default_openai_key`
14+
1. Apply the `@function_tool` decorator on the three tool functions
15+
1. Remove the tools and output configurations from the system prompt
16+
1. Now we need to update the `run_orchestrator_agent` function
17+
1. Create a `SQLiteSession` with the name `screening-{session_id}`
18+
1. Create the `Agent` class with model `gpt-5.1` and name `Interview Orchestrator Agent`
19+
1. Fill in the job id and session id into the user prompt and set it as the initial user input
20+
1. Call `Runner.run_sync` passing in the agent and session along with the user input
21+
1. Print the response
22+
1. Get the next user input
23+
1. Repeat steps 8-10 until the user enters `bye`
24+
25+
## Hints
26+
27+
### How do I add the function_tool decorator?
28+
29+
<details>
30+
<summary>Answer</summary>
31+
32+
First import
33+
34+
```python
35+
from agents import function_tool
36+
```
37+
38+
Then repeat this for all three tools
39+
40+
```
41+
@function_tool
42+
def extract_skills(session_id: str, job_id: int) -> list[str]:
43+
...
44+
```
45+
</details>
46+
47+
### How do I configure the OPENAI_API_KEY
48+
49+
<details>
50+
<summary>Answer</summary>
51+
52+
Import the required function
53+
54+
```python
55+
from agents import set_default_openai_key
56+
```
57+
58+
Then set it in `main` function
59+
60+
```python
61+
def main():
62+
set_default_openai_key(settings.OPENAI_API_KEY)
63+
...
64+
```
65+
</details>
66+
67+
### How do I set up the agent session?
68+
69+
<details>
70+
<summary>Answer</summary>
71+
72+
```python
73+
session = SQLiteSession(f"screening-{session_id}")
74+
```
75+
</details>
76+
77+
### What change do I do to the system prompt?
78+
79+
<details>
80+
<summary>Answer</summary>
81+
82+
Remove the Tools and Output configuration instructions so that it looks like this
83+
84+
```python
85+
ORCHESTRATOR_SYSTEM_PROMPT = """
86+
You are an interview orchestrator. Your goal is to evaluate the candidate on the required skills.
87+
88+
# INSTRUCTIONS
89+
90+
Follow the following steps exactly
91+
92+
1. Extract key skills from the job description using extract_skills tool
93+
2. Then welcome the candidate, explain the screening process and ask the candidate if they are ready
94+
3. Then, for EACH skill in the list, use transfer_to_skill_evaluator tool to delegate evaluation
95+
4. Once you get the response, use the update_evaluation tool to save the evaluation result into the database
96+
5. Once all skills are evaluated, mention that the screening is complete and thank the candidate for their time
97+
"""
98+
```
99+
</details>
100+
101+
### How do I create the Agent?
102+
103+
<details>
104+
<summary>Answer</summary>
105+
106+
```python
107+
agent = Agent(
108+
name="Interview Orchestrator Agent",
109+
instructions=ORCHESTRATOR_SYSTEM_PROMPT,
110+
model="gpt-5.1",
111+
tools=[extract_skills, transfer_to_skill_evaluator, update_evaluation]
112+
)
113+
```
114+
</details>
115+
116+
### How do I fill in job_id and session_id into the user prompt?
117+
118+
<details>
119+
<summary>Answer</summary>
120+
121+
```python
122+
user_input = ORCHESTRATOR_USER_PROMPT.format(job_id=job_id, session_id=session_id)
123+
```
124+
</details>
125+
126+
### How do I run the agent?
127+
128+
<details>
129+
<summary>Answer</summary>
130+
131+
```python
132+
result = Runner.run_sync(agent, user_input, session=session)
133+
```
134+
</details>
135+
136+
### How do I do keep running till the user inputs bye
137+
138+
<details>
139+
<summary>Answer</summary>
140+
141+
```python
142+
while user_input != 'bye':
143+
result = Runner.run_sync(agent, user_input, session=session)
144+
print(result.final_output)
145+
user_input = input("User: ")
146+
```
147+
</details>

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ pandas==2.3.3 # For data analysis
2020

2121
langchain==1.1.0 # Langchain framework
2222
langchain-openai==1.1.0 # Langchain Open AI interface
23-
langchain-qdrant==1.1.0 # Langchain with Qdrant vector DB
23+
langchain-qdrant==1.1.0 # Langchain with Qdrant vector DB
24+
openai-agents==0.6.2 # OpenAI Agent SDK

0 commit comments

Comments
 (0)