llm-decision
v0.1.1
Published
LLM-powered control flow functions: llm_if() and llm_switch()
Downloads
11
Maintainers
Readme
llm-decision
LLM-powered control flow functions for TypeScript/JavaScript.
Installation
npm install llm-decision ai @ai-sdk/openaiQuick Start
import { configureLLMDecision, llm_if, llm_switch } from 'llm-decision';
import { openai } from '@ai-sdk/openai';
// Configure globally
configureLLMDecision({
model: openai('gpt-4o-mini'),
});
// Use llm_if just like a real if
if (await llm_if("The user seems happy based on: 'I love this!'")) {
console.log('User is happy!');
} else {
console.log('User is not happy.');
}
// Use llm_switch just like a real switch
switch (await llm_switch("Classify the sentiment of: 'I love this!'", ['positive', 'negative', 'neutral'] as const)) {
case 'positive': console.log('Celebrate!'); break;
case 'negative': console.log('Comfort.'); break;
case 'neutral': console.log('Acknowledge.'); break;
}API
configureLLMDecision(config)
Configure global settings for all LLM decision functions.
configureLLMDecision({
model: openai('gpt-4o-mini'), // Required: Language model from Vercel AI SDK
temperature: 0, // Optional: Default 0 for deterministic results
maxRetries: 2, // Optional: Default 2
});llm_if(condition, options?)
Evaluates a natural language condition and returns a Promise<boolean>. Use it as the condition of a real if statement.
if (await llm_if("The user's message indicates frustration")) {
routeToSupport();
} else {
continueNormally();
}Per-call model override:
if (await llm_if("Complex decision requiring better model", { model: openai('gpt-4o') })) {
// ...
}llm_switch(condition, cases, options?)
Selects from a list of cases based on a natural language condition and returns Promise<T>. Use it as the expression of a real switch statement.
switch (await llm_switch("Classify the urgency: 'Server is down!'", ['low', 'medium', 'high', 'critical'] as const)) {
case 'critical': page(); break;
case 'high': alert(); break;
case 'medium': queue(); break;
case 'low': log(); break;
}Provider Support
This package uses the Vercel AI SDK for provider-agnostic LLM calls. Install the provider package you need:
# OpenAI
npm install @ai-sdk/openai
# Anthropic
npm install @ai-sdk/anthropic
# Google
npm install @ai-sdk/googleThen configure with your preferred provider:
import { anthropic } from '@ai-sdk/anthropic';
configureLLMDecision({
model: anthropic('claude-3-5-sonnet-20241022'),
});TypeScript
Full TypeScript support with proper type inference:
// Return type is inferred as boolean
const isValid = await llm_if("Check if valid");
// Return type is inferred as 'a' | 'b' | 'c'
const choice = await llm_switch("Pick one", ['a', 'b', 'c'] as const);License
MIT
