diff --git a/docs/extras/modules/agents/agent_types/openai_multi_functions_agent.ipynb b/docs/extras/modules/agents/agent_types/openai_multi_functions_agent.ipynb new file mode 100644 index 00000000..838544bf --- /dev/null +++ b/docs/extras/modules/agents/agent_types/openai_multi_functions_agent.ipynb @@ -0,0 +1,253 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "9502d5b0", + "metadata": {}, + "source": [ + "# OpenAI Functions Agent\n", + "\n", + "This notebook showcases using an agent that uses the OpenAI functions ability to respond to the prompts of the user using a Large Language Model\n", + "\n", + "Install openai,google-search-results packages which are required as the langchain packages call them internally\n", + "\n", + ">pip install openai google-search-results\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "c0a83623", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.5) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n", + " warnings.warn(\n" + ] + } + ], + "source": [ + "from langchain import (\n", + " LLMMathChain,\n", + " OpenAI,\n", + " SerpAPIWrapper,\n", + " SQLDatabase,\n", + " SQLDatabaseChain,\n", + ")\n", + "from langchain.agents import initialize_agent, Tool\n", + "from langchain.agents import AgentType\n", + "from langchain.chat_models import ChatOpenAI" + ] + }, + { + "cell_type": "markdown", + "id": "86198d9c", + "metadata": {}, + "source": [ + "The agent is given ability to perform 3 search functionalities with the respective tools\n", + "\n", + "SerpAPIWrapper:\n", + ">This initializes the SerpAPIWrapper for search functionality (search).\n", + "\n", + "LLMMathChain Initialization:\n", + ">This component provides math-related functionality.\n", + "\n", + "SQL Database Initialization:\n", + ">This component provides the agent to query in Custom Data Base.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6fefaba2", + "metadata": {}, + "outputs": [], + "source": [ + "# Initialize the OpenAI language model\n", + "#Replace in openai_api_key=\"\" with your actual OpenAI key.\n", + "llm = ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-0613\")\n", + "\n", + "# Initialize the SerpAPIWrapper for search functionality\n", + "#Replace in openai_api_key=\"\" with your actual SerpAPI key.\n", + "search = SerpAPIWrapper()\n", + "\n", + "# Define a list of tools offered by the agent\n", + "tools = [\n", + " Tool(\n", + " name=\"Search\",\n", + " func=search.run,\n", + " description=\"Useful when you need to answer questions about current events. You should ask targeted questions.\"\n", + " ),\n", + "]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9ff6cee9", + "metadata": {}, + "outputs": [], + "source": [ + "mrkl = initialize_agent(tools, llm, agent=AgentType.OPENAI_MULTI_FUNCTIONS, verbose=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "cbe95c81", + "metadata": {}, + "outputs": [], + "source": [ + "# Do this so we can exactly what's going on under the hood\n", + "import langchain\n", + "langchain.debug = True" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "ba8e4cbe", + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor] Entering Chain run with input:\n", + "\u001b[0m{\n", + " \"input\": \"What is the weather in LA and SF?\"\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:llm:ChatOpenAI] Entering LLM run with input:\n", + "\u001b[0m{\n", + " \"prompts\": [\n", + " \"System: You are a helpful AI assistant.\\nHuman: What is the weather in LA and SF?\"\n", + " ]\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:llm:ChatOpenAI] [2.76s] Exiting LLM run with output:\n", + "\u001b[0m{\n", + " \"generations\": [\n", + " [\n", + " {\n", + " \"text\": \"\",\n", + " \"generation_info\": null,\n", + " \"message\": {\n", + " \"content\": \"\",\n", + " \"additional_kwargs\": {\n", + " \"function_call\": {\n", + " \"name\": \"tool_selection\",\n", + " \"arguments\": \"{\\n \\\"actions\\\": [\\n {\\n \\\"action_name\\\": \\\"Search\\\",\\n \\\"action\\\": {\\n \\\"tool_input\\\": \\\"weather in Los Angeles\\\"\\n }\\n },\\n {\\n \\\"action_name\\\": \\\"Search\\\",\\n \\\"action\\\": {\\n \\\"tool_input\\\": \\\"weather in San Francisco\\\"\\n }\\n }\\n ]\\n}\"\n", + " }\n", + " },\n", + " \"example\": false\n", + " }\n", + " }\n", + " ]\n", + " ],\n", + " \"llm_output\": {\n", + " \"token_usage\": {\n", + " \"prompt_tokens\": 81,\n", + " \"completion_tokens\": 75,\n", + " \"total_tokens\": 156\n", + " },\n", + " \"model_name\": \"gpt-3.5-turbo-0613\"\n", + " },\n", + " \"run\": null\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[tool/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:Search] Entering Tool run with input:\n", + "\u001b[0m\"{'tool_input': 'weather in Los Angeles'}\"\n", + "\u001b[36;1m\u001b[1;3m[tool/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:Search] [112.99199999999999ms] Exiting Tool run with output:\n", + "\u001b[0m\"Mostly cloudy early, then sunshine for the afternoon. High 76F. Winds SW at 5 to 10 mph. Humidity59%.\"\n", + "\u001b[32;1m\u001b[1;3m[tool/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 4:tool:Search] Entering Tool run with input:\n", + "\u001b[0m\"{'tool_input': 'weather in San Francisco'}\"\n", + "\u001b[36;1m\u001b[1;3m[tool/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 4:tool:Search] [522.576ms] Exiting Tool run with output:\n", + "\u001b[0m\"Partly cloudy this evening, then becoming cloudy after midnight. Low 53F. Winds WSW at 10 to 20 mph. Humidity83%.\"\n", + "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 5:llm:ChatOpenAI] Entering LLM run with input:\n", + "\u001b[0m{\n", + " \"prompts\": [\n", + " \"System: You are a helpful AI assistant.\\nHuman: What is the weather in LA and SF?\\nAI: {'name': 'tool_selection', 'arguments': '{\\\\n \\\"actions\\\": [\\\\n {\\\\n \\\"action_name\\\": \\\"Search\\\",\\\\n \\\"action\\\": {\\\\n \\\"tool_input\\\": \\\"weather in Los Angeles\\\"\\\\n }\\\\n },\\\\n {\\\\n \\\"action_name\\\": \\\"Search\\\",\\\\n \\\"action\\\": {\\\\n \\\"tool_input\\\": \\\"weather in San Francisco\\\"\\\\n }\\\\n }\\\\n ]\\\\n}'}\\nFunction: Mostly cloudy early, then sunshine for the afternoon. High 76F. Winds SW at 5 to 10 mph. Humidity59%.\\nAI: {'name': 'tool_selection', 'arguments': '{\\\\n \\\"actions\\\": [\\\\n {\\\\n \\\"action_name\\\": \\\"Search\\\",\\\\n \\\"action\\\": {\\\\n \\\"tool_input\\\": \\\"weather in Los Angeles\\\"\\\\n }\\\\n },\\\\n {\\\\n \\\"action_name\\\": \\\"Search\\\",\\\\n \\\"action\\\": {\\\\n \\\"tool_input\\\": \\\"weather in San Francisco\\\"\\\\n }\\\\n }\\\\n ]\\\\n}'}\\nFunction: Partly cloudy this evening, then becoming cloudy after midnight. Low 53F. Winds WSW at 10 to 20 mph. Humidity83%.\"\n", + " ]\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 5:llm:ChatOpenAI] [1.88s] Exiting LLM run with output:\n", + "\u001b[0m{\n", + " \"generations\": [\n", + " [\n", + " {\n", + " \"text\": \"The weather in Los Angeles is mostly cloudy with a high of 76°F and a humidity of 59%. The weather in San Francisco is partly cloudy in the evening, becoming cloudy after midnight, with a low of 53°F and a humidity of 83%.\",\n", + " \"generation_info\": null,\n", + " \"message\": {\n", + " \"content\": \"The weather in Los Angeles is mostly cloudy with a high of 76°F and a humidity of 59%. The weather in San Francisco is partly cloudy in the evening, becoming cloudy after midnight, with a low of 53°F and a humidity of 83%.\",\n", + " \"additional_kwargs\": {},\n", + " \"example\": false\n", + " }\n", + " }\n", + " ]\n", + " ],\n", + " \"llm_output\": {\n", + " \"token_usage\": {\n", + " \"prompt_tokens\": 307,\n", + " \"completion_tokens\": 54,\n", + " \"total_tokens\": 361\n", + " },\n", + " \"model_name\": \"gpt-3.5-turbo-0613\"\n", + " },\n", + " \"run\": null\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor] [5.28s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"output\": \"The weather in Los Angeles is mostly cloudy with a high of 76°F and a humidity of 59%. The weather in San Francisco is partly cloudy in the evening, becoming cloudy after midnight, with a low of 53°F and a humidity of 83%.\"\n", + "}\n" + ] + }, + { + "data": { + "text/plain": [ + "'The weather in Los Angeles is mostly cloudy with a high of 76°F and a humidity of 59%. The weather in San Francisco is partly cloudy in the evening, becoming cloudy after midnight, with a low of 53°F and a humidity of 83%.'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mrkl.run(\n", + " \"What is the weather in LA and SF?\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9f5f6743", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/extras/modules/agents/toolkits/document_comparison_toolkit.ipynb b/docs/extras/modules/agents/toolkits/document_comparison_toolkit.ipynb index 26aa8325..1571378e 100644 --- a/docs/extras/modules/agents/toolkits/document_comparison_toolkit.ipynb +++ b/docs/extras/modules/agents/toolkits/document_comparison_toolkit.ipynb @@ -22,7 +22,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "/Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.4) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n", + "/Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.5) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n", " warnings.warn(\n" ] } @@ -100,7 +100,9 @@ "cell_type": "code", "execution_count": 4, "id": "c4d56c25", - "metadata": {}, + "metadata": { + "scrolled": false + }, "outputs": [ { "name": "stdout", @@ -113,11 +115,11 @@ "Invoking: `alphabet-earnings` with `{'question': 'revenue'}`\n", "\n", "\n", - "\u001b[0m\u001b[36;1m\u001b[1;3m{'query': 'revenue', 'result': 'The revenue for Alphabet Inc. in the first quarter of 2023 was $69,787 million.'}\u001b[0m\u001b[32;1m\u001b[1;3m\n", + "\u001b[0m\u001b[36;1m\u001b[1;3m{'query': 'revenue', 'result': 'The revenue for Alphabet Inc. for the quarter ended March 31, 2023, was $69,787 million.'}\u001b[0m\u001b[32;1m\u001b[1;3m\n", "Invoking: `tesla-earnings` with `{'question': 'revenue'}`\n", "\n", "\n", - "\u001b[0m\u001b[33;1m\u001b[1;3m{'query': 'revenue', 'result': 'Total revenue for Q1-2023 was $23.3 billion.'}\u001b[0m\u001b[32;1m\u001b[1;3mAlphabet Inc. had more revenue than Tesla. In the first quarter of 2023, Alphabet Inc. had a revenue of $69,787 million, while Tesla had a revenue of $23.3 billion.\u001b[0m\n", + "\u001b[0m\u001b[33;1m\u001b[1;3m{'query': 'revenue', 'result': 'Total revenue for Q1-2023 was $23.3 billion.'}\u001b[0m\u001b[32;1m\u001b[1;3mAlphabet Inc. had more revenue than Tesla. Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million, while Tesla's total revenue for Q1-2023 was $23.3 billion.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] @@ -126,7 +128,7 @@ "data": { "text/plain": [ "{'input': 'did alphabet or tesla have more revenue?',\n", - " 'output': 'Alphabet Inc. had more revenue than Tesla. In the first quarter of 2023, Alphabet Inc. had a revenue of $69,787 million, while Tesla had a revenue of $23.3 billion.'}" + " 'output': \"Alphabet Inc. had more revenue than Tesla. Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million, while Tesla's total revenue for Q1-2023 was $23.3 billion.\"}" ] }, "execution_count": 4, @@ -150,13 +152,271 @@ "agent({\"input\": \"did alphabet or tesla have more revenue?\"})" ] }, + { + "cell_type": "markdown", + "id": "6f512043", + "metadata": {}, + "source": [ + "## OpenAI Multi Functions\n", + "\n", + "This type of agent allows calling multiple functions at once. This is really useful when some steps can be computed in parallel - like when asked to compare multiple documents" + ] + }, { "cell_type": "code", - "execution_count": null, - "id": "6db4c853", + "execution_count": 5, + "id": "0fb099d2", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import langchain\n", + "langchain.debug = True" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6db4c853", + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor] Entering Chain run with input:\n", + "\u001b[0m{\n", + " \"input\": \"did alphabet or tesla have more revenue?\"\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:llm:ChatOpenAI] Entering LLM run with input:\n", + "\u001b[0m{\n", + " \"prompts\": [\n", + " \"System: You are a helpful AI assistant.\\nHuman: did alphabet or tesla have more revenue?\"\n", + " ]\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 2:llm:ChatOpenAI] [2.66s] Exiting LLM run with output:\n", + "\u001b[0m{\n", + " \"generations\": [\n", + " [\n", + " {\n", + " \"text\": \"\",\n", + " \"generation_info\": null,\n", + " \"message\": {\n", + " \"content\": \"\",\n", + " \"additional_kwargs\": {\n", + " \"function_call\": {\n", + " \"name\": \"tool_selection\",\n", + " \"arguments\": \"{\\n \\\"actions\\\": [\\n {\\n \\\"action_name\\\": \\\"alphabet-earnings\\\",\\n \\\"action\\\": {\\n \\\"question\\\": \\\"What was Alphabet's revenue?\\\"\\n }\\n },\\n {\\n \\\"action_name\\\": \\\"tesla-earnings\\\",\\n \\\"action\\\": {\\n \\\"question\\\": \\\"What was Tesla's revenue?\\\"\\n }\\n }\\n ]\\n}\"\n", + " }\n", + " },\n", + " \"example\": false\n", + " }\n", + " }\n", + " ]\n", + " ],\n", + " \"llm_output\": {\n", + " \"token_usage\": {\n", + " \"prompt_tokens\": 99,\n", + " \"completion_tokens\": 82,\n", + " \"total_tokens\": 181\n", + " },\n", + " \"model_name\": \"gpt-3.5-turbo-0613\"\n", + " },\n", + " \"run\": null\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[tool/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings] Entering Tool run with input:\n", + "\u001b[0m\"{'question': \"What was Alphabet's revenue?\"}\"\n", + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA] Entering Chain run with input:\n", + "\u001b[0m{\n", + " \"query\": \"What was Alphabet's revenue?\"\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain] Entering Chain run with input:\n", + "\u001b[0m[inputs]\n", + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain > 6:chain:LLMChain] Entering Chain run with input:\n", + "\u001b[0m{\n", + " \"question\": \"What was Alphabet's revenue?\",\n", + " \"context\": \"Alphabet Inc.\\nCONSOLIDATED STATEMENTS OF INCOME\\n(In millions, except per share amounts, unaudited)\\nQuarter Ended March 31,\\n2022 2023\\nRevenues $ 68,011 $ 69,787 \\nCosts and expenses:\\nCost of revenues 29,599 30,612 \\nResearch and development 9,119 11,468 \\nSales and marketing 5,825 6,533 \\nGeneral and administrative 3,374 3,759 \\nTotal costs and expenses 47,917 52,372 \\nIncome from operations 20,094 17,415 \\nOther income (expense), net (1,160) 790 \\nIncome before income taxes 18,934 18,205 \\nProvision for income taxes 2,498 3,154 \\nNet income $ 16,436 $ 15,051 \\nBasic earnings per share of Class A, Class B, and Class C stock $ 1.24 $ 1.18 \\nDiluted earnings per share of Class A, Class B, and Class C stock $ 1.23 $ 1.17 \\nNumber of shares used in basic earnings per share calculation 13,203 12,781 \\nNumber of shares used in diluted earnings per share calculation 13,351 12,823 \\n6\\n\\nAlphabet Announces First Quarter 2023 Results\\nMOUNTAIN VIEW, Calif. – April 25, 2023 – Alphabet Inc. (NASDAQ: GOOG, GOOGL) today announced financial \\nresults for the quarter ended March 31, 2023 .\\nSundar Pichai, CEO of Alphabet and Google, said: “We are pleased with our business performance in the first \\nquarter, with Search performing well and momentum in Cloud. We introduced important product updates anchored \\nin deep computer science and AI. Our North Star is providing the most helpful answers for our users, and we see \\nhuge opportunities ahead, continuing our long track record of innovation.”\\nRuth Porat, CFO of Alphabet and Google, said: “Resilience in Search and momentum in Cloud resulted in Q1 \\nconsolidated revenues of $69.8 billion, up 3% year over year, or up 6% in constant currency. We remain committed \\nto delivering long-term growth and creating capacity to invest in our most compelling growth areas by re-engineering \\nour cost base.”\\nQ1 2023 financial highlights (unaudited)\\nOur first quarter 2023 results reflect:\\ni.$2.6 billion in charges related to reductions in our workforce and office space; \\nii.a $988 million reduction in depreciation expense from the change in estimated useful life of our servers and \\ncertain network equipment; and\\niii.a shift in the timing of our annual employee stock-based compensation awards resulting in relatively less \\nstock-based compensation expense recognized in the first quarter compared to the remaining quarters of \\nthe ye ar. The shift in timing itself will not affect the amount of stock-based compensation expense over the \\nfull fiscal year 2023.\\nFor further information, please refer to our blog post also filed with the SEC via Form 8-K on April 20, 2023.\\nThe following table summarizes our consolidated financial results for the quarters ended March 31, 2022 and 2023 \\n(in millions, except for per share information and percentages). \\nQuarter Ended March 31,\\n2022 2023\\nRevenues $ 68,011 $ 69,787 \\nChange in revenues year over year 23 % 3 %\\nChange in constant currency revenues year over year(1) 26 % 6 %\\nOperating income $ 20,094 $ 17,415 \\nOperating margin 30 % 25 %\\nOther income (expense), net $ (1,160) $ 790 \\nNet income $ 16,436 $ 15,051 \\nDiluted EPS $ 1.23 $ 1.17 \\n(1) Non-GAAP measure. See the table captioned “Reconciliation from GAAP revenues to non-GAAP constant currency \\nrevenues and GAAP percentage change in revenues to non-GAAP percentage change in constant currency revenues” for \\nmore details.\\n\\nQ1 2023 supplemental information (in millions, except for number of employees; unaudited)\\nRevenues, T raffic Acquisition Costs (TAC), and number of employees\\nQuarter Ended March 31,\\n2022 2023\\nGoogle Search & other $ 39,618 $ 40,359 \\nYouTube ads 6,869 6,693 \\nGoogle Network 8,174 7,496 \\nGoogle advertising 54,661 54,548 \\nGoogle other 6,811 7,413 \\nGoogle Services total 61,472 61,961 \\nGoogle Cloud 5,821 7,454 \\nOther Bets 440 288 \\nHedging gains (losses) 278 84 \\nTotal revenues $ 68,011 $ 69,787 \\nTotal TAC $ 11,990 $ 11,721 \\nNumber of employees(1) 163,906 190,711 \\n(1) As of March 31, 2023, the number of employees includes almost all of the employees affected by the reduction of our \\nworkforce. We expect most of those affected will no longer be reflected in our headcount by the end of the second quarter \\nof 2023, subject to local law and consultation requirements.\\nSegment Operating Results\\nReflecting DeepMind’s increasing collaboration with Google Services, Google Cloud, and Other Bets, beginning in \\nthe first quarter of 2023 DeepMind is reported as part of Alphabet’s unallocated corporate costs instead of within \\nOther Bets. Additionally, beginning in the first quarter of 2023, we updated and simplified our cost allocation \\nmethodologies to provide our business leaders with increased transparency for decision-making . Prior periods have \\nbeen recast to reflect the revised presentation and are shown in Recast Historical Segment Results below .\\nAs announced on April 20, 2023 , we are bringing together part of Google Research (the Brain Team) and DeepMind \\nto significantly accelerate our progress in AI. This change does not affect first quarter reporting. The group, called \\nGoogle DeepMind, will be reported within Alphabet's unallocated corporate costs beginning in the second quarter of \\n2023.\\nQuarter Ended March 31,\\n2022 2023\\n(recast)\\nOperating income (loss):\\nGoogle Services $ 21,973 $ 21,737 \\nGoogle Cloud (706) 191 \\nOther Bets (835) (1,225) \\nCorporate costs, unallocated(1) (338) (3,288) \\nTotal income from operations $ 20,094 $ 17,415 \\n(1)Hedging gains (losses) related to revenue included in unallocated corporate costs were $278 million and $84 million for the \\nthree months ended March 31, 2022 and 2023 , respectively. For the three months ended March 31, 2023, unallocated \\ncorporate costs include charges related to the reductions in our workforce and office space totaling $2.5 billion . \\n2\\n\\nSegment results\\nThe following table presents our segment revenues and operating income (loss) (in millions; unaudited):\\nQuarter Ended March 31,\\n2022 2023\\n(recast)\\nRevenues:\\nGoogle Services $ 61,472 $ 61,961 \\nGoogle Cloud 5,821 7,454 \\nOther Bets 440 288 \\nHedging gains (losses) 278 84 \\nTotal revenues $ 68,011 $ 69,787 \\nOperating income (loss):\\nGoogle Services $ 21,973 $ 21,737 \\nGoogle Cloud (706) 191 \\nOther Bets (835) (1,225) \\nCorporate costs, unallocated (338) (3,288) \\nTotal income from operations $ 20,094 $ 17,415 \\nWe report our segment results as Google Services, Google Cloud, and Other Bets:\\n•Google Services includes products and services such as ads, Android, Chrome, hardware, Google Maps, \\nGoogle Play, Search, and YouTube. Google Services generates revenues primarily from advertising; sales \\nof apps and in-app purchases, and hardware; and fees received for subscription-based products such as \\nYouTube Premium and YouTube TV.\\n•Google Cloud includes infrastructure and platform services, collaboration tools, and other services for \\nenterprise customers. Google Cloud generates revenues from fees received for Google Cloud Platform \\nservices, Google Workspace communication and collaboration tools, and other enterprise services.\\n•Other Bets is a combination of multiple operating segments that are not individually material. Revenues \\nfrom Other Bets are generated primarily from the sale of health technology and internet services.\\nAfter the segment reporting changes discussed above, unallocated corporate costs primarily include AI-focused \\nshared R&D activities; corporate initiatives such as our philanthropic activities; and corporate shared costs such as \\nfinance, certain human resource costs, and legal, including certain fines and settlements. In the first quarter of 2023, \\nunallocated corporate costs also include charges associated with reductions in our workforce and office space. \\nAdditionally, hedging gains (losses) related to revenue are included in unallocated corporate costs.\\nRecast Historical Segment Results\\nRecast historical segment results are as follows (in millions; unaudited):\\nQuarter Fiscal Year\\nRecast Historical Results\\nQ1 2022 Q2 2022 Q3 2022 Q4 2022 2021 2022\\nOperating income (loss):\\nGoogle Services $ 21,973 $ 21,621 $ 18,883 $ 20,222 $ 88,132 $ 82,699 \\nGoogle Cloud (706) (590) (440) (186) (2,282) (1,922) \\nOther Bets (835) (1,339) (1,225) (1,237) (4,051) (4,636) \\nCorporate costs, unallocated(1) (338) (239) (83) (639) (3,085) (1,299) \\nTotal income from operations $ 20,094 $ 19,453 $ 17,135 $ 18,160 $ 78,714 $ 74,842 \\n(1)Includes hedging gains (losses); in fiscal years 2021 and 2022 hedging gains of $149 million and $2.0 billion, respectively.\\n8\"\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain > 6:chain:LLMChain > 7:llm:ChatOpenAI] Entering LLM run with input:\n", + "\u001b[0m{\n", + " \"prompts\": [\n", + " \"System: Use the following pieces of context to answer the users question. \\nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\\n----------------\\nAlphabet Inc.\\nCONSOLIDATED STATEMENTS OF INCOME\\n(In millions, except per share amounts, unaudited)\\nQuarter Ended March 31,\\n2022 2023\\nRevenues $ 68,011 $ 69,787 \\nCosts and expenses:\\nCost of revenues 29,599 30,612 \\nResearch and development 9,119 11,468 \\nSales and marketing 5,825 6,533 \\nGeneral and administrative 3,374 3,759 \\nTotal costs and expenses 47,917 52,372 \\nIncome from operations 20,094 17,415 \\nOther income (expense), net (1,160) 790 \\nIncome before income taxes 18,934 18,205 \\nProvision for income taxes 2,498 3,154 \\nNet income $ 16,436 $ 15,051 \\nBasic earnings per share of Class A, Class B, and Class C stock $ 1.24 $ 1.18 \\nDiluted earnings per share of Class A, Class B, and Class C stock $ 1.23 $ 1.17 \\nNumber of shares used in basic earnings per share calculation 13,203 12,781 \\nNumber of shares used in diluted earnings per share calculation 13,351 12,823 \\n6\\n\\nAlphabet Announces First Quarter 2023 Results\\nMOUNTAIN VIEW, Calif. – April 25, 2023 – Alphabet Inc. (NASDAQ: GOOG, GOOGL) today announced financial \\nresults for the quarter ended March 31, 2023 .\\nSundar Pichai, CEO of Alphabet and Google, said: “We are pleased with our business performance in the first \\nquarter, with Search performing well and momentum in Cloud. We introduced important product updates anchored \\nin deep computer science and AI. Our North Star is providing the most helpful answers for our users, and we see \\nhuge opportunities ahead, continuing our long track record of innovation.”\\nRuth Porat, CFO of Alphabet and Google, said: “Resilience in Search and momentum in Cloud resulted in Q1 \\nconsolidated revenues of $69.8 billion, up 3% year over year, or up 6% in constant currency. We remain committed \\nto delivering long-term growth and creating capacity to invest in our most compelling growth areas by re-engineering \\nour cost base.”\\nQ1 2023 financial highlights (unaudited)\\nOur first quarter 2023 results reflect:\\ni.$2.6 billion in charges related to reductions in our workforce and office space; \\nii.a $988 million reduction in depreciation expense from the change in estimated useful life of our servers and \\ncertain network equipment; and\\niii.a shift in the timing of our annual employee stock-based compensation awards resulting in relatively less \\nstock-based compensation expense recognized in the first quarter compared to the remaining quarters of \\nthe ye ar. The shift in timing itself will not affect the amount of stock-based compensation expense over the \\nfull fiscal year 2023.\\nFor further information, please refer to our blog post also filed with the SEC via Form 8-K on April 20, 2023.\\nThe following table summarizes our consolidated financial results for the quarters ended March 31, 2022 and 2023 \\n(in millions, except for per share information and percentages). \\nQuarter Ended March 31,\\n2022 2023\\nRevenues $ 68,011 $ 69,787 \\nChange in revenues year over year 23 % 3 %\\nChange in constant currency revenues year over year(1) 26 % 6 %\\nOperating income $ 20,094 $ 17,415 \\nOperating margin 30 % 25 %\\nOther income (expense), net $ (1,160) $ 790 \\nNet income $ 16,436 $ 15,051 \\nDiluted EPS $ 1.23 $ 1.17 \\n(1) Non-GAAP measure. See the table captioned “Reconciliation from GAAP revenues to non-GAAP constant currency \\nrevenues and GAAP percentage change in revenues to non-GAAP percentage change in constant currency revenues” for \\nmore details.\\n\\nQ1 2023 supplemental information (in millions, except for number of employees; unaudited)\\nRevenues, T raffic Acquisition Costs (TAC), and number of employees\\nQuarter Ended March 31,\\n2022 2023\\nGoogle Search & other $ 39,618 $ 40,359 \\nYouTube ads 6,869 6,693 \\nGoogle Network 8,174 7,496 \\nGoogle advertising 54,661 54,548 \\nGoogle other 6,811 7,413 \\nGoogle Services total 61,472 61,961 \\nGoogle Cloud 5,821 7,454 \\nOther Bets 440 288 \\nHedging gains (losses) 278 84 \\nTotal revenues $ 68,011 $ 69,787 \\nTotal TAC $ 11,990 $ 11,721 \\nNumber of employees(1) 163,906 190,711 \\n(1) As of March 31, 2023, the number of employees includes almost all of the employees affected by the reduction of our \\nworkforce. We expect most of those affected will no longer be reflected in our headcount by the end of the second quarter \\nof 2023, subject to local law and consultation requirements.\\nSegment Operating Results\\nReflecting DeepMind’s increasing collaboration with Google Services, Google Cloud, and Other Bets, beginning in \\nthe first quarter of 2023 DeepMind is reported as part of Alphabet’s unallocated corporate costs instead of within \\nOther Bets. Additionally, beginning in the first quarter of 2023, we updated and simplified our cost allocation \\nmethodologies to provide our business leaders with increased transparency for decision-making . Prior periods have \\nbeen recast to reflect the revised presentation and are shown in Recast Historical Segment Results below .\\nAs announced on April 20, 2023 , we are bringing together part of Google Research (the Brain Team) and DeepMind \\nto significantly accelerate our progress in AI. This change does not affect first quarter reporting. The group, called \\nGoogle DeepMind, will be reported within Alphabet's unallocated corporate costs beginning in the second quarter of \\n2023.\\nQuarter Ended March 31,\\n2022 2023\\n(recast)\\nOperating income (loss):\\nGoogle Services $ 21,973 $ 21,737 \\nGoogle Cloud (706) 191 \\nOther Bets (835) (1,225) \\nCorporate costs, unallocated(1) (338) (3,288) \\nTotal income from operations $ 20,094 $ 17,415 \\n(1)Hedging gains (losses) related to revenue included in unallocated corporate costs were $278 million and $84 million for the \\nthree months ended March 31, 2022 and 2023 , respectively. For the three months ended March 31, 2023, unallocated \\ncorporate costs include charges related to the reductions in our workforce and office space totaling $2.5 billion . \\n2\\n\\nSegment results\\nThe following table presents our segment revenues and operating income (loss) (in millions; unaudited):\\nQuarter Ended March 31,\\n2022 2023\\n(recast)\\nRevenues:\\nGoogle Services $ 61,472 $ 61,961 \\nGoogle Cloud 5,821 7,454 \\nOther Bets 440 288 \\nHedging gains (losses) 278 84 \\nTotal revenues $ 68,011 $ 69,787 \\nOperating income (loss):\\nGoogle Services $ 21,973 $ 21,737 \\nGoogle Cloud (706) 191 \\nOther Bets (835) (1,225) \\nCorporate costs, unallocated (338) (3,288) \\nTotal income from operations $ 20,094 $ 17,415 \\nWe report our segment results as Google Services, Google Cloud, and Other Bets:\\n•Google Services includes products and services such as ads, Android, Chrome, hardware, Google Maps, \\nGoogle Play, Search, and YouTube. Google Services generates revenues primarily from advertising; sales \\nof apps and in-app purchases, and hardware; and fees received for subscription-based products such as \\nYouTube Premium and YouTube TV.\\n•Google Cloud includes infrastructure and platform services, collaboration tools, and other services for \\nenterprise customers. Google Cloud generates revenues from fees received for Google Cloud Platform \\nservices, Google Workspace communication and collaboration tools, and other enterprise services.\\n•Other Bets is a combination of multiple operating segments that are not individually material. Revenues \\nfrom Other Bets are generated primarily from the sale of health technology and internet services.\\nAfter the segment reporting changes discussed above, unallocated corporate costs primarily include AI-focused \\nshared R&D activities; corporate initiatives such as our philanthropic activities; and corporate shared costs such as \\nfinance, certain human resource costs, and legal, including certain fines and settlements. In the first quarter of 2023, \\nunallocated corporate costs also include charges associated with reductions in our workforce and office space. \\nAdditionally, hedging gains (losses) related to revenue are included in unallocated corporate costs.\\nRecast Historical Segment Results\\nRecast historical segment results are as follows (in millions; unaudited):\\nQuarter Fiscal Year\\nRecast Historical Results\\nQ1 2022 Q2 2022 Q3 2022 Q4 2022 2021 2022\\nOperating income (loss):\\nGoogle Services $ 21,973 $ 21,621 $ 18,883 $ 20,222 $ 88,132 $ 82,699 \\nGoogle Cloud (706) (590) (440) (186) (2,282) (1,922) \\nOther Bets (835) (1,339) (1,225) (1,237) (4,051) (4,636) \\nCorporate costs, unallocated(1) (338) (239) (83) (639) (3,085) (1,299) \\nTotal income from operations $ 20,094 $ 19,453 $ 17,135 $ 18,160 $ 78,714 $ 74,842 \\n(1)Includes hedging gains (losses); in fiscal years 2021 and 2022 hedging gains of $149 million and $2.0 billion, respectively.\\n8\\nHuman: What was Alphabet's revenue?\"\n", + " ]\n", + "}\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain > 6:chain:LLMChain > 7:llm:ChatOpenAI] [1.61s] Exiting LLM run with output:\n", + "\u001b[0m{\n", + " \"generations\": [\n", + " [\n", + " {\n", + " \"text\": \"Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million.\",\n", + " \"generation_info\": null,\n", + " \"message\": {\n", + " \"content\": \"Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million.\",\n", + " \"additional_kwargs\": {},\n", + " \"example\": false\n", + " }\n", + " }\n", + " ]\n", + " ],\n", + " \"llm_output\": {\n", + " \"token_usage\": {\n", + " \"prompt_tokens\": 2335,\n", + " \"completion_tokens\": 23,\n", + " \"total_tokens\": 2358\n", + " },\n", + " \"model_name\": \"gpt-3.5-turbo-0613\"\n", + " },\n", + " \"run\": null\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain > 6:chain:LLMChain] [1.61s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"text\": \"Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million.\"\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain] [1.61s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"output_text\": \"Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million.\"\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA] [1.85s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"result\": \"Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million.\"\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[tool/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 3:tool:alphabet-earnings] [1.86s] Exiting Tool run with output:\n", + "\u001b[0m\"{'query': \"What was Alphabet's revenue?\", 'result': \"Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million.\"}\"\n", + "\u001b[32;1m\u001b[1;3m[tool/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings] Entering Tool run with input:\n", + "\u001b[0m\"{'question': \"What was Tesla's revenue?\"}\"\n", + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA] Entering Chain run with input:\n", + "\u001b[0m{\n", + " \"query\": \"What was Tesla's revenue?\"\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain] Entering Chain run with input:\n", + "\u001b[0m[inputs]\n", + "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain > 11:chain:LLMChain] Entering Chain run with input:\n", + "\u001b[0m{\n", + " \"question\": \"What was Tesla's revenue?\",\n", + " \"context\": \"S U M M A R Y H I G H L I G H T S \\n(1) Excludes SBC (stock -based compensation).\\n(2) Free cash flow = operating cash flow less capex.\\n(3) Includes cash, cash equivalents and investments.Profitability 11.4% operating margin in Q1\\n$2.7B GAAP operating income in Q1\\n$2.5B GAAP net income in Q1\\n$2.9B non -GAAP net income1in Q1In the current macroeconomic environment, we see this year as a unique \\nopportunity for Tesla. As many carmakers are working through challenges with the \\nunit economics of their EV programs, we aim to leverage our position as a cost \\nleader. We are focused on rapidly growing production, investments in autonomy \\nand vehicle software, and remaining on track with our growth investments.\\nOur near -term pricing strategy considers a long -term view on per vehicle \\nprofitability given the potential lifetime value of a Tesla vehicle through autonomy, \\nsupercharging, connectivity and service. We expect that our product pricing will \\ncontinue to evolve, upwards or downwards, depending on a number of factors.\\nAlthough we implemented price reductions on many vehicle models across regions \\nin the first quarter, our operating margins reduced at a manageable rate. We \\nexpect ongoing cost reduction of our vehicles, including improved production \\nefficiency at our newest factories and lower logistics costs, and remain focused on \\noperating leverage as we scale.\\nWe are rapidly growing energy storage production capacity at our Megafactory in \\nLathrop and we recently announced a new Megafactory in Shanghai. We are also \\ncontinuing to execute on our product roadmap, including Cybertruck, our next \\ngeneration vehicle platform, autonomy and other AI enabled products. \\nOur balance sheet and net income enable us to continue to make these capital \\nexpenditures in line with our future growth. In this environment, we believe it \\nmakes sense to push forward to ensure we lay a proper foundation for the best \\npossible future.Cash Operating cash flow of $2.5B\\nFree cash flow2of $0.4B in Q1\\n$0.2B increase in our cash and investments3in Q1 to $22.4B\\nOperations Cybertruck factory tooling on track; producing Alpha versions\\nModel Y was the best -selling vehicle in Europe in Q1\\nModel Y was the best -selling vehicle in the US in Q1 (ex -pickups)\\n\\n01234O T H E R H I G H L I G H T S\\n9Services & Other gross margin\\nEnergy Storage deployments (GWh)Energy Storage\\nEnergy storage deployments increased by 360% YoY in Q1 to 3.9 GWh, the highest \\nlevel of deployments we have achieved due to ongoing Megafactory ramp. The ramp of our 40 GWh Megapack factory in Lathrop, California has been successful with still more room to reach full capacity. This Megapack factory will be the first of many. We recently announced our second 40 GWh Megafactory, this time in Shanghai, with construction starting later this year. \\nSolar\\nSolar deployments increased by 40% YoY in Q1 to 67 MW, but declined sequentially in \\nthe quarter, predominantly due to volatile weather and other factors. In addition, the solar industry has been impacted by supply chain challenges.\\nServices and Other\\nBoth revenue and gross profit from Services and Other reached an all -time high in Q1 \\n2023. Within this business division, growth of used vehicle sales remained strong YoY and had healthy margins. Supercharging, while still a relatively small part of the business, continued to grow as we gradually open up the network to non- Tesla \\nvehicles. \\n-4%-2%0%2%4%6%8%\\nQ3'21 Q4'21 Q1'22 Q2'22 Q3'22 Q4'22 Q1'23\\n\\nIn millions of USD or shares as applicable, except per share data Q1-2022 Q2-2022 Q3-2022 Q4-2022 Q1-2023\\nREVENUES\\nAutomotive sales 15,514 13,670 17,785 20,241 18,878 \\nAutomotive regulatory credits 679 344 286 467 521 \\nAutomotive leasing 668 588 621 599 564 \\nTotal automotive revenues 16,861 14,602 18,692 21,307 19,963 \\nEnergy generation and storage 616 866 1,117 1,310 1,529 \\nServices and other 1,279 1,466 1,645 1,701 1,837 \\nTotal revenues 18,756 16,934 21,454 24,318 23,329 \\nCOST OF REVENUES\\nAutomotive sales 10,914 10,153 13,099 15,433 15,422 \\nAutomotive leasing 408 368 381 352 333 \\nTotal automotive cost of revenues 11,322 10,521 13,480 15,785 15,755 \\nEnergy generation and storage 688 769 1,013 1,151 1,361 \\nServices and other 1,286 1,410 1,579 1,605 1,702 \\nTotal cost of revenues 13,296 12,700 16,072 18,541 18,818 \\nGross profit 5,460 4,234 5,382 5,777 4,511 \\nOPERATING EXPENSES\\nResearch and development 865 667 733 810 771 \\nSelling, general and administrative 992 961 961 1,032 1,076 \\nRestructuring and other — 142 — 34 —\\nTotal operating expenses 1,857 1,770 1,694 1,876 1,847 \\nINCOME FROM OPERATIONS 3,603 2,464 3,688 3,901 2,664 \\nInterest income 28 26 86 157 213 \\nInterest expense (61) (44) (53) (33) (29)\\nOther income (expense), net 56 28 (85) (42) (48)\\nINCOME BEFORE INCOME TAXES 3,626 2,474 3,636 3,983 2,800 \\nProvision for income taxes 346 205 305 276 261 \\nNET INCOME 3,280 2,269 3,331 3,707 2,539 \\nNet (loss) income attributable to noncontrolling interests and redeemable noncontrolling interests in \\nsubsidiaries(38) 10 39 20 26 \\nNET INCOME ATTRIBUTABLE TO COMMON STOCKHOLDERS 3,318 2,259 3,292 3,687 2,513 \\nNet income per share of common stock attributable to common stockholders(1)\\nBasic $ 1.07 $ 0.73 $ 1.05 $ 1.18 $ 0.80 \\nDiluted $ 0.95 $ 0.65 $ 0.95 $ 1.07 $ 0.73 \\nWeighted average shares used in computing net income per share of common stock(1)\\nBasic 3,103 3,111 3,146 3,160 3,166\\nDiluted 3,472 3,464 3,468 3,471 3,468\\nS T A T E M E N T O F O P E R A T I O N S\\n(Unaudited)\\n23 (1) Prior period results have been retroactively adjusted to reflect the three -for-one stock split effected in the form of a stock d ividend in August 2022.\\n\\nQ1-2022 Q2-2022 Q3-2022 Q4-2022 Q1-2023 YoY\\nModel S/X production 14,218 16,411 19,935 20,613 19,437 37%\\nModel 3/Y production 291,189 242,169 345,988 419,088 421,371 45%\\nTotal production 305,407 258,580 365,923 439,701 440,808 44%\\nModel S/X deliveries 14,724 16,162 18,672 17,147 10,695 -27%\\nModel 3/Y deliveries 295,324 238,533 325,158 388,131 412,180 40%\\nTotal deliveries 310,048 254,695 343,830 405,278 422,875 36%\\nof which subject to operating lease accounting 12,167 9,227 11,004 15,184 22,357 84%\\nTotal end of quarter operating lease vehicle count 128,402 131,756 135,054 140,667 153,988 20%\\nGlobal vehicle inventory (days of supply )(1)3 4 8 13 15 400%\\nSolar deployed (MW) 48 106 94 100 67 40%\\nStorage deployed (MWh) 846 1,133 2,100 2,462 3,889 360%\\nTesla locations(2)787 831 903 963 1,000 27%\\nMobile service fleet 1,372 1,453 1,532 1,584 1,692 23%\\nSupercharger stations 3,724 3,971 4,283 4,678 4,947 33%\\nSupercharger connectors 33,657 36,165 38,883 42,419 45,169 34%\\n(1)Days of supply is calculated by dividing new car ending inventory by the relevant quarter’s deliveries and using 75 trading days (aligned with Automotive News definition).\\n(2)Starting in Q1 -2023, we revised our methodology for reporting Tesla’s physical footprint. This count now includes all sales, del ivery, body shop and service locations globally. O P E R A T I O N A L S U M MA R Y\\n(Unaudited)\\n6\"\n", + "}\n", + "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain > 11:chain:LLMChain > 12:llm:ChatOpenAI] Entering LLM run with input:\n", + "\u001b[0m{\n", + " \"prompts\": [\n", + " \"System: Use the following pieces of context to answer the users question. \\nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\\n----------------\\nS U M M A R Y H I G H L I G H T S \\n(1) Excludes SBC (stock -based compensation).\\n(2) Free cash flow = operating cash flow less capex.\\n(3) Includes cash, cash equivalents and investments.Profitability 11.4% operating margin in Q1\\n$2.7B GAAP operating income in Q1\\n$2.5B GAAP net income in Q1\\n$2.9B non -GAAP net income1in Q1In the current macroeconomic environment, we see this year as a unique \\nopportunity for Tesla. As many carmakers are working through challenges with the \\nunit economics of their EV programs, we aim to leverage our position as a cost \\nleader. We are focused on rapidly growing production, investments in autonomy \\nand vehicle software, and remaining on track with our growth investments.\\nOur near -term pricing strategy considers a long -term view on per vehicle \\nprofitability given the potential lifetime value of a Tesla vehicle through autonomy, \\nsupercharging, connectivity and service. We expect that our product pricing will \\ncontinue to evolve, upwards or downwards, depending on a number of factors.\\nAlthough we implemented price reductions on many vehicle models across regions \\nin the first quarter, our operating margins reduced at a manageable rate. We \\nexpect ongoing cost reduction of our vehicles, including improved production \\nefficiency at our newest factories and lower logistics costs, and remain focused on \\noperating leverage as we scale.\\nWe are rapidly growing energy storage production capacity at our Megafactory in \\nLathrop and we recently announced a new Megafactory in Shanghai. We are also \\ncontinuing to execute on our product roadmap, including Cybertruck, our next \\ngeneration vehicle platform, autonomy and other AI enabled products. \\nOur balance sheet and net income enable us to continue to make these capital \\nexpenditures in line with our future growth. In this environment, we believe it \\nmakes sense to push forward to ensure we lay a proper foundation for the best \\npossible future.Cash Operating cash flow of $2.5B\\nFree cash flow2of $0.4B in Q1\\n$0.2B increase in our cash and investments3in Q1 to $22.4B\\nOperations Cybertruck factory tooling on track; producing Alpha versions\\nModel Y was the best -selling vehicle in Europe in Q1\\nModel Y was the best -selling vehicle in the US in Q1 (ex -pickups)\\n\\n01234O T H E R H I G H L I G H T S\\n9Services & Other gross margin\\nEnergy Storage deployments (GWh)Energy Storage\\nEnergy storage deployments increased by 360% YoY in Q1 to 3.9 GWh, the highest \\nlevel of deployments we have achieved due to ongoing Megafactory ramp. The ramp of our 40 GWh Megapack factory in Lathrop, California has been successful with still more room to reach full capacity. This Megapack factory will be the first of many. We recently announced our second 40 GWh Megafactory, this time in Shanghai, with construction starting later this year. \\nSolar\\nSolar deployments increased by 40% YoY in Q1 to 67 MW, but declined sequentially in \\nthe quarter, predominantly due to volatile weather and other factors. In addition, the solar industry has been impacted by supply chain challenges.\\nServices and Other\\nBoth revenue and gross profit from Services and Other reached an all -time high in Q1 \\n2023. Within this business division, growth of used vehicle sales remained strong YoY and had healthy margins. Supercharging, while still a relatively small part of the business, continued to grow as we gradually open up the network to non- Tesla \\nvehicles. \\n-4%-2%0%2%4%6%8%\\nQ3'21 Q4'21 Q1'22 Q2'22 Q3'22 Q4'22 Q1'23\\n\\nIn millions of USD or shares as applicable, except per share data Q1-2022 Q2-2022 Q3-2022 Q4-2022 Q1-2023\\nREVENUES\\nAutomotive sales 15,514 13,670 17,785 20,241 18,878 \\nAutomotive regulatory credits 679 344 286 467 521 \\nAutomotive leasing 668 588 621 599 564 \\nTotal automotive revenues 16,861 14,602 18,692 21,307 19,963 \\nEnergy generation and storage 616 866 1,117 1,310 1,529 \\nServices and other 1,279 1,466 1,645 1,701 1,837 \\nTotal revenues 18,756 16,934 21,454 24,318 23,329 \\nCOST OF REVENUES\\nAutomotive sales 10,914 10,153 13,099 15,433 15,422 \\nAutomotive leasing 408 368 381 352 333 \\nTotal automotive cost of revenues 11,322 10,521 13,480 15,785 15,755 \\nEnergy generation and storage 688 769 1,013 1,151 1,361 \\nServices and other 1,286 1,410 1,579 1,605 1,702 \\nTotal cost of revenues 13,296 12,700 16,072 18,541 18,818 \\nGross profit 5,460 4,234 5,382 5,777 4,511 \\nOPERATING EXPENSES\\nResearch and development 865 667 733 810 771 \\nSelling, general and administrative 992 961 961 1,032 1,076 \\nRestructuring and other — 142 — 34 —\\nTotal operating expenses 1,857 1,770 1,694 1,876 1,847 \\nINCOME FROM OPERATIONS 3,603 2,464 3,688 3,901 2,664 \\nInterest income 28 26 86 157 213 \\nInterest expense (61) (44) (53) (33) (29)\\nOther income (expense), net 56 28 (85) (42) (48)\\nINCOME BEFORE INCOME TAXES 3,626 2,474 3,636 3,983 2,800 \\nProvision for income taxes 346 205 305 276 261 \\nNET INCOME 3,280 2,269 3,331 3,707 2,539 \\nNet (loss) income attributable to noncontrolling interests and redeemable noncontrolling interests in \\nsubsidiaries(38) 10 39 20 26 \\nNET INCOME ATTRIBUTABLE TO COMMON STOCKHOLDERS 3,318 2,259 3,292 3,687 2,513 \\nNet income per share of common stock attributable to common stockholders(1)\\nBasic $ 1.07 $ 0.73 $ 1.05 $ 1.18 $ 0.80 \\nDiluted $ 0.95 $ 0.65 $ 0.95 $ 1.07 $ 0.73 \\nWeighted average shares used in computing net income per share of common stock(1)\\nBasic 3,103 3,111 3,146 3,160 3,166\\nDiluted 3,472 3,464 3,468 3,471 3,468\\nS T A T E M E N T O F O P E R A T I O N S\\n(Unaudited)\\n23 (1) Prior period results have been retroactively adjusted to reflect the three -for-one stock split effected in the form of a stock d ividend in August 2022.\\n\\nQ1-2022 Q2-2022 Q3-2022 Q4-2022 Q1-2023 YoY\\nModel S/X production 14,218 16,411 19,935 20,613 19,437 37%\\nModel 3/Y production 291,189 242,169 345,988 419,088 421,371 45%\\nTotal production 305,407 258,580 365,923 439,701 440,808 44%\\nModel S/X deliveries 14,724 16,162 18,672 17,147 10,695 -27%\\nModel 3/Y deliveries 295,324 238,533 325,158 388,131 412,180 40%\\nTotal deliveries 310,048 254,695 343,830 405,278 422,875 36%\\nof which subject to operating lease accounting 12,167 9,227 11,004 15,184 22,357 84%\\nTotal end of quarter operating lease vehicle count 128,402 131,756 135,054 140,667 153,988 20%\\nGlobal vehicle inventory (days of supply )(1)3 4 8 13 15 400%\\nSolar deployed (MW) 48 106 94 100 67 40%\\nStorage deployed (MWh) 846 1,133 2,100 2,462 3,889 360%\\nTesla locations(2)787 831 903 963 1,000 27%\\nMobile service fleet 1,372 1,453 1,532 1,584 1,692 23%\\nSupercharger stations 3,724 3,971 4,283 4,678 4,947 33%\\nSupercharger connectors 33,657 36,165 38,883 42,419 45,169 34%\\n(1)Days of supply is calculated by dividing new car ending inventory by the relevant quarter’s deliveries and using 75 trading days (aligned with Automotive News definition).\\n(2)Starting in Q1 -2023, we revised our methodology for reporting Tesla’s physical footprint. This count now includes all sales, del ivery, body shop and service locations globally. O P E R A T I O N A L S U M MA R Y\\n(Unaudited)\\n6\\nHuman: What was Tesla's revenue?\"\n", + " ]\n", + "}\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain > 11:chain:LLMChain > 12:llm:ChatOpenAI] [1.17s] Exiting LLM run with output:\n", + "\u001b[0m{\n", + " \"generations\": [\n", + " [\n", + " {\n", + " \"text\": \"Tesla's revenue for Q1-2023 was $23.329 billion.\",\n", + " \"generation_info\": null,\n", + " \"message\": {\n", + " \"content\": \"Tesla's revenue for Q1-2023 was $23.329 billion.\",\n", + " \"additional_kwargs\": {},\n", + " \"example\": false\n", + " }\n", + " }\n", + " ]\n", + " ],\n", + " \"llm_output\": {\n", + " \"token_usage\": {\n", + " \"prompt_tokens\": 2246,\n", + " \"completion_tokens\": 16,\n", + " \"total_tokens\": 2262\n", + " },\n", + " \"model_name\": \"gpt-3.5-turbo-0613\"\n", + " },\n", + " \"run\": null\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain > 11:chain:LLMChain] [1.17s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"text\": \"Tesla's revenue for Q1-2023 was $23.329 billion.\"\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain] [1.17s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"output_text\": \"Tesla's revenue for Q1-2023 was $23.329 billion.\"\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA] [1.61s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"result\": \"Tesla's revenue for Q1-2023 was $23.329 billion.\"\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[tool/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 8:tool:tesla-earnings] [1.61s] Exiting Tool run with output:\n", + "\u001b[0m\"{'query': \"What was Tesla's revenue?\", 'result': \"Tesla's revenue for Q1-2023 was $23.329 billion.\"}\"\n", + "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 13:llm:ChatOpenAI] Entering LLM run with input:\n", + "\u001b[0m{\n", + " \"prompts\": [\n", + " \"System: You are a helpful AI assistant.\\nHuman: did alphabet or tesla have more revenue?\\nAI: {'name': 'tool_selection', 'arguments': '{\\\\n \\\"actions\\\": [\\\\n {\\\\n \\\"action_name\\\": \\\"alphabet-earnings\\\",\\\\n \\\"action\\\": {\\\\n \\\"question\\\": \\\"What was Alphabet\\\\'s revenue?\\\"\\\\n }\\\\n },\\\\n {\\\\n \\\"action_name\\\": \\\"tesla-earnings\\\",\\\\n \\\"action\\\": {\\\\n \\\"question\\\": \\\"What was Tesla\\\\'s revenue?\\\"\\\\n }\\\\n }\\\\n ]\\\\n}'}\\nFunction: {\\\"query\\\": \\\"What was Alphabet's revenue?\\\", \\\"result\\\": \\\"Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million.\\\"}\\nAI: {'name': 'tool_selection', 'arguments': '{\\\\n \\\"actions\\\": [\\\\n {\\\\n \\\"action_name\\\": \\\"alphabet-earnings\\\",\\\\n \\\"action\\\": {\\\\n \\\"question\\\": \\\"What was Alphabet\\\\'s revenue?\\\"\\\\n }\\\\n },\\\\n {\\\\n \\\"action_name\\\": \\\"tesla-earnings\\\",\\\\n \\\"action\\\": {\\\\n \\\"question\\\": \\\"What was Tesla\\\\'s revenue?\\\"\\\\n }\\\\n }\\\\n ]\\\\n}'}\\nFunction: {\\\"query\\\": \\\"What was Tesla's revenue?\\\", \\\"result\\\": \\\"Tesla's revenue for Q1-2023 was $23.329 billion.\\\"}\"\n", + " ]\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor > 13:llm:ChatOpenAI] [1.69s] Exiting LLM run with output:\n", + "\u001b[0m{\n", + " \"generations\": [\n", + " [\n", + " {\n", + " \"text\": \"Alphabet had a revenue of $69,787 million, while Tesla had a revenue of $23.329 billion. Therefore, Alphabet had more revenue than Tesla.\",\n", + " \"generation_info\": null,\n", + " \"message\": {\n", + " \"content\": \"Alphabet had a revenue of $69,787 million, while Tesla had a revenue of $23.329 billion. Therefore, Alphabet had more revenue than Tesla.\",\n", + " \"additional_kwargs\": {},\n", + " \"example\": false\n", + " }\n", + " }\n", + " ]\n", + " ],\n", + " \"llm_output\": {\n", + " \"token_usage\": {\n", + " \"prompt_tokens\": 353,\n", + " \"completion_tokens\": 34,\n", + " \"total_tokens\": 387\n", + " },\n", + " \"model_name\": \"gpt-3.5-turbo-0613\"\n", + " },\n", + " \"run\": null\n", + "}\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:AgentExecutor] [7.83s] Exiting Chain run with output:\n", + "\u001b[0m{\n", + " \"output\": \"Alphabet had a revenue of $69,787 million, while Tesla had a revenue of $23.329 billion. Therefore, Alphabet had more revenue than Tesla.\"\n", + "}\n" + ] + }, + { + "data": { + "text/plain": [ + "{'input': 'did alphabet or tesla have more revenue?',\n", + " 'output': 'Alphabet had a revenue of $69,787 million, while Tesla had a revenue of $23.329 billion. Therefore, Alphabet had more revenue than Tesla.'}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "llm = ChatOpenAI(\n", + " temperature=0,\n", + " model=\"gpt-3.5-turbo-0613\", \n", + ")\n", + "\n", + "agent = initialize_agent(\n", + " agent=AgentType.OPENAI_MULTI_FUNCTIONS,\n", + " tools=tools,\n", + " llm=llm,\n", + " verbose=True,\n", + ")\n", + "\n", + "agent({\"input\": \"did alphabet or tesla have more revenue?\"})" + ] } ], "metadata": { diff --git a/langchain/agents/agent_types.py b/langchain/agents/agent_types.py index 6d9aff1f..db20f354 100644 --- a/langchain/agents/agent_types.py +++ b/langchain/agents/agent_types.py @@ -12,3 +12,4 @@ class AgentType(str, Enum): "structured-chat-zero-shot-react-description" ) OPENAI_FUNCTIONS = "openai-functions" + OPENAI_MULTI_FUNCTIONS = "openai-multi-functions" diff --git a/langchain/agents/loading.py b/langchain/agents/loading.py index 359909cd..5e3a8817 100644 --- a/langchain/agents/loading.py +++ b/langchain/agents/loading.py @@ -6,7 +6,7 @@ from typing import Any, List, Optional, Union import yaml -from langchain.agents.agent import BaseSingleActionAgent +from langchain.agents.agent import BaseMultiActionAgent, BaseSingleActionAgent from langchain.agents.tools import Tool from langchain.agents.types import AGENT_TO_CLASS from langchain.base_language import BaseLanguageModel @@ -20,7 +20,7 @@ URL_BASE = "https://raw.githubusercontent.com/hwchase17/langchain-hub/master/age def _load_agent_from_tools( config: dict, llm: BaseLanguageModel, tools: List[Tool], **kwargs: Any -) -> BaseSingleActionAgent: +) -> Union[BaseSingleActionAgent, BaseMultiActionAgent]: config_type = config.pop("_type") if config_type not in AGENT_TO_CLASS: raise ValueError(f"Loading {config_type} agent not supported") @@ -35,7 +35,7 @@ def load_agent_from_config( llm: Optional[BaseLanguageModel] = None, tools: Optional[List[Tool]] = None, **kwargs: Any, -) -> BaseSingleActionAgent: +) -> Union[BaseSingleActionAgent, BaseMultiActionAgent]: """Load agent from Config Dict.""" if "_type" not in config: raise ValueError("Must specify an agent Type in config") @@ -75,7 +75,9 @@ def load_agent_from_config( return agent_cls(**combined_config) # type: ignore -def load_agent(path: Union[str, Path], **kwargs: Any) -> BaseSingleActionAgent: +def load_agent( + path: Union[str, Path], **kwargs: Any +) -> Union[BaseSingleActionAgent, BaseMultiActionAgent]: """Unified method for loading a agent from LangChainHub or local fs.""" if hub_result := try_load_from_hub( path, _load_agent_from_file, "agents", {"json", "yaml"} @@ -87,7 +89,7 @@ def load_agent(path: Union[str, Path], **kwargs: Any) -> BaseSingleActionAgent: def _load_agent_from_file( file: Union[str, Path], **kwargs: Any -) -> BaseSingleActionAgent: +) -> Union[BaseSingleActionAgent, BaseMultiActionAgent]: """Load agent from file.""" # Convert file to Path object. if isinstance(file, str): diff --git a/langchain/agents/openai_functions_multi_agent/__init__.py b/langchain/agents/openai_functions_multi_agent/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/langchain/agents/openai_functions_multi_agent/base.py b/langchain/agents/openai_functions_multi_agent/base.py new file mode 100644 index 00000000..8c29d086 --- /dev/null +++ b/langchain/agents/openai_functions_multi_agent/base.py @@ -0,0 +1,362 @@ +"""Module implements an agent that uses OpenAI's APIs function enabled API.""" +import json +from dataclasses import dataclass +from json import JSONDecodeError +from typing import Any, List, Optional, Sequence, Tuple, Union + +from pydantic import root_validator + +from langchain.agents import BaseMultiActionAgent +from langchain.base_language import BaseLanguageModel +from langchain.callbacks.base import BaseCallbackManager +from langchain.callbacks.manager import Callbacks +from langchain.chat_models.openai import ChatOpenAI +from langchain.prompts.base import BasePromptTemplate +from langchain.prompts.chat import ( + BaseMessagePromptTemplate, + ChatPromptTemplate, + HumanMessagePromptTemplate, + MessagesPlaceholder, +) +from langchain.schema import ( + AgentAction, + AgentFinish, + AIMessage, + BaseMessage, + FunctionMessage, + OutputParserException, + SystemMessage, +) +from langchain.tools import BaseTool + + +@dataclass +class _FunctionsAgentAction(AgentAction): + message_log: List[BaseMessage] + + +def _convert_agent_action_to_messages( + agent_action: AgentAction, observation: str +) -> List[BaseMessage]: + """Convert an agent action to a message. + + This code is used to reconstruct the original AI message from the agent action. + + Args: + agent_action: Agent action to convert. + + Returns: + AIMessage that corresponds to the original tool invocation. + """ + if isinstance(agent_action, _FunctionsAgentAction): + return agent_action.message_log + [ + _create_function_message(agent_action, observation) + ] + else: + return [AIMessage(content=agent_action.log)] + + +def _create_function_message( + agent_action: AgentAction, observation: str +) -> FunctionMessage: + """Convert agent action and observation into a function message. + Args: + agent_action: the tool invocation request from the agent + observation: the result of the tool invocation + Returns: + FunctionMessage that corresponds to the original tool invocation + """ + if not isinstance(observation, str): + try: + content = json.dumps(observation) + except Exception: + content = str(observation) + else: + content = observation + return FunctionMessage( + name=agent_action.tool, + content=content, + ) + + +def _format_intermediate_steps( + intermediate_steps: List[Tuple[AgentAction, str]], +) -> List[BaseMessage]: + """Format intermediate steps. + Args: + intermediate_steps: Steps the LLM has taken to date, along with observations + Returns: + list of messages to send to the LLM for the next prediction + """ + messages = [] + + for intermediate_step in intermediate_steps: + agent_action, observation = intermediate_step + messages.extend(_convert_agent_action_to_messages(agent_action, observation)) + + return messages + + +def _parse_ai_message(message: BaseMessage) -> Union[List[AgentAction], AgentFinish]: + """Parse an AI message.""" + if not isinstance(message, AIMessage): + raise TypeError(f"Expected an AI message got {type(message)}") + + function_call = message.additional_kwargs.get("function_call", {}) + + if function_call: + function_call = message.additional_kwargs["function_call"] + try: + tools = json.loads(function_call["arguments"])["actions"] + except JSONDecodeError: + raise OutputParserException( + f"Could not parse tool input: {function_call} because " + f"the `arguments` is not valid JSON." + ) + final_tools: List[AgentAction] = [] + for tool_schema in tools: + _tool_input = tool_schema["action"] + function_name = tool_schema["action_name"] + + # HACK HACK HACK: + # The code that encodes tool input into Open AI uses a special variable + # name called `__arg1` to handle old style tools that do not expose a + # schema and expect a single string argument as an input. + # We unpack the argument here if it exists. + # Open AI does not support passing in a JSON array as an argument. + if "__arg1" in _tool_input: + tool_input = _tool_input["__arg1"] + else: + tool_input = _tool_input + + content_msg = "responded: {content}\n" if message.content else "\n" + log = f"\nInvoking: `{function_name}` with `{tool_input}`\n{content_msg}\n" + _tool = _FunctionsAgentAction( + tool=function_name, + tool_input=tool_input, + log=log, + message_log=[message], + ) + final_tools.append(_tool) + return final_tools + + return AgentFinish(return_values={"output": message.content}, log=message.content) + + +class OpenAIMultiFunctionsAgent(BaseMultiActionAgent): + """An Agent driven by OpenAIs function powered API. + + Args: + llm: This should be an instance of ChatOpenAI, specifically a model + that supports using `functions`. + tools: The tools this agent has access to. + prompt: The prompt for this agent, should support agent_scratchpad as one + of the variables. For an easy way to construct this prompt, use + `OpenAIFunctionsAgent.create_prompt(...)` + """ + + llm: BaseLanguageModel + tools: Sequence[BaseTool] + prompt: BasePromptTemplate + + def get_allowed_tools(self) -> List[str]: + """Get allowed tools.""" + return [t.name for t in self.tools] + + @root_validator + def validate_llm(cls, values: dict) -> dict: + if not isinstance(values["llm"], ChatOpenAI): + raise ValueError("Only supported with ChatOpenAI models.") + return values + + @root_validator + def validate_prompt(cls, values: dict) -> dict: + prompt: BasePromptTemplate = values["prompt"] + if "agent_scratchpad" not in prompt.input_variables: + raise ValueError( + "`agent_scratchpad` should be one of the variables in the prompt, " + f"got {prompt.input_variables}" + ) + return values + + @property + def input_keys(self) -> List[str]: + """Get input keys. Input refers to user input here.""" + return ["input"] + + @property + def functions(self) -> List[dict]: + enum_vals = [t.name for t in self.tools] + tool_selection = { + # OpenAI functions returns a single tool invocation + # Here we force the single tool invocation it returns to + # itself be a list of tool invocations. We do this by constructing + # a new tool that has one argument which is a list of tools + # to use. + "name": "tool_selection", + "description": "A list of actions to take.", + "parameters": { + "title": "tool_selection", + "description": "A list of actions to take.", + "type": "object", + "properties": { + "actions": { + "title": "actions", + "type": "array", + "items": { + # This is a custom item which bundles the action_name + # and the action. We do this because some actions + # could have the same schema, and without this there + # is no way to differentiate them. + "title": "tool_call", + "type": "object", + "properties": { + # This is the name of the action to take + "action_name": { + "title": "action_name", + "enum": enum_vals, + "type": "string", + "description": ( + "Name of the action to take. The name " + "provided here should match up with the " + "parameters for the action below." + ), + }, + # This is the action to take. + "action": { + "title": "Action", + "anyOf": [ + { + "title": t.name, + "type": "object", + "properties": t.args, + } + for t in self.tools + ], + }, + }, + "required": ["action_name", "action"], + }, + } + }, + "required": ["actions"], + }, + } + return [tool_selection] + + def plan( + self, + intermediate_steps: List[Tuple[AgentAction, str]], + callbacks: Callbacks = None, + **kwargs: Any, + ) -> Union[List[AgentAction], AgentFinish]: + """Given input, decided what to do. + + Args: + intermediate_steps: Steps the LLM has taken to date, along with observations + **kwargs: User inputs. + + Returns: + Action specifying what tool to use. + """ + agent_scratchpad = _format_intermediate_steps(intermediate_steps) + selected_inputs = { + k: kwargs[k] for k in self.prompt.input_variables if k != "agent_scratchpad" + } + full_inputs = dict(**selected_inputs, agent_scratchpad=agent_scratchpad) + prompt = self.prompt.format_prompt(**full_inputs) + messages = prompt.to_messages() + predicted_message = self.llm.predict_messages( + messages, functions=self.functions, callbacks=callbacks + ) + agent_decision = _parse_ai_message(predicted_message) + return agent_decision + + async def aplan( + self, + intermediate_steps: List[Tuple[AgentAction, str]], + callbacks: Callbacks = None, + **kwargs: Any, + ) -> Union[List[AgentAction], AgentFinish]: + """Given input, decided what to do. + + Args: + intermediate_steps: Steps the LLM has taken to date, + along with observations + **kwargs: User inputs. + + Returns: + Action specifying what tool to use. + """ + agent_scratchpad = _format_intermediate_steps(intermediate_steps) + selected_inputs = { + k: kwargs[k] for k in self.prompt.input_variables if k != "agent_scratchpad" + } + full_inputs = dict(**selected_inputs, agent_scratchpad=agent_scratchpad) + prompt = self.prompt.format_prompt(**full_inputs) + messages = prompt.to_messages() + predicted_message = await self.llm.apredict_messages( + messages, functions=self.functions + ) + agent_decision = _parse_ai_message(predicted_message) + return agent_decision + + @classmethod + def create_prompt( + cls, + system_message: Optional[SystemMessage] = SystemMessage( + content="You are a helpful AI assistant." + ), + extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None, + ) -> BasePromptTemplate: + """Create prompt for this agent. + + Args: + system_message: Message to use as the system message that will be the + first in the prompt. + extra_prompt_messages: Prompt messages that will be placed between the + system message and the new human input. + + Returns: + A prompt template to pass into this agent. + """ + _prompts = extra_prompt_messages or [] + messages: List[Union[BaseMessagePromptTemplate, BaseMessage]] + if system_message: + messages = [system_message] + else: + messages = [] + + messages.extend( + [ + *_prompts, + HumanMessagePromptTemplate.from_template("{input}"), + MessagesPlaceholder(variable_name="agent_scratchpad"), + ] + ) + return ChatPromptTemplate(messages=messages) + + @classmethod + def from_llm_and_tools( + cls, + llm: BaseLanguageModel, + tools: Sequence[BaseTool], + callback_manager: Optional[BaseCallbackManager] = None, + extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]] = None, + system_message: Optional[SystemMessage] = SystemMessage( + content="You are a helpful AI assistant." + ), + **kwargs: Any, + ) -> BaseMultiActionAgent: + """Construct an agent from an LLM and tools.""" + prompt = cls.create_prompt( + extra_prompt_messages=extra_prompt_messages, + system_message=system_message, + ) + return cls( + llm=llm, + prompt=prompt, + tools=tools, + callback_manager=callback_manager, + **kwargs, + ) diff --git a/langchain/agents/types.py b/langchain/agents/types.py index b9e73c5f..8d342ea9 100644 --- a/langchain/agents/types.py +++ b/langchain/agents/types.py @@ -1,4 +1,4 @@ -from typing import Dict, Type +from typing import Dict, Type, Union from langchain.agents.agent import BaseSingleActionAgent from langchain.agents.agent_types import AgentType @@ -7,11 +7,14 @@ from langchain.agents.conversational.base import ConversationalAgent from langchain.agents.conversational_chat.base import ConversationalChatAgent from langchain.agents.mrkl.base import ZeroShotAgent from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent +from langchain.agents.openai_functions_multi_agent.base import OpenAIMultiFunctionsAgent from langchain.agents.react.base import ReActDocstoreAgent from langchain.agents.self_ask_with_search.base import SelfAskWithSearchAgent from langchain.agents.structured_chat.base import StructuredChatAgent -AGENT_TO_CLASS: Dict[AgentType, Type[BaseSingleActionAgent]] = { +AGENT_TYPE = Union[Type[BaseSingleActionAgent], Type[OpenAIMultiFunctionsAgent]] + +AGENT_TO_CLASS: Dict[AgentType, AGENT_TYPE] = { AgentType.ZERO_SHOT_REACT_DESCRIPTION: ZeroShotAgent, AgentType.REACT_DOCSTORE: ReActDocstoreAgent, AgentType.SELF_ASK_WITH_SEARCH: SelfAskWithSearchAgent, @@ -20,4 +23,5 @@ AGENT_TO_CLASS: Dict[AgentType, Type[BaseSingleActionAgent]] = { AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION: ConversationalChatAgent, AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION: StructuredChatAgent, AgentType.OPENAI_FUNCTIONS: OpenAIFunctionsAgent, + AgentType.OPENAI_MULTI_FUNCTIONS: OpenAIMultiFunctionsAgent, }