Skip to content

cnosuke/go-wolfram-llm

Repository files navigation

go-wolfram-llm

Go Reference Go Report Card License: MIT

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).

Features

  • 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

Installation

go get github.com/cnosuke/go-wolfram-llm

Quick Start

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)
}

Advanced Usage

With Options

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)
}

Error Handling

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
}

API Reference

For detailed API documentation, see the Go Reference.

Client

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"),
)

Query Execution

// 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)

Response Structure

type LLMResponse struct {
	Result           string             // Computation result
}

Configuration Options

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
)

Supported Parameters

The library supports all parameters mentioned in the official Wolfram Alpha LLM API documentation, including:

  • maxchars: Maximum character count in response
  • assumption: Assumption parameter
  • ip: Client IP address
  • latlong: Latitude/longitude
  • timezone: Timezone
  • location: Location information
  • units: Unit system ("metric" or "nonmetric")
  • currency: Currency
  • countrycode: Country code
  • languagecode: Language code
  • And many others

Development Status

This library is actively under development. While it is functional, we are continuously improving it. Feedback and contributions are welcome!

License

MIT License

Acknowledgments

  • This library is not officially provided or endorsed by Wolfram Research, Inc.
  • Thanks to Wolfram Research for providing the excellent LLM API.

Author

cnosuke ( x.com/cnosuke )

About

Library for using Wolfram|Alpha API with Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published