Framework-agnostic core for AI Kit - streaming, agents, state management
npm install @ainative/ai-kit-core
# or
pnpm add @ainative/ai-kit-core
# or
yarn add @ainative/ai-kit-coreThe core package provides the fundamental building blocks for building AI-powered applications. It includes:
- Streaming: Real-time streaming from LLM providers (OpenAI, Anthropic)
- Agents: AI agent creation, execution, and orchestration
- Security: PII detection, prompt injection prevention, content moderation
- Memory: Long-term memory management and fact extraction
- Context: Context window management and token counting
- Summarization: Automatic conversation summarization
- Authentication: AINative authentication provider
- Session Management: User session tracking and storage
- ZeroDB Integration: Database operations and query building
- Usage Tracking: Token counting and cost monitoring
- Instrumentation: Auto-instrumentation and observability
- RLHF: Reinforcement learning logging and feedback
- Search: Semantic search capabilities
- Store: Conversation and state persistence
- Monitoring: Query monitoring and alerting
- Reporting: Usage reports and analytics
- AIStream - Stream responses from LLMs
- StreamingResponse - HTTP streaming response utilities
- Provider Adapters - OpenAI and Anthropic adapters
- Agent - Create and configure AI agents
- AgentExecutor - Execute agent tasks
- StreamingAgentExecutor - Stream agent responses
- AgentSwarm - Multi-agent orchestration
- LLM Providers - OpenAI and Anthropic providers
- PIIDetector - Detect and redact PII
- PromptInjectionDetector - Prevent prompt injection
- ContentModerator - Moderate content
- JailbreakDetector - Detect jailbreak attempts
- MemoryStore - Abstract memory interface
- UserMemory - User-specific memory
- FactExtractor - Extract facts from conversations
- InMemoryMemoryStore - In-memory implementation
- RedisMemoryStore - Redis-backed memory
- ZeroDBMemoryStore - ZeroDB-backed memory
- ContextManager - Manage context windows
- TokenCounter - Count tokens efficiently
- ConversationSummarizer - Summarize conversations
- extractKeyPoints - Extract key points
- AINativeAuthProvider - Authenticate with AINative platform
- SessionManager - Manage user sessions
- InMemorySessionStore - In-memory sessions
- RedisSessionStore - Redis-backed sessions
- ZeroDBSessionStore - ZeroDB-backed sessions
- ZeroDBClient - Database client
- QueryBuilder - Build type-safe queries
- UsageTracker - Track API usage and costs
- calculateCost - Calculate LLM costs
- InstrumentationManager - Manage instrumentation
- Interceptors - Request/response interceptors
- RLHFLogger - Log interactions for RLHF
- RLHFInstrumentation - Auto-instrument for RLHF
- SemanticSearch - Semantic similarity search
- ConversationStore - Store conversations
- createStore - Create custom stores
- QueryMonitor - Monitor database queries
- AlertManager - Manage cost and usage alerts
- ReportGenerator - Generate usage reports
import { AIStream } from '@ainative/ai-kit-core/streaming';
import { OpenAI } from 'openai';
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const response = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }],
stream: true
});
const stream = AIStream(response);
// Use in your framework (Next.js, Express, etc.)
return new Response(stream);import { Agent, AgentExecutor } from '@ainative/ai-kit-core/agents';
import { Calculator, WebSearch } from '@ainative/ai-kit-tools';
const agent = new Agent({
name: 'ResearchAssistant',
description: 'Helps with research and calculations',
tools: [Calculator, WebSearch],
llm: {
provider: 'openai',
model: 'gpt-4',
apiKey: process.env.OPENAI_API_KEY
}
});
const executor = new AgentExecutor({ agent });
const result = await executor.execute('What is 15% of the GDP of France?');
console.log(result.output);import { PIIDetector } from '@ainative/ai-kit-core/security';
const detector = new PIIDetector({
enabledTypes: ['email', 'phone', 'ssn', 'credit_card']
});
const result = detector.detect('My email is john@example.com');
console.log(result.containsPII); // true
console.log(result.detectedTypes); // ['email']
console.log(result.redacted); // 'My email is [REDACTED_EMAIL]'import { UserMemory } from '@ainative/ai-kit-core/memory';
import { ZeroDBMemoryStore } from '@ainative/ai-kit-core/memory';
const store = new ZeroDBMemoryStore({
projectId: process.env.ZERODB_PROJECT_ID,
apiKey: process.env.ZERODB_API_KEY
});
const memory = new UserMemory({
userId: 'user-123',
store
});
// Store facts
await memory.addFact('User prefers dark mode');
await memory.addFact('User is interested in machine learning');
// Retrieve relevant memories
const relevant = await memory.getRelevantMemories('What are my preferences?');
console.log(relevant);import { UsageTracker } from '@ainative/ai-kit-core/tracking';
const tracker = new UsageTracker();
await tracker.trackSuccess({
model: 'gpt-4',
promptTokens: 100,
completionTokens: 50,
durationMs: 1000,
userId: 'user-123'
});
const stats = await tracker.getAggregated();
console.log(`Total cost: $${stats.totalCost}`);
console.log(`Total tokens: ${stats.totalTokens}`);All modules include complete TypeScript definitions:
import type {
AIStreamConfig,
AgentConfig,
ToolDefinition,
SecurityConfig,
MemoryRecord,
UsageRecord
} from '@ainative/ai-kit-core';Most modules support configuration via environment variables or config objects:
// Environment variables
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
ZERODB_PROJECT_ID=proj_...
ZERODB_API_KEY=zdb_...
// Config objects
const config = {
llm: {
provider: 'openai',
model: 'gpt-4',
apiKey: process.env.OPENAI_API_KEY
},
security: {
enablePIIDetection: true,
enablePromptInjection: true
},
memory: {
enabled: true,
maxRecords: 1000
}
};- Always handle errors: Wrap API calls in try-catch blocks
- Use streaming for real-time UX: Stream responses when possible
- Enable security by default: Use PII detection and prompt injection prevention
- Track usage: Monitor costs with UsageTracker
- Implement memory: Use UserMemory for context-aware conversations
- Validate inputs: Always validate user inputs before processing
- Use TypeScript: Leverage type safety for better development experience
MIT