diff --git a/01_ai_agents_first/05_model_configuration/Global,_Run_and_Agent_Level_Agents_SDK_Config.ipynb b/01_ai_agents_first/05_model_configuration/Global,_Run_and_Agent_Level_Agents_SDK_Config.ipynb index b917e859..640b1527 100644 --- a/01_ai_agents_first/05_model_configuration/Global,_Run_and_Agent_Level_Agents_SDK_Config.ipynb +++ b/01_ai_agents_first/05_model_configuration/Global,_Run_and_Agent_Level_Agents_SDK_Config.ipynb @@ -1,45 +1,28 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { "cell_type": "markdown", - "source": [ - "### Install openai-agents SDK" - ], "metadata": { "id": "PdKwzEluDBN7" - } + }, + "source": [ + "### Install openai-agents SDK" + ] }, { "cell_type": "code", - "source": [ - "!pip install -Uq openai-agents" - ], + "execution_count": 1, "metadata": { - "id": "3QdkOviEB2ay", "colab": { "base_uri": "https://localhost:8080/" }, + "id": "3QdkOviEB2ay", "outputId": "d77b08ca-7a76-4d04-e458-ca4bc4f6f4a7" }, - "execution_count": 1, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m61.0/61.0 kB\u001b[0m \u001b[31m506.0 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m161.4/161.4 kB\u001b[0m \u001b[31m1.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", @@ -49,52 +32,100 @@ "\u001b[?25h" ] } + ], + "source": [ + "!pip install -Uq openai-agents" ] }, { "cell_type": "markdown", - "source": [ - "### Make your Jupyter Notebook capable of running asynchronous functions." - ], "metadata": { "id": "7yD91lz4DIAx" - } + }, + "source": [ + "### Make your Jupyter Notebook capable of running asynchronous functions." + ] }, { "cell_type": "code", - "source": [ - "import nest_asyncio\n", - "nest_asyncio.apply()" - ], + "execution_count": 2, "metadata": { "id": "7A5YLi3HCfBV" }, - "execution_count": 2, - "outputs": [] + "outputs": [], + "source": [ + "import nest_asyncio\n", + "nest_asyncio.apply()" + ] }, { "cell_type": "markdown", - "source": [ - "### Get your LLM API Key" - ], "metadata": { "id": "mJoDmfdcUm81" - } + }, + "source": [ + "### Get your LLM API Key" + ] }, { "cell_type": "code", - "source": [ - "from google.colab import userdata\n", - "gemini_api_key = userdata.get(\"GEMINI_API_KEY\")\n" - ], + "execution_count": null, "metadata": { "id": "dLjHL03bT8_D" }, - "execution_count": 3, - "outputs": [] + "outputs": [], + "source": [ + "from google.colab import userdata\n", + "gemini_api_key = userdata.get(\"GEMINI_API_KEY\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 🔑 Setup OpenAI API Key & Enable Tracing in Agents SDK\n", + "\n", + "To enable **traces/logs** in your Agents SDK setup, you’ll need an **OpenAI API Key**. \n", + "Follow these steps:\n", + "\n", + "---\n", + "\n", + "## 1. Create OpenAI Account\n", + "- Go to [OpenAI Platform](https://platform.openai.com/).\n", + "- Sign up / Log in.\n", + "\n", + "---\n", + "\n", + "## 2. Generate API Key\n", + "- After login, navigate to: \n", + " 👉 [API Keys](https://platform.openai.com/account/api-keys) \n", + "- Click **“Create new secret key”**.\n", + "- Copy your new key safely.\n", + "\n", + "---\n", + "\n", + "## 3. Add API Key in `.env` file\n", + "Inside your project, create a `.env` file and add:\n", + "\n", + "```env\n", + "OPENAI_API_KEY=your_api_key_here\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from google.colab import userdata\n", + "openai_api_key = userdata.get(\"OPENAI_API_KEY\")\n" + ] }, { "cell_type": "markdown", + "metadata": { + "id": "j2dv85_EUwVT" + }, "source": [ "## How to configure LLM Providers at different levels (Global, Run and Agent)?\n", "\n", @@ -104,22 +135,36 @@ "3. Global Level\n", "\n", "We will always your Agent Level Configuration so each agent can use the LLM best fit for it." - ], - "metadata": { - "id": "j2dv85_EUwVT" - } + ] }, { "cell_type": "markdown", - "source": [ - "### 1. AGENT LEVEL" - ], "metadata": { "id": "r6mLXIwYTxwK" - } + }, + "source": [ + "### 1. AGENT LEVEL" + ] }, { "cell_type": "code", + "execution_count": 4, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dEkPY49XTwbP", + "outputId": "74de1603-a88d-411d-fd89-6813186f5acd" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "یہ بہت اچھی بات ہے۔ ایجنٹک AI ایک دلچسپ اور ابھرتا ہوا شعبہ ہے۔ امید ہے آپ پاناورسٹی کمیونٹی کے ساتھ اچھا سیکھ رہے ہوں گے۔\n" + ] + } + ], "source": [ "import asyncio\n", "from openai import AsyncOpenAI\n", @@ -149,36 +194,37 @@ "\n", "if __name__ == \"__main__\":\n", " asyncio.run(main())" - ], + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Wb5cWUWAURHe" + }, + "source": [ + "### 2. RUN LEVEL" + ] + }, + { + "cell_type": "code", + "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, - "id": "dEkPY49XTwbP", - "outputId": "74de1603-a88d-411d-fd89-6813186f5acd" + "id": "8vUgBE2jUPi7", + "outputId": "ea506ee7-31ad-4e9e-df89-967defce5e95" }, - "execution_count": 4, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ - "یہ بہت اچھی بات ہے۔ ایجنٹک AI ایک دلچسپ اور ابھرتا ہوا شعبہ ہے۔ امید ہے آپ پاناورسٹی کمیونٹی کے ساتھ اچھا سیکھ رہے ہوں گے۔\n" + "Hello! As a large language model, I don't experience feelings like humans do. However, I am functioning optimally and ready to assist you. How can I help you today?\n", + "\n" ] } - ] - }, - { - "cell_type": "markdown", - "source": [ - "### 2. RUN LEVEL" ], - "metadata": { - "id": "Wb5cWUWAURHe" - } - }, - { - "cell_type": "code", "source": [ "from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel\n", "from agents.run import RunConfig\n", @@ -205,41 +251,43 @@ "result = Runner.run_sync(agent, \"Hello, how are you.\", run_config=config)\n", "\n", "print(result.final_output)" - ], + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Hb9QgpqKUaIi" + }, + "source": [ + "### GLOBAL" + ] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, - "id": "8vUgBE2jUPi7", - "outputId": "ea506ee7-31ad-4e9e-df89-967defce5e95" + "id": "KHLr1P7vUWhn", + "outputId": "4fdc6140-867a-4178-b0ac-69c5fe164b2c" }, - "execution_count": 5, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ - "Hello! As a large language model, I don't experience feelings like humans do. However, I am functioning optimally and ready to assist you. How can I help you today?\n", + "Hi there! How can I help you today?\n", "\n" ] } - ] - }, - { - "cell_type": "markdown", - "source": [ - "### GLOBAL" ], - "metadata": { - "id": "Hb9QgpqKUaIi" - } - }, - { - "cell_type": "code", "source": [ - "from agents import Agent, Runner, AsyncOpenAI, set_default_openai_client, set_tracing_disabled, set_default_openai_api\n", + "from agents import Agent, Runner, AsyncOpenAI, set_default_openai_client, set_tracing_disabled, set_default_openai_api,set_tracing_export_api_key\n", + "\n", + "# 🔹 Enable/Disable tracing logs (set to False if you want to see traces on OpenAI platform)\n", + "# set_tracing_disabled(True)\n", "\n", - "set_tracing_disabled(True)\n", "set_default_openai_api(\"chat_completions\")\n", "\n", "external_client = AsyncOpenAI(\n", @@ -248,67 +296,51 @@ ")\n", "set_default_openai_client(external_client)\n", "\n", + "# 🔹 Export traces/logs to OpenAI using your OpenAI API key\n", + "# NOTE: This only needs to be set at the global level\n", + "set_tracing_export_api_key(openai_api_key)\n", + "\n", "agent: Agent = Agent(name=\"Assistant\", instructions=\"You are a helpful assistant\", model=\"gemini-2.0-flash\")\n", "\n", "result = Runner.run_sync(agent, \"Hello\")\n", "\n", "print(result.final_output)" - ], - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "KHLr1P7vUWhn", - "outputId": "4fdc6140-867a-4178-b0ac-69c5fe164b2c" - }, - "execution_count": 6, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Hi there! How can I help you today?\n", - "\n" - ] - } ] }, { "cell_type": "markdown", - "source": [ - "### Set debug mode on (Optional)" - ], "metadata": { "id": "sai9zXdAu4-t" - } + }, + "source": [ + "### Set debug mode on (Optional)" + ] }, { "cell_type": "code", + "execution_count": 7, + "metadata": { + "id": "bdWaK-w8u3dD" + }, + "outputs": [], "source": [ "from agents import enable_verbose_stdout_logging\n", "\n", "enable_verbose_stdout_logging()" - ], - "metadata": { - "id": "bdWaK-w8u3dD" - }, - "execution_count": 7, - "outputs": [] + ] }, { "cell_type": "markdown", - "source": [ - "> This is for Debugging and looking what happens inside of Agent SDK" - ], "metadata": { "id": "t47o3UhOUf3v" - } + }, + "source": [ + "> This is for Debugging and looking what happens inside of Agent SDK" + ] }, { "cell_type": "code", - "source": [ - "result = Runner.run_sync(agent, \"Hello\")\n" - ], + "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -316,81 +348,80 @@ "id": "vi8QzcUaUewY", "outputId": "7c5e31ea-d300-4538-b4d9-1bba66d8be85" }, - "execution_count": 8, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Tracing is disabled. Not creating trace Agent workflow\n" ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "DEBUG:openai.agents:Tracing is disabled. Not creating trace Agent workflow\n" ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Setting current trace: no-op\n" ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "DEBUG:openai.agents:Setting current trace: no-op\n" ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Tracing is disabled. Not creating span \n" ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "DEBUG:openai.agents:Tracing is disabled. Not creating span \n" ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Running agent Assistant (turn 1)\n" ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "DEBUG:openai.agents:Running agent Assistant (turn 1)\n" ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Tracing is disabled. Not creating span \n" ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "DEBUG:openai.agents:Tracing is disabled. Not creating span \n" ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "[\n", " {\n", @@ -411,8 +442,8 @@ ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "DEBUG:openai.agents:[\n", " {\n", @@ -433,8 +464,8 @@ ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "LLM resp:\n", "{\n", @@ -450,8 +481,8 @@ ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "DEBUG:openai.agents:LLM resp:\n", "{\n", @@ -467,20 +498,37 @@ ] }, { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "Resetting current trace\n" ] }, { - "output_type": "stream", "name": "stderr", + "output_type": "stream", "text": [ "DEBUG:openai.agents:Resetting current trace\n" ] } + ], + "source": [ + "result = Runner.run_sync(agent, \"Hello\")\n" ] } - ] -} \ No newline at end of file + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}