Set your OpenAI API key (replace with your actual key)
openai.api_key = “my key was here Now I have removed it”
def sentiment_analysis(text):
“”"Analyzes the sentiment of a given text using OpenAI’s completions API.
Args:
text (str): The text to analyze.
Returns:
str: The determined sentiment (positive, negative, or not sure).
"""
# Create a prompt for the model
prompt = f"""You are trained to analyze and detect the sentiment of the given text.
If you are unsure of an answer, you can say "not sure" and recommend the user review manually.
Analyze the following text and determine if the sentiment is: Positive, Negative, or Neutral.
{text}"""
# Call the OpenAI API to generate a response
response = openai.Completion.create(
model="text-davinci-003", # Use a powerful model for sentiment analysis
prompt=prompt,
max_tokens=1, # Limit response to a single word
n=1,
stop=None,
temperature=0 # Keep response consistent
)
# Extract the sentiment from the response
sentiment = response.choices[0].text.strip().lower()
return sentiment
Example usage
input_text = “I’m very happy with the product!”
sentiment = sentiment_analysis(input_text)
print(input_text, “: Sentiment is:”, sentiment)
Based on the information provided in the extracts, it seems like you’re trying to use the openai.Completion.create method, which is no longer supported in the OpenAI Python library version 1.0.0 and above. Instead, you should use the openai.ChatCompletion.create or client.chat.completions.create method. Here’s an example of how to modify your code based on the information from the extracts:
import openai
from openai import OpenAI
# Set your OpenAI API key (replace with your actual key)
openai.api_key = "your-api-key"
# Initialize the OpenAI client
client = OpenAI(api_key=openai.api_key)
def sentiment_analysis(text):
# Create a prompt for the model
prompt = f"""You are trained to analyze and detect the sentiment of the given text.
If you are unsure of an answer, you can say "not sure" and recommend the user review manually.
Analyze the following text and determine if the sentiment is: Positive, Negative, or Neutral.
{text}"""
# Call the OpenAI API to generate a response
response = client.chat.completions.create(
model="gpt-3.5-turbo", # Use a powerful model for sentiment analysis
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
max_tokens=1, # Limit response to a single word
temperature=0 # Keep response consistent
)
# Extract the sentiment from the response
sentiment = response.choices[0].message.content.strip().lower()
return sentiment
# Example usage
input_text = "I’m very happy with the product!"
sentiment = sentiment_analysis(input_text)
print(input_text, ": Sentiment is:", sentiment)
Please note that the model “gpt-3.5-turbo” is used in this example as the “text-davinci-003” model was not mentioned in the provided extracts. You may need to adjust the model based on your specific requirements and the models available to you.