dspy icon indicating copy to clipboard operation
dspy copied to clipboard

Bedrock LM produces extraneous key error in unclear circumstances

Open JamesScharf opened this issue 1 year ago • 14 comments

AWS's Bedrock runtime does not accept the following parameters, however they are still being passed to the LM at some point: n and max_tokens

At what point is this being done? I haven't specified either value in my code which uses DSPy.

Here's the full error from boto3:

ValidationException: An error occurred (ValidationException) when calling the InvokeModel operation: Malformed input request: #: extraneous key [max_tokens] is not permitted#: extraneous key [n] is not permitted, please reformat your input and try again.

JamesScharf avatar Feb 09 '24 20:02 JamesScharf

I am also getting the same exact error when trying to use "Getting Started" example notebook. Here is my code

bedrock = dspy.Bedrock(model='anthropic.claude-v2', region_name="us-west-2") colbertv2_wiki17_abstracts = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts') dspy.settings.configure(lm=bedrock, rm=colbertv2_wiki17_abstracts) Evaluation `from dspy.evaluate.evaluate import Evaluate

evaluate_on_hotpotqa = Evaluate(devset=devset, num_threads=1, display_progress=True, display_table=5)

metric = dspy.evaluate.answer_exact_match evaluate_on_hotpotqa(compiled_rag, metric=metric)`

tech4life87 avatar Feb 09 '24 21:02 tech4life87

I see. Does Bedrock support something else in place of n and max_tokens? Both are necessary in DSPy.

i.e., num_completions and maximum_output_tokens

If the former isn't supported, we can do it with a loop... but that's more expensive.

okhat avatar Feb 09 '24 21:02 okhat

Anyway can fix now by just ignoring n and max_tokens in the request function for Bedrock @JamesScharf if they can't be replaced.

Otherwise, can fix by replacing these values on the fly with the correct kwarg names

okhat avatar Feb 09 '24 21:02 okhat

Re: n, I don't believe that Bedrock has any equivalent. So, we'd have to loop I think. The equivalent to max_new_tokens is max_tokens_to_sample; there is no equivalent for max_tokens

JamesScharf avatar Feb 09 '24 21:02 JamesScharf

@okhat It depends with which model one is using with Bedrock. If using Claude then as @JamesScharf correctly points out ismax_tokens_to_sample if using Titan Models it is maxTokenCount . I am kind of curious if using something like LiteLLM or langChain would abstract these model/provider specific nuances.

tech4life87 avatar Feb 10 '24 01:02 tech4life87

I'd be hesitant to use LiteLLM for this abstraction. I've used its Sagemaker wrapper and it did not seem to support temperature.

JamesScharf avatar Feb 10 '24 01:02 JamesScharf

I'll make a PR with the following updates to Bedrock--let me know if I'm missing anything:

  • Raise not supported error if user tries to use non-Claude models [for now]
  • Implement n as a loop since n is not currently supported by Bedrock (to my knowledge)
  • Remap max_tokens to max_tokens_to_sample

JamesScharf avatar Feb 14 '24 16:02 JamesScharf

Bedrock's parameters are provider specific and Langchain makes no attempt to unify them (which will anyways be a bad idea). So I am not sure how much introduction of Langchain help.

shabie avatar Feb 18 '24 22:02 shabie

Hello @JamesScharf,

The issue persists for me. As a workaround, I implemented a removal of the n key from kwargs in the following manner: if "n" in kwargs: if self._batch_n: del kwargs["n"] llm_out = self._simple_api_call(formatted_prompt=formatted_prompt, **kwargs) is that correct?

aazizisoufiane avatar Mar 10 '24 13:03 aazizisoufiane

Good catch, would you mind making a PR? I am away from my computer for a few days and can't do so myself.

JamesScharf avatar Mar 10 '24 16:03 JamesScharf

I'm encountering a permission issues for the PR

aazizisoufiane avatar Mar 11 '24 16:03 aazizisoufiane

@JamesScharf I have a new error with bedrock botocore.errorfactory.ValidationException: An error occurred (ValidationException) when calling the InvokeModel operation: max_tokens_to_sample: range: 1..1,000,000

aazizisoufiane avatar Mar 27 '24 11:03 aazizisoufiane

@tech4life87 I got the same error using Litellm: openai.BadRequestError: Error code: 400 - {'error': {'message': 'BedrockException - BedrockException - An error occurred (ValidationException) when calling the InvokeModel operation: Malformed input request: #: extraneous key [drop_params] is not permitted, please reformat your input and try again.', 'type': None, 'param': None, 'code': 400}}

aazizisoufiane avatar Mar 27 '24 12:03 aazizisoufiane

I have a PR #772 that tackles this issue. Almost done...

drawal1 avatar Apr 05 '24 14:04 drawal1