Assigning chat roles

Working with Llama 3

Imtihan Ahmed

Machine Learning Engineer

Defining roles

  • Refining model's tone
  • Example: customer support chatbot

Conversation with a chatbot - 1

Working with Llama 3

Defining roles

  • Refining model's tone
  • Example: customer support chatbot

Conversation with a chatbot - 2

Working with Llama 3

Defining roles

  • Refining model's tone
  • Example: customer support chatbot

Conversation with a chatbot - 3

Working with Llama 3

Using roles in chat completion

  • Chat roles to guide Llama's responses

Screenshot 2025-02-26 at 15.32.28.png

  • Sets the personality and style

 

Screenshot 2025-02-26 at 15.31.50.png

  • Represents the person asking the question
Working with Llama 3

Using roles in chat completion

  • Sending a structured conversation

  • create_chat_completion() function

from llama_cpp import Llama

llm = Llama(model_path="path/to/model.gguf")

message_list = [...] # This list includes roles
response = llm.create_chat_completion(
messages = message_list
)
Working with Llama 3

The system role

  • System message: instructions about how model should behave
system_message = "You are a business consultant who gives data-driven answers."

message_list = [{
"role": "system",
"content": system_message
}]
Working with Llama 3

The user role

  • User message: the prompt being asked to the model
system_message = "You are a business consultant who gives data-driven answers."

user_message = "What are the key factors in a successful marketing strategy?"
message_list = [{"role": "system", "content": system_message},
{ "role": "user", "content": user_message }
]
Working with Llama 3

Generating the response

from llama_cpp import Llama
llm = Llama(model_path="path/to/model.gguf")

system_message = "You are a business consultant who gives data-driven answers."
user_message = "What are the key factors in a successful marketing strategy?"

message_list = [{"role": "system", "content": system_message},
               {"role": "user", "content": user_message}]

response = llm.create_chat_completion(messages = message_list) print(response)
{'id': ..., 'object': ..., 'created': ..., 'model': ..., 'choices': [...], ...}
Working with Llama 3

The assistant role

response["choices"][0]

Response output

Working with Llama 3

The assistant role

response["choices"][0]

Response output with message highlighted

Working with Llama 3

The assistant role

response["choices"][0]

Response output with role highlighted

Working with Llama 3

The assistant role

response["choices"][0]

Response output with content highlighted

result['choices'][0]['message']['content']
'A successful marketing strategy relies on clear objectives, established 
through specific, measurable goals. Understanding the target audience ...'
Working with Llama 3

Let's practice!

Working with Llama 3

Preparing Video For Download...