diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7a1bf51..e2708c1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,11 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.3.0 + rev: v5.0.0 hooks: - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.7.1 + rev: v0.8.0 hooks: + - id: ruff - id: ruff-format diff --git a/Quickstart.ipynb b/Quickstart.ipynb index f0c3590..5eb86fa 100644 --- a/Quickstart.ipynb +++ b/Quickstart.ipynb @@ -1,403 +1,403 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Agent Gateway Quickstart" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Agent Gateway is a multi-agent framework that offers native support for Snowflake tools. \n", - "\n", - "The system can be configured to work with 3 types of tools:\n", - "- Cortex Search Tool: For unstructured data analysis, which requires a standard RAG access pattern.\n", - "- Cortex Analyst Tool: For supporting structured data analysis, which requires a Text2SQL access pattern.\n", - "- Python Tool: For supporting custom user operations (using 3rd Party API's), which requires calling arbitrary python.\n", - "\n", - "This notebook walks through how to configure and run a system with all 3 types of tools. The demo is designed to illustrate how the agent can answer questions that require a divserse combination of tools (RAG,Text2SQL, Python, or a combination).\n", - "\n", - "Note that Agent Gateway does not configure the underlying Cortex Search or Cortex Analyst services for the user. Those services must be configured before initializing the agent." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Agent Configuration" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Connection Setup" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Authenticate with Snowpark + set token as environment variable for use by the agents." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/alherrera/Downloads/Cube/cortex-cube/.venv/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", - " from .autonotebook import tqdm as notebook_tqdm\n" - ] - } - ], - "source": [ - "from agent_gateway import Agent\n", - "from agent_gateway.tools import CortexSearchTool, CortexAnalystTool, PythonTool\n", - "from snowflake.snowpark import Session\n", - "import os\n", - "\n", - "connection_parameters = {\n", - " \"account\": os.getenv(\"SNOWFLAKE_ACCOUNT\"),\n", - " \"user\": os.getenv(\"SNOWFLAKE_USER\"),\n", - " \"password\": os.getenv(\"SNOWFLAKE_PASSWORD\"),\n", - " \"role\": os.getenv(\"SNOWFLAKE_ROLE\"),\n", - " \"warehouse\": os.getenv(\"SNOWFLAKE_WAREHOUSE\"),\n", - " \"database\": os.getenv(\"SNOWFLAKE_DATABASE\"),\n", - " \"schema\": os.getenv(\"SNOWFLAKE_SCHEMA\"),\n", - "}\n", - "\n", - "snowpark = Session.builder.configs(connection_parameters).create()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Snowflake Tool Configuration" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The Cortex Search Tool and the Cortex Analyst Tool need to be configured as follows. Note that a connection object is required for each config. In the case below we're using the same connection object for both because the services are both in the same account/database/schema. Users have the option to pass in different connection objects as needed." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "search_config = {\n", - " \"service_name\": \"SEC_SEARCH_SERVICE\",\n", - " \"service_topic\": \"Snowflake's business,product offerings,and performance.\",\n", - " \"data_description\": \"Snowflake annual reports\",\n", - " \"retrieval_columns\": [\"CHUNK\"],\n", - " \"snowflake_connection\": snowpark,\n", - "}\n", - "\n", - "analyst_config = {\n", - " \"semantic_model\": \"sp500_semantic_model.yaml\",\n", - " \"stage\": \"ANALYST\",\n", - " \"service_topic\": \"S&P500 company and stock metrics\",\n", - " \"data_description\": \"a table with stock and financial metrics about S&P500 companies \",\n", - " \"snowflake_connection\": snowpark,\n", - "}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Python Tool Configuration" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Configuring a Python Tool for the Agent Gateway requires 1) Python Callable 2) Tool Description (what does the tool do) 3) Output Description (what does the tool output). \n", - "\n", - "In the example below we create a NewsTool object that submits a HTTP request to a 3rd Party News API. The python callable is passed into the Python Tool as `news_api_func`.To use the tool below get your free token by signing up for an account at thenewsapi.com or just create your own python function and pass it into the PythonTool." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "import requests\n", - "import json\n", - "\n", - "\n", - "class NewsTool:\n", - " def __init__(self, token, limit) -> None:\n", - " self.api_token = token\n", - " self.limit = limit\n", - "\n", - " def news_search(self, news_query: str) -> str:\n", - " news_request = f\"\"\"https://api.thenewsapi.com/v1/news/all?api_token={self.api_token}&search={news_query}&language=en&limit={self.limit}\"\"\"\n", - " response = requests.get(news_request)\n", - " json_response = json.loads(response.content)[\"data\"]\n", - "\n", - " return str(json_response)\n", - "\n", - "\n", - "python_config = {\n", - " \"tool_description\": \"searches for relevant news based on user query\",\n", - " \"output_description\": \"relevant articles\",\n", - " \"python_func\": NewsTool(token=os.getenv(\"NEWS_API_TOKEN\"), limit=3).news_search,\n", - "}" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Agent Config" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "After the tools have been configured, initialize them and configure the agent." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:AgentGatewayLogger:Cortex Search Tool successfully initialized\n", - "INFO:AgentGatewayLogger:Cortex Analyst Tool successfully initialized\n", - "INFO:AgentGatewayLogger:Python Tool successfully initialized\n", - "INFO:AgentGatewayLogger:Cortex gateway successfully initialized\n" - ] - } - ], - "source": [ - "annual_reports = CortexSearchTool(**search_config)\n", - "sp500 = CortexAnalystTool(**analyst_config)\n", - "news_search = PythonTool(**python_config)\n", - "\n", - "snowflake_tools = [annual_reports, sp500, news_search]\n", - "agent = Agent(snowflake_connection=snowpark, tools=snowflake_tools)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Agent Testing" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The 3 types of questions below are designed to showcase the breadth of tool use patterns possible with the Agent Gateway. \n", - "\n", - "- The Structured Data Questions use the Cortex Analyst agent. \n", - "- The Unstructured Data Questions use either the Cortex Search agent or the Python (News API) agent.\n", - "- The last section includes a question that requires the use of both types of tools." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Structured Data Questions" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:AgentGatewayLogger:running sp500_semantic_model_cortexanalyst task\n" - ] - }, - { - "data": { - "text/plain": [ - "'The market cap of Apple is $3,019,131,060,224 or approximately $3.02 trillion.'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "agent(\"What is the market cap of Apple?\")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:AgentGatewayLogger:running sp500_semantic_model_cortexanalyst task\n", - "INFO:AgentGatewayLogger:running sp500_semantic_model_cortexanalyst task\n" - ] - }, - { - "data": { - "text/plain": [ - "'Apple has a bigger EBITDA than Microsoft.'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "agent(\"Which company has the bigger EBITDA, Apple or MSFT?\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Unstructured Data Questions" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:AgentGatewayLogger:running sec_search_service_cortexsearch task\n" - ] - }, - { - "data": { - "text/plain": [ - "'As of January 31, 2024, Snowflake had 9,437 total customers.'" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "agent(\"How many customers does Snowflake have?\")" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:AgentGatewayLogger:running news_search task\n" - ] - }, - { - "data": { - "text/plain": [ - "'\\'Recent news about Azure\\'s AI investments includes:\\\\n- Article: \"Microsoft\\'s SWOT analysis: azure growth, ai investments shape stock outlook\" Source: [Investing.com](https://www.investing.com/news/company-news/microsofts-swot-analysis-azure-growth-ai-investments-shape-stock-outlook-93CH-3701643)\\\\n- Article: \"AI Investments: Key Driver for MSFT and META Earnings\" Source: [GuruFocus](https://www.gurufocus.com/news/2573539/ai-investments-key-driver-for-msft-and-meta-earnings)\\\\n- Article: \"Microsoft: Navigating AI Investments Amid Market Volatility\" Source: [GuruFocus](https://www.gurufocus.com/news/2499558/microsoft-navigating-ai-investments-amid-market-volatility)'" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "agent(\"Give me recent news about Azure's AI investments\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Unstructured + Structured Data Questions" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "INFO:AgentGatewayLogger:running sec_search_service_cortexsearch task\n", - "INFO:AgentGatewayLogger:running summarize task\n", - "INFO:AgentGatewayLogger:running sp500_semantic_model_cortexanalyst task\n" - ] - }, - { - "data": { - "text/plain": [ - "\"The market cap of Snowflake's competitors in the S&P500 are: Microsoft Corporation ($3,150,184,448,000), Alphabet Inc. ($2,164,350,779,392), Amazon.com, Inc. ($1,917,936,336,896), and Oracle Corporation ($346,092,371,968).\"" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "agent(\n", - " \"Which S&P500 companies are considered competitors in Snowflake's annual report? What is the market cap of each company mentioned?\"\n", - ")" - ] - } - ], - "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.11.6" - } + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Agent Gateway Quickstart" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Agent Gateway is a multi-agent framework that offers native support for Snowflake tools. \n", + "\n", + "The system can be configured to work with 3 types of tools:\n", + "- Cortex Search Tool: For unstructured data analysis, which requires a standard RAG access pattern.\n", + "- Cortex Analyst Tool: For supporting structured data analysis, which requires a Text2SQL access pattern.\n", + "- Python Tool: For supporting custom user operations (using 3rd Party API's), which requires calling arbitrary python.\n", + "\n", + "This notebook walks through how to configure and run a system with all 3 types of tools. The demo is designed to illustrate how the agent can answer questions that require a divserse combination of tools (RAG,Text2SQL, Python, or a combination).\n", + "\n", + "Note that Agent Gateway does not configure the underlying Cortex Search or Cortex Analyst services for the user. Those services must be configured before initializing the agent." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Agent Configuration" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Connection Setup" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Authenticate with Snowpark + set token as environment variable for use by the agents." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/alherrera/Downloads/Cube/cortex-cube/.venv/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + } + ], + "source": [ + "from agent_gateway import Agent\n", + "from agent_gateway.tools import CortexSearchTool, CortexAnalystTool, PythonTool\n", + "from snowflake.snowpark import Session\n", + "import os\n", + "\n", + "connection_parameters = {\n", + " \"account\": os.getenv(\"SNOWFLAKE_ACCOUNT\"),\n", + " \"user\": os.getenv(\"SNOWFLAKE_USER\"),\n", + " \"password\": os.getenv(\"SNOWFLAKE_PASSWORD\"),\n", + " \"role\": os.getenv(\"SNOWFLAKE_ROLE\"),\n", + " \"warehouse\": os.getenv(\"SNOWFLAKE_WAREHOUSE\"),\n", + " \"database\": os.getenv(\"SNOWFLAKE_DATABASE\"),\n", + " \"schema\": os.getenv(\"SNOWFLAKE_SCHEMA\"),\n", + "}\n", + "\n", + "snowpark = Session.builder.configs(connection_parameters).create()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Snowflake Tool Configuration" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The Cortex Search Tool and the Cortex Analyst Tool need to be configured as follows. Note that a connection object is required for each config. In the case below we're using the same connection object for both because the services are both in the same account/database/schema. Users have the option to pass in different connection objects as needed." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "search_config = {\n", + " \"service_name\": \"SEC_SEARCH_SERVICE\",\n", + " \"service_topic\": \"Snowflake's business,product offerings,and performance.\",\n", + " \"data_description\": \"Snowflake annual reports\",\n", + " \"retrieval_columns\": [\"CHUNK\"],\n", + " \"snowflake_connection\": snowpark,\n", + "}\n", + "\n", + "analyst_config = {\n", + " \"semantic_model\": \"sp500_semantic_model.yaml\",\n", + " \"stage\": \"ANALYST\",\n", + " \"service_topic\": \"S&P500 company and stock metrics\",\n", + " \"data_description\": \"a table with stock and financial metrics about S&P500 companies \",\n", + " \"snowflake_connection\": snowpark,\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Python Tool Configuration" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Configuring a Python Tool for the Agent Gateway requires 1) Python Callable 2) Tool Description (what does the tool do) 3) Output Description (what does the tool output). \n", + "\n", + "In the example below we create a NewsTool object that submits a HTTP request to a 3rd Party News API. The python callable is passed into the Python Tool as `news_api_func`.To use the tool below get your free token by signing up for an account at thenewsapi.com or just create your own python function and pass it into the PythonTool." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "import json\n", + "\n", + "\n", + "class NewsTool:\n", + " def __init__(self, token, limit) -> None:\n", + " self.api_token = token\n", + " self.limit = limit\n", + "\n", + " def news_search(self, news_query: str) -> str:\n", + " news_request = f\"\"\"https://api.thenewsapi.com/v1/news/all?api_token={self.api_token}&search={news_query}&language=en&limit={self.limit}\"\"\"\n", + " response = requests.get(news_request)\n", + " json_response = json.loads(response.content)[\"data\"]\n", + "\n", + " return str(json_response)\n", + "\n", + "\n", + "python_config = {\n", + " \"tool_description\": \"searches for relevant news based on user query\",\n", + " \"output_description\": \"relevant articles\",\n", + " \"python_func\": NewsTool(token=os.getenv(\"NEWS_API_TOKEN\"), limit=3).news_search,\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Agent Config" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "After the tools have been configured, initialize them and configure the agent." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:AgentGatewayLogger:Cortex Search Tool successfully initialized\n", + "INFO:AgentGatewayLogger:Cortex Analyst Tool successfully initialized\n", + "INFO:AgentGatewayLogger:Python Tool successfully initialized\n", + "INFO:AgentGatewayLogger:Cortex gateway successfully initialized\n" + ] + } + ], + "source": [ + "annual_reports = CortexSearchTool(**search_config)\n", + "sp500 = CortexAnalystTool(**analyst_config)\n", + "news_search = PythonTool(**python_config)\n", + "\n", + "snowflake_tools = [annual_reports, sp500, news_search]\n", + "agent = Agent(snowflake_connection=snowpark, tools=snowflake_tools)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Agent Testing" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The 3 types of questions below are designed to showcase the breadth of tool use patterns possible with the Agent Gateway. \n", + "\n", + "- The Structured Data Questions use the Cortex Analyst agent. \n", + "- The Unstructured Data Questions use either the Cortex Search agent or the Python (News API) agent.\n", + "- The last section includes a question that requires the use of both types of tools." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Structured Data Questions" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:AgentGatewayLogger:running sp500_semantic_model_cortexanalyst task\n" + ] }, - "nbformat": 4, - "nbformat_minor": 2 -} + { + "data": { + "text/plain": [ + "'The market cap of Apple is $3,019,131,060,224 or approximately $3.02 trillion.'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "agent(\"What is the market cap of Apple?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:AgentGatewayLogger:running sp500_semantic_model_cortexanalyst task\n", + "INFO:AgentGatewayLogger:running sp500_semantic_model_cortexanalyst task\n" + ] + }, + { + "data": { + "text/plain": [ + "'Apple has a bigger EBITDA than Microsoft.'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "agent(\"Which company has the bigger EBITDA, Apple or MSFT?\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Unstructured Data Questions" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:AgentGatewayLogger:running sec_search_service_cortexsearch task\n" + ] + }, + { + "data": { + "text/plain": [ + "'As of January 31, 2024, Snowflake had 9,437 total customers.'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "agent(\"How many customers does Snowflake have?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:AgentGatewayLogger:running news_search task\n" + ] + }, + { + "data": { + "text/plain": [ + "'\\'Recent news about Azure\\'s AI investments includes:\\\\n- Article: \"Microsoft\\'s SWOT analysis: azure growth, ai investments shape stock outlook\" Source: [Investing.com](https://www.investing.com/news/company-news/microsofts-swot-analysis-azure-growth-ai-investments-shape-stock-outlook-93CH-3701643)\\\\n- Article: \"AI Investments: Key Driver for MSFT and META Earnings\" Source: [GuruFocus](https://www.gurufocus.com/news/2573539/ai-investments-key-driver-for-msft-and-meta-earnings)\\\\n- Article: \"Microsoft: Navigating AI Investments Amid Market Volatility\" Source: [GuruFocus](https://www.gurufocus.com/news/2499558/microsoft-navigating-ai-investments-amid-market-volatility)'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "agent(\"Give me recent news about Azure's AI investments\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unstructured + Structured Data Questions" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:AgentGatewayLogger:running sec_search_service_cortexsearch task\n", + "INFO:AgentGatewayLogger:running summarize task\n", + "INFO:AgentGatewayLogger:running sp500_semantic_model_cortexanalyst task\n" + ] + }, + { + "data": { + "text/plain": [ + "\"The market cap of Snowflake's competitors in the S&P500 are: Microsoft Corporation ($3,150,184,448,000), Alphabet Inc. ($2,164,350,779,392), Amazon.com, Inc. ($1,917,936,336,896), and Oracle Corporation ($346,092,371,968).\"" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "agent(\n", + " \"Which S&P500 companies are considered competitors in Snowflake's annual report? What is the market cap of each company mentioned?\"\n", + ")" + ] + } + ], + "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.11.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/agent_gateway/agents/agent.py b/agent_gateway/agents/agent.py index 4b89fb1..12aba72 100644 --- a/agent_gateway/agents/agent.py +++ b/agent_gateway/agents/agent.py @@ -21,7 +21,7 @@ import yaml from chains.llm_chain import LLMChain -from langchain.agents.agent import AgentOutputParser, BaseSingleActionAgent +from langchain.agents.agent import BaseSingleActionAgent from langchain.agents.agent_types import AgentType from langchain.callbacks.base import BaseCallbackManager from langchain.callbacks.manager import Callbacks @@ -130,7 +130,7 @@ def plan( full_inputs = self.get_full_inputs(intermediate_steps, **kwargs) full_output = self.llm_chain.predict(callbacks=callbacks, **full_inputs) return self.output_parser.parse(full_output) - except Exception as e: + except Exception: full_inputs["agent_scratchpad"] = ( full_inputs["agent_scratchpad"] + full_output + "\nAction: " ) @@ -161,7 +161,7 @@ async def aplan( callbacks=callbacks, **full_inputs ) agent_output = await self.output_parser.aparse(full_output) - except Exception as e: + except Exception: full_inputs["agent_scratchpad"] = ( full_inputs["agent_scratchpad"] + full_output + "\nAction: " ) diff --git a/agent_gateway/chains/llm_chain.py b/agent_gateway/chains/llm_chain.py index b657568..741e975 100644 --- a/agent_gateway/chains/llm_chain.py +++ b/agent_gateway/chains/llm_chain.py @@ -133,7 +133,7 @@ def generate( callbacks=run_manager.get_child() if run_manager else None, **self.llm_kwargs, ) - except: + except Exception: text = prompts[0].text # Try removing in-context examples first_index = text.find("Question:") @@ -167,7 +167,7 @@ async def agenerate( callbacks=run_manager.get_child() if run_manager else None, **self.llm_kwargs, ) - except: + except Exception: text = prompts[0].text # Try removing in-context examples first_index = text.find("Question:") diff --git a/agent_gateway/executors/agent_executor.py b/agent_gateway/executors/agent_executor.py index 53d42e0..d211b2d 100644 --- a/agent_gateway/executors/agent_executor.py +++ b/agent_gateway/executors/agent_executor.py @@ -30,7 +30,6 @@ ) from langchain.pydantic_v1 import root_validator from langchain.schema import AgentAction, AgentFinish, OutputParserException -from langchain.tools import BaseTool from langchain.utilities.asyncio import asyncio_timeout from langchain.utils.input import get_color_mapping diff --git a/agent_gateway/gateway/gateway.py b/agent_gateway/gateway/gateway.py index ab0a9b7..7324135 100644 --- a/agent_gateway/gateway/gateway.py +++ b/agent_gateway/gateway/gateway.py @@ -69,7 +69,7 @@ async def arun(self, prompt: str) -> str: try: snowflake_response = self._parse_snowflake_response(response_text) return snowflake_response - except: + except Exception: raise AgentGatewayError( message=f"Failed Cortex LLM Request. Unable to parse response. See details:{response_text}" ) diff --git a/agent_gateway/gateway/output_parser.py b/agent_gateway/gateway/output_parser.py index 30f27b9..01b4ed5 100644 --- a/agent_gateway/gateway/output_parser.py +++ b/agent_gateway/gateway/output_parser.py @@ -188,7 +188,7 @@ def instantiate_task( args = _parse_llm_compiler_action_args(args) if tool_name == "fuse": # fuse does not have a tool - tool_func = lambda x: None + tool_func = lambda x: None # noqa: E731 stringify_rule = None else: tool = _find_tool(tool_name, tools) diff --git a/agent_gateway/gateway/planner.py b/agent_gateway/gateway/planner.py index 670a971..50a5628 100644 --- a/agent_gateway/gateway/planner.py +++ b/agent_gateway/gateway/planner.py @@ -259,7 +259,7 @@ async def run_llm( try: snowflake_response = self._parse_snowflake_response(response_text) return snowflake_response - except: + except Exception: raise AgentGatewayError( message=f"Failed Cortex LLM Request. Unable to parse response. See details:{response_text}" ) diff --git a/agent_gateway/tools/utils.py b/agent_gateway/tools/utils.py index f7a7370..56abb63 100644 --- a/agent_gateway/tools/utils.py +++ b/agent_gateway/tools/utils.py @@ -40,7 +40,7 @@ class Headers(TypedDict): def _determine_runtime(): try: - from _stored_proc_restful import StoredProcRestful + from _stored_proc_restful import StoredProcRestful # noqa: F401 return True except ImportError: diff --git a/demo_app/demo_app.py b/demo_app/demo_app.py index fa35d3b..2366792 100644 --- a/demo_app/demo_app.py +++ b/demo_app/demo_app.py @@ -13,6 +13,7 @@ import asyncio import io import json +import logging import os import queue import re @@ -114,8 +115,6 @@ def create_prompt(prompt_key: str): source_list = [] -import logging - class StreamlitLogHandler(logging.Handler): def __init__(self):