Explaining chat-based generative AI models

Explainable AI in Python

Fouad Trad

Machine Learning Engineer

Chat-based generative AI models

  • Used to generate text
  • Predicts based on context and knowledge

 

Image showing a user sending a prompt for a generative AI model and receiving a response.

Explainable AI in Python

Chain-of-thought prompt

  • Encourages model to articulate its reasoning

Image showing how a chain-of-thought prompt asks the model to solve a problem step by step, and the output contains the reasoning for each step followed.

Explainable AI in Python

Creating a chain-of-thought prompt

prompt = """A shop starts with 20 apples. It sells 5 apples and then receives 8 more. 
How many apples does the shop have now? Show your reasoning step-by-step."""

response = get_response(prompt)
print(response)
To find out how many apples the shop has now, let's follow the transactions step-by-step:

1- The shop begins with 20 apples.
2- The shop sells 5 apples. We subtract this number from the starting quantity: 20 - 5 = 15
3- The shop receives 8 apples. We add this number to the remaining apples: 15 + 8 = 23
So, after these transactions, the shop has 23 apples.
Explainable AI in Python

Self-consistency

Assesses model's confidence in generated answers

Image showing that a self-consistency prompt is just about sending the same prompt multiple times to the model, each of which having a response.

Explainable AI in Python

Self-consistency in text classification

Useful for text classification tasks

Image showing a self consistency example where the model needs to classify a user review as positive or negative and the model provides multiple responses.

Explainable AI in Python

Self-consistency in text classification

Useful for text classification tasks

Image showing a self consistency example where the model needs to classify a user review as positive or negative and the model provides multiple responses. The confidence is computed as a proportion of each category with respect to the total number of responses.

Explainable AI in Python

Creating self-consistency prompts

prompt = """Classify the following review as positive or negative. 
You should reply with either "positive" or "negative", nothing else.
Review: 'The customer service was great, but the product itself did not meet my expectations.'"""

responses = []
for i in range(5): sentiment = get_response(review)
responses.append(sentiment.lower())
confidence = { 'positive': responses.count('positive') / len(responses), 'negative': responses.count('negative') / len(responses) }
Explainable AI in Python

Creating self-consistency prompts

print(confidence)
{
    'positive': 0.6,
    'negative': 0.4
}
Explainable AI in Python

Let's practice!

Explainable AI in Python

Preparing Video For Download...