A Go client library for the Wolfram Alpha LLM API. This library provides a simple and idiomatic Go interface to interact with the Wolfram Alpha API optimized for Large Language Models (LLMs).
- Simple, idiomatic Go API
- Full support for Wolfram Alpha LLM API
- Flexible configuration via functional options pattern
- Clear error handling with detailed information
- Context-aware API requests
- Fully typed request and response structures
- Support for both URL parameter and Bearer Token authentication methods
go get github.com/cnosuke/go-wolfram-llm
package main
import (
"context"
"fmt"
"log"
wolframllm "github.com/cnosuke/go-wolfram-llm"
)
func main() {
// Create a client with your Wolfram Alpha AppID
client, err := wolframllm.NewClient("YOUR_APP_ID_HERE")
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Create a context
ctx := context.Background()
// Execute a query
response, err := client.Query(ctx, "10 densest elemental metals")
if err != nil {
log.Fatalf("Query execution failed: %v", err)
}
// Display the results
fmt.Printf("Result:\n%s\n", response.Result)
}
package main
import (
"context"
"fmt"
"log"
wolframllm "github.com/cnosuke/go-wolfram-llm"
)
func main() {
// Create a client with options
client, err := wolframllm.NewClient(
"YOUR_APP_ID_HERE",
wolframllm.WithTimeout(60), // Timeout in seconds
wolframllm.WithBearer(true), // Use Bearer Token authentication
wolframllm.WithDefaultMaxChars(1000), // Set default maximum characters
wolframllm.WithRetries(3), // Set maximum retry count
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Create a context
ctx := context.Background()
// Create query parameters
params := &wolframllm.QueryParams{
MaxChars: 2000, // Max characters for this request
Assumption: "DateOrder_**", // Specify date interpretation
}
response, err := client.QueryWithParams(ctx, "integrate x^2", params)
if err != nil {
log.Fatalf("Query execution failed: %v", err)
}
// Display the result
fmt.Printf("Result:\n%s\n", response.Result)
}
response, err := client.Query(ctx, "population of Earth")
if err != nil {
switch {
case wolframllm.IsInvalidInputError(err):
fmt.Println("Invalid input:", err)
case wolframllm.IsAuthError(err):
fmt.Println("Authentication error:", err)
case wolframllm.IsServerError(err):
fmt.Println("Server error:", err)
case wolframllm.IsNetworkError(err):
fmt.Println("Network error:", err)
default:
fmt.Println("Error:", err)
}
return
}
For detailed API documentation, see the Go Reference.
Client
is the main entry point for using the library:
// Create a basic client
client, err := wolframllm.NewClient("APP_ID")
// Create a client with options
client, err := wolframllm.NewClient(
"APP_ID",
wolframllm.WithTimeout(30),
wolframllm.WithUserAgent("MyApp/1.0"),
)
// Basic query
response, err := client.Query(ctx, "calculate pi to 100 digits")
// Query with parameters
params := &wolframllm.QueryParams{
MaxChars: 1000,
Assumption: "DateOrder_**",
Units: "metric",
CountryCode: "JP",
}
response, err := client.QueryWithParams(ctx, "query", params)
type LLMResponse struct {
Result string // Computation result
}
The library supports several configuration options through the functional options pattern:
client, err := wolframllm.NewClient(
"APP_ID",
wolframllm.WithTimeout(30), // Request timeout in seconds
wolframllm.WithRetries(2), // Number of retries on transient errors
wolframllm.WithUserAgent("MyApp/1.0"), // Custom User-Agent
wolframllm.WithBearer(true), // Use Bearer Token authentication
wolframllm.WithDefaultMaxChars(1000), // Default maximum character count
wolframllm.WithHTTPClient(customClient), // Custom HTTP client
wolframllm.WithBaseURL("https://custom-url.com"), // Custom base URL
)
The library supports all parameters mentioned in the official Wolfram Alpha LLM API documentation, including:
maxchars
: Maximum character count in responseassumption
: Assumption parameterip
: Client IP addresslatlong
: Latitude/longitudetimezone
: Timezonelocation
: Location informationunits
: Unit system ("metric" or "nonmetric")currency
: Currencycountrycode
: Country codelanguagecode
: Language code- And many others
This library is actively under development. While it is functional, we are continuously improving it. Feedback and contributions are welcome!
MIT License
- This library is not officially provided or endorsed by Wolfram Research, Inc.
- Thanks to Wolfram Research for providing the excellent LLM API.
cnosuke ( x.com/cnosuke )