GitHub - aifunc-dev/aifunc: AI as functions. Install, import, call. Zero dependencies.

5 min read Original article ↗

AI as Function. Prompt as Code.

Call AI like a function. Share AI like an npm package.

Typed · Testable · Cross-language · Zero-dependency · Model-agnostic · Git-native

License Go CLI Node.js ≥ 18 Python ≥ 3.10 TypeScript types


Why AIFunc

Adding an AI feature to your product should be simple.

The reality: adopt a new framework, learn new orchestration abstractions, study prompt engineering, write glue code, figure out how to test, worry about switching models. A week of work for 3 lines of core logic.

80% of real-world AI needs are straightforward: text in, structured data out. Stateless. No memory. That's a function.

AIFunc makes this work out of the box. No new concepts, no architecture invasion. Just import.


Quick Start

# Install CLI
brew tap aifunc-dev/aifn && brew install aifn   # macOS/Linux
scoop bucket add aifn https://github.com/aifunc-dev/scoop-aifn && scoop install aifn  # Windows
# Install an AI function
aifn install github:aifunc-dev/aifunc-packages/summarize

TypeScript

import { summarize, AIFuncConfig, SummarizeInput } from './aifunc/summarize';

// Mock mode: no API key needed, works offline
const config: AIFuncConfig = { mock: true };

const text =
  'The James Webb Space Telescope captured its first full-color images in July 2022, ' +
  'revealing thousands of galaxies in a single image.';

async function main() {
  const result = await summarize(config, { text, maxLength: 30 } as SummarizeInput);
  console.log(`Summary   : ${result.summary}`);   // ← IDE autocomplete, type-safe
  console.log(`Word count: ${result.wordCount}`);
}

main().catch(console.error);

Python

import asyncio
from aifunc.summarize import summarize, AIFuncConfig, SummarizeInput

config = AIFuncConfig(mock=True)

text = (
    'The James Webb Space Telescope captured its first full-color images in July 2022, '
    'revealing thousands of galaxies in a single image.'
)

async def main():
    result = await summarize(config, SummarizeInput(text=text, max_length=30))
    print(f"Summary   : {result.summary}")
    print(f"Word count: {result.word_count}")

asyncio.run(main())

To connect a real model, replace mock: true with actual config:

const config: AIFuncConfig = {
  baseURL: 'https://your-api-endpoint/v1',
  model: 'your-model-name',
  apiKey: 'your-api-key',
};

Compose Multiple AI Functions

AI functions compose like regular functions. Use familiar control flow to wire business logic:

import { analyzeSentiment, AIFuncConfig } from './aifunc/analyze-sentiment';
import { recognizeIntent } from './aifunc/recognize-intent';
import { extractJson } from './aifunc/extract-json';

const config: AIFuncConfig = { /* ... */ };

async function handleTicket(message: string) {
  const sentiment = await analyzeSentiment(config, {
    text: message,
    labels: ['angry', 'frustrated', 'neutral', 'happy'],
  });

  if (sentiment.label === 'angry' && sentiment.confidence > 0.7) {
    return { action: 'escalate', priority: 'HIGH' };
  }

  const intent = await recognizeIntent(config, {
    text: message,
    intents: ['query_order', 'request_refund', 'technical_support', 'billing_issue'],
  });

  const info = await extractJson(config, {
    text: message,
    fields: [
      { name: 'order_id', description: 'Order number', type: 'string' },
      { name: 'issue', description: 'What the customer wants', type: 'string' },
    ],
  });

  return { action: intent.intent, ...info.extracted };
}

if, switch, Promise.all — control flow you already know. Nothing new to learn.

Full examples: examples/typescript/customer-support and examples/python/customer-support


Features

Typed — Full type definitions for inputs and outputs. IDE autocomplete. Misspell a field? Caught at compile time.

Testable — Every package ships with mock data. mock: true runs offline. CI without API keys, zero cost.

Cross-language — One package definition (api.json + package.json + prompts/) compiles to TypeScript and Python with identical behavior.

Zero-dependency — The runtime engine is pure source code generated into your project. No third-party npm/pip packages.

Model-agnostic — Works with any OpenAI-compatible endpoint. Switch models by changing config, zero code changes.

Git-native — Compiled output commits to Git. Team members clone and import directly, no CLI needed. Version control, code review, and access control all reuse your existing Git workflow.


Share and Reuse

Write an AI function, push to Git, anyone installs with one command:

# Others install your published package
aifn install github:your-name/your-packages/summarize

# Local packages work too
aifn install ../shared-packages/translate

No npm publish, no registry. A Git repo is a package registry. Fork, PR, Tag, access control — your existing Git workflow, directly reused.

Want to create your own package? Just a package.json for metadata, an api.json for the interface, and a prompts/ directory for the prompt:

aifn create my-analyzer
# Generates package skeleton — fill in package.json, api.json, and prompt

See Create an AIFunc Package and Sharing & Publishing


Team Workflow

Role Responsibility
AI Engineer Create packages, write prompts, tune quality
App Developer import → call, no prompt knowledge needed
Platform Lead Manage package repo, review PRs, control versions

Available Packages

The official package registry provides ready-to-use AI functions:

Package Purpose
summarize Text summarization
analyze-sentiment Sentiment analysis
recognize-intent Intent recognition
classify General text classification
extract-json Extract structured fields from text
extract-entities Entity extraction (names, places, orgs)
extract-keywords Keyword extraction
detect-language Language detection
translate Translation
rewrite Text rewriting
generate-reply Reply generation
generate-title Title generation
generate-slug URL slug generation
generate-email Email generation
generate-post Post generation
answer-question Question answering
score-quality Content quality scoring

Install any package:

aifn install github:aifunc-dev/aifunc-packages/summarize  # Short form
aifn install https://github.com/aifunc-dev/aifunc-packages/tree/main/summarize  # Full URL

Full package list and docs: aifunc-packages


Documentation


Licensed under Apache-2.0.