Last active
February 22, 2024 15:26
-
-
Save financial-python/d0034c44728f3a71a4904e70a2a77668 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
2 Pre-Requisites - install the package, and get an OpenAI API key | |
pip install openai | |
""" | |
#import the package and add your key | |
import openai | |
openai.api_key = "YOUR_API_KEY_HERE" | |
#gather your text from wherever - reddit, twitter, manual input, etc | |
text = "I am bullish on $AAPL" | |
#make callout to the Completion endpoint | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=(f"Sentiment analysis of the following text: '{text}'\n\nSentiment score: "), | |
temperature=0, | |
max_tokens=10 | |
) | |
#get the sentiment from the response | |
sentiment = response.choices[0].text.strip() | |
print(sentiment) | |
#use the sentiment as needed: | |
if sentiment == "Positive": | |
print("😊") | |
elif sentiment == "Negative": | |
print("😔") | |
else: | |
print("😐") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment