Prompt Engineering für Chatbot-Entwicklung

Prompt-Engineering mit der OpenAI-API

Fouad Trad

Machine Learning Engineer

Warum Prompt Engineering für Chatbots?

  • Schwierig, Fragen der Nutzer vorherzusagen
  • Herausforderung, effektive Antworten zu garantieren
  • Prompt Engineering steuert das Verhalten von Chatbots

Bild, das einen Chatbot auf einem Handy zeigt.

Prompt-Engineering mit der OpenAI-API

Chatbot Prompt-Engineering mit der OpenAI-API

Bild, das die drei Rollen einer Nachricht und die Kommunikation zwischen ihnen zeigt.

  • Jeder message-Typ hat eine bestimmte Aufgabe.
Prompt-Engineering mit der OpenAI-API

Chatbot Prompt-Engineering mit der OpenAI-API

Bild, das die drei Rollen einer Nachricht und die Kommunikation zwischen ihnen zeigt, mit einem Fokus auf die Interaktion zwischen dem Nutzer und dem Assistenten.

  • Jeder message-Typ hat eine bestimmte Aufgabe.
  • Der Fokus lag auf User Prompts.
Prompt-Engineering mit der OpenAI-API

Chatbot Prompt-Engineering mit der OpenAI-API

Bild, das die drei Rollen einer Nachricht und die Kommunikation zwischen ihnen zeigt, mit einem Fokus auf die Interaktion zwischen dem System und dem Assistenten.

  • Jeder message-Typ hat eine bestimmte Aufgabe.
  • Der Fokus lag auf User Prompts.
  • System Prompts steuern das Verhalten des Chatbots
Prompt-Engineering mit der OpenAI-API

Chat completions Endpunkt zur Entwicklung von Chatbots

  • Eine Reihe von messages als Liste an das Modell schicken.
response = client.chat.completions.create(
  model="gpt-3.5-turbo",
  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 mit der OpenAI-API

get_response() für den Chatbot anpassen

  • Einen Prompt senden
def get_response(prompt):
    messages = [
      {"role": "user", "content": prompt}
    ]
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message.content

prompt = "<PROMPT>" print(get_response(prompt))
  • Zwei Prompts senden
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-3.5-turbo", 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 mit der OpenAI-API

System message: den Zweck festlegen

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?
  • Den Chatbot domänenbezogene Hilfe anbieten lassen.
  • Wenn man den Zweck nicht deutlich formuliert, kann das zu Antworten führen, die für den Kontext nicht relevant sind.
Prompt-Engineering mit der OpenAI-API

System message: Richtlinien für die Antwort

  • Zielgruppe, Tonfall, Länge und Struktur festlegen
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 mit der OpenAI-API

System message: Richtlinien für die Antwort

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 mit der OpenAI-API

System message: Verhaltenshinweis

  • Conditional Prompts, um auf Fragen zu antworten
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
Prompt-Engineering mit der OpenAI-API

System message: Verhaltenshinweis

  • Conditional Prompts, um auf Fragen zu antworten
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 mit der OpenAI-API

System message: Verhaltenshinweis

  • Conditional Prompts, um auf Fragen zu antworten
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 mit der OpenAI-API

Lass uns üben!

Prompt-Engineering mit der OpenAI-API

Preparing Video For Download...