0% found this document useful (0 votes)
59 views11 pages

LLMs in Financial Data

The document discusses using LLMs to summarize complex financial documents like earnings calls and forecasts. It provides a code sample to summarize a financial report text using OpenAI's GPT-3.5 model. The code takes in a report, chunks it if too long, then calls the API to generate a 3 paragraph summary of the key points from the report. It prints the summary generated from a sample text discussing the 2024 macroeconomic outlook.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views11 pages

LLMs in Financial Data

The document discusses using LLMs to summarize complex financial documents like earnings calls and forecasts. It provides a code sample to summarize a financial report text using OpenAI's GPT-3.5 model. The code takes in a report, chunks it if too long, then calls the API to generate a 3 paragraph summary of the key points from the report. It prints the summary generated from a sample text discussing the 2024 macroeconomic outlook.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

LLMs_in_financial_data

December 22, 2023

[5]: !pip install openai


!pip install tiktoken

Collecting openai
Downloading openai-1.6.0-py3-none-any.whl (225 kB)
���������������������������������������� 225.4/225.4
kB 3.1 MB/s eta 0:00:00
Requirement already satisfied: anyio<5,>=3.5.0 in
/usr/local/lib/python3.10/dist-packages (from openai) (3.7.1)
Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-
packages (from openai) (1.7.0)
Collecting httpx<1,>=0.23.0 (from openai)
Downloading httpx-0.26.0-py3-none-any.whl (75 kB)
���������������������������������������� 75.9/75.9 kB
8.8 MB/s eta 0:00:00
Requirement already satisfied: pydantic<3,>=1.9.0 in
/usr/local/lib/python3.10/dist-packages (from openai) (1.10.13)
Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-
packages (from openai) (1.3.0)
Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages
(from openai) (4.66.1)
Collecting typing-extensions<5,>=4.7 (from openai)
Downloading typing_extensions-4.9.0-py3-none-any.whl (32 kB)
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-
packages (from anyio<5,>=3.5.0->openai) (3.6)
Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-
packages (from anyio<5,>=3.5.0->openai) (1.2.0)
Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-
packages (from httpx<1,>=0.23.0->openai) (2023.11.17)
Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai)
Downloading httpcore-1.0.2-py3-none-any.whl (76 kB)
���������������������������������������� 76.9/76.9 kB
8.3 MB/s eta 0:00:00
Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx<1,>=0.23.0->openai)
Downloading h11-0.14.0-py3-none-any.whl (58 kB)
���������������������������������������� 58.3/58.3 kB
6.2 MB/s eta 0:00:00

1
Installing collected packages: typing-extensions, h11, httpcore, httpx,
openai
Attempting uninstall: typing-extensions
Found existing installation: typing_extensions 4.5.0
Uninstalling typing_extensions-4.5.0:
Successfully uninstalled typing_extensions-4.5.0
ERROR: pip's dependency resolver does not currently take into account all
the packages that are installed. This behaviour is the source of the following
dependency conflicts.
llmx 0.0.15a0 requires cohere, which is not installed.
llmx 0.0.15a0 requires tiktoken, which is not installed.
tensorflow-probability 0.22.0 requires typing-extensions<4.6.0, but you have
typing-extensions 4.9.0 which is incompatible.
Successfully installed h11-0.14.0 httpcore-1.0.2 httpx-0.26.0 openai-1.6.0
typing-extensions-4.9.0
Collecting tiktoken
Downloading
tiktoken-0.5.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0
MB)
���������������������������������������� 2.0/2.0 MB
18.2 MB/s eta 0:00:00
Requirement already satisfied: regex>=2022.1.18 in
/usr/local/lib/python3.10/dist-packages (from tiktoken) (2023.6.3)
Requirement already satisfied: requests>=2.26.0 in
/usr/local/lib/python3.10/dist-packages (from tiktoken) (2.31.0)
Requirement already satisfied: charset-normalizer<4,>=2 in
/usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken)
(3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-
packages (from requests>=2.26.0->tiktoken) (3.6)
Requirement already satisfied: urllib3<3,>=1.21.1 in
/usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken)
(2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in
/usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken)
(2023.11.17)
Installing collected packages: tiktoken
ERROR: pip's dependency resolver does not currently take into account all
the packages that are installed. This behaviour is the source of the following
dependency conflicts.
llmx 0.0.15a0 requires cohere, which is not installed.
Successfully installed tiktoken-0.5.2

2
[6]: from google.colab import userdata
openai_api_key = userdata.get('OPENAI_API_KEY')

[7]: from openai import OpenAI


