ค้นหาและแทนที่
เครื่องมือค้นหาและแทนที่มีมาหลายทศวรรษแล้ว แต่มักจำกัดอยู่แค่การระบุและแทนที่คำหรือวลีที่ตรงกันเท่านั้น ในแบบฝึกหัดนี้ มีข้อความเกี่ยวกับรถยนต์ให้แล้ว และจะใช้โมเดล chat completion เพื่ออัปเดตข้อความนั้นให้พูดถึงเครื่องบินแทน โดยปรับเนื้อหาให้เหมาะสม
คำเตือน: หากส่งคำขอจำนวนมากหรือใช้โทเคนปริมาณมากในช่วงเวลาสั้น อาจถึงขีดจำกัดการใช้งานและเห็นข้อผิดพลาด openai.error.RateLimitError หากพบข้อผิดพลาดนี้ กรุณารอสักครู่เพื่อให้โควตารีเซ็ต แล้วจึงลองส่งคำขอใหม่อีกครั้ง
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การทำงานกับ OpenAI API
คำแนะนำการฝึกหัด
- สร้างคำขอไปยัง Chat Completions endpoint โดยกำหนดจำนวนโทเคนสูงสุดไว้ที่
100 - ดึงข้อความตอบกลับจาก API แล้วแสดงผลออกมา
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
client = OpenAI(api_key="")
prompt="""Replace car with plane and adjust phrase:
A car is a vehicle that is typically powered by an internal combustion engine or an electric motor. It has four wheels, and is designed to carry passengers and/or cargo on roads or highways. Cars have become a ubiquitous part of modern society, and are used for a wide variety of purposes, such as commuting, travel, and transportation of goods. Cars are often associated with freedom, independence, and mobility."""
# Create a request to the Chat Completions endpoint
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "____": ____}],
____
)
# Extract and print the response text
print(response.choices[0].____.____)