aws-samples / amazon-bedrock-samples

This repository contains examples for customers to get started using the Amazon Bedrock Service. This contains examples for all available foundational models
https://aws.amazon.com/bedrock/
MIT No Attribution
358 stars 183 forks source link

Adding Converse API notebooks #177

Closed rodzanto closed 1 month ago

rodzanto commented 1 month ago

Issue #, if available: None.

Description of changes: Adding 2 notebooks for the Converse API in Bedrock, one in the 'introduction-to-bedrock' folder, and another in the 'function-calling' folder.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

w601sxs commented 1 month ago
  1. Intro notebook works
  2. Can we simplify this:
    tools_list_obj = ToolsList()
    function_calling = ''
    tool_args = {}
    for c in output['output']['message']['content']:
        if 'toolUse' in c:
            function_calling = c['toolUse']

    #Check if function calling is triggered:
    if (function_calling):
2. Error in the function calling example, full trace

```
---------------------------------------------------------------------------

ParamValidationError Traceback (most recent call last) Cell In[10], line 7 1 prompts = [ 2 "What is the weather like in Queens, NY?", 3 "What is the capital of France?" 4 ] 6 for prompt in prompts: ----> 7 converse( 8 system = "You're provided with a tool that can get the weather information for a specific locations 'get_weather'; \ 9 only use the tool if required. Don't make reference to the tools in your final answer.", 10 prompt = prompt 11 )

Cell In[9], line 18, in converse(prompt, system) 15 print(f"\n{datetime.now().strftime('%H:%M:%S')} - Initial prompt:\n{json.dumps(messages, indent=2)}") 17 #Invoke the model the first time: ---> 18 output = converse_with_tools(messages, system) 19 print(f"\n{datetime.now().strftime('%H:%M:%S')} - Output so far:\n{json.dumps(output['output'], indent=2)}") 21 #Add the intermediate output to the prompt:

Cell In[7], line 3, in converse_with_tools(messages, system, toolConfig) 2 def converse_with_tools(messages, system='', toolConfig=toolConfig): ----> 3 response = bedrock.converse( 4 modelId=modelId, 5 system=system, 6 messages=messages, 7 toolConfig=toolConfig 8 ) 9 return response

File /opt/conda/lib/python3.10/site-packages/botocore/client.py:565, in ClientCreator._create_api_method.._api_call(self, *args, **kwargs) 561 raise TypeError( 562 f"{py_operation_name}() only accepts keyword arguments." 563 ) 564 # The "self" in this scope is referring to the BaseClient. --> 565 return self._make_api_call(operation_name, kwargs)

File /opt/conda/lib/python3.10/site-packages/botocore/client.py:974, in BaseClient._make_api_call(self, operation_name, api_params) 970 if properties: 971 # Pass arbitrary endpoint info with the Request 972 # for use during construction. 973 request_context['endpoint_properties'] = properties --> 974 request_dict = self._convert_to_request_dict( 975 api_params=api_params, 976 operation_model=operation_model, 977 endpoint_url=endpoint_url, 978 context=request_context, 979 headers=additional_headers, 980 ) 981 resolve_checksum_context(request_dict, operation_model, api_params) 983 service_id = self._service_model.service_id.hyphenize()

File /opt/conda/lib/python3.10/site-packages/botocore/client.py:1048, in BaseClient._convert_to_request_dict(self, api_params, operation_model, endpoint_url, context, headers, set_user_agent_header) 1039 def _convert_to_request_dict( 1040 self, 1041 api_params, (...) 1046 set_user_agent_header=True, 1047 ): -> 1048 request_dict = self._serializer.serialize_to_request( 1049 api_params, operation_model 1050 ) 1051 if not self._client_config.inject_host_prefix: 1052 request_dict.pop('host_prefix', None)

File /opt/conda/lib/python3.10/site-packages/botocore/validate.py:381, in ParamValidationDecorator.serialize_to_request(self, parameters, operation_model) 377 report = self._param_validator.validate( 378 parameters, operation_model.input_shape 379 ) 380 if report.has_errors(): --> 381 raise ParamValidationError(report=report.generate_report()) 382 return self._serializer.serialize_to_request( 383 parameters, operation_model 384 )

ParamValidationError: Parameter validation failed: Invalid type for parameter system, value: You're provided with a tool that can get the weather information for a specific locations 'get_weather'; only use the tool if required. Don't make reference to the tools in your final answer., type: <class 'str'>, valid types: <class 'list'>, <class 'tuple'>



updated boto3 and botocore to latest as of now. @markproy @rodzanto 
w601sxs commented 1 month ago

works without passing in the system message FYI

system_prompts = [{"text" : system_text}] ?