client = OpenAI(api_key=openai_api_key)

1 Financial Report Summarization with LLMs


Financial reports, earnings calls, and forecasts are crucial. However, these documents are often
lengthy and time-consuming to digest. With Language Model Models (LLMs) - advanced AI
models, you can summarize complex financial content in the blink of an eye.
Here is a simple code to help you perform that. If the report is too long for the window context,
you need to chunk it. For that, you can use LangChain library (among others) that will do that
for you.
[32]: # path='LOCAL'
text=path+"Outlook_MorganStanley.txt"

with open(text, 'r') as file:


file_content = file.read()

# Chunk the text here with LangChain for example

#Call gpt-3.5-turbo
def summarize_report(report_text):

prompt='Summarize the following financial report: """\n' + report_text +␣


'\n"""',

response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": file_content}
]
)

return response.choices[0].message

# # Sample financial report text (replace with your own)


# financial_report = """
# [Insert the chunk from you file_content here, of the file_content if it's not␣
↪too long]

# """

summary = summarize_report(file_content)

3
print("Summary:\n" + summary.content)

Summary:
Based on the discussion provided in the transcript, here is a summary of the
outlook for the macro economy in 2024:

1. Government Bond Yields:


- The outlook for government bond yields in 2024 is uncertain and brings
confusion.
- Central banks have been implementing accommodative monetary policies, but they
are expected to reduce rates alongside balance sheet declines.
- G10 and emerging market central banks have raised rates significantly in the
past couple of years.
- Equilibrium rates may have trended lower, resulting in more restricted central
bank policy rates than in previous decades.
- G4 central bank balance sheets (Fed, ECB, Bank of England, Bank of Japan) will
remain larger than pre-pandemic levels by the end of 2024 and 2025.
- Europe, especially Germany, Austria, and Portugal, is seen as offering
opportunities in government bond markets.

2. Currencies:
- The US dollar is expected to continue strengthening in 2024 as the US
outperforms other regions in terms of growth and rate differentials.
- The dollar is seen as a defensive currency that offers liquidity and safe-
haven status, as well as high yields.
- However, the dollar's strength is expected to fade as fiscal support wanes, US
growth slows down, and the Federal Reserve cuts rates.
- Once the Fed starts cutting rates, it is expected to continue easing,
potentially pushing the dollar down.

3. Credit Markets:
- The credit markets are expected to perform well in 2024, supported by
favorable fundamentals, technicals, and average valuations.
- Higher-quality fixed income, especially investment-grade corporate credit, is
seen as attractive compared to other assets.
- The earnings yield on S&P 500 stocks relative to U.S. triple B-rated corporate
bonds has flipped, making bonds relatively more appealing.
- A potential undersupply of corporate bonds could increase demand, especially
if the Federal Reserve begins cutting rates.
- Credit curves, such as the 3 to 5-year part of the U.S. credit curve and the 5
to 10-year part of the investment-grade curve in Europe, are seen as offering
opportunities.

4. Housing Market:
- The US housing market held up remarkably well in 2023 despite higher mortgage
rates.
- Affordability is expected to improve, and for-sale inventory is projected to

4
increase, supporting housing activity.
- Home prices are forecasted to see modest declines as the growth in inventory
offsets increased demand.
- Homeowners are expected to retain strong hands in the housing cycle, reducing
the likelihood of significant price corrections.
- Issuance in the agency mortgage-backed securities (MBS) market is expected to
slightly increase in 2024.
- The Federal Reserve is anticipated to reduce its mortgage portfolio throughout
the year, which will need to be absorbed by the private market.

Please note that these insights are based on the discussion provided and may not
capture the complete outlook for the macro economy in 2024. It is always
advisable to refer to additional sources and market research for a comprehensive
understanding.

[9]: ### EXAMPLE OF PDF EXTRACTION : (Need to be worked further ... )


# !pip install pypdf

# from pypdf import PdfReader

# reader = PdfReader(pdf) # In pypdf directly you can't use the pdf url␣
↪directly, you need to load it locally beore using it or Use LangChain pdf␣

↪loader

# pages = reader.pages

# file_content=""
# for page in pages:
# file_content += page.extract_text()

2 Sentiment Analysis
In the dynamic world of finance, staying ahead of market sentiment is crucial for making informed
investment decisions. Whether it’s monitoring news articles, tweets, or social media chatter, gauging
sentiment provides valuable insights.
Market sentiment, the overall attitude of investors and traders toward the market, can significantly
impact stock prices and market trends. Positive sentiment often leads to bullish markets, while
negative sentiment can trigger bearish movements. Keeping tabs on this sentiment can give you a
competitive edge in the financial landscape.

