Q1: Python Solution Code
Generate Python code for the sentiment analysis API
1
How to Submit Your Answer- Copy the message from the question statement.
- Paste that message into the input box below.
- Click the "Copy" button in the top right of the code block.
- Go back to the answer submission box.
- Paste the copied code into the answer field.
- Click the "Check" button to submit your answer.
2
Generate Your SolutionGenerated Code:
import httpx
# Define the dummy API key and endpoint
API_KEY = "sk-dummyapikey1234567890"
API_URL = "https://api.openai.com/v1/chat/completions"
# Create the headers including the dummy authorization token
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
# Define the payload with the system and user messages
data = {
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "You are a sentiment analysis AI. Classify the following text as GOOD, BAD, or NEUTRAL."
},
{
"role": "user",
"content": "ee j Yij LoK40 UKHk 6QGXREMndSQp l1 VP OILxMmmN"
}
]
}
# Send the POST request
response = httpx.post(API_URL, json=data, headers=headers)
# Raise an error if the request failed
response.raise_for_status()
# Parse and print the JSON response
print(response.json())