產生客戶回覆

使用 OpenAI API 的多模態系統

James Chapman

Curriculum Manager, DataCamp

提醒

print(corrected_text)

已更正的文字(重點標示)

使用 OpenAI API 的多模態系統

個案研究規劃

$$

  • 客戶訊息審核

$$

  • 產生回覆

$$

  • 回覆審核

個案研究步驟

使用 OpenAI API 的多模態系統

客戶訊息審核

from openai import OpenAI

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

response = client.moderations.create( input=corrected_text )
# Extract scores and convert to dictionary scores = response.results[0].category_scores.model_dump()
使用 OpenAI API 的多模態系統

客戶訊息審核

print(scores)
{'harassment': 1.0383088920207229e-05,
  ...
 'hate': 6.848756015642721e-07,
  ...
 'violence': 6.475193367805332e-05,
 ...}
使用 OpenAI API 的多模態系統

客戶訊息審核

# 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.
使用 OpenAI API 的多模態系統

產生回覆

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 // 
...
使用 OpenAI API 的多模態系統

產生回覆

含角色設定的提示

使用 OpenAI API 的多模態系統

產生回覆

將資源傳入提示

使用 OpenAI API 的多模態系統

產生回覆

完整提示

使用 OpenAI API 的多模態系統

產生回覆

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": instruction_prompt},
        {"role": "user", "content": corrected_text}
    ],
    max_completion_tokens=400
)
使用 OpenAI API 的多模態系統

產生回覆

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

聊天機器人回覆(重點標示)

使用 OpenAI API 的多模態系統

回覆審核

response = client.moderations.create(
    input=chatbot_reply
)

scores = response.results[0].category_scores.model_dump()
使用 OpenAI API 的多模態系統

回覆審核

# Check if any scores exceed 0.7
if any(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.
使用 OpenAI API 的多模態系統

重點回顧

個案研究步驟(含勾選)

使用 OpenAI API 的多模態系統

一起來練習吧!

使用 OpenAI API 的多模態系統

Preparing Video For Download...