Yapay zekâ sohbetlerini yönetme

Amazon Bedrock'a Giriş

Nikhil Rangarajan

Data Scientist

Bedrock'ta sohbet durumu yönetimi

  • Sohbet yönetimi: doğal, tutarlı YZ etkileşimleri için kritik
  • Sohbet bağlamı: geçmiş ve üst verileri içerir
  • Claude modelleri bağlamı dikkate alır

Sohbetin öğelerini gösteren daireler: mesaj, geçmiş ve üst veri.

Amazon Bedrock'a Giriş

Sohbet yönetimini uygulama

  • ConversationManager sınıfı temel sohbet işlevlerini yönetir
  • Mesaj geçmişi, rollerle hem kullanıcı hem asistan mesajlarını saklar
  • Yapılandırılmış yaklaşım ölçeklenebilirlik ve sürdürülebilirlik sağlar
class ConversationManager:

def __init__(self): self.bedrock = boto3.client('bedrock-runtime')
self.conversation_history = [] self.max_tokens = 8000
def add_message(self, role, content): self.conversation_history .append({"role": role,
"content": content
})
Amazon Bedrock'a Giriş

ConversationManager'ı pratikte kullanma

  • Yeni mesajları günlükte saklayın
  • Kullanıcı girdisinin kaydedildiğinden emin olun
  • Modelin bağlamı korumasına yardımcı olun

 

conversation_manager = ConversationManager()
conversation_manager.add_message("user", "What's the weather like?")
Amazon Bedrock'a Giriş

ConversationManager'ı pratikte kullanma

  • Mesaj olarak biçimlendirin
  • Gönderilen metni sınırlayın
  • Biçimlendirilmiş sohbeti modele gönderin
# Send the latest context to the model
conversation_history = conversation_manager.conversation_history
messages = "\n\n".join(
    f"{msg['role']}: {msg['content']}" 
    for msg in conversation_history[-2:]) # Only most recent exchanges


# Send the full message to the model response = conversation_manager.bedrock.invoke_model( modelId="amazon.nova-micro-v1:0", body=messages)
Amazon Bedrock'a Giriş

Hadi pratik yapalım!

Amazon Bedrock'a Giriş

Preparing Video For Download...