Prompt engineering สำหรับการพัฒนาแชทบอท

Prompt Engineering กับ OpenAI API

Fouad Trad

Machine Learning Engineer

ทำไมต้องใช้ prompt engineering กับแชทบอท

  • ยากที่จะคาดเดาคำถามของผู้ใช้
  • ยากที่จะรับประกันว่าคำตอบจะมีประสิทธิภาพ
  • Prompt engineering ช่วยกำหนดพฤติกรรมของแชทบอท

ภาพแสดงแชทบอทบนโทรศัพท์มือถือ

Prompt Engineering กับ OpenAI API

Prompt engineering สำหรับแชทบอทด้วย OpenAI API

ภาพแสดงบทบาททั้งสามของข้อความและการสื่อสารระหว่างกัน

  • แต่ละข้อความมีบทบาทที่กำหนดไว้
Prompt Engineering กับ OpenAI API

Prompt engineering สำหรับแชทบอทด้วย OpenAI API

ภาพแสดงบทบาททั้งสามของข้อความและการสื่อสารระหว่างกัน โดยเน้นที่การโต้ตอบระหว่าง user และ assistant

  • แต่ละข้อความมีบทบาทที่กำหนดไว้
  • จนถึงตอนนี้เน้นที่ user prompt
Prompt Engineering กับ OpenAI API

Prompt engineering สำหรับแชทบอทด้วย OpenAI API

ภาพแสดงบทบาททั้งสามของข้อความและการสื่อสารระหว่างกัน โดยเน้นที่การโต้ตอบระหว่าง system และ assistant

  • แต่ละข้อความมีบทบาทที่กำหนดไว้
  • จนถึงตอนนี้เน้นที่ user prompt
  • System prompt ช่วยกำหนดพฤติกรรมของแชทบอท
Prompt Engineering กับ OpenAI API

Chat completions endpoint สำหรับการพัฒนาแชทบอท

  • ส่งชุดข้อความไปยังโมเดลในรูปแบบลิสต์
response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are an expert data scientist that explains complex concepts in simple terms"},

{"role": "user", "content": "What is prompt engineering?"}] )
print(response.choices[0].message.content)
Imagine you're giving instructions to a computer program, like teaching a robot to make a sandwich. 
Prompt engineering is all about crafting those instructions, or "prompts," in a way that helps the 
computer understand and perform the task better.
Prompt Engineering กับ OpenAI API

ปรับ get_response() สำหรับแชทบอท

  • ส่ง prompt เดียว
def get_response(prompt):
    messages = [
      {"role": "user", "content": prompt}
    ]
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message.content

prompt = "<PROMPT>" print(get_response(prompt))
  • ส่งสอง prompt
def get_response(system_prompt, user_prompt):

messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}]
response = client.chat.completions.create( model="gpt-4o-mini", messages=messages, temperature=0, ) return response.choices[0].message.content
system_prompt = "<SYSTEM_PROMPT>" user_prompt = "<USER_PROMPT>" print(get_response(system_prompt, user_prompt))
Prompt Engineering กับ OpenAI API

System message: กำหนดจุดประสงค์

system_prompt = "You are a chatbot that answers financial questions."

user_prompt = "Who are you?" print(get_response(system_prompt, user_prompt))
I'm a financial chatbot that answers financial questions. How can I help you?
  • ช่วยให้แชทบอทตอบได้ถูกต้องตามโดเมน
  • หากไม่กำหนดจุดประสงค์ อาจได้คำตอบที่ไม่ตรงบริบท
Prompt Engineering กับ OpenAI API

System message: แนวทางการตอบสนอง

  • ระบุกลุ่มเป้าหมาย น้ำเสียง ความยาว และโครงสร้าง
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective"""

user_prompt = "What do you think about cryptocurrencies?" print(get_response(system_prompt, user_prompt))
Prompt Engineering กับ OpenAI API

System message: แนวทางการตอบสนอง

Cryptocurrencies are digital or virtual currencies that use cryptography for security and operate on 
decentralized networks based on blockchain technology. 
[...]

Advantages of cryptocurrencies include: - Decentralization: [...] - Security:[...] - Global Accessibility: [...]
However, there are also notable concerns: - Volatility: [...] - Regulatory Uncertainty: [...] - Lack of Consumer Protection: [...]
In summary, cryptocurrencies have the potential to offer various benefits, but their adoption and impact on the financial landscape are still evolving [...]
Prompt Engineering กับ OpenAI API

System message: การกำหนดพฤติกรรม

  • ใช้ conditional prompt เพื่อตอบสนองต่อคำถาม
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
Prompt Engineering กับ OpenAI API

System message: การกำหนดพฤติกรรม

  • ใช้ conditional prompt เพื่อตอบสนองต่อคำถาม
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.

"""
Prompt Engineering กับ OpenAI API

System message: การกำหนดพฤติกรรม

  • ใช้ conditional prompt เพื่อตอบสนองต่อคำถาม
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.
Otherwise, answer with 'Sorry, I only know about finance.'
"""
user_prompt = "How's the weather today?"

print(get_response(system_prompt, user_prompt))
Sorry, I only know about finance.
Prompt Engineering กับ OpenAI API

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

Prompt Engineering กับ OpenAI API

Preparing Video For Download...