使用 Wolfram Alpha LLM API 作为 Claude 的工具
在本教程中,我们将展示如何将 Wolfram Alpha LLM API 集成作为 Claude 的工具。Claude 将能够向 Wolfram Alpha API 发送查询并接收计算出的响应,然后利用这些响应来回答用户的问题。
步骤 1:设置环境
首先,让我们安装所需的库并设置 Anthropic API 客户端。我们还需要设置 APP ID 以使用 WolframAlpha。您可以 在此处 免费注册并为此项目创建一个新的 App ID。
from anthropic import Anthropic
import requests
import urllib.parse
import json
client = Anthropic()
# 将 'YOUR_APP_ID' 替换为您的实际 Wolfram Alpha AppID
WOLFRAM_APP_ID = 'YOUR_APP_ID'
步骤 2:定义 Wolfram Alpha LLM API 工具
我们将定义一个工具,允许 Claude 向 Wolfram Alpha LLM API 发送查询并接收计算出的响应。
def wolfram_alpha_query(query):
# 对查询进行 URL 编码
encoded_query = urllib.parse.quote(query)
# 向 Wolfram Alpha LLM API 发送请求
url = f'https://www.wolframalpha.com/api/v1/llm-api?input={encoded_query}&appid={WOLFRAM_APP_ID}'
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
return f"Error: {response.status_code}: {response.text}"
tools = [
{
"name": "wolfram_alpha",
"description": "一个允许查询 Wolfram Alpha 知识库的工具。适用于数学计算、科学数据和常识性问题。",
"input_schema": {
"type": "object",
"properties": {
"search_query": {
"type": "string",
"description": "要发送到 Wolfram Alpha API 的查询。"
}
},
"required": ["query"]
}
}
]
在此代码中,我们定义了一个 wolfram_alpha_query
函数,该函数接受查询作为输入,对其进行 URL 编码,并使用提供的 AppID 向 Wolfram Alpha LLM API 发送请求。如果请求成功,该函数将返回 API 的计算响应,如果出现问题,则返回错误消息。
然后,我们定义了 wolfram_alpha
工具,其输入模式期望一个字符串类型的查询属性。
步骤 3:与 Claude 互动
现在,让我们看看 Claude 如何与 Wolfram Alpha 工具互动以回答用户问题。
def process_tool_call(tool_name, tool_input):
if tool_name == "wolfram_alpha":
return wolfram_alpha_query(tool_input["search_query"])
def chat_with_claude(user_message):
print(f"\n{'='*50}\nUser Message: {user_message}\n{'='*50}")
prompt = f"""这是个问题:{user_message}。请使用 Wolfram Alpha 工具来回答它。请不要在您的回答中反映返回的搜索结果的质量。"""
message = client.beta.tools.messages.create(
model=MODEL_NAME,
max_tokens=4096,
tools=tools,
messages=[{"role": "user", "content": prompt}]
)
print(f"\nInitial Response:")
print(f"Stop Reason: {message.stop_reason}")
print(f"Content: {message.content}")
if message.stop_reason == "tool_use":
tool_use = next(block for block in message.content if block.type == "tool_use")
tool_name = tool_use.name
tool_input = tool_use.input
print(f"\nTool Used: {tool_name}")
print(f"Tool Input:")
print(json.dumps(tool_input, indent=2))
tool_result = process_tool_call(tool_name, tool_input)
print(f"\nTool Result:")
print(str(json.dumps(tool_result, indent=2)))
response = client.beta.tools.messages.create(
model=MODEL_NAME,
max_tokens=2000,
tools=tools,
messages=[
{"role": "user", "content": prompt},
{"role": "assistant", "content": message.content},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": tool_use.id,
"content": str(tool_result)
}
]
}
]
)
print(f"\nResponse:")
print(f"Stop Reason: {response.stop_reason}")
print(f"Content: {response.content}")
else:
response = message
final_response = None
for block in response.content:
if hasattr(block, 'text'):
final_response = block.text
break
print(f"\nFinal Response: {final_response}")
return final_response
步骤 4:试一试!
现在我们已经让 Claude 能够访问 Wolfram Alpha 了,让我们尝试给它一些示例问题。
# 示例用法
print(chat_with_claude("What are the 5 largest countries in the world by population?"))
print(chat_with_claude("Calculate the square root of 1764."))
print(chat_with_claude("What is the distance between Earth and Mars?"))
==================================================
User Message: What are the 5 largest countries in the world by population?
==================================================
Initial Response:
Stop Reason: tool_use
Content: [ContentBlock(text='<thinking>\nThe query "What are the 5 largest countries in the world by population?" can be answered well using the wolfram_alpha tool, which has knowledge about countries and populations. The search_query parameter is the only required parameter, and the query text provided by the user can be used directly as the search_query value without any additional information needed. \n</thinking>', type='text'), ContentBlockToolUse(id='toolu_01VCQ5xAzMNdyXYsepbSAJLY', input={'search_query': 'What are the 5 largest countries in the world by population?'}, name='wolfram_alpha', type='tool_use')]
Tool Used: wolfram_alpha
Tool Input:
{
"search_query": "What are the 5 largest countries in the world by population?"
}
Tool Result:
"Query:\n\"What are the 5 largest countries in the world by population?\"\n\nInput interpretation:\n5 largest countries | by population\nin all countries, dependencies, and territories\n\nResult:\n1 | India | 1.43 billion people | \n2 | China | 1.43 billion people | \n3 | United States | 340 million people | \n4 | Indonesia | 278 million people | \n5 | Pakistan | 241 million people | \n\nNames:\n | full name | full native name\nIndia | Republic of India | Bh\u0101rat\u012bya Ga\u1e47ar\u0101jya\nChina | People's Republic of China | Zhonghua Renmin Gongheguo\nUnited States | United States of America | \nIndonesia | Republic of Indonesia | Republik Indonesia\nPakistan | Islamic Republic of Pakistan | Isl\u0101m\u012b Jumh\u016br\u012b-ye P\u0101kist\u0101n\n\nFlags:\nimage: https://www6b3.wolframalpha.com/Calculate/MSP/MSP2444241bhh00e1he97fd0000383004g53722f9bd?MSPStoreType=image/png&s=14\nWolfram Language code: Dataset[EntityValue[{Entity[\"Country\", \"India\"], Entity[\"Country\", \"China\"], Entity[\"Country\", \"UnitedStates\"], Entity[\"Country\", \"Indonesia\"], Entity[\"Country\", \"Pakistan\"]}, EntityProperty[\"Country\", \"Flag\"], \"EntityAssociation\"]]\n\nLocations:\nimage: https://www6b3.wolframalpha.com/Calculate/MSP/MSP2445241bhh00e1he97fd00002684a23aaahea47d?MSPStoreType=image/png&s=14\n\nGeographic properties:\ntotal area | total | 9.736 million mi^2 (square miles)\n | largest | 3.719 million mi^2 (square miles) (world rank: 3rd) (United States)\n | smallest | 307374 mi^2 (square miles) (world rank: 36th) (Pakistan)\nland area | total | 9.283 million mi^2 (square miles)\n | largest | 3.601 million mi^2 (square miles) (world rank: 2nd) (China)\n | smallest | 297637 mi^2 (square miles) (world rank: 36th) (Pakistan)\ncontinent | all | Asia | North America\n\nDemographics:\npopulation | total | 3.71 billion people\n | highest | 1.43 billion people (world rank: 1st) (2023 estimate) (India)\n | lowest | 241 million people (world rank: 5th) (2023 estimate) (Pakistan)\npopulation density | average | 390 people/mi^2\n | median | 385 people/mi^2\n | highest | 1226 people/mi^2 (world rank: 31st) (2021 estimate) (India)\n | lowest | 95.4 people/mi^2 (world rank: 183rd) (2021 estimate) (United States)\npopulation growth | average | 0.455 %/yr\n | median | 0.631 %/yr\n | highest | 1.83 %/yr (world rank: 56th) (2021 estimate) (Pakistan)\n | lowest | 0.004 %/yr (world rank: 188th) (2021 estimate) (China)\nlife expectancy | mean | 73 yr\n | median | 71.9 yr\n | highest | 78.9 yr (world rank: 66th) (2020 estimate) (United States)\n | lowest | 67.4 yr (world rank: 186th) (2020 estimate) (Pakistan)\nmedian age | median | 29.4 yr\n | highest | 37.9 yr (world rank: 70th) (2021 estimate) (China)\n | lowest | 20.2 yr (world rank: 191st) (2021 estimate) (Pakistan)\n\nCapital cities:\nIndia | New Delhi, Delhi\nChina | Beijing\nUnited States | Washington, District of Columbia\nIndonesia | Jakarta\nPakistan | Islamabad, F.C.T.\n\nEconomic properties:\nGDP | total | $48.51 trillion per year\n | median | $3.385 trillion per year\n | highest | $25.46 trillion per year (world rank: 1st) (2022 estimates) (United States)\n | lowest | $376.5 billion per year (world rank: 43rd) (2022 estimates) (Pakistan)\nGDP at parity | total | $73.22 trillion per year\n | median | $11.87 trillion per year\n | highest | $30.33 trillion per year (world rank: 1st) (2022 estimates) (China)\n | lowest | $1.518 trillion per year (world rank: 22nd) (2022 estimates) (Pakistan)\nreal GDP | total | $41.72 trillion per year\n | median | $2.955 trillion per year\n | highest | $20.95 trillion per year (world rank: 1st) (2022 estimates) (United States)\n | lowest | $362.2 billion per year (world rank: 37th) (2022 estimates) (Pakistan)\nGDP per capita | average | $13203 per year per person\n | median | $4788 per year per person\n | highest | $76399 per year per person (world rank: 12th) (2022 estimates) (United States)\n | lowest | $1597 per year per person (world rank: 197th) (2022 estimates) (Pakistan)\nGDP real growth | mean | +4.71% per year\n | median | +5.309% per year\n | highest | +7.003% per year (world rank: 38th) (2022 estimates) (India)\n | lowest | +2.062% per year (world rank: 157th) (2022 estimates) (United States)\nGini index | median | 0.371\n | highest | 0.398 (world rank: 102nd) (2018, 2020, 2021, and 2022 estimates) (United States)\n | lowest | 0.296 (world rank: 24th) (2018, 2020, 2021, and 2022 estimates) (Pakistan)\n | distribution | \nconsumer price inflation | mean | +8.15% per year\n | median | +6.7% per year\n | highest | +19.87% per year (world rank: 21st) (2022 estimates) (Pakistan)\n | lowest | +1.97% per year (world rank: 175th) (2022 estimates) (China)\n\nEmployment:\nunemployment rate | mean | 5.16%\n | median | 4.89%\n | highest | 7.33% (world rank: 74th highest) (2022 estimates) (India)\n | lowest | 3.55% (world rank: 162nd highest) (2022 estimates) (Indonesia)\nlong-term unemployment rate | mean | 1.57%\n | median | 1.42%\n | highest | 3.18% (world rank: 44th highest) (2010 and 2014 estimates) (India)\n | lowest | 0.127% (world rank: 111th highest) (2010 and 2014 estimates) (Pakistan)\nlabor force | highest | 781.8 million people (58.24% of population) (world rank: 1st) (2022 estimates) (China)\n | lowest | 78.91 million people (35.22% of population) (world rank: 6th) (2022 estimates) (Pakistan)\n | distribution | \neconomically active children | median | 3.7%\n | highest | 13% (world rank: 50th) (2010, 2011, and 2012 estimates) (Pakistan)\n | lowest | 1.7% (world rank: 90th) (2010, 2011, and 2012 estimates) (India)\n\nUN Human Development Index:\n | India | China | United States | Indonesia | Pakistan\nhealth | 0.743 (world rank: 131st) | 0.861 (world rank: 58th) | 0.911 (world rank: 36th) | 0.755 (world rank: 126th) | 0.713 (world rank: 137th)\neducation | 0.535 (world rank: 131st) | 0.631 (world rank: 108th) | 0.9 (world rank: 8th) | 0.622 (world rank: 112th) | 0.395 (world rank: 169th)\nliving standards | 0.61 (world rank: 126th) | 0.739 (world rank: 82nd) | 0.948 (world rank: 11th) | 0.696 (world rank: 103rd) | 0.592 (world rank: 133rd)\ntotal | 0.624 (world rank: 131st) | 0.738 (world rank: 90th) | 0.92 (world rank: 10th) | 0.689 (world rank: 113th) | 0.55 (world rank: 147th)\n(2015 estimate)\n\nWikipedia page hits history:\nimage: https://www6b3.wolframalpha.com/Calculate/MSP/MSP2448241bhh00e1he97fd00006a6g1ee3gbhghh18?MSPStoreType=image/png&s=14\n\nWolfram|Alpha website result for \"What are the 5 largest countries in the world by population?\":\nhttps://www6b3.wolframalpha.com/input?i=What+are+the+5+largest+countries+in+the+world+by+population%3F"
Response:
Stop Reason: end_turn
Content: [ContentBlock(text='根据 Wolfram Alpha 的数据,世界上最大的五个国家按人口计算是:\n\n1. 印度 - 14.3 亿人\n2. 中国 - 14.3 亿人 \n3. 美国 - 3.4 亿人\n4. 印度尼西亚 - 2.78 亿人\n5. 巴基斯坦 - 2.41 亿人\n\n这五个国家加起来约有 37.1 亿人口。印度和中国的人口最多,各有约 14.3 亿人。美国以 3.4 亿人口位居第三。印度尼西亚和巴基斯坦分别以 2.78 亿和 2.41 亿人口位列第五。\n', type='text')]
Final Response: 根据 Wolfram Alpha 的数据,世界上最大的五个国家按人口计算是:
1. 印度 - 14.3 亿人
2. 中国 - 14.3 亿人
3. 美国 - 3.4 亿人
4. 印度尼西亚 - 2.78 亿人
5. 巴基斯坦 - 2.41 亿人
这五个国家加起来约有 37.1 亿人口。印度和中国的人口最多,各有约 14.3 亿人。美国以 3.4 亿人口位居第三。印度尼西亚和巴基斯坦分别以 2.78 亿和 2.41 亿人口位列第五。
根据 Wolfram Alpha 的数据,世界上最大的五个国家按人口计算是:
1. 印度 - 14.3 亿人
2. 中国 - 14.3 亿人
3. 美国 - 3.4 亿人
4. 印度尼西亚 - 2.78 亿人
5. 巴基斯坦 - 2.41 亿人
这五个国家加起来约有 37.1 亿人口。印度和中国的人口最多,各有约 14.3 亿人。美国以 3.4 亿人口位居第三。印度尼西亚和巴基斯坦分别以 2.78 亿和 2.41 亿人口位列第五。
==================================================
User Message: Calculate the square root of 1764.
==================================================
Initial Response:
Stop Reason: tool_use
Content: [ContentBlock(text='<thinking>\nThe relevant tool to use for this query is wolfram_alpha, since the question is asking for a mathematical calculation. The wolfram_alpha tool takes a single required parameter:\nsearch_query: The query to send to the Wolfram Alpha API\n\nThe user\'s request directly provides the search query needed, which is "square root of 1764". So all the required parameters are available to make the API call.\n</thinking>', type='text'), ContentBlockToolUse(id='toolu_011CkyJcwahqGaL513uFhdYJ', input={'search_query': 'square root of 1764'}, name='wolfram_alpha', type='tool_use')]
Tool Used: wolfram_alpha
Tool Input:
{
"search_query": "square root of 1764"
}
Tool Result:
"Query:\n\"square root of 1764\"\n\nInput:\nsqrt(1764)\n\nResult:\n42\n\nNumber line:\nimage: https://www6b3.wolframalpha.com/Calculate/MSP/MSP191312396298479fhh7a0000212h885f5h3d6gig?MSPStoreType=image/png&s=4\nWolfram Language code: NumberLinePlot[42]\n\nNumber name:\nforty-two\n\nAll 2nd roots of 1764:\n42 e^0 = 42 (real, principal root)\n\n42 e^(i \u03c0) = -42 (real root)\n\nPlot of all roots in the complex plane:\nimage: https://www6b3.wolframalpha.com/Calculate/MSP/MSP191412396298479fhh7a0000282bg11dc3e822ca?MSPStoreType=image/png&s=4\n\nWolfram|Alpha website result for \"square root of 1764\":\nhttps://www6b3.wolframalpha.com/input?i=square+root+of+1764"
Response:
Stop Reason: end_turn
Content: [ContentBlock(text='所以 1764 的平方根是 42。', type='text')]
Final Response: 所以 1764 的平方根是 42。
所以 1764 的平方根是 42。
==================================================
User Message: What is the distance between Earth and Mars?
==================================================
Initial Response:
Stop Reason: tool_use
Content: [ContentBlock(text='<thinking>\nThe wolfram_alpha tool is the relevant tool for answering this question about the distance between Earth and Mars, as it can provide scientific data like astronomical distances.\nThe search_query parameter can be filled in directly with the user\'s question "What is the distance between Earth and Mars?". No additional information is needed to fill the parameter.\nSince the search_query parameter is provided, the function can be called to answer the question.\n</thinking>', type='text'), ContentBlockToolUse(id='toolu_01PotM7Z1ooJ1PdK8QK1WgEu', input={'search_query': 'What is the distance between Earth and Mars?'}, name='wolfram_alpha', type='tool_use')]
Tool Used: wolfram_alpha
Tool Input:
{
"search_query": "What is the distance between Earth and Mars?"
}
Tool Result:
"Query:\n\"What is the distance between Earth and Mars?\"\n\nInput interpretation:\nMars | distance from Earth\n\nCurrent result:\n2.078 au (astronomical units)\n\nHistory:\nimage: https://www6b3.wolframalpha.com/Calculate/MSP/MSP101d6f8bccif83bf9800004259ii3cbcih744h?MSPStoreType=image/png&s=19\n\nUnit conversions:\n3.109\u00d710^8 km (kilometers)\n\n3.109\u00d710^11 meters\n\n193.2 million miles\n\nComparisons as distance:\n \u2248 ( 0.024 \u2248 1/42 ) \u00d7 smallest distance from the Sun to the heliosheath ( 79 to 100 au )\n\n \u2248 2.1 \u00d7 mean Earth-Sun distance ( 1.0000010178 au )\n\nCorresponding quantities:\nLight travel time t in vacuum from t = x/c:\n | 17 minutes\n\nLight travel time t in an optical fiber t = 1.48x/c:\n | 26 minutes\n\nSolar radiation pressure from P = L_\u2609/(c4\u03c0r^2):\n | 1.1 \u03bcPa (micropascals)\n | (assuming solar constant \u2248 1.36 kW/m^2)\n\nOrbital properties:\ncurrent distance from Earth | 2.078 au\n17.29 light minutes\naverage distance from Earth | 1.7 au\n14.1 light minutes\ncurrent distance from Sun | 1.39 au\n11.56 light minutes\nlargest distance from Sun | 154.863553 million mi\n1.66599116 au\nsmallest distance from Sun | 128.402967 million mi\n1.38133346 au\norbital period | 1.8808476 a\n\nWolfram|Alpha website result for \"What is the distance between Earth and Mars?\":\nhttps://www6b3.wolframalpha.com/input?i=What+is+the+distance+between+Earth+and+Mars%3F"
Response:
Stop Reason: end_turn
Content: [ContentBlock(text='地球和火星之间的当前距离为 2.078 天文单位 (au),相当于 3.109×10^8 公里或 1.932 亿英里。\n\n两颗行星之间的平均距离约为 1.7 au 或 1.4 亿英里。\n\n火星绕太阳的平均距离为 1.52 au,范围从最小的 1.38 au 到最大的 1.67 au。其轨道周期为 1.88 个地球年。\n\n在当前地火距离 2.078 au 时,光线在两颗行星之间传播大约需要 17 分钟。此时的太阳辐射压力约为 1.1 μPa(微帕)。', type='text')]
Final Response: 地球和火星之间的当前距离为 2.078 天文单位 (au),相当于 3.109×10^8 公里或 1.932 亿英里。
两颗行星之间的平均距离约为 1.7 au 或 1.4 亿英里。
火星绕太阳的平均距离为 1.52 au,范围从最小的 1.38 au 到最大的 1.67 au。其轨道周期为 1.88 个地球年。
在当前地火距离 2.078 au 时,光线在两颗行星之间传播大约需要 17 分钟。此时的太阳辐射压力约为 1.1 μPa(微帕)。
地球和火星之间的当前距离为 2.078 天文单位 (au),相当于 3.109×10^8 公里或 1.932 亿英里。
两颗行星之间的平均距离约为 1.7 au 或 1.4 亿英里。
火星绕太阳的平均距离为 1.52 au,范围从最小的 1.38 au 到最大的 1.67 au。其轨道周期为 1.88 个地球年。
在当前地火距离 2.078 au 时,光线在两颗行星之间传播大约需要 17 分钟。此时的太阳辐射压力约为 1.1 μPa(微帕)。