Kỹ thuật prompt cho phát triển chatbot

Kỹ thuật Prompt với OpenAI API

Fouad Trad

Machine Learning Engineer

Nhu cầu kỹ thuật prompt cho chatbot

  • Khó dự đoán câu hỏi của người dùng
  • Khó bảo đảm câu trả lời hiệu quả
  • Kỹ thuật prompt định hướng hành vi chatbot

Hình ảnh hiển thị một chatbot trên điện thoại di động.

Kỹ thuật Prompt với OpenAI API

Kỹ thuật prompt cho chatbot với OpenAI API

Hình ảnh hiển thị ba vai trò của một tin nhắn và cách chúng giao tiếp.

  • Mỗi tin nhắn có một vai trò xác định
Kỹ thuật Prompt với OpenAI API

Kỹ thuật prompt cho chatbot với OpenAI API

Hình ảnh hiển thị ba vai trò của một tin nhắn và cách chúng giao tiếp, được tô đậm phần tương tác giữa người dùng và trợ lý.

  • Mỗi tin nhắn có một vai trò xác định
  • Trọng tâm trước đây là prompt của người dùng
Kỹ thuật Prompt với OpenAI API

Kỹ thuật prompt cho chatbot với OpenAI API

Hình ảnh hiển thị ba vai trò của một tin nhắn và cách chúng giao tiếp, được tô đậm phần tương tác giữa hệ thống và trợ lý.

  • Mỗi tin nhắn có một vai trò xác định
  • Trọng tâm trước đây là prompt của người dùng
  • Prompt hệ thống định hướng hành vi chatbot
Kỹ thuật Prompt với OpenAI API

Endpoint chat completions cho phát triển chatbot

  • Gửi chuỗi tin nhắn cho mô hình dưới dạng danh sách
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.
Kỹ thuật Prompt với OpenAI API

Điều chỉnh get_response() cho chatbot

  • Gửi một 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))
  • Gửi hai 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))
Kỹ thuật Prompt với OpenAI API

Tin nhắn hệ thống: xác định mục tiêu

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?
  • Giúp chatbot hỗ trợ đúng chuyên môn
  • Không xác định mục tiêu có thể dẫn tới trả lời lệch ngữ cảnh
Kỹ thuật Prompt với OpenAI API

Tin nhắn hệ thống: hướng dẫn trả lời

  • Chỉ định đối tượng, giọng điệu, độ dài, cấu trúc
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))
Kỹ thuật Prompt với OpenAI API

Tin nhắn hệ thống: hướng dẫn trả lời

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 [...]
Kỹ thuật Prompt với OpenAI API

Tin nhắn hệ thống: định hướng hành vi

  • Prompt điều kiện để trả lời câu hỏi
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
Kỹ thuật Prompt với OpenAI API

Tin nhắn hệ thống: định hướng hành vi

  • Prompt điều kiện để trả lời câu hỏi
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.

"""
Kỹ thuật Prompt với OpenAI API

Tin nhắn hệ thống: định hướng hành vi

  • Prompt điều kiện để trả lời câu hỏi
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.
Kỹ thuật Prompt với OpenAI API

Ayo berlatih!

Kỹ thuật Prompt với OpenAI API

Preparing Video For Download...