การสร้างข้อความ

การทำงานกับ OpenAI API

James Chapman

Curriculum Manager, DataCamp

ผลลัพธ์ถูกสร้างขึ้นอย่างไร?

  • ข้อความที่ มีโอกาสมากที่สุด ที่จะต่อจาก prompt
response = client.chat.completions.create(
  model="gpt-4o-mini",

messages=[{"role": "user", "content": "Life is like a box of chocolates."}]
) print(response.choices[0].message.content)
You never know what you're going to get. This famous quote from the movie
"Forrest Gump"...
  • การตอบสนองเป็น non-deterministic (สุ่มโดยธรรมชาติ)
การทำงานกับ OpenAI API

การควบคุมความสุ่มของการตอบสนอง

  • temperature: ควบคุม ความแน่นอน ของผลลัพธ์
  • ค่าอยู่ระหว่าง 0 (แน่นอนสูง) ถึง 2 (สุ่มมาก)
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Life is like a box of chocolates."}],

temperature=2
) print(response.choices[0].message.content)
"...you never know what you're gonna get." That quote reminds us of the
unpredictability of life and the diverse set of experiences we might encounter.
Whether sweet, nutty, bitter, or flashy, life holds a treasure trove of surprises.
การทำงานกับ OpenAI API

การสร้างข้อความ: การตลาด

prompt = "Generate a powerful tagline for a new electric vehicle 
that highlights innovation and sustainability."
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)

print(response.choices[0].message.content)
"Drive the Future - Electric, Effortless, Extraordinary."
การทำงานกับ OpenAI API

การสร้างข้อความ: คำอธิบายสินค้า

prompt = """Write a compelling product description for the UltraFit Smartwatch. 
Highlight its key features:  10-day battery life, 24/7 heart rate and sleep 
tracking, built-in GPS, water resistance up to 50 meters, and lightweight design.

Use a persuasive and engaging tone to appeal to fitness enthusiasts 
and busy professionals.
"""

response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": prompt}] )
การทำงานกับ OpenAI API

การสร้างข้อความ: คำอธิบายสินค้า

print(response.choices[0].message.content)
"The UltraFit Smartwatch is your all-in-one health and fitness companion. 
Stay on top of your wellness with 24/7 heart rate and sleep tracking, 
while the built-in GPS keeps you on course during runs. 
With a 10-day battery life, you can track your progress without constant recharging. 
Designed for both workouts and daily wear, its  lightweight build and 50-meter
water resistance make it the perfect smartwatch for an active lifestyle."
  • ปรับปรุงซ้ำ! เพิ่มรายละเอียดใน prompt แล้วรันคำขอใหม่
  • วิดีโอถัดไป → การให้ตัวอย่างแก่โมเดล
การทำงานกับ OpenAI API

มาฝึกกันเถอะ!

การทำงานกับ OpenAI API

Preparing Video For Download...