customer_support_agent / openai_api.py
guptavishal79's picture
generate add image
ed86c67
import openai
from openai import OpenAI
import os
# Set your OpenAI API key (replace with your actual key)
openai.api_key = os.getenv("OPENAI_KEY")
# Initialize the OpenAI client
client = OpenAI(api_key=openai.api_key)
def model_api(input, prompt_type):
return prompt_type(input)
def sentiment(text):
print(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.
Reply in single word.
Examples
Input: dress was beautiful. Output: POSITIVE
Input: pizza had weird smell. Output: NEGATIVE
Input: {text}. Output:"""
# 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
)
print(response)
# Extract the sentiment from the response
sentiment = response.choices[0].message.content.strip()
return sentiment
def image_gen(text):
print(text)
response = client.images.generate(
model="dall-e-3",
prompt= text,
size="1024x1024",
quality="standard",
n=1,
)
image_url = response.data[0].url
return image_url