Generating a customer response

Multi-Modal Systems with the OpenAI API

James Chapman

Curriculum Manager, DataCamp

Reminder

print(corrected_text)

Corrected text (highlighted)

Multi-Modal Systems with the OpenAI API

Case study plan

$$

  • Customer message moderation

$$

  • Generating a response

$$

  • Response moderation

Case study steps

Multi-Modal Systems with the OpenAI API

Customer message moderation

from openai import OpenAI

client = OpenAI(api_key="ENTER YOUR KEY HERE")

response = client.moderations.create( model="text-moderation-latest", input=corrected_text )
# Extract scores and convert to dictionary scores = response.results[0].category_scores.model_dump()
Multi-Modal Systems with the OpenAI API

Customer message moderation

print(scores)
{'harassment': 1.0383088920207229e-05,
  ...
 'hate': 6.848756015642721e-07,
  ...
 'violence': 6.475193367805332e-05,
 ...}
Multi-Modal Systems with the OpenAI API

Customer message moderation

# Extract violence score
violence_score = scores['violence']

# Check if violence score is above 0.7 if violence_score > 0.7: print("Content flagged for violence!") else: print("Content is safe from violence.")
Content is safe from violence.
Multi-Modal Systems with the OpenAI API

Generating a response

print(FAQs)
Q: How can I upgrade my subscription?
A: You can upgrade your plan anytime in your account settings under 'Billing'. 
...

$$

print(content_overview)
Content Type: Career Track // Title: Associate AI Engineer for Developers // 
...
Multi-Modal Systems with the OpenAI API

Generating a response

A prompt with role specified

Multi-Modal Systems with the OpenAI API

Generating a response

Passing resources to the prompt

Multi-Modal Systems with the OpenAI API

Generating a response

Full prompt

Multi-Modal Systems with the OpenAI API

Generating a response

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": instruction_prompt},
        {"role": "user", "content": corrected_text}
    ],
    max_completion_tokens=400
)
Multi-Modal Systems with the OpenAI API

Generating a response

# Extract chatbot response
chatbot_reply = response.choices[0].message.content

Chatbot reply highlighted

Multi-Modal Systems with the OpenAI API

Response moderation

response = client.moderations.create(
    model="text-moderation-latest",
    input=chatbot_reply))

scores = response.results[0].category_scores.model_dump()
Multi-Modal Systems with the OpenAI API

Response moderation

# Check if any scores exceed 0.7
if all(score > 0.7 for score in scores.values()):
    print("AI Response flagged for moderation!")
    chatbot_reply = """I'm sorry, but I can't provide a response to that request.
    Please contact [email protected] for further assistance."""
else:
    print("AI Response is safe.")
AI Response is safe.
Multi-Modal Systems with the OpenAI API

Recap

Case study steps with checkmarks

Multi-Modal Systems with the OpenAI API

Let's practice!

Multi-Modal Systems with the OpenAI API

Preparing Video For Download...