Tool use/Function calling using Langchain, AWS Bedrock and Anthropic does not return output

I am trying to get Langchain Bedrock and Anthropic to use a tool/function I have defined in my python file. It does kick off the right tool use and seems to be returning output_tokens count but no output.

My first query does kick off the right tool called get_weather. But returns no output.

My second query does not kick off any tool usage correctly, as expected, and returns answer.

Code:

import os                                                   # Access to env varibales
from dotenv import find_dotenv, load_dotenv                 # Set up to read API keys from .env #install python-dotenv
dotenv_path = find_dotenv()                                 # find_dotenv wil find teh .env up te parent path 
load_dotenv(dotenv_path)                                    # now you have access to os.environ[HUGGINGFACEHUB_API_TOKEN]

import json

import boto3
from langchain_community.llms import Bedrock

aws_region = "us-east-1"
model_id = "anthropic.claude-v2:1"
model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
model_id = "anthropic.claude-3-sonnet-20240229-v1:0"


model = boto3.client(service_name="bedrock-runtime",  region_name="us-east-1")

# Define a custom tool
def get_weather(input: str) -> str:
    # Replace this with your actual tool implementation
    return f"The weather in {input} is sunny and sujata."

# Define the tool schema
tool_schema = {
    "type": "object",
    "properties": {
        "location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}
    },
    "required": ["location"]
}

# Define the tool
tool = [{
    "name": "get_weather",
    "description": "Get the current weather in a given location",
    "input_schema": tool_schema
},]



# Define the tools to use
tools = [
    {"name": "get_weather", "description": "Get the current weather in a given location"},
#    {"name": "get_temperature", "description": "Get the current temperature in a given location"}
]

# Create the request body

def query(prompt):
    body = json.dumps({
        "max_tokens": 256,
        "messages": [{"role": "user", "content": prompt}],
        "anthropic_version": "bedrock-2023-05-31",
        "tools": tool
    })


    # Define the prompt
    response = model.invoke_model(body=body, modelId=model_id)
    
    
    print("------------------------------------")
    print("q: " + prompt)

    # Print the response
    #print(response["body"])
    streaming_body=response["body"]
    # Print the contents of the StreamingBody object
    s= streaming_body.read().decode('utf-8')
    sJson= json.loads(s)
    
    
    #print("a: "+ json.dumps(sJson["content"],indent=4))
    print(json.dumps(sJson,indent=4))#["content"])
    
query("What is the weather like in San Francisco?")
query("What is teh capital of India?")

output:

------------------------------------
q: What is the weather like in San Francisco?
{
    "id": "msg_bdrk_01KCn2u3P2f2WCiQ4yuvkpNL",
    "type": "message",
    "role": "assistant",
    "model": "claude-3-sonnet-20240229",
    "content": [
        {
            "type": "text",
            "text": "Okay, let's get the current weather for San Francisco:"
        },
        {
            "type": "tool_use",
            "id": "toolu_bdrk_01J6xY1kfNA1dVMbkqCpAp3K",
            "name": "get_weather",
            "input": {
                "location": "San Francisco, CA"
            }
        }
    ],
    "stop_reason": "tool_use",
    "stop_sequence": null,
    "usage": {
        "input_tokens": 249,
        "output_tokens": 70
    }
}
------------------------------------
q: What is teh capital of India?
{
    "id": "msg_bdrk_011qdVBM8cAq69Z7w7TnVsMs",
    "type": "message",
    "role": "assistant",
    "model": "claude-3-sonnet-20240229",
    "content": [
        {
            "type": "text",
            "text": "The capital of India is New Delhi."
        }
    ],
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
        "input_tokens": 248,
        "output_tokens": 11
    }
}

New contributor

Sujata Pradhan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật