Conception d'invites pour développer un robot conversationnel

Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Fouad Trad

Machine Learning Engineer

Pourquoi concevoir des invites pour les robots conversationnels

  • Difficile de prévoir les questions des utilisatrices et utilisateurs
  • Difficile d'assurer des réponses efficaces
  • L'ingénierie des invites oriente le comportement du robot

Image montrant un robot conversationnel sur un téléphone mobile.

Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Invites pour robots avec l'API OpenAI

Image montrant les trois rôles d'un message et la communication entre eux.

  • Chaque message a un rôle défini
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Invites pour robots avec l'API OpenAI

Image montrant les trois rôles d'un message et la communication entre eux, en surlignant l'interaction entre l'utilisatrice ou l'utilisateur et l'assistant.

  • Chaque message a un rôle défini
  • L'accent a été mis sur les invites de l'utilisatrice ou de l'utilisateur
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Invites pour robots avec l'API OpenAI

Image montrant les trois rôles d'un message et la communication entre eux, en surlignant l'interaction entre le système et l'assistant.

  • Chaque message a un rôle défini
  • L'accent a été mis sur les invites de l'utilisatrice ou de l'utilisateur
  • Les invites du système guident le comportement du robot
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Point de terminaison chat completions pour créer un robot

  • Envoie une série de messages au modèle sous forme de liste
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.
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Adapter get_response() pour le robot

  • Envoyer une seule invite
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))
  • Envoyer deux invites
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))
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Message système : définir le but

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?
  • Permettre au robot d'offrir une aide exacte pour le domaine
  • Sans but défini, risque de réponses hors contexte
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Message système : directives de réponse

  • Préciser public cible, ton, longueur, structure
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))
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Message système : directives de réponse

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 [...]
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Message système : encadrement du comportement

  • Invites conditionnelles pour répondre aux questions
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Message système : encadrement du comportement

  • Invites conditionnelles pour répondre aux questions
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.

"""
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Message système : encadrement du comportement

  • Invites conditionnelles pour répondre aux questions
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.
Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Passons à la pratique !

Conception de invites (Prompt Engineering) avec l'API d'OpenAI

Preparing Video For Download...