Closed
Description
Did you check docs and existing issues?
- I have read all the NeMo-Guardrails docs
- I have updated the package to the latest version before submitting this issue
- (optional) I have used the develop branch
- I have searched the existing issues of NeMo-Guardrails
Python version (python --version)
Python 3.13.1
Operating system/version
windows 11
NeMo-Guardrails version (if you must use a specific version and not the latest
0.11.0
Describe the bug
Im currently using claude model via amazon_bedrock engine for nemo but facing the max_tokens issue
below is my config.yml
models:
- type: main
engine: amazon_bedrock
parameters:
streaming: true
model_id: anthropic.claude-3-5-sonnet-20240620-v1:0
model_kwargs:
temperature: 0.2
top_k: 250
top_p: 0.5
rails:
input:
flows:
- self check input
dialog:
enabled: false
single_call:
enabled: false
output:
enabled: true
flows:
- self check output
Below is my main code
import logging
import sys
import os
import boto3
import nest_asyncio
from nemoguardrails import RailsConfig
from nemoguardrails.integrations.langchain.runnable_rails import RunnableRails
class GuardrailedMobileServiceAgent:
def __init__(self, config_path: str = "./config"):
"""
Initialize the guardrailed mobile service agent
Args:
config_path: Path to the NeMo Guardrails config directory
"""
# Set up logging
self.logger = self._setup_logging()
self.logger.info("Initializing GuardrailedMobileServiceAgent")
# Apply nest_asyncio to allow async operations in Jupyter/interactive environments
nest_asyncio.apply()
# Initialize the Bedrock client
self.bedrock_client = boto3.client('bedrock', region_name='us-east-1')
# Load the guardrails configuration
self.config = RailsConfig.from_path(config_path)
# Create the guardrails wrapper using the configuration
self.guardrails = RunnableRails(config=self.config, input_key="input", output_key="answer")
# Initialize conversation history
self.conversation_history = []
def _setup_logging(self) -> logging.Logger:
"""Set up logging configuration"""
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('guardrail.log'),
logging.StreamHandler()
]
)
return logging.getLogger(__name__)
def _create_prompt(self, user_input: str) -> str:
"""Create a prompt for the LLM with context about current state"""
return f"User: {user_input}\nAssistant:"
def _serialize_message(self, msg):
"""Serialize a message for the conversation history"""
if isinstance(msg, dict):
return msg
else:
return {"role": "user" if msg.startswith("Human:") else "assistant",
"content": msg.replace("Human: ", "").replace("Assistant: ", "")}
def chat(self, user_input: str) -> str:
"""
Process user input with guardrails
Args:
user_input: The user's input message
Returns:
str: The agent's response
"""
try:
self.logger.info(f"Received user input: {user_input}")
# Create the prompt
prompt = self._create_prompt(user_input)
# Serialize the conversation history
chat_history = [self._serialize_message(msg) for msg in self.conversation_history]
# Apply guardrails
response = self.guardrails.invoke({
"chat_history": chat_history,
"input": user_input,
"context": [] # Add empty context to satisfy the requirement
})
# Extract the answer
answer = response.get("answer", "I'm sorry, I couldn't process your request.")
# Update conversation history
self.conversation_history.append({"role": "user", "content": user_input})
self.conversation_history.append({"role": "assistant", "content": answer})
self.logger.info(f"Generated response: {answer}")
return answer
except Exception as e:
self.logger.error(f"Error in chat: {str(e)}")
return f"I apologize, but I encountered an error. Please try again. Error: {str(e)}"
def reset_conversation(self):
"""Reset the conversation history"""
self.conversation_history = []
if __name__ == "__main__":
agent = GuardrailedMobileServiceAgent()
while True:
user_input = input("You: ")
if user_input.lower() in ["exit", "quit", "bye"]:
break
response = agent.chat(user_input)
print(f"Agent: {response}")
Steps To Reproduce
The config and main file are given should be able to reproduce it using aws profile
Expected Behavior
Should be able to initialize the rails using the claude models via bedrock
Actual Behavior
below is the error that im getting