Prompt engineering per lo sviluppo di chatbot

Prompt Engineering con l'API di OpenAI

Fouad Trad

Machine Learning Engineer

Perché serve il prompt engineering nei chatbot

  • Difficile prevedere le domande dell'utente
  • Difficile garantire risposte efficaci
  • Il prompt engineering guida il comportamento del chatbot

Immagine che mostra un chatbot su uno smartphone.

Prompt Engineering con l'API di OpenAI

Prompt engineering per chatbot con OpenAI API

Immagine che mostra i tre ruoli di un messaggio e la comunicazione tra loro.

  • Ogni messaggio ha un ruolo definito
Prompt Engineering con l'API di OpenAI

Prompt engineering per chatbot con OpenAI API

Immagine che mostra i tre ruoli di un messaggio e la comunicazione tra loro, con evidenza dell'interazione tra utente e assistente.

  • Ogni messaggio ha un ruolo definito
  • Il focus finora è stato sui prompt utente
Prompt Engineering con l'API di OpenAI

Prompt engineering per chatbot con OpenAI API

Immagine che mostra i tre ruoli di un messaggio e la comunicazione tra loro, con evidenza dell'interazione tra sistema e assistente.

  • Ogni messaggio ha un ruolo definito
  • Il focus finora è stato sui prompt utente
  • I prompt di sistema guidano il comportamento del chatbot
Prompt Engineering con l'API di OpenAI

Endpoint chat completions per lo sviluppo di chatbot

  • Invia una serie di messaggi al modello come lista
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 con l'API di OpenAI

Modificare get_response() per il chatbot

  • Invio di un singolo 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))
  • Invio di due 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 con l'API di OpenAI

Messaggio di sistema: definire lo scopo

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?
  • Consente al chatbot di offrire assistenza accurata nel dominio
  • Senza scopo definito, rischia risposte fuori contesto
Prompt Engineering con l'API di OpenAI

Messaggio di sistema: linee guida di risposta

  • Specifica audience, tono, lunghezza, struttura
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 con l'API di OpenAI

Messaggio di sistema: linee guida di risposta

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 con l'API di OpenAI

Messaggio di sistema: guida al comportamento

  • Prompt condizionali per rispondere alle domande
system_prompt = """You are a chatbot that answers financial questions. 
Your answers should be precise, formal and objective.


"""
Prompt Engineering con l'API di OpenAI

Messaggio di sistema: guida al comportamento

  • Prompt condizionali per rispondere alle domande
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 con l'API di OpenAI

Messaggio di sistema: guida al comportamento

  • Prompt condizionali per rispondere alle domande
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 con l'API di OpenAI

Ayo berlatih!

Prompt Engineering con l'API di OpenAI

Preparing Video For Download...