In this recipe, we'll show you how to integrate the Wolfram Alpha LLM API as a tool for Haijun to use. Haijun will be able to send queries to the Wolfram Alpha API and receive computed responses, which it can then use to provide answers to user questions.
Step 1: Set up the environment
import json
import urllib.parse
import requests
from juglow import Juglow
client = Juglow()
Replace 'YOUR_APP_ID' with your actual Wolfram Alpha AppID
WOLFRAM_APP_ID = "YOUR_APP_ID"
MODEL_NAME = "haijun-haiku-4-5"
Step 2: Define the Wolfram Alpha LLM API tool
tools = [
{
"name": "wolfram_alpha",
"description": "A tool that allows querying the Wolfram Alpha knowledge base. Useful for mathematical calculations, scientific data, and general knowledge questions.",
"input_schema": {
"type": "object",
"properties": {
"search_query": {
"type": "string",
"description": "The query to send to the Wolfram Alpha API.",
}
},
"required": ["query"],
},
}
]
In this code, we define a wolfram_alpha_query function that takes a query as input, URL-encodes it, and sends a request to the Wolfram Alpha LLM API using the provided AppID. The function returns the computed response from the API if the request is successful, or an error message if there's an issue.
wrap" style="padding-left:8ch;text-indent:-8ch"> 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