Xây dựng hội thoại

Làm việc với Llama 3

Imtihan Ahmed

Machine Learning Engineer

Duy trì ngữ cảnh

Quy trình duy trì ngữ cảnh - bước 1

Làm việc với Llama 3

Duy trì ngữ cảnh

Quy trình duy trì ngữ cảnh - bước 2

Làm việc với Llama 3

Duy trì ngữ cảnh

Quy trình duy trì ngữ cảnh - bước 3

Làm việc với Llama 3

Duy trì ngữ cảnh

Quy trình duy trì ngữ cảnh - bước 4

Làm việc với Llama 3

Duy trì ngữ cảnh

Quy trình duy trì ngữ cảnh - bước 5

  • Theo dõi lịch sử chat với lớp Conversation
Làm việc với Llama 3

Lớp Conversation

  • Có thể lưu lịch sử tin nhắn trước đó
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
Làm việc với Llama 3

Lớp Conversation

  • Có thể lưu lịch sử tin nhắn trước đó
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}) # Thêm đầu vào
output = self.llm.create_chat_completion(messages=self.history)
conversation_result = output['choices'][0]['message'] self.history.append(conversation_result) # Thêm đầu ra return conversation_result['content'] # Trả về đầu ra
Làm việc với Llama 3

Chạy hội thoại nhiều lượt

conversation = Conversation(llm, system_prompt="Bạn là một trợ lý du lịch ảo 
                                                hỗ trợ lập kế hoạch chuyến đi.")


response1 = conversation.create_completion("Có điểm đến nào ở Pháp cho kỳ nghỉ cuối tuần ngắn không?")
print(f"Response 1: {response1}")
response2 = conversation.create_completion("Còn Tây Ban Nha thì sao?")
print(f"Response 2: {response2}")
Làm việc với Llama 3

Chạy hội thoại nhiều lượt

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

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

Đầu ra

Làm việc với Llama 3

Chạy hội thoại nhiều lượt

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

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

Đầu ra với phần tô sáng

Làm việc với Llama 3

Ayo berlatih!

Làm việc với Llama 3

Preparing Video For Download...