{ "cells": [ { "cell_type": "markdown", "id": "6aa29ed3", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "id": "dbe3c872", "metadata": {}, "source": [ "This code performs sentiment analysis on news articles related to Microsoft (MSFT) using a predefined language model. It imports necessary libraries, loads environment variables, and sets up a language chain model with a prompt template. The code then fetches news articles, applies the sentiment analysis model to each news description, and appends the sentiment results. Finally, it displays the news descriptions along with their corresponding sentiments." ] }, { "cell_type": "code", "execution_count": null, "id": "a7beec45", "metadata": {}, "outputs": [], "source": [ "from openbb_terminal.sdk import openbb" ] }, { "cell_type": "code", "execution_count": null, "id": "dcdaade8", "metadata": {}, "outputs": [], "source": [ "from langchain.chains import LLMChain\n", "from langchain.prompts import PromptTemplate\n", "from langchain.chat_models import ChatOpenAI" ] }, { "cell_type": "code", "execution_count": null, "id": "86d35212", "metadata": {}, "outputs": [], "source": [ "from dotenv import load_dotenv" ] }, { "cell_type": "code", "execution_count": null, "id": "cb878e28", "metadata": {}, "outputs": [], "source": [ "load_dotenv()" ] }, { "cell_type": "code", "execution_count": null, "id": "c6d9e010", "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": null, "id": "cee1e48e", "metadata": {}, "outputs": [], "source": [ "pd.set_option(\"max_colwidth\", None)" ] }, { "cell_type": "markdown", "id": "ecdeff99", "metadata": {}, "source": [ "Initialize the language model with GPT-4 and set temperature for response variability" ] }, { "cell_type": "code", "execution_count": null, "id": "cc0e296a", "metadata": {}, "outputs": [], "source": [ "llm = ChatOpenAI(model=\"gpt-4\", temperature=0)" ] }, { "cell_type": "markdown", "id": "929eb3e1", "metadata": {}, "source": [ "Define the sentiment analysis prompt template" ] }, { "cell_type": "code", "execution_count": null, "id": "34670050", "metadata": {}, "outputs": [], "source": [ "prompt = \"\"\"\n", "Is the predominant sentiment in the following statement positive, negative, or neutral?\n", "---------\n", "Statement: {statement}\n", "---------\n", "Respond with one word in lowercase: positive, negative, or neutral.\n", "Sentiment:\n", "\"\"\"" ] }, { "cell_type": "markdown", "id": "a1067c85", "metadata": {}, "source": [ "Create a sentiment analysis chain using the language model and prompt template" ] }, { "cell_type": "code", "execution_count": null, "id": "540facbd", "metadata": {}, "outputs": [], "source": [ "chain = LLMChain.from_string(\n", " llm=llm,\n", " template=prompt\n", ")" ] }, { "cell_type": "markdown", "id": "905ccc67", "metadata": {}, "source": [ "Fetch news articles related to Microsoft (MSFT) using the OpenBB SDK" ] }, { "cell_type": "code", "execution_count": null, "id": "a85fdbbe", "metadata": {}, "outputs": [], "source": [ "msft = openbb.news(term=\"msft\")" ] }, { "cell_type": "markdown", "id": "0e72a0f5", "metadata": {}, "source": [ "Apply the sentiment analysis chain to each news description and add the sentiment results to a new column" ] }, { "cell_type": "code", "execution_count": null, "id": "d7c08925", "metadata": {}, "outputs": [], "source": [ "msft[\"Sentiment\"] = msft.Description.apply(chain.run)" ] }, { "cell_type": "markdown", "id": "ae31394d", "metadata": {}, "source": [ "Display the news descriptions along with their corresponding sentiment results" ] }, { "cell_type": "code", "execution_count": null, "id": "259bb33e", "metadata": {}, "outputs": [], "source": [ "msft[[\"Description\", \"Sentiment\"]]" ] }, { "cell_type": "markdown", "id": "29c68af1", "metadata": {}, "source": [ "PyQuant News is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to get started with Python for quant finance. For educational purposes. Not investment advise. Use at your own risk." ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }