Use OpenAI prompts for stock news sentiment

October 14, 2023
Facebook logo.
Twitter logo.
LinkedIn logo.
Get this code in Google Colab

Use OpenAI prompts for stock news sentiment

In today’s newsletter, you’ll use the OpenBB SDK to download news for a topic. Then, you’ll use OpenAI and build a prompt to predict the sentiment of a news headline. You’ll bring it all together with LangChain.

The result is a pandas DataFrame with a column of news headlines and a column with the predicted sentiment.

Let’s go!

Use OpenAI prompts for stock news sentiment

Generative Pre-trained Transformers (GPT) have significantly advanced the field of sentiment analysis by providing more nuanced and context-aware interpretations of text.

Their ability to capture long-range dependencies and understand context allows for a more accurate assessment of sentiment, reducing the likelihood of misclassification that is often seen in simpler models like Naive Bayes or Support Vector Machines.

The fine-tuning capabilities of these models enable domain-specific adaptations (like BloombergGPT or TimeGPT) which makes them useful for various applications in sentiment analysis.

In just a few lines of code, we can use the OpenAI GPT-4 model to predict the sentiment of a news headline.

Let’s get started!

Imports and set up

You’ll need an OpenAI API key to run this code. Once you have it, create a .env file in your working directly. Add the following line:

1OPENAI_API_KEY=YOUR_API_KEY

Let’s import the OpenBB SDK for news and LangChain for building the app. Afterward, we instantiate an OpenAI GPT-4 LLM.

1from openbb_terminal.sdk import openbb
2
3from langchain.chains import LLMChain
4from langchain.prompts import PromptTemplate
5from langchain.chat_models import ChatOpenAI
6
7from dotenv import load_dotenv
8load_dotenv()
9
10llm = ChatOpenAI(model="gpt-4", temperature=0)

Predict the sentiment

We use a straightforward prompt which outputs positive, negative, or neutral depending on the LLM’s interpretation of the sentiment.

1prompt = """
2Is the predominant sentiment in the following statement positive, negative, or neutral?
3---------
4Statement: {statement}
5---------
6Respond with one word in lowercase: positive, negative, or neutral.
7Sentiment:
8"""
9
10chain = LLMChain.from_string(
11    llm=llm,
12    template=prompt
13)

After creating the prompt, we create a LangChain chain using the OpenAI LLM and the prompt.

The LLMChain integrates a PromptTemplate, a Model, and Guardrails. It processes user input through formatting, submits it to the model for a response, and subsequently validates and corrects the model's output as needed.

Next, we’ll download news headlines using the OpenBB SDK and apply the chain’s run method to each one.

1msft = openbb.news(term="msft")
2msft["Sentiment"] = msft.Description.apply(chain.run)
3msft[["Description", "Sentiment"]]

This code creates a pandas DataFrame. We apply the chain.run method to the Description column and set the predicted sentiment in the Sentiment column.

Printing the output gives us a DataFrame with the predicted sentiment.

Use OpenAI prompts for stock news sentiment. In today’s newsletter, you’ll use the OpenBB SDK to download news for a topic.
Use OpenAI prompts for stock news sentiment. In today’s newsletter, you’ll use the OpenBB SDK to download news for a topic.

nextSteps

If you’re unfamiliar with prompt engineering, it’s the art of getting LLMs to return predictive, accurate results. As a next step, spend some time tweaking the prompt to improve it’s performance and predictability. For example, do you agree that MSFT’s $69B purchase of Activision is neutral?

Man with glasses and a wristwatch, wearing a white shirt, looking thoughtfully at a laptop with a data screen in the background.