Skip to main content

Document Comparison

This notebook shows how to use an agent to compare two documents.

The high level idea is we will create a question-answering chain for each document, and then use that

from langchain.chains import RetrievalQA
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import FAISS
from langchain_core.tools import Tool
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
from pydantic import BaseModel, Field
class DocumentInput(BaseModel):
question: str = Field()


llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")

tools = []
files = [
# https://abc.xyz/investor/static/pdf/2023Q1_alphabet_earnings_release.pdf
{
"name": "alphabet-earnings",
"path": "/Users/harrisonchase/Downloads/2023Q1_alphabet_earnings_release.pdf",
},
# https://digitalassets.tesla.com/tesla-contents/image/upload/IR/TSLA-Q1-2023-Update
{
"name": "tesla-earnings",
"path": "/Users/harrisonchase/Downloads/TSLA-Q1-2023-Update.pdf",
},
]

for file in files:
loader = PyPDFLoader(file["path"])
pages = loader.load_and_split()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(pages)
embeddings = OpenAIEmbeddings()
retriever = FAISS.from_documents(docs, embeddings).as_retriever()

# Wrap retrievers in a Tool
tools.append(
Tool(
args_schema=DocumentInput,
name=file["name"],
description=f"useful when you want to answer questions about {file['name']}",
func=RetrievalQA.from_chain_type(llm=llm, retriever=retriever),
)
)
from langchain.agents import AgentType, initialize_agent
llm = ChatOpenAI(
temperature=0,
model="gpt-3.5-turbo-0613",
)

agent = initialize_agent(
agent=AgentType.OPENAI_FUNCTIONS,
tools=tools,
llm=llm,
verbose=True,
)

agent({"input": "did alphabet or tesla have more revenue?"})


> Entering new chain...

Invoking: `alphabet-earnings` with `{'question': 'revenue'}`


{'query': 'revenue', 'result': 'The revenue for Alphabet Inc. for the quarter ended March 31, 2023, was $69,787 million.'}
Invoking: `tesla-earnings` with `{'question': 'revenue'}`


{'query': 'revenue', 'result': 'Total revenue for Q1-2023 was $23.3 billion.'}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.

> Finished chain.
{'input': 'did alphabet or tesla have more revenue?',
'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."}

OpenAI Multi Functions​

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

from langchain.globals import set_debug

set_debug(True)

API Reference:

llm = ChatOpenAI(
temperature=0,
model="gpt-3.5-turbo-0613",
)

agent = initialize_agent(
agent=AgentType.OPENAI_MULTI_FUNCTIONS,
tools=tools,
llm=llm,
verbose=True,
)

agent({"input": "did alphabet or tesla have more revenue?"})
[chain/start] [1:chain:AgentExecutor] Entering Chain run with input:
{
"input": "did alphabet or tesla have more revenue?"
}
[llm/start] [1:chain:AgentExecutor > 2:llm:ChatOpenAI] Entering LLM run with input:
{
"prompts": [
"System: You are a helpful AI assistant.\nHuman: did alphabet or tesla have more revenue?"
]
}
[llm/end] [1:chain:AgentExecutor > 2:llm:ChatOpenAI] [2.66s] Exiting LLM run with output:
{
"generations": [
[
{
"text": "",
"generation_info": null,
"message": {
"content": "",
"additional_kwargs": {
"function_call": {
"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}"
}
},
"example": false
}
}
]
],
"llm_output": {
"token_usage": {
"prompt_tokens": 99,
"completion_tokens": 82,
"total_tokens": 181
},
"model_name": "gpt-3.5-turbo-0613"
},
"run": null
}
[tool/start] [1:chain:AgentExecutor > 3:tool:alphabet-earnings] Entering Tool run with input:
"{'question': "What was Alphabet's revenue?"}"
[chain/start] [1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA] Entering Chain run with input:
{
"query": "What was Alphabet's revenue?"
}
[chain/start] [1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain] Entering Chain run with input:
[inputs]
[chain/start] [1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain > 6:chain:LLMChain] Entering Chain run with input:
{
"question": "What was Alphabet's revenue?",
"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"
}
[llm/start] [1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain > 6:chain:LLMChain > 7:llm:ChatOpenAI] Entering LLM run with input:
{
"prompts": [
"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?"
]
}
[llm/end] [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:
{
"generations": [
[
{
"text": "Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million.",
"generation_info": null,
"message": {
"content": "Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million.",
"additional_kwargs": {},
"example": false
}
}
]
],
"llm_output": {
"token_usage": {
"prompt_tokens": 2335,
"completion_tokens": 23,
"total_tokens": 2358
},
"model_name": "gpt-3.5-turbo-0613"
},
"run": null
}
[chain/end] [1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain > 6:chain:LLMChain] [1.61s] Exiting Chain run with output:
{
"text": "Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million."
}
[chain/end] [1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA > 5:chain:StuffDocumentsChain] [1.61s] Exiting Chain run with output:
{
"output_text": "Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million."
}
[chain/end] [1:chain:AgentExecutor > 3:tool:alphabet-earnings > 4:chain:RetrievalQA] [1.85s] Exiting Chain run with output:
{
"result": "Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million."
}
[tool/end] [1:chain:AgentExecutor > 3:tool:alphabet-earnings] [1.86s] Exiting Tool run with output:
"{'query': "What was Alphabet's revenue?", 'result': "Alphabet's revenue for the quarter ended March 31, 2023, was $69,787 million."}"
[tool/start] [1:chain:AgentExecutor > 8:tool:tesla-earnings] Entering Tool run with input:
"{'question': "What was Tesla's revenue?"}"
[chain/start] [1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA] Entering Chain run with input:
{
"query": "What was Tesla's revenue?"
}
[chain/start] [1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain] Entering Chain run with input:
[inputs]
[chain/start] [1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain > 11:chain:LLMChain] Entering Chain run with input:
{
"question": "What was Tesla's revenue?",
"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"
}
[llm/start] [1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain > 11:chain:LLMChain > 12:llm:ChatOpenAI] Entering LLM run with input:
{
"prompts": [
"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?"
]
}
[llm/end] [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:
{
"generations": [
[
{
"text": "Tesla's revenue for Q1-2023 was $23.329 billion.",
"generation_info": null,
"message": {
"content": "Tesla's revenue for Q1-2023 was $23.329 billion.",
"additional_kwargs": {},
"example": false
}
}
]
],
"llm_output": {
"token_usage": {
"prompt_tokens": 2246,
"completion_tokens": 16,
"total_tokens": 2262
},
"model_name": "gpt-3.5-turbo-0613"
},
"run": null
}
[chain/end] [1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain > 11:chain:LLMChain] [1.17s] Exiting Chain run with output:
{
"text": "Tesla's revenue for Q1-2023 was $23.329 billion."
}
[chain/end] [1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA > 10:chain:StuffDocumentsChain] [1.17s] Exiting Chain run with output:
{
"output_text": "Tesla's revenue for Q1-2023 was $23.329 billion."
}
[chain/end] [1:chain:AgentExecutor > 8:tool:tesla-earnings > 9:chain:RetrievalQA] [1.61s] Exiting Chain run with output:
{
"result": "Tesla's revenue for Q1-2023 was $23.329 billion."
}
[tool/end] [1:chain:AgentExecutor > 8:tool:tesla-earnings] [1.61s] Exiting Tool run with output:
"{'query': "What was Tesla's revenue?", 'result': "Tesla's revenue for Q1-2023 was $23.329 billion."}"
[llm/start] [1:chain:AgentExecutor > 13:llm:ChatOpenAI] Entering LLM run with input:
{
"prompts": [
"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.\"}"
]
}
[llm/end] [1:chain:AgentExecutor > 13:llm:ChatOpenAI] [1.69s] Exiting LLM run with output:
{
"generations": [
[
{
"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.",
"generation_info": null,
"message": {
"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.",
"additional_kwargs": {},
"example": false
}
}
]
],
"llm_output": {
"token_usage": {
"prompt_tokens": 353,
"completion_tokens": 34,
"total_tokens": 387
},
"model_name": "gpt-3.5-turbo-0613"
},
"run": null
}
[chain/end] [1:chain:AgentExecutor] [7.83s] Exiting Chain run with output:
{
"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."
}
{'input': 'did alphabet or tesla have more revenue?',
'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.'}

Was this page helpful?


You can leave detailed feedback on GitHub.