homeschool
Version:
🏠 Teach AI to understand natural language like a patient tutor. Advanced embedding-based function calling with semantic understanding, confidence scoring, and natural language parameter extraction.
322 lines (244 loc) • 7.6 kB
Markdown
Teach AI to understand natural language like a patient tutor. Advanced embedding-based function calling with semantic understanding, confidence scoring, and natural language parameter extraction.
> **Why "Homeschool"?** Just like homeschooling provides personalized, one-on-one education that adapts to how each student learns best, this library teaches AI to understand your specific vocabulary, context, and intent. It learns your patterns, builds confidence gradually, and grows smarter with each interaction.
- **Multi-Layer Semantic Analysis**: Analyzes user queries at intent, context, and action levels
- **Semantic Parameter Extraction**: Uses embeddings to find parameter values instead of regex patterns
- **Confidence Scoring**: Provides transparency and prevents low-confidence executions
- **Content Isolation**: Distinguishes between command words and actual content semantically
- **Extensible Architecture**: Easy to add new tools without complex regex engineering
- **Robust**: Handles natural language variations, synonyms, and contextual understanding
```bash
npm install homeschool @xenova/transformers
```
```typescript
import { SemanticFunctionCaller, exampleTools } from 'homeschool';
// Initialize the caller
const caller = new SemanticFunctionCaller({
verbose: true,
defaultConfidenceThreshold: 0.25,
});
// Register tools
caller.registerTools(exampleTools);
// Execute function calling
const result = await caller.execute('make the background orange');
if (result.success) {
console.log(`Tool: ${result.tool}`);
console.log(`Parameters:`, result.parameters);
console.log(`Confidence: ${(result.confidence * 100).toFixed(1)}%`);
}
```
```typescript
import { Tool } from 'homeschool';
const customTool: Tool = {
name: 'playMusic',
description: 'Plays music or audio content',
contexts: [
'audio and entertainment',
'media control and playback',
'music streaming and sound',
],
intentPatterns: [
'user wants to play audio',
'user wants to listen to music',
'user wants to start playback',
],
parameters: {
genre: {
type: 'semantic_category',
semanticCandidates: ['rock', 'pop', 'jazz', 'classical', 'electronic'],
fallback: 'pop',
},
volume: {
type: 'semantic_number',
semanticCandidates: ['quiet', 'low', 'medium', 'high', 'loud'],
fallback: 'medium',
},
},
};
caller.registerTools([customTool]);
```
```typescript
const result = await caller.execute('remember to call mom', {
confidenceThreshold: 0.3,
});
```
```typescript
const result = await caller.execute('make it blue', {
gutInstinct: true, // Lower confidence threshold, trusts model intuition
});
```
```typescript
const result = await caller.executeFirstInstinct(
'take a note about the meeting',
);
// Bypasses confidence checks completely, trusts top choice
```
Extracts colors using semantic similarity:
```typescript
{
type: 'semantic_color',
semanticCandidates: ['red', 'blue', 'green', /* ... */],
modifierCandidates: ['light', 'dark', 'bright'],
fallback: 'blue'
}
```
Extracts categories through contextual understanding:
```typescript
{
type: 'semantic_category',
semanticCandidates: ['work', 'personal', 'ideas', /* ... */],
fallback: 'general'
}
```
Isolates content from command words:
```typescript
{
type: 'extracted_content',
extractionStrategy: 'semantic_content_isolation'
}
```
```typescript
const caller = new SemanticFunctionCaller({
embeddingModel: 'Xenova/all-MiniLM-L6-v2', // Hugging Face model
defaultConfidenceThreshold: 0.25, // Confidence threshold
enableCaching: true, // Cache embeddings
verbose: false, // Debug logging
});
```
```typescript
interface ExecutionResult {
success: boolean;
tool?: string; // Selected tool name
parameters?: Record<string, any>; // Extracted parameters
confidence?: number; // Confidence score (0-1)
reasoning?: Array<{
// Detailed reasoning
type: string;
text: string;
score: number;
}>;
mode?: 'standard' | 'first_instinct';
reason?: string; // Failure reason if success = false
}
```
```typescript
// "remember to buy groceries and pick up dry cleaning"
{
tool: 'takeNote',
parameters: {
note: 'buy groceries and pick up dry cleaning',
category: 'personal'
},
confidence: 0.847
}
```
```typescript
// "make the page look more vibrant and energetic"
{
tool: 'changeBackgroundColor',
parameters: {
color: 'vibrant orange'
},
confidence: 0.721
}
```
```typescript
// "show the user a welcome message"
{
tool: 'displayText',
parameters: {
text: 'welcome message'
},
confidence: 0.892
}
```
```typescript
import { extractSemanticColor, extractSemanticContent } from 'homeschool';
// Use extractors directly
const color = await extractSemanticColor(
'make it crimson',
colorConfig,
embedder,
);
const content = await extractSemanticContent('display hello world', embedder);
```
```typescript
const queries = ['make it blue', 'remember the meeting', 'show welcome text'];
const results = await Promise.all(
queries.map((query) => caller.execute(query)),
);
```
```typescript
console.log(`Cache size: ${caller.getCacheSize()}`);
caller.clearCache(); // Clear embedding cache if needed
```
- **Chatbots & Virtual Assistants**: Natural language command processing
- **Game Development**: Voice commands and natural language game controls
- **Mobile Apps**: Voice-driven app navigation and control
- **Smart Home**: Natural language device control
- **Business Tools**: Voice-driven workflow automation
- **Creative Tools**: Natural language creative software control
Traditional regex approach:
```javascript
// Brittle regex patterns
if (/make.*background.*blue/i.test(query)) {
return { tool: 'changeColor', color: 'blue' };
}
```
Semantic approach:
```typescript
// Robust semantic understanding
const result = await caller.execute(query);
// Handles: "make it blue", "blue background", "change to blue", etc.
```
Use the automated release scripts for easy publishing:
```bash
npm run release
npm run publish:minor
npm run publish:major
```
These scripts automatically:
- Bump the version in package.json
- Run tests to ensure quality
- Build the TypeScript code
- Publish to npm
See [scripts/README.md](scripts/README.md) for more details.
We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.
MIT License - see the [LICENSE](LICENSE) file for details.
- Built on top of [Transformers.js](https://github.com/xenova/transformers.js)
- Inspired by advances in semantic search and embedding-based AI
- Thanks to the open-source AI community
---
**Made with care for developers who want to homeschool their AI to truly understand human language.**