Sohbet botu geliştirme için prompt mühendisliği

OpenAI API ile Prompt Engineering

Fouad Trad

Machine Learning Engineer

Sohbet botları için prompt mühendisliğinin gereği

  • Kullanıcı sorularını öngörmek zordur
  • Etkili yanıtları garanti etmek zordur
  • Prompt mühendisliği, chatbot davranışını yönlendirir

Bir cep telefonunda sohbet botunu gösteren görsel.

OpenAI API ile Prompt Engineering

OpenAI API ile sohbet botu prompt mühendisliği

Bir iletinin üç rolünü ve aralarındaki iletişimi gösteren görsel.

  • Her iletinin belirlenmiş bir rolü vardır
OpenAI API ile Prompt Engineering

OpenAI API ile sohbet botu prompt mühendisliği

Bir iletinin üç rolünü ve aralarındaki iletişimi gösteren, kullanıcı–asistan etkileşimini vurgulayan görsel.

  • Her iletinin belirlenmiş bir rolü vardır
  • Odak, kullanıcı prompt'larındaydı
OpenAI API ile Prompt Engineering

OpenAI API ile sohbet botu prompt mühendisliği

Bir iletinin üç rolünü ve aralarındaki iletişimi gösteren, sistem–asistan etkileşimini vurgulayan görsel.

  • Her iletinin belirlenmiş bir rolü vardır
  • Odak, kullanıcı prompt'larındaydı
  • Sistem prompt'ları, botun davranışını yönlendirir
OpenAI API ile Prompt Engineering

Sohbet botu geliştirme için Chat Completions uç noktası

  • Modele iletileri bir liste olarak gönderir
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.
OpenAI API ile Prompt Engineering

Chatbot için get_response()'ı değiştirme

  • Tek bir prompt gönderme
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))
  • İki prompt gönderme
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))
OpenAI API ile Prompt Engineering

Sistem iletisi: amacı tanımlayın

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?
  • Sohbet botunun alana uygun yanıt vermesini sağlar
  • Amaç tanımlanmazsa bağlama uymayan yanıtlar olabilir
OpenAI API ile Prompt Engineering

Sistem iletisi: yanıt yönergeleri

  • Hedef kitle, ton, uzunluk, yapı belirtin
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))
OpenAI API ile Prompt Engineering

Sistem iletisi: yanıt yönergeleri

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 [...]
OpenAI API ile Prompt Engineering

Sistem iletisi: davranış rehberi

  • Sorulara koşullu promptlarla yanıt verin
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
OpenAI API ile Prompt Engineering

Sistem iletisi: davranış rehberi

  • Sorulara koşullu promptlarla yanıt verin
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.

"""
OpenAI API ile Prompt Engineering

Sistem iletisi: davranış rehberi

  • Sorulara koşullu promptlarla yanıt verin
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.
OpenAI API ile Prompt Engineering

Hadi pratik yapalım!

OpenAI API ile Prompt Engineering

Preparing Video For Download...