Main topics:
• Predictive Insights: Sentiment analysis can help predict market movements and identify po-
tential trends before they materialize.
• Risk Management: Understanding sentiment can aid in managing risks associated with in-
vestments and portfolios.

5
• Real-Time Monitoring: With ChatGPT, you can perform real-time sentiment analysis on
news articles, social media posts, and financial reports to make timely decisions.

2.1 Fetch data from the web using : yfinance, request and BeautifulSoup
You can use yahoo finance to fetch news and then pass them through an LLMs to analyse sentiment:
[19]: import yfinance as yf

# Create a Yahoo Finance ticker symbol for the stock or topic you're interested␣
↪in

ticker_symbol = 'AAPL' # Replace with your desired stock symbol

# Create a Yahoo Finance Ticker object


ticker = yf.Ticker(ticker_symbol)

# Get the latest news headlines related to the specified stock or topic
news_data = ticker.news

# Display the headlines and URLs


for idx, news_item in enumerate(news_data, start=1):
title = news_item['title']
link = news_item['link']
print(f"Headline {idx}: {title}")
print(f"URL: {link}\n")

Headline 1: Dow Jones Futures Fall As Nike Dives; Apple Leads Six Stocks To
Watch With Inflation Data Due
URL: https://finance.yahoo.com/m/5c596659-27b6-309c-a7bf-88719cb53abf/dow-jones-
futures-fall-as.html

Headline 2: 2024 Technology Stock & ETF Outlook


URL: https://finance.yahoo.com/news/2024-technology-stock-etf-
outlook-220600263.html

Headline 3: Apple Watch woes 'a temporary blip': Analyst


URL: https://finance.yahoo.com/video/apple-watch-woes-temporary-
blip-214215330.html

Headline 4: Mag 7 Stock In Buy Range; Makes Historic AI Labor Deal


URL:
https://finance.yahoo.com/m/e118c64a-84d9-363a-b225-c1c7e1edb791/mag-7-stock-in-
buy-range%3B.html

Headline 5: Magnificent Seven Stocks To Buy And Watch: Alphabet, Nvidia, Tesla
Stock Rally
URL:
https://finance.yahoo.com/m/4205eaa9-f620-3a0b-a81a-0e82c7c9fd0b/magnificent-

6
seven-stocks-to.html

Headline 6: UPDATE 1-Apple cannot repair older out-of-warranty watches during


ban - Bloomberg News
URL: https://finance.yahoo.com/news/1-apple-cannot-repair-older-204128889.html

Headline 7: Apple cannot repair older out-of-warranty watches during ban -


Bloomberg News
URL: https://finance.yahoo.com/news/apple-cant-repair-older-
warranty-202712896.html

Headline 8: Apple can't repair older out-of-warranty watches during ban -


Bloomberg News
URL: https://finance.yahoo.com/news/apple-cant-repair-older-
warranty-202343513.html

I’ll take this news about Apple to ask for sentiment analysis classification from LLms:
[29]: title = news_data[5]['title']
link = news_data[5]['link']
print(f"Headline : {title}")
print(f"URL: {link}\n")

Headline : UPDATE 1-Apple cannot repair older out-of-warranty watches during ban
- Bloomberg News
URL: https://finance.yahoo.com/news/1-apple-cannot-repair-older-204128889.html

[30]: import requests


from bs4 import BeautifulSoup

# link = 'https://finance.yahoo.com/news/apple-aapl-stock-moves-1-224518814.
↪html'

# link = 'https://finance.yahoo.com/news/1-apple-cannot-repair-older-204128889.
↪html'

response = requests.get(link)

if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
article_content = soup.find('div', {'class': 'caas-body'})
article_text = article_content.get_text()
print(article_text)

else:
print('Failed to fetch the article. Check the URL or your internet␣
↪connection.')

7
(Adds status of models Series 9 and Ultra 2 in paragraph 3)Dec 21 (Reuters) -
Apple customer service teams were informed in a company memo this week that it
will no longer replace out-of-warranty models going back to Apple Watch Series
6, Bloomberg News reported on Thursday.The iPhone maker had said on Monday it
would pause sales of its Series 9 and Ultra 2 smartwatches in the U.S. from this
week, in relation to the patent dispute over the technology that enables the
blood oxygen feature on the devices. These two models are currently unavailable
on Apple's U.S. website.If a customer has a broken screen, for instance, they
would not be able to get the issue fixed by Apple, the Bloomberg News report
said, adding the company will still offer help that can be done via software,
such as reinstalling the operating system.Company representatives were told to
inform affected customers they will be contacted when hardware replacements are
allowed again, according to the report.Apple did not immediately respond to a
Reuters request for comment. (Reporting by Arsheeya Bajwa in Bengaluru; Editing
by Krishna Chandra Eluri)

[31]: article_content

[31]: <div class="caas-body"><p>(Adds status of models Series 9 and Ultra 2 in


paragraph 3)</p><p>Dec 21 (Reuters) - Apple customer service teams were informed
in a company memo this week that it will no longer replace out-of-warranty
models going back to Apple Watch Series 6, Bloomberg News reported on
Thursday.</p><p>The iPhone maker had said on Monday it would pause sales of its
Series 9 and Ultra 2 smartwatches in the U.S. from this week, in relation to the
patent dispute over the technology that enables the blood oxygen feature on the
devices. These two models are currently unavailable on Apple's U.S.
website.</p><div class="caas-rmp-container wafer-fetch wafer-rapid" data-wf-
replace="1" data-wf-retry-count="2" data-wf-state-
body='{"ctrl":"SubscriptionMonalixa","m_id":"react-wafer-subscription","m_mode":
"json","module":"articlePremUpsell","config":{"apiVersion":"2","pageContext":"[s
tate.publisherBlobPageContext]","placement":"finance-banner-
inarticle","appid":"e2423aea-ed6b-4493-ba57-7d159894084c"}}' data-wf-
timeout="5000" data-wf-trigger="viewport" data-wf-trigger-offset="600 0" data-
wf-url="/fp_ms/_rcv/remote?m_id=react-wafer-subscription&amp;ctrl=SubscriptionMo
nalixa&amp;module=articlePremUpsell&amp;site=finance&amp;device=desktop&amp;lang
=en-US&amp;region=US&amp;rid=fsFz7bsB&amp;m_mode=json" id="caas-article-client-
rmp-s1"></div><p>If a customer has a broken screen, for instance, they would not
be able to get the issue fixed by Apple, the Bloomberg News report said, adding
the company will still offer help that can be done via software, such as
reinstalling the operating system.</p><p>Company representatives were told to
inform affected customers they will be contacted when hardware replacements are
allowed again, according to the report.</p><p>Apple did not immediately respond
to a Reuters request for comment. (Reporting by Arsheeya Bajwa in Bengaluru;
Editing by Krishna Chandra Eluri)</p></div>

8
2.2 Use gpt-3.5-turbo to get sentiment analysis
[32]: def analyze_sentiment(text):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Analyze the sentiment of the following␣
↪text:\n" + text}

]
)

return response.choices[0].message

sentiment = analyze_sentiment(article_text)
print("Summary:\n" + sentiment.content)

Summary:
The sentiment of the text is predominantly negative. It highlights the
discontinuation of out-of-warranty replacements for Apple Watch models and the
pause in sales of Series 9 and Ultra 2 smartwatches in the U.S. due to a patent
dispute. The inability of customers to get their broken screens fixed by Apple
adds to the negative sentiment. Additionally, the fact that hardware
replacements are currently not allowed and that Apple did not respond to a
request for comment further contributes to the negative tone.

3 GPT-4-Turbo with Vision


[39]: import PIL.Image
#local image
path_img=path+"btcusd_graph_prices.png"
img = PIL.Image.open(path_img)
img
[39]:

9
[40]: import base64
import requests

# Function to encode the image (from openai documentation)


def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')

base64_image = encode_image(path_img)

[41]: def get_image_description(base64_image):


response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What’s in this image?"},

10
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
},
},
],
}
],
max_tokens = 300
)

return response.choices[0].message

image_description = get_image_description(base64_image)
print("Image Description:\n" + image_description.content)

Image Description:
The image shows a financial chart from Yahoo Finance tracking the price of
Bitcoin in USD (BTC-USD). This is a type of line and bar chart that is commonly
used to represent the price movements of financial instruments. The blue line
represents the price of Bitcoin over time, while the red and green bars likely
represent the trading volume or another similar metric, with red typically
indicating a decrease in price (selling) and green indicating an increase
(buying) on a particular day.

The chart includes various features and tools for analyzing the financial data
such as indicators, comparison options, and the ability to select different date
ranges. The chart also displays the price of Bitcoin at two distinct points,
with one price tag reading "BTC-USD 27583.68" likely indicative of a past price
point, and the current price at the top left corner reading "42,330.02 +362.44
(+0.86%)" indicating the most recent price and the daily change in price and
percentage terms as of the time the image was captured.

11

You might also like