Skip to content

Commit ade89c3

Browse files
committed
tools jupyter notebook
1 parent ff7988e commit ade89c3

File tree

58 files changed

+582
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+582
-0
lines changed
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "markdown",
19+
"source": [
20+
"# Install openai-agents SDK"
21+
],
22+
"metadata": {
23+
"id": "PdKwzEluDBN7"
24+
}
25+
},
26+
{
27+
"cell_type": "code",
28+
"source": [
29+
"!pip install -Uq openai-agents"
30+
],
31+
"metadata": {
32+
"id": "3QdkOviEB2ay",
33+
"colab": {
34+
"base_uri": "https://localhost:8080/"
35+
},
36+
"outputId": "d558d9ba-c9c5-4f4c-cb51-72b50b7fcb98"
37+
},
38+
"execution_count": null,
39+
"outputs": [
40+
{
41+
"output_type": "stream",
42+
"name": "stdout",
43+
"text": [
44+
"\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/76.1 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m76.1/76.1 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
45+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m128.5/128.5 kB\u001b[0m \u001b[31m4.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
46+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m567.4/567.4 kB\u001b[0m \u001b[31m12.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
47+
"\u001b[?25h"
48+
]
49+
}
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"source": [
55+
"# Make your Jupyter Notebook capable of running asynchronous functions."
56+
],
57+
"metadata": {
58+
"id": "7yD91lz4DIAx"
59+
}
60+
},
61+
{
62+
"cell_type": "code",
63+
"source": [
64+
"import nest_asyncio\n",
65+
"nest_asyncio.apply()"
66+
],
67+
"metadata": {
68+
"id": "7A5YLi3HCfBV"
69+
},
70+
"execution_count": null,
71+
"outputs": []
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"source": [
76+
"# Run Google Gemini with OPENAI-Agent SDK"
77+
],
78+
"metadata": {
79+
"id": "K3VTUWDaGFcV"
80+
}
81+
},
82+
{
83+
"cell_type": "code",
84+
"source": [
85+
"import os\n",
86+
"from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel\n",
87+
"from agents.run import RunConfig\n",
88+
"from google.colab import userdata\n",
89+
"\n",
90+
"\n",
91+
"gemini_api_key = userdata.get(\"GEMINI_API_KEY\")\n",
92+
"\n",
93+
"\n",
94+
"# Check if the API key is present; if not, raise an error\n",
95+
"if not gemini_api_key:\n",
96+
" raise ValueError(\"GEMINI_API_KEY is not set. Please ensure it is defined in your .env file.\")\n",
97+
"\n",
98+
"#Reference: https://ai.google.dev/gemini-api/docs/openai\n",
99+
"external_client = AsyncOpenAI(\n",
100+
" api_key=gemini_api_key,\n",
101+
" base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\",\n",
102+
")\n",
103+
"\n",
104+
"model = OpenAIChatCompletionsModel(\n",
105+
" model=\"gemini-2.0-flash\",\n",
106+
" openai_client=external_client\n",
107+
")\n",
108+
"\n",
109+
"config = RunConfig(\n",
110+
" model=model,\n",
111+
" model_provider=external_client,\n",
112+
" tracing_disabled=True\n",
113+
")"
114+
],
115+
"metadata": {
116+
"id": "QSIWS6RvC-a4"
117+
},
118+
"execution_count": null,
119+
"outputs": []
120+
},
121+
{
122+
"cell_type": "code",
123+
"source": [
124+
"import os\n",
125+
"from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel, set_default_openai_client\n",
126+
"from agents.run import RunConfig\n",
127+
"from google.colab import userdata\n",
128+
"\n",
129+
"\n",
130+
"gemini_api_key = userdata.get(\"GEMINI_API_KEY\")\n",
131+
"\n",
132+
"\n",
133+
"external_client = AsyncOpenAI(\n",
134+
" api_key=gemini_api_key,\n",
135+
" base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\",\n",
136+
")\n",
137+
"set_default_openai_client(external_client)"
138+
],
139+
"metadata": {
140+
"id": "GsQmoaAAJ4JT"
141+
},
142+
"execution_count": null,
143+
"outputs": []
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"source": [
148+
"# Tools\n",
149+
"The OpenAI Agents SDK provides a robust framework for integrating various tools into agents, enabling them to perform tasks such as data retrieval, web searches, and code execution. Here's an overview of the key points regarding tool integration:\n",
150+
"\n",
151+
"**Types of Tools:**\n",
152+
"\n",
153+
"1. **Hosted Tools:** These are pre-built tools running on OpenAI's servers, accessible via the [`OpenAIResponsesModel`]. Examples include:\n",
154+
" - **WebSearchTool:** Enables agents to perform web searches.\n",
155+
" - **FileSearchTool:** Allows retrieval of information from OpenAI Vector Stores.\n",
156+
" - **ComputerTool:** Facilitates automation of computer-based tasks.\n",
157+
"\n",
158+
"2. **Function Calling:** This feature allows agents to utilize any Python function as a tool, enhancing their versatility.\n",
159+
"\n",
160+
"3. **Agents as Tools:** Agents can employ other agents as tools, enabling hierarchical task management without transferring control.\n",
161+
"\n",
162+
"**Implementing Tools:**\n",
163+
"\n",
164+
"- **Function Tools:** By decorating Python functions with `@function_tool`, they can be seamlessly integrated as tools for agents.\n",
165+
"\n",
166+
"**Tool Execution Flow:**\n",
167+
"\n",
168+
"- During an agent's operation, if a tool call is identified in the response, the SDK processes the tool call, appends the tool's response to the message history, and continues the loop until a final output is produced.\n",
169+
"\n",
170+
"**Error Handling:**\n",
171+
"\n",
172+
"- The SDK offers mechanisms to handle errors gracefully, allowing agents to recover from tool-related issues and continue their tasks effectively.\n",
173+
"\n",
174+
"For a comprehensive understanding and implementation details, refer to the [tools documentation](https://github.com/openai/openai-agents-python/blob/main/docs/tools.md)."
175+
],
176+
"metadata": {
177+
"id": "FXBrF52IPBM9"
178+
}
179+
},
180+
{
181+
"cell_type": "markdown",
182+
"source": [
183+
"# Create tools"
184+
],
185+
"metadata": {
186+
"id": "jLqBzJVcBQwU"
187+
}
188+
},
189+
{
190+
"cell_type": "code",
191+
"source": [
192+
"from agents.tool import function_tool"
193+
],
194+
"metadata": {
195+
"id": "enCDgHfABsEO"
196+
},
197+
"execution_count": null,
198+
"outputs": []
199+
},
200+
{
201+
"cell_type": "code",
202+
"source": [
203+
"@function_tool(\"get_weather\")\n",
204+
"def get_weather(location: str, unit: str = \"C\") -> str:\n",
205+
" \"\"\"\n",
206+
" Fetch the weather for a given location, returning a short description.\n",
207+
" \"\"\"\n",
208+
" # Example logic\n",
209+
" return f\"The weather in {location} is 22 degrees {unit}.\""
210+
],
211+
"metadata": {
212+
"id": "ULWmuAWsBSb8"
213+
},
214+
"execution_count": null,
215+
"outputs": []
216+
},
217+
{
218+
"cell_type": "code",
219+
"source": [
220+
"@function_tool(\"piaic_student_finder\")\n",
221+
"def student_finder(student_roll: int) -> str:\n",
222+
" \"\"\"\n",
223+
" find the PIAIC student based on the roll number\n",
224+
" \"\"\"\n",
225+
" data = {1: \"Qasim\",\n",
226+
" 2: \"Sir Zia\",\n",
227+
" 3: \"Daniyal\"}\n",
228+
"\n",
229+
" return data.get(student_roll, \"Not Found\")"
230+
],
231+
"metadata": {
232+
"id": "6pP3qlQDB27P"
233+
},
234+
"execution_count": null,
235+
"outputs": []
236+
},
237+
{
238+
"cell_type": "markdown",
239+
"source": [
240+
"# Connect tools with Agent"
241+
],
242+
"metadata": {
243+
"id": "sVs3-Q8HK_VI"
244+
}
245+
},
246+
{
247+
"cell_type": "code",
248+
"source": [
249+
"import asyncio\n",
250+
"\n",
251+
"from agents import Agent, Runner\n",
252+
"\n",
253+
"\n",
254+
"async def main():\n",
255+
" agent = Agent(\n",
256+
" name=\"Assistant\",\n",
257+
" instructions=\"You only respond in haikus.\",\n",
258+
" tools=[get_weather, student_finder], # add tools here\n",
259+
" model=model\n",
260+
" )\n",
261+
"\n",
262+
" result = await Runner.run(agent, \"Share PIAIC roll no1 student details.\")\n",
263+
" print(result.final_output)\n",
264+
"\n",
265+
"\n",
266+
"\n",
267+
"if __name__ == \"__main__\":\n",
268+
" asyncio.run(main())"
269+
],
270+
"metadata": {
271+
"id": "741wmWdpS22Z",
272+
"colab": {
273+
"base_uri": "https://localhost:8080/"
274+
},
275+
"outputId": "6df2c623-15ae-47f0-b534-6a209cc8a3cc"
276+
},
277+
"execution_count": null,
278+
"outputs": [
279+
{
280+
"output_type": "stream",
281+
"name": "stdout",
282+
"text": [
283+
"Qasim is the name.\n",
284+
"Roll number one is his own.\n",
285+
"A bright future dawns.\n",
286+
"\n"
287+
]
288+
}
289+
]
290+
}
291+
]
292+
}

00_openai_agents/05_tools/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
🚀 **[Open in Google Colab](https://colab.research.google.com/drive/18owxL5MyPPlmp4IqfveN1JOSCYQ4GFnu?usp=sharing)**
2+
3+
The OpenAI Agents SDK provides a robust framework for integrating various tools into agents, enabling them to perform tasks such as data retrieval, web searches, and code execution. Here's an overview of the key points regarding tool integration:
4+
5+
**Types of Tools:**
6+
7+
1. **Hosted Tools:** These are pre-built tools running on OpenAI's servers, accessible via the [`OpenAIResponsesModel`]. Examples include:
8+
- **WebSearchTool:** Enables agents to perform web searches.
9+
- **FileSearchTool:** Allows retrieval of information from OpenAI Vector Stores.
10+
- **ComputerTool:** Facilitates automation of computer-based tasks.
11+
12+
2. **Function Calling:** This feature allows agents to utilize any Python function as a tool, enhancing their versatility.
13+
14+
3. **Agents as Tools:** Agents can employ other agents as tools, enabling hierarchical task management without transferring control.
15+
16+
**Implementing Tools:**
17+
18+
- **Function Tools:** By decorating Python functions with `@function_tool`, they can be seamlessly integrated as tools for agents.
19+
20+
**Tool Execution Flow:**
21+
22+
- During an agent's operation, if a tool call is identified in the response, the SDK processes the tool call, appends the tool's response to the message history, and continues the loop until a final output is produced.
23+
24+
**Error Handling:**
25+
26+
- The SDK offers mechanisms to handle errors gracefully, allowing agents to recover from tool-related issues and continue their tasks effectively.
27+
28+
For a comprehensive understanding and implementation details, refer to the [tools documentation](https://github.com/openai/openai-agents-python/blob/main/docs/tools.md).

0 commit comments

Comments
 (0)