Q1: Python Solution Code
Generate Python code for the sentiment analysis API
1
How to Submit Your Answer
  1. Copy the message from the question statement.
  2. Paste that message into the input box below.
  3. Click the "Copy" button in the top right of the code block.
  4. Go back to the answer submission box.
  5. Paste the copied code into the answer field.
  6. Click the "Check" button to submit your answer.
2
Generate Your Solution

Generated 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())