Note: This is a demonstration project to show that "all we need is HTTP" for an Inferable integration. We don't recommend creating bash-based AI clients for production use.
A lightweight Bash SDK for interacting with the Inferable API. This SDK allows you to register and run Inferable functions directly from shell scripts.
- Download the SDK:
curl -O https://raw.githubusercontent.com/inferablehq/inferable-bash/main/inferable.sh
chmod +x inferable.sh
- Set up your environment:
export INFERABLE_API_SECRET="your-api-secret" # Required
export INFERABLE_API_ENDPOINT="https://api.inferable.ai" # Optional, defaults to https://api.inferable.ai
export INFERABLE_MACHINE_ID="custom-machine-id" # Optional, auto-generated if not provided
Here's a minimal example to get you started with the Inferable Bash SDK:
#!/bin/bash
# Source the SDK
source ./inferable.sh
# Initialize the SDK
inferable_init || exit 1
# Create a handler file (handler.sh)
cat > handler.sh << 'EOF'
#!/bin/bash
function_name=$1
input=$2
case "$function_name" in
"greet")
name=$(echo "$input" | jq -r '.name')
echo "{\"message\": \"Hello, $name!\"}"
;;
*)
echo "{\"error\": \"Unknown function\"}"
;;
esac
EOF
chmod +x handler.sh
# Define your functions
FUNCTIONS='[
{
"name": "greet",
"description": "Greet a user",
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"}
},
"required": ["name"]
}
}
]'
# Register the service
CLUSTER_ID=$(register_service "greeting-service" "$FUNCTIONS")
# Start polling with your handler
start_service "$CLUSTER_ID" "greeting-service" "./handler.sh"
bash
(version 4.0 or later)curl
for making HTTP requestsjq
for JSON processing
Initializes the SDK and validates the connection to Inferable.
inferable_init || exit 1
Registers a new service with Inferable.
register_service <service_name> <functions_json>
Parameters:
service_name
: Name of your servicefunctions_json
: JSON array of function definitions
Returns: Cluster ID on success
Starts the service and begins polling for jobs.
start_service <cluster_id> <service_name> <handler_script> [poll_interval]
Parameters:
cluster_id
: ID returned from register_serviceservice_name
: Name of your servicehandler_script
: Path to your handler scriptpoll_interval
: Optional polling interval in seconds (default: 10)
Generates a unique machine identifier.
machine_id=$(generate_machine_id <length>)
Makes an HTTP request to the Inferable API.
make_request <method> <path> [body]
Your handler script should accept two arguments:
- Function name
- JSON input data
Example handler:
#!/bin/bash
function_name=$1
input=$2
case "$function_name" in
"myFunction")
# Process the input
value=$(echo "$input" | jq -r '.someField')
# Return JSON result
echo "{\"result\": \"Processed $value\"}"
;;
*)
echo "{\"error\": \"Unknown function\"}"
;;
esac
Variable | Required | Default | Description |
---|---|---|---|
INFERABLE_API_SECRET |
Yes | - | Your Inferable API secret |
INFERABLE_API_ENDPOINT |
No | https://api.inferable.ai |
Inferable API endpoint |
INFERABLE_MACHINE_ID |
No | Auto-generated | Unique identifier for this machine |
The SDK includes basic error handling. Functions will return non-zero exit codes on failure. We recommend wrapping critical operations in error checks:
if ! inferable_init; then
echo "Failed to initialize SDK" >&2
exit 1
fi
CLUSTER_ID=$(register_service "my-service" "$FUNCTIONS")
if [ -z "$CLUSTER_ID" ]; then
echo "Failed to register service" >&2
exit 1
fi
- Always source the SDK rather than executing it:
source ./inferable.sh # Correct
./inferable.sh # Incorrect
- Validate the initialization:
inferable_init || exit 1
- Use error handling in your handler scripts:
#!/bin/bash
function_name=$1
input=$2
if [ -z "$input" ]; then
echo "{\"error\": \"No input provided\"}"
exit 1
fi
# Process function...
- Set reasonable polling intervals based on your needs:
# More frequent polling (5 seconds)
start_service "$CLUSTER_ID" "my-service" "./handler.sh" 5
# Less frequent polling (30 seconds)
start_service "$CLUSTER_ID" "my-service" "./handler.sh" 30
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.