构建对话

使用 Llama 3

Imtihan Ahmed

Machine Learning Engineer

维护上下文

维护上下文工作流 - 步骤 1

使用 Llama 3

维护上下文

维护上下文工作流 - 步骤 2

使用 Llama 3

维护上下文

维护上下文工作流 - 步骤 3

使用 Llama 3

维护上下文

维护上下文工作流 - 步骤 4

使用 Llama 3

维护上下文

维护上下文工作流 - 步骤 5

  • 使用 Conversation 类跟踪聊天历史
使用 Llama 3

Conversation 类

  • 可保存先前消息的历史
class Conversation:

def __init__(self, llm: Llama, system_prompt='', history=[]): self.llm = llm self.system_prompt = system_prompt
self.history = [{"role": "system", "content": self.system_prompt}] + history
使用 Llama 3

Conversation 类

  • 可保存先前消息的历史
class Conversation:
    def __init__(self, llm: Llama, system_prompt='', history=[]):
        self.llm = llm
        self.system_prompt = system_prompt
        self.history = [{"role": "system", "content": self.system_prompt}] + history

def create_completion(self, user_prompt=''):
self.history.append({"role": "user", "content": user_prompt}) # 追加输入
output = self.llm.create_chat_completion(messages=self.history)
conversation_result = output['choices'][0]['message'] self.history.append(conversation_result) # 追加输出 return conversation_result['content'] # 返回输出
使用 Llama 3

运行多轮对话

conversation = Conversation(llm, system_prompt="You are a virtual travel assistant 
                                                helping with planning trips.")


response1 = conversation.create_completion("What are some destinations in France for a short weekend break?")
print(f"Response 1: {response1}")
response2 = conversation.create_completion("How about Spain?")
print(f"Response 2: {response2}")
使用 Llama 3

运行多轮对话

print(f"Response 1: {response1}")

print(f"Response 2: {response2}")

输出

使用 Llama 3

运行多轮对话

print(f"Response 1: {response1}")

print(f"Response 2: {response2}")

突出显示的输出

使用 Llama 3

Passons à la pratique !

使用 Llama 3

Preparing Video For Download...