分配聊天角色

使用 Llama 3

Imtihan Ahmed

Machine Learning Engineer

定义角色

  • 优化模型语气
  • 示例:客服聊天机器人

与聊天机器人的对话 - 1

使用 Llama 3

定义角色

  • 优化模型语气
  • 示例:客服聊天机器人

与聊天机器人的对话 - 2

使用 Llama 3

定义角色

  • 优化模型语气
  • 示例:客服聊天机器人

与聊天机器人的对话 - 3

使用 Llama 3

在聊天补全中使用角色

  • 使用聊天角色引导 Llama 的回复

Screenshot 2025-02-26 at 15.32.28.png

  • 设定个性与风格

 

Screenshot 2025-02-26 at 15.31.50.png

  • 代表提问者
使用 Llama 3

在聊天补全中使用角色

  • 发送结构化对话

  • create_chat_completion() 函数

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
)
使用 Llama 3

system 角色

  • System 消息:指导模型应如何行为
system_message = "You are a business consultant who gives data-driven answers."

message_list = [{
"role": "system",
"content": system_message
}]
使用 Llama 3

user 角色

  • User 消息:给模型的提问
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 }
]
使用 Llama 3

生成回复

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': [...], ...}
使用 Llama 3

assistant 角色

response["choices"][0]

响应输出

使用 Llama 3

assistant 角色

response["choices"][0]

高亮消息的响应输出

使用 Llama 3

assistant 角色

response["choices"][0]

高亮角色的响应输出

使用 Llama 3

assistant 角色

response["choices"][0]

高亮内容的响应输出

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

Passons à la pratique !

使用 Llama 3

Preparing Video For Download...