Getting Started
This guide will help you get started with Mojentic in just a few minutes.
Prerequisites
- Node.js 18.0 or higher
- A package manager (npm, yarn, or pnpm)
- Ollama installed (for local LLMs)
Installation
npm install mojenticyarn add mojenticpnpm add mojenticSet Up Ollama
If you want to use local models with Ollama:
- Install Ollama from ollama.ai
- Pull a model:bash
ollama pull qwen3:32b - Verify it's running:bash
ollama list
Your First Program
Create a new file hello-mojentic.ts:
import { LlmBroker, OllamaGateway, Message } from 'mojentic';
import { isOk } from 'mojentic';
async function main() {
// Create a gateway for Ollama
const gateway = new OllamaGateway();
// Create a broker with the model and gateway
const broker = new LlmBroker('qwen3:32b', gateway);
// Create a message
const messages = [
Message.user('Explain TypeScript in one sentence.')
];
// Generate a response
const result = await broker.generate(messages);
// Handle the result
if (isOk(result)) {
console.log('Response:', result.value);
} else {
console.error('Error:', result.error.message);
}
}
main();Run it:
npx ts-node hello-mojentic.tsUnderstanding the Code
Let's break down what's happening:
1. Create a Gateway
const gateway = new OllamaGateway();The gateway is responsible for communicating with the LLM provider. Each provider (Ollama, OpenAI, Anthropic) has its own gateway implementation.
2. Create a Broker
const broker = new LlmBroker('qwen3:32b', gateway);The broker is your main interface to the LLM. It handles message formatting, tool calling, and response processing.
3. Create Messages
const messages = [Message.user('Explain TypeScript in one sentence.')];Messages represent the conversation. The Message helper provides convenient methods for creating different message types.
4. Generate a Response
const result = await broker.generate(messages);The generate() method sends your messages to the LLM and returns a Result type.
5. Handle the Result
if (isOk(result)) {
console.log('Response:', result.value);
} else {
console.error('Error:', result.error.message);
}The Result type makes error handling explicit and type-safe. Use isOk() to check for success.
Next Steps
Now that you have a working example, explore more features:
- Structured Output - Get type-safe JSON from LLMs
- Tool System - Let LLMs call your functions
- Streaming - Real-time response streaming
- Error Handling - Robust error handling patterns
Common Issues
"Cannot find module 'mojentic'"
Make sure you've installed the package:
npm install mojentic"Ollama connection refused"
Ensure Ollama is running:
ollama serveTypeScript errors
Make sure you have TypeScript and Node types installed:
npm install -D typescript @types/nodeGetting Help
- 📚 Check the API Reference
- 💬 Open an issue on GitHub
- 📧 Email: stacey@mojility.com