DSPy.ts helps you build powerful AI applications right in your web browser. It's based on Stanford's DSPy framework but made specifically for JavaScript and TypeScript developers. Unlike traditional AI frameworks that require expensive servers and complex infrastructure, DSPy.ts lets you create and run sophisticated AI models directly in your users' browsers. This means you can build everything from smart chatbots to image recognition systems that work entirely on your users' devices, making your AI applications faster, cheaper, and more private.
Here's what makes it special:
- Run AI Models in Your Browser: Build and run complete AI models (both applied and generative) directly in your users' browsers - no server needed!
- Use Your Device's Power: Takes advantage of your computer's GPU or CPU to run AI tasks faster
- Save Money: Cut costs by running AI on users' devices instead of expensive cloud servers
- Works Everywhere: Run the same models on computers, phones, or IoT devices
- Easy to Use: Write clean TypeScript code instead of complex AI prompts
created by rUv, cause he could.
npm install dspy.ts
DSPy.ts stands for Declarative Self-improving TypeScript. It makes building AI apps easier by:
- Simple Code Instead of Prompts: Write clear TypeScript code instead of complex prompts
- Gets Better Over Time: Your AI learns and improves automatically as you use it
- Catches Mistakes Early: TypeScript helps prevent errors before running your code
- Works Everywhere: buid & Run AI models right in your browser:
- Fast Local Processing: Run models directly on your device, no server needed
- Uses Your Graphics Card: Speed up AI tasks using your computer's GPU
- Backup Cloud Option: Switch to cloud services when you need more power
Key Benefits:
- Run AI models without a server
- Fast performance using your device's hardware
- Save memory with optimized models
- Works in any modern browser
- Easy to test and debug
- Simple to deploy and scale
DSPy.ts seamlessly integrates with:
- ONNX Runtime Web: Run models locally in browsers and Node.js
- js-pytorch: Use PyTorch models directly in JavaScript
- OpenRouter: Access various LLM providers
- Vector Databases: Connect with Pinecone, Weaviate, etc.
- Development Tools: VS Code extensions, ESLint rules
- Monitoring: Prometheus, Grafana dashboards
Build sophisticated AI agents that can:
- Reason & Act: Use the ReAct pattern for structured thinking
- Use Tools: Integrate with APIs, databases, and external services
- Learn & Improve: Automatically optimize performance
- Chain Thoughts: Break complex tasks into manageable steps
Example of an agentic system:
// Create a research agent with tools
const researcher = new ReActModule({
tools: [
new WebSearch(),
new PDFReader(),
new Summarizer(),
new CitationManager()
],
strategy: 'ReAct',
optimization: {
metric: accuracyMetric,
method: 'BootstrapFewShot'
}
});
// The agent can:
// 1. Search for relevant papers
// 2. Read and understand PDFs
// 3. Generate summaries
// 4. Manage citations
// 5. Learn from feedback
- Programming, Not Prompting: Focus on building modular AI systems with code, not strings
- Self-Improving: Automatically optimize prompts and weights based on your metrics
- Type-Safe: Catch errors at compile time with TypeScript's static typing
- Local Inference: Run models locally with ONNX Runtime Web and js-pytorch
- Production Ready: Built for enterprise deployment with monitoring and scaling
npm install dspy.ts onnxruntime-web js-pytorch
import { PredictModule, configureLM, ONNXModel } from 'dspy.ts';
// Configure local inference with ONNX Runtime
const model = new ONNXModel({
modelPath: 'path/to/model.onnx',
executionProvider: 'wasm'
});
configureLM(model);
// Create a self-improving module
class MathSolver extends PredictModule {
constructor() {
super({
name: 'MathSolver',
signature: {
inputs: [{ name: 'question', type: 'string' }],
outputs: [
{ name: 'reasoning', type: 'string' },
{ name: 'answer', type: 'number' }
]
},
strategy: 'ChainOfThought'
});
}
}
// Use and optimize the module
const solver = new MathSolver();
const optimizer = new BootstrapFewShot(exactMatchMetric);
const optimizedSolver = await optimizer.compile(solver, trainset);
Build AI systems as composable TypeScript modules:
// Question answering with context
const qa = new Pipeline([
new ContextRetriever(),
new QuestionAnswerer(),
new ResponseValidator()
]);
Automatically optimize your systems:
// Optimize with few-shot learning
const optimizer = new BootstrapFewShot(metric);
const betterQA = await optimizer.compile(qa, examples);
Choose your execution environment:
// Local inference with ONNX
const localLM = new ONNXModel({
modelPath: 'model.onnx',
executionProvider: 'wasm'
});
// Cloud fallback
const cloudLM = new OpenRouterLM(API_KEY);
- Catch errors at compile time
- Validate inputs/outputs automatically
- Ensure consistent data flow
- Automatic few-shot learning
- Metric-based optimization
- Continuous improvement
- ONNX Runtime integration
- Complete neural network execution in browser
- Browser and Node.js compatibility
- Production monitoring
- Error handling
- Scalable deployment
Task Type | Model | Local (ONNX) | Cloud API | Memory Usage | Optimization Gain |
---|---|---|---|---|---|
QA (RAG) | BERT | 80-150ms | 500-800ms | 150-300MB | +15-25% accuracy |
Classification | DistilBERT | 30-50ms | 300-500ms | 80-120MB | +10-20% accuracy |
Agents | GPT-2 | 100-200ms | 600-1000ms | 200-400MB | +20-30% success |
Generation | T5 | 150-250ms | 700-1200ms | 250-500MB | +15-25% quality |
Benchmarks run on standard hardware (4-core CPU, 16GB RAM). Local inference uses ONNX Runtime with WASM backend.
// Intelligent support agent
const supportAgent = new Pipeline([
new IntentClassifier(),
new ContextRetriever({ source: 'knowledge-base' }),
new ResponseGenerator({ style: 'professional' }),
new SentimentValidator()
]);
// Optimize for your metrics
const optimizer = new BootstrapFewShot(satisfactionMetric);
const betterAgent = await optimizer.compile(supportAgent, examples);
// Automated document analysis
const docProcessor = new Pipeline([
new DocumentParser(),
new EntityExtractor(),
new RelationshipMapper(),
new SummaryGenerator()
]);
// Intelligent coding assistant
const codeAssistant = new ReActModule({
tools: [
new CodeAnalyzer(),
new TestGenerator(),
new DocumentationWriter()
],
strategy: 'ReAct'
});
// OpenAPI spec generator
const apiGenerator = new Pipeline([
new SchemaAnalyzer(),
new EndpointDesigner(),
new DocumentationBuilder()
]);
// Cross-platform content generator
const contentEngine = new Pipeline([
new TopicExpander(),
new ContentGenerator({
variants: ['blog', 'social', 'email']
}),
new ToneOptimizer(),
new SEOEnhancer()
]);
// Market intelligence system
const marketAnalyzer = new Pipeline([
new DataCollector({ sources: ['news', 'social', 'reports'] }),
new TrendAnalyzer(),
new InsightGenerator(),
new RecommendationEngine()
]);
// Research assistant
const researchAssistant = new ReActModule({
tools: [
new PaperSearch(),
new CitationAnalyzer(),
new SummaryGenerator(),
new BibtexFormatter()
],
strategy: 'ChainOfThought'
});
// Automated data analysis
const dataAnalyst = new Pipeline([
new DataCleaner(),
new StatisticalAnalyzer(),
new VisualizationGenerator(),
new InsightExtractor()
]);
- Run models locally
- Hardware acceleration
- Build & run both applied and generative models
- Reduced latency
- Direct PyTorch model usage
- Leverage client GPU/CPU resources
- Efficient inference
- Edge computing across devices (computers, mobile, IoT)
- Monitoring & logging
- Error recovery
- Load balancing
- Reduced infrastructure costs through edge computing
We welcome contributions! See our Contributing Guide for details.
DSPy.ts is MIT licensed. See the LICENSE file for details.
DSPy.ts is inspired by Stanford's DSPy project, bringing its powerful concepts of declarative, self-improving AI systems to the JavaScript ecosystem. We extend our gratitude to the Stanford NLP group and the DSPy community.