{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"mount_file_id":"16EwbVEKZB0Z1BZmVtlh5vZBTchwxNaCs","authorship_tag":"ABX9TyMIBAsXK3Vv6FHLuqPrBcD1"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":1,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"collapsed":true,"id":"dyrVIqQ5EO6-","executionInfo":{"status":"ok","timestamp":1749625205156,"user_tz":-120,"elapsed":20686,"user":{"displayName":"Diogo Resende","userId":"05706604408624562002"}},"outputId":"8e66b64a-24bc-4029-eba5-e8473ef62759"},"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[?25l   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/121.9 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m121.9/121.9 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25h\u001b[?25l   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/129.3 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m129.3/129.3 kB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m131.1/131.1 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.4/44.4 kB\u001b[0m \u001b[31m1.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25h"]}],"source":["!pip install openai-agents -q"]},{"cell_type":"code","source":["%cd /content/drive/MyDrive/AI Agents/OpenAI Agents SDK/AI Sommelier"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"VmORFahCRRVb","executionInfo":{"status":"ok","timestamp":1749625205596,"user_tz":-120,"elapsed":438,"user":{"displayName":"Diogo Resende","userId":"05706604408624562002"}},"outputId":"3566df3c-5703-474f-efa7-4fa7a61294a4"},"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["/content/drive/MyDrive/AI Agents/OpenAI Agents SDK/AI Sommelier\n"]}]},{"cell_type":"code","source":["from google.colab import userdata\n","import os\n","\n","os.environ[\"OPENAI_API_KEY\"] = userdata.get(\"OPENAI_API_KEY\")"],"metadata":{"id":"CqsdnFwkErkh","executionInfo":{"status":"ok","timestamp":1749625206018,"user_tz":-120,"elapsed":414,"user":{"displayName":"Diogo Resende","userId":"05706604408624562002"}}},"execution_count":3,"outputs":[]},{"cell_type":"code","source":["import asyncio\n","from uuid import uuid4\n","\n","from openai import OpenAI\n","from agents import (\n","    Agent,\n","    Runner,\n","    trace,\n","    ItemHelpers,\n","    MessageOutputItem,\n","    HandoffOutputItem,\n","    ToolCallOutputItem,\n","    FileSearchTool,\n","    function_tool,\n",")\n","from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX\n","\n","client = OpenAI()"],"metadata":{"id":"A7SV7a3REUNQ","executionInfo":{"status":"ok","timestamp":1749627675767,"user_tz":-120,"elapsed":179,"user":{"displayName":"Diogo Resende","userId":"05706604408624562002"}}},"execution_count":31,"outputs":[]},{"cell_type":"markdown","source":["# Upload the file to OpenAI"],"metadata":{"id":"422R1bNG_UnI"}},{"cell_type":"code","source":["# Upload a file once, reuse everywhere\n","path = \"Vinhos baba d'urso.pdf\"\n","upload = client.files.create(\n","    file=open(path, \"rb\"),\n","    purpose=\"assistants\",\n",")"],"metadata":{"id":"ywyqpfZk-24S"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Retrieve and print the file ID\n","FILE_ID = upload.id\n","FILE_ID"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":36},"id":"RRExtW6l_-_n","executionInfo":{"status":"ok","timestamp":1748957747434,"user_tz":-120,"elapsed":15,"user":{"displayName":"Diogo Resende","userId":"05706604408624562002"}},"outputId":"6defebfd-647e-4f6e-9d2a-ff69908d8d72"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":["'file-8GWrzJBcLBBuR8XktaP5fv'"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"}},"metadata":{},"execution_count":6}]},{"cell_type":"code","source":["# Create the vector store with code\n","vector_store = client.vector_stores.create(\n","    name = \"Vector-Store-X\"\n",")"],"metadata":{"id":"ekNnfcuzBB6R"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Insert the file in the vector store\n","vs_file = client.vector_stores.files.create(\n","    vector_store_id = 'vs_683efab3660c8191ba3e4df02943ed51',\n","    file_id=FILE_ID,\n",")"],"metadata":{"id":"K13qoiTjAVFm"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# AI Sommelier Agent"],"metadata":{"id":"cb9WQ_2MBuKT"}},{"cell_type":"code","source":["# Define the instructions and other configs\n","instructions = f\"\"\"{RECOMMENDED_PROMPT_PREFIX}\n","You are Bitte's AI Sommelier:\n","Your task is to provide the user with a wine recommendation for their dish.\n","Use FileSearchTool to pick up to 3 wines from the vector store.\n","If the interaction has nothing to do with the wine, do the handoff.\n","\"\"\"\n","vs_id = \"vs_683f09129dcc81919305e1510f0c9da4\""],"metadata":{"id":"FmrACnHTCkMJ","executionInfo":{"status":"ok","timestamp":1749625519107,"user_tz":-120,"elapsed":4,"user":{"displayName":"Diogo Resende","userId":"05706604408624562002"}}},"execution_count":5,"outputs":[]},{"cell_type":"code","source":["# Define the escalate to boss function\n","@function_tool(\n","    name_override = \"escalate_to_boss\",\n","    description_override=\"Talk to the guest about wine. If the topic is not wine, escalate to the boss\"\n",")\n","\n","async def escalate_to_boss(request: str) -> str:\n","  print(f\"Escalating to the boss {request}\")\n","  return \"The boss will be here soon\""],"metadata":{"id":"bUBM0bMyDufS","executionInfo":{"status":"ok","timestamp":1749627415686,"user_tz":-120,"elapsed":18,"user":{"displayName":"Diogo Resende","userId":"05706604408624562002"}}},"execution_count":28,"outputs":[]},{"cell_type":"code","source":["# Build the AI Agent\n","sommelier = Agent(\n","    name = \"Bitte AI Sommelier\",\n","    model = \"gpt-4.1-mini\",\n","    instructions = instructions,\n","    tools = [\n","        FileSearchTool(vector_store_ids = [vs_id],\n","                       max_num_results = 3),\n","        escalate_to_boss\n","    ]\n",")"],"metadata":{"id":"7SyfZN55Bv1E","executionInfo":{"status":"ok","timestamp":1749627420903,"user_tz":-120,"elapsed":9,"user":{"displayName":"Diogo Resende","userId":"05706604408624562002"}}},"execution_count":29,"outputs":[]},{"cell_type":"code","source":["# Build the Chat interactions\n","async def chat():\n","  # Store the conversation\n","  history = []\n","\n","  # while loop for the conversation\n","  while True:\n","    user = input(\"User: \")\n","    if user.lower() in {\"exit\", \"quit\"}:\n","      break\n","\n","    history.append({\"role\": \"user\", \"content\": user})\n","\n","    # Apply tracing to the conversation\n","    with trace(\"Bitte Sommelier\", group_id = uuid4().hex):\n","      run = await Runner.run(sommelier, history)\n","\n","    stop_chat = False\n","\n","    for itm in run.new_items:\n","      if isinstance(itm, MessageOutputItem):\n","        print(\"Sommelier\", ItemHelpers.text_message_output(itm))\n","\n","      elif isinstance(itm, ToolCallOutputItem):\n","        print(\"Boss was summoned\")\n","        stop_chat = True\n","        break\n","\n","    if stop_chat:\n","      break\n","\n","    history = run.to_input_list()\n","\n","# Run the chat\n","await chat()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"RwezdiiIHWXg","executionInfo":{"status":"ok","timestamp":1749627914186,"user_tz":-120,"elapsed":42031,"user":{"displayName":"Diogo Resende","userId":"05706604408624562002"}},"outputId":"5a9c25ab-4f36-4bd7-ba73-561d15e5c848"},"execution_count":34,"outputs":[{"output_type":"stream","name":"stdout","text":["User: I would like to pair wine with a seafood rice\n","Sommelier For a seafood rice dish, a white wine with good acidity and freshness typically pairs well to complement the flavors without overpowering the seafood.\n","\n","From the wines available, I recommend these options:\n","\n","1. Branco Alentejo Adega Mayor Caiado or Reserva: These are white wines made from Arinto and Fernão Pires grapes, which tend to have bright acidity and citrus notes that would enhance the seafood flavors in the rice.\n","\n","2. Branco Algarve Villa Alvor Sauvignon Blanc: Sauvignon Blanc usually offers a zesty acidity and herbaceous notes, which can be refreshing with a seafood rice dish.\n","\n","3. Branco Espanha Rosa Ruiz Albariño: Albariño is known for its crisp acidity and stone fruit flavors, making it a classic match for seafood dishes.\n","\n","Any of these white wines would be excellent choices to pair with your seafood rice, bringing balance and enhancing the dish's flavors without overwhelming it. Would you like more details about any of these wines?\n","User: Where is the toilet?\n","Escalating to the boss User asked 'Where is the toilet?', which is unrelated to wine topics.\n","Boss was summoned\n"]}]},{"cell_type":"markdown","source":["# What would you do to improve?\n","If you submit an improvement, I will give you personalized feedback"],"metadata":{"id":"NASFBqNU860e"}},{"cell_type":"code","source":[],"metadata":{"id":"NUTTncTYzsuC"},"execution_count":null,"outputs":[]}]}