@angularai/core
v0.0.3
Published
<div align="center"> <h1>@angularai/core</h1> <p>π§ Core AI functionality for Angular applications</p>
Downloads
544
Readme
Overview
@angularai/core provides a unified interface for working with multiple AI providers in Angular applications. It serves as the foundation for all AngularAI components, offering a consistent API for interacting with various AI services.
β¨ Features
- π Multi-provider support: Works with OpenAI, Claude, Gemini, HuggingFace, Ollama, DeepSeek, and more
- π Fallback mechanism: Continues to work even without API keys during development
- π Streaming support: Real-time streaming of AI responses via RxJS Observables
- π‘οΈ Type safety: Full TypeScript support with comprehensive type definitions
- π§© Modular design: Use only what you need with tree-shakable exports
- π§ Customizable: Configure providers, models, and parameters
- π± Angular-native: Built with Angular services, dependency injection, and RxJS
π¦ Installation
npm install @angularai/coreπ Quick Start
1. Configure the AI Provider
In your app.config.ts or module:
import { ApplicationConfig } from '@angular/core';
import { provideAIClient, AI_CONFIG } from '@angularai/core';
export const appConfig: ApplicationConfig = {
providers: [
provideAIClient({
provider: 'openai',
apiKey: 'your-api-key', // Use environment variables in production
model: 'gpt-4o'
})
]
};2. Use the AI Client Service
import { Component, inject } from '@angular/core';
import { AIClientService } from '@angularai/core';
@Component({
selector: 'app-example',
template: `
<button (click)="askAI()">Ask AI</button>
<p>{{ response }}</p>
`
})
export class ExampleComponent {
private aiClient = inject(AIClientService);
response = '';
askAI() {
this.aiClient.chat([
{ role: 'user', content: 'Hello, can you help me with Angular?' }
]).subscribe({
next: (response) => this.response = response,
error: (error) => console.error('Error:', error)
});
}
}π Streaming Responses
import { Component, inject } from '@angular/core';
import { AIClientService } from '@angularai/core';
@Component({
selector: 'app-streaming',
template: `
<button (click)="streamResponse()">Stream Response</button>
<p>{{ streamedText }}</p>
`
})
export class StreamingComponent {
private aiClient = inject(AIClientService);
streamedText = '';
streamResponse() {
this.streamedText = '';
this.aiClient.chatStream([
{ role: 'user', content: 'Write a short poem about Angular' }
]).subscribe({
next: (token) => this.streamedText += token,
complete: () => console.log('Stream complete'),
error: (error) => console.error('Error:', error)
});
}
}π Supported Providers
| Provider | Models | Status | |----------|--------|--------| | OpenAI | GPT-4o, GPT-4, GPT-3.5-turbo | β Available | | Anthropic | Claude 3.5 Sonnet, Claude 3 Opus | β Available | | Google | Gemini Pro, Gemini Ultra | β Available | | HuggingFace | Open-source models | β Available | | Ollama | Local LLM deployment | β Available | | DeepSeek | DeepSeek models | β Available | | Fallback | Mock responses for development | β Available |
π API Reference
AIClientService
@Injectable({ providedIn: 'root' })
export class AIClientService {
// Send a chat request
chat(messages: Message[], options?: ChatOptions): Observable<string>;
// Stream a chat response token by token
chatStream(messages: Message[], options?: ChatOptions): Observable<string>;
// Simple ask method for single questions
ask(prompt: string, options?: ChatOptions): Observable<string>;
// Configure the AI client
configure(config: AIConfig): void;
}Configuration Options
interface AIConfig {
provider: 'openai' | 'claude' | 'gemini' | 'huggingface' | 'ollama' | 'deepseek' | 'fallback';
apiKey?: string;
model?: string;
baseUrl?: string;
organizationId?: string;
temperature?: number;
maxTokens?: number;
}Message Interface
interface Message {
role: 'system' | 'user' | 'assistant';
content: string;
}π§ Advanced Configuration
Runtime Configuration
import { Component, inject } from '@angular/core';
import { AIClientService } from '@angularai/core';
@Component({ ... })
export class ConfigComponent {
private aiClient = inject(AIClientService);
switchToClaudeAI() {
this.aiClient.configure({
provider: 'claude',
apiKey: 'your-anthropic-key',
model: 'claude-3-sonnet-20240229'
});
}
}Using with Environment Variables
// environment.ts
export const environment = {
openaiApiKey: 'your-api-key'
};
// app.config.ts
import { environment } from './environments/environment';
export const appConfig: ApplicationConfig = {
providers: [
provideAIClient({
provider: 'openai',
apiKey: environment.openaiApiKey
})
]
};π¦ Related Packages
Explore the complete @angularai ecosystem:
| Package | Description | |---------|-------------| | @angularai/chatbot | AI-powered chat components | | @angularai/autosuggest | Smart AI suggestions | | @angularai/smartform | AI form validation | | @angularai/analytics | AI-powered analytics | | @angularai/image-caption | AI image captioning | | @angularai/emotion-ui | Emotion-aware UI | | @angularai/doc-intelligence | Document processing | | @angularai/predictive-input | Predictive text input | | @angularai/smart-notify | Smart notifications | | @angularai/voice-actions | Voice commands | | @angularai/smart-datatable | AI data tables | | @angularai/spin-360 | 360Β° product viewer with AI generation |
π Related Projects
| Framework | Repository | Status | |-----------|-----------|--------| | Vue.js | @aivue | β Available | | React | @anthropic-ai/react | β Available | | Angular | @angularai | β Available | | Svelte | @svelteai | π‘ Planned |
π License
MIT Β© AngularAI
