OpenAI. OpenBB. LangChain. Cutting-edge sentiment analysis using generative pre-trained transformers. All in a few lines of Python. Here's the code:
By reading this thread, you’ll use the OpenBB SDK to download news for a topic. Then, you’ll build a prompt to predict the sentiment of a news headline. You’ll bring it all together with LangChain. Let's go!
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: OPENAI_API_KEY=YOUR_API_KEY Once that's done, move on:
Let’s import the OpenBB SDK for news and LangChain for building the app. Afterward, we instantiate an OpenAI GPT-4 LLM.
We use a straightforward prompt that outputs positive, negative, or neutral depending on the LLM’s interpretation of the sentiment. After creating the prompt, we create a LangChain chain using the OpenAI LLM and the prompt.
Next, we’ll download news headlines using the OpenBB SDK and apply the chain’s run method to each one.
This code creates a pandas DataFrame. We apply the []() 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.
If you’re unfamiliar with prompt engineering, it’s the art of getting LLMs to return predictive, accurate results. Spend some time tweaking the prompt to improve its performance and predictability. For example, do you agree that MSFT’s $69B purchase of Activision is neutral?
65