Closed MeghaWalia-eco closed 3 months ago
llama_index==0.10.30 llama-index-core==0.10.57 langchain==0.0.312 tiktoken python-dotenv fastapi==0.98.0 loguru pymongo==4.6.1 motor bs4 newrelic==8.8.1 okta_jwt_verifier sqlalchemy==2.0.25 pgvector>=0.2.4,<0.3.0 # Adjusted for compatibility psycopg2-binary==2.9.9 asyncpg==0.29.0 tenacity==8.2.3 opensearch-py==2.5.0 greenlet==2.0.2 uvicorn==0.24.0.post1 nltk==3.8.1 xmltodict==0.13.0 llama-hub==0.0.12 boto3==1.34.122 certifi==2023.7.22 alembic==1.11.3 python-jose==3.3.0 redis cachetools==5.3.2 unstructured==0.10.19
llama-index-llms-openai==0.1.28 llama-index-agent-openai llama-index-vector-stores-postgres==0.1.5 llama-index-vector-stores-opensearch==0.1.11 llama-index-embeddings-text-embeddings-inference==0.1.2 llama-index-storage-docstore-mongodb llama-index-readers-s3 llama-index-storage-index-store-mongodb llama-index-storage-kvstore-mongodb==0.1.2 llama-index-embeddings-openai llama-index-llms-bedrock-converse==0.1.6 PyJWT==2.0.0
pytest==7.2.1 pytest-asyncio==0.23.7 parameterized==0.9.0 aioresponses==0.7.6 pre-commit sagemaker==2.223.0
aws-cdk-lib==2.146.0 constructs>=10.0.0,<11.0.0 setuptools==70.2.0
To resolve the "Invalid payload! Need prompt and completion or messages and response" error when using the BedrockConverse
LLM in your dev environment, ensure that you are providing the required parameters correctly. The error indicates that either the prompt
and completion
or messages
and response
parameters are missing or incorrectly formatted.
Here is an example of how to correctly use the complete
method:
complete
MethodEnsure you provide the prompt
parameter:
from llama_index.llms.bedrock_converse import BedrockConverse
llm = BedrockConverse(
model="anthropic.claude-3-5-sonnet-20240620-v1:0",
region_name="us-east-1",
timeout=60,
callback_manager=self.__rag_svc.get_llm_callback_manager(
handlers=[llm_debug_callback, llm_counter_callback]
)
)
resp = llm.complete(prompt="The Economist Style guide is below: ...")
print(resp)
The specific differences in the configuration or environment between the local and dev environments that could affect the BedrockConverse
LLM's behavior include:
AWS Credentials:
aws_access_key_id
aws_secret_access_key
aws_session_token
AWS Region:
region_name
AWS Profile:
profile_name
Botocore Session and Configuration:
botocore_session
botocore_config
Timeout and Retries:
timeout
max_retries
Additional Configuration:
additional_kwargs
Enable additional logging to get more insight into the error:
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
Additionally, you can enable the LlamaLogger
in the service context to get detailed logs.
By ensuring the correct parameters are provided and checking the configuration differences, you should be able to resolve the "Invalid payload" error [1][2][3].
To continue talking to Dosu, mention @dosu.
O i think you are right , but llm.complete has first parameter as prompt def complete( self, prompt: str, formatted: bool = False, **kwargs: Any ) -> CompletionResponse:
i don't think we need to pass in explicipely. config is absolutely in sync, please grill down a bit more, i don't think it is the basic one
To further investigate the "Invalid payload! Need prompt and completion or messages and response" error, let's focus on the converse_with_retry
function and ensure the payload is correctly formed before making the API call.
Add debugging statements to verify the payload content before invoking the converse_with_retry
function. This will help identify if the payload is correctly formed:
def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
converse_messages, system_prompt = messages_to_converse_messages(messages)
if len(system_prompt) > 0 or self.system_prompt is None:
self.system_prompt = system_prompt
all_kwargs = self._get_all_kwargs(**kwargs)
# Debugging statement
print(f"Payload: messages={converse_messages}, system_prompt={self.system_prompt}, kwargs={all_kwargs}")
response = converse_with_retry(
client=self._client,
messages=converse_messages,
system_prompt=self.system_prompt,
max_retries=self.max_retries,
stream=False,
**all_kwargs,
)
...
The converse_with_retry
function requires a specific payload structure. Ensure the payload includes the necessary fields:
modelId
: The model identifier.messages
: A sequence of dictionaries, each representing a message with a specific role and content.inferenceConfig
: A dictionary containing:
maxTokens
: The maximum number of tokens.temperature
: The temperature setting for the inference.system
(optional): A list containing a dictionary with the system prompt text.toolConfig
(optional): The tool configuration if provided in kwargs
.Here is the relevant code snippet for converse_with_retry
:
def converse_with_retry(
client: Any,
model: str,
messages: Sequence[Dict[str, Any]],
max_retries: int = 3,
system_prompt: Optional[str] = None,
max_tokens: int = 1000,
temperature: float = 0.1,
stream: bool = False,
**kwargs: Any,
) -> Any:
"""Use tenacity to retry the completion call."""
retry_decorator = _create_retry_decorator(client=client, max_retries=max_retries)
converse_kwargs = {
"modelId": model,
"messages": messages,
"inferenceConfig": {
"maxTokens": max_tokens,
"temperature": temperature,
},
}
if system_prompt:
converse_kwargs["system"] = [{"text": system_prompt}]
if tool_config := kwargs.get("tools"):
converse_kwargs["toolConfig"] = tool_config
converse_kwargs = join_two_dicts(
converse_kwargs, {k: v for k, v in kwargs.items() if k != "tools"}
)
@retry_decorator
def _conversion_with_retry(**kwargs: Any) -> Any:
if stream:
return client.converse_stream(**kwargs)
return client.converse(**kwargs)
return _conversion_with_retry(**converse_kwargs)
Ensure that the environment configurations are consistent between local and dev environments, especially the following:
AWS Credentials and Session Tokens:
aws_access_key_id
aws_secret_access_key
aws_session_token
profile_name
AWS Region:
region_name
Botocore Session and Configuration:
botocore_session
botocore_config
Timeout and Retries:
timeout
max_retries
By ensuring the correct payload structure and consistent environment configurations, you should be able to resolve the "Invalid payload" error [1][2].
To continue talking to Dosu, mention @dosu.
as i alrready mentioned i debugged it locally it is working fine, i cannot apply debugger on dev as packages are installed at runtime there
what permissions shall i add in ecs container to access bedrock converse api, right now i have added below ones for k in sm_secrets: sm_secrets[k].grant_read(task_definition.task_role)
content_drop_bucket.grant_read(task_definition.task_role)
bedrock_policy = iam.PolicyStatement(
actions=[
"bedrock:InvokeEndpoint",
"bedrock:ListEndpoints"
],
resources=["*"]
)
task_definition.add_to_task_role_policy(bedrock_policy)
To ensure your ECS container can access the Bedrock Converse API correctly, you need to configure the following permissions and settings:
AWS Credentials: Ensure that your ECS task has the necessary AWS credentials to access the Bedrock Converse API. This can be done by providing the aws_access_key_id
, aws_secret_access_key
, and optionally aws_session_token
in your configuration. Alternatively, you can use an IAM role with the necessary permissions.
IAM Role: Attach an IAM role to your ECS task with the necessary permissions to access the Bedrock Converse API. The role should have policies that allow actions such as bedrock:InvokeModel
.
Region Configuration: Ensure that the region_name
is correctly set to the AWS region where the Bedrock Converse API is available.
Network Configuration: Ensure that your ECS task is running in a VPC with the necessary network configurations to access the Bedrock Converse API endpoints.
Here is an example of how you might configure the BedrockConverse
class with the necessary AWS credentials and region:
from llama_index.llms.bedrock_converse import BedrockConverse
llm = BedrockConverse(
model="anthropic.claude-3-5-sonnet-20240620-v1:0",
aws_access_key_id="YOUR_AWS_ACCESS_KEY_ID",
aws_secret_access_key="YOUR_AWS_SECRET_ACCESS_KEY",
aws_session_token="YOUR_AWS_SESSION_TOKEN", # Optional
region_name="us-east-1",
)
resp = llm.complete("Paul Graham is ")
print(resp)
Additionally, ensure that your ECS task definition includes the necessary IAM role and network configurations. Here is an example of an IAM policy that you might attach to the role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel"
],
"Resource": "*"
}
]
}
By ensuring these configurations and permissions, your ECS container should be able to access the Bedrock Converse API correctly [1].
To continue talking to Dosu, mention @dosu.
do you think Error in summarization Invalid payload! Need prompt and completion or messages and response.this error can anyhow be related to ecs container not able to access converse api, my prompt structure is right as bedrock convers api itself convers the prompt into correct format
Yes, the error "Invalid payload! Need prompt and completion or messages and response" could be related to the ECS container's inability to access the Bedrock Converse API. To resolve this issue, ensure the following:
IAM Role and Permissions:
bedrock:InvokeModel
action. Here is an example policy:{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel"
],
"Resource": "*"
}
]
}
Network Configuration:
Environment Variables:
AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
, AWS_SESSION_TOKEN
) and region (AWS_REGION
) are correctly set in the ECS task definition or provided through the IAM role.By ensuring these configurations, the ECS container should be able to access the Bedrock Converse API without issues [1].
To continue talking to Dosu, mention @dosu.
Ok my ecs task is running in a vpc and i want to access bedrock converse api from that ecs task, tell me what configurations do i need to change in addition to above one
To access the Bedrock Converse API from an ECS task running in a VPC, ensure the following configurations are in place:
IAM Role and Policies:
bedrock:InvokeModel
and any other required actions.{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel"
],
"Resource": "*"
}
]
}
VPC Configuration:
Endpoint Configuration:
AWS Credentials and Region:
AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
, AWS_SESSION_TOKEN
) and region (AWS_REGION
) are correctly set in the ECS task definition or provided through the IAM role.Here is an example of how you might configure the BedrockConverse
class to use specific AWS credentials and region:
from llama_index.llms.bedrock_converse import BedrockConverse
llm = BedrockConverse(
model="anthropic.claude-3-5-sonnet-20240620-v1:0",
aws_access_key_id="YOUR_AWS_ACCESS_KEY_ID",
aws_secret_access_key="YOUR_AWS_SECRET_ACCESS_KEY",
aws_session_token="YOUR_AWS_SESSION_TOKEN", # Optional
region_name="us-east-1",
)
resp = llm.complete("Paul Graham is ")
print(resp)
By ensuring these configurations, your ECS container should be able to access the Bedrock Converse API correctly [1].
To continue talking to Dosu, mention @dosu.
Going to close this out, the issue was that the token counter was masking the real error
Removing the token counter would have shown the real error
This is fixed in latest versions (releasing the fix soon)
Bug Description
I am trying to use bedrockConverse LLM and use it to generate the summary in following way
same prompt is working fine on my local, But as soon as i move to dev environment it is giving me error Invalid payload! Need prompt and completion or messages and response
my ecs is present under vpc which is in eu-west-2 but the model i am using is anthropic.claude-3-5-sonnet-20240620-v1:0 which is in us-east-1, but the error message does not signify this is the issue
prompt used is
Version
0.1.30
Steps to Reproduce
mentioned above
Relevant Logs/Tracbacks
No response