Керування розмовами ШІ

Вступ до Amazon Bedrock

Nikhil Rangarajan

Data Scientist

Керування станом розмови в Bedrock

  • Керування розмовами: ключове для природної, зв'язної взаємодії з ШІ
  • Контекст розмови: включає історію та метадані
  • Моделі Claude враховують контекст

Ілюстрація з колами, що показують елементи розмови: повідомлення, історію та метадані.

Вступ до Amazon Bedrock

Реалізація керування розмовою

  • Клас ConversationManager відповідає за основні функції розмови
  • Історія повідомлень зберігає і користувацькі, і асистентські ролі
  • Структурований підхід гарантує масштабованість і підтримуваність
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

Використання ConversationManager на практиці

  • Add new messages to the conversation log
  • Ensure the user's input is logged
  • Help the model maintain context

 

conversation_manager = ConversationManager()
conversation_manager.add_message("user", "What's the weather like?")
Вступ до Amazon Bedrock

Використання ConversationManager на практиці

  • Format as message
  • Limit the amount of text sent
  • Send formatted conversation to the model
# 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

Давайте потренуємось!

Вступ до Amazon Bedrock

Preparing Video For Download...