Skip to content

Instantly share code, notes, and snippets.

@financial-python
Last active February 22, 2024 15:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save financial-python/d0034c44728f3a71a4904e70a2a77668 to your computer and use it in GitHub Desktop.
Save financial-python/d0034c44728f3a71a4904e70a2a77668 to your computer and use it in GitHub Desktop.
"""
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