{ "cells": [ { "cell_type": "markdown", "id": "aa0c6e52", "metadata": {}, "source": [ "Libraries" ] }, { "cell_type": "code", "execution_count": 1, "id": "06252c0a", "metadata": {}, "outputs": [], "source": [ "from dotenv import load_dotenv\n", "from openai import OpenAI\n", "import json\n", "import os\n", "import requests\n", "from pypdf import PdfReader\n", "import gradio as gr\n", "\n", "from agents import Agent, WebSearchTool, trace, Runner, gen_trace_id, function_tool\n", "from agents.model_settings import ModelSettings\n", "from pydantic import BaseModel, Field\n", "import asyncio\n", "import os\n", "from typing import Dict\n", "from IPython.display import display, Markdown" ] }, { "cell_type": "code", "execution_count": 2, "id": "aa257a14", "metadata": {}, "outputs": [], "source": [ "load_dotenv(override=True)\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 5, "id": "dd36390e", "metadata": {}, "outputs": [], "source": [ "reader = PdfReader(\"input/dashie_bot_input.pdf\")\n", "input = \"\"\n", "for page in reader.pages:\n", " text = page.extract_text()\n", " if text:\n", " input += text" ] }, { "cell_type": "code", "execution_count": 6, "id": "6a704414", "metadata": {}, "outputs": [], "source": [ "instructions = f\"You are a data analyst helping connect people having a question about data to the correct dashboard containing the needed data. \\\n", " You are given the full descriptions of the dashboards and each graph and filtering that they contain in the file dashie_bot_input\\\n", " Do not use any information outside of the information provided. If requested data is not in described in the sheet or you do not know answer, refer the requester to #ask_product_owners channel on Slack or directly to Audrius\\\n", " When giving answers, be coincise and practical, share the link to the relevant dashboard\"\n", "\n", "instructions += f\"\\n\\n## Dashie_bot_input:\\n{input}\\n\\n\"" ] }, { "cell_type": "code", "execution_count": 7, "id": "74fd2e85", "metadata": {}, "outputs": [], "source": [ "sales_manager = Agent(\n", " name=\"@dashie_bot\",\n", " instructions=instructions,\n", " model=\"gpt-4o-mini\")" ] }, { "cell_type": "code", "execution_count": 17, "id": "f812f272", "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " messages = [{\"role\": \"system\", \"content\": instructions}] + history + [{\"role\": \"user\", \"content\": message}]\n", " done = False\n", " while not done:\n", "\n", " # This is the call to the LLM - see that we pass in the tools json\n", "\n", " response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", "\n", " finish_reason = response.choices[0].finish_reason\n", " \n", " # If the LLM wants to call a tool, we do that!\n", " \n", " if finish_reason==\"tool_calls\":\n", " message = response.choices[0].message\n", " tool_calls = message.tool_calls\n", " results = handle_tool_calls(tool_calls)\n", " messages.append(message)\n", " messages.extend(results)\n", " else:\n", " done = True\n", " return response.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 19, "id": "b33f35dc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7862\n", "* To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gr.ChatInterface(chat, type=\"messages\").launch()" ] }, { "cell_type": "code", "execution_count": 20, "id": "4d825d46", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\berno\\projects\\agents\\.venv\\Scripts\\python.exe: No module named uv\n" ] } ], "source": [ "uv run gradio deploy" ] }, { "cell_type": "code", "execution_count": null, "id": "98bf06fa", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.11" } }, "nbformat": 4, "nbformat_minor": 5 }