@perkos/service-ai 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # @perkos/service-ai
2
+
3
+ Multi-provider AI service abstraction with OpenRouter, Replicate, and OpenAI support.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @perkos/service-ai
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { AIService } from "@perkos/service-ai";
15
+
16
+ const aiService = new AIService({
17
+ openRouterApiKey: process.env.OPENROUTER_API_KEY!,
18
+ replicateApiKey: process.env.REPLICATE_API_KEY,
19
+ openAIApiKey: process.env.OPENAI_API_KEY,
20
+ defaultModel: "anthropic/claude-3.5-sonnet",
21
+ });
22
+
23
+ // Analyze an image
24
+ const analysis = await aiService.analyzeImage(imageUrl, {
25
+ prompt: "Describe this image in detail",
26
+ });
27
+
28
+ // Generate an image
29
+ const result = await aiService.generateImage("A sunset over mountains", {
30
+ style: "photorealistic",
31
+ aspectRatio: "16:9",
32
+ });
33
+
34
+ // Transcribe audio
35
+ const transcription = await aiService.transcribeAudio(audioUrl, {
36
+ language: "en",
37
+ });
38
+
39
+ // Generate speech
40
+ const audio = await aiService.synthesizeSpeech("Hello world", {
41
+ voice: "alloy",
42
+ });
43
+ ```
44
+
45
+ ## Available Methods
46
+
47
+ ### Vision & Image
48
+
49
+ - `analyzeImage(imageUrl, options?)` - Analyze image content
50
+ - `generateImage(prompt, options?)` - Generate images with DALL-E or Replicate
51
+
52
+ ### Audio
53
+
54
+ - `transcribeAudio(audioUrl, options?)` - Transcribe audio to text
55
+ - `synthesizeSpeech(text, options?)` - Convert text to speech
56
+
57
+ ### Text Processing
58
+
59
+ - `summarizeText(text, options?)` - Summarize long text
60
+ - `translateText(text, targetLanguage, options?)` - Translate text
61
+ - `analyzeSentiment(text, options?)` - Analyze sentiment
62
+ - `moderateContent(text, options?)` - Content moderation
63
+ - `simplifyText(text, options?)` - Simplify complex text
64
+ - `extractEntities(text, options?)` - Extract named entities
65
+
66
+ ### Content Generation
67
+
68
+ - `generateEmail(context, options?)` - Generate professional emails
69
+ - `generateProductDescription(product, options?)` - E-commerce descriptions
70
+ - `optimizeSEO(content, options?)` - SEO optimization suggestions
71
+
72
+ ### Code & Technical
73
+
74
+ - `generateCode(prompt, options?)` - Generate code snippets
75
+ - `reviewCode(code, options?)` - Code review and suggestions
76
+ - `generateSQLQuery(description, options?)` - Generate SQL queries
77
+ - `generateRegex(description, options?)` - Generate regex patterns
78
+ - `generateAPIDocs(code, options?)` - Generate API documentation
79
+
80
+ ### Other
81
+
82
+ - `extractTextOCR(imageUrl, options?)` - Extract text from images
83
+ - `generateQuiz(topic, options?)` - Generate educational quizzes
84
+
85
+ ## Configuration
86
+
87
+ ```typescript
88
+ interface AIServiceConfig {
89
+ openRouterApiKey: string;
90
+ replicateApiKey?: string;
91
+ openAIApiKey?: string;
92
+ defaultModel?: string;
93
+ imageModel?: string;
94
+ siteUrl?: string;
95
+ siteName?: string;
96
+ }
97
+ ```
98
+
99
+ ## Provider Fallback
100
+
101
+ The service automatically falls back between providers:
102
+
103
+ - **Image Generation**: Tries OpenAI DALL-E first, falls back to Replicate SDXL
104
+ - **Speech Synthesis**: Uses OpenAI TTS with fallback to Replicate
105
+ - **Text Tasks**: Uses OpenRouter with configurable models
106
+
107
+ ## Supported Models
108
+
109
+ ### OpenRouter (Text/Vision)
110
+
111
+ - `anthropic/claude-3.5-sonnet` (default)
112
+ - `openai/gpt-4o`
113
+ - `google/gemini-pro`
114
+ - Any model supported by OpenRouter
115
+
116
+ ### OpenAI
117
+
118
+ - `dall-e-3` for image generation
119
+ - `whisper-1` for transcription
120
+ - `tts-1` / `tts-1-hd` for speech
121
+
122
+ ### Replicate
123
+
124
+ - `black-forest-labs/flux-schnell` for fast image generation
125
+ - `stability-ai/sdxl` for high-quality images
126
+ - Various other models as fallbacks
127
+
128
+ ## Error Handling
129
+
130
+ ```typescript
131
+ try {
132
+ const result = await aiService.generateImage(prompt);
133
+ } catch (error) {
134
+ if (error.message.includes("rate limit")) {
135
+ // Handle rate limiting
136
+ }
137
+ // Handle other errors
138
+ }
139
+ ```
140
+
141
+ ## License
142
+
143
+ MIT
@@ -0,0 +1,234 @@
1
+ /**
2
+ * AI Service Types
3
+ */
4
+ interface AIServiceConfig {
5
+ openrouterApiKey?: string;
6
+ openaiApiKey?: string;
7
+ replicateApiToken?: string;
8
+ openrouterBaseUrl?: string;
9
+ openrouterReferer?: string;
10
+ openrouterTitle?: string;
11
+ imageModel?: string;
12
+ ttsModel?: string;
13
+ whisperModel?: string;
14
+ moderationModel?: string;
15
+ chatModel?: string;
16
+ }
17
+ interface ImageGenerateResult {
18
+ url?: string;
19
+ base64?: string;
20
+ revisedPrompt?: string;
21
+ }
22
+ interface TranslationResult {
23
+ translation: string;
24
+ confidence: number;
25
+ }
26
+ interface SentimentResult {
27
+ sentiment: "positive" | "negative" | "neutral";
28
+ score: number;
29
+ emotions: string[];
30
+ }
31
+ interface ModerationResult {
32
+ flagged: boolean;
33
+ categories: Record<string, boolean>;
34
+ categoryScores: Record<string, number>;
35
+ reasoning?: string;
36
+ }
37
+ interface EntityResult {
38
+ entities: Array<{
39
+ text: string;
40
+ type: string;
41
+ position: number;
42
+ }>;
43
+ }
44
+ interface EmailResult {
45
+ subject: string;
46
+ body: string;
47
+ }
48
+ interface CodeGenerateResult {
49
+ code: string;
50
+ explanation: string;
51
+ }
52
+ interface CodeReviewResult {
53
+ issues: string[];
54
+ suggestions: string[];
55
+ securityConcerns: string[];
56
+ }
57
+ interface SQLResult {
58
+ query: string;
59
+ explanation: string;
60
+ }
61
+ interface RegexResult {
62
+ pattern: string;
63
+ explanation: string;
64
+ examples: string[];
65
+ }
66
+ interface SEOResult {
67
+ optimizedContent: string;
68
+ analysis: string;
69
+ }
70
+ interface APIDocsResult {
71
+ documentation: string;
72
+ openapi?: string;
73
+ }
74
+ interface OCRResult {
75
+ text: string;
76
+ confidence: number;
77
+ }
78
+ interface QuizQuestion {
79
+ question: string;
80
+ options: string[];
81
+ correctIndex: number;
82
+ explanation: string;
83
+ }
84
+ interface QuizResult {
85
+ questions: QuizQuestion[];
86
+ }
87
+ type SummaryLength = "short" | "medium" | "long";
88
+ type ReadingLevel = "elementary" | "middle" | "high";
89
+ type EmailTone = "formal" | "casual" | "friendly";
90
+ type Difficulty = "easy" | "medium" | "hard";
91
+ type Voice = "alloy" | "echo" | "fable" | "onyx" | "nova" | "shimmer";
92
+ type AIProvider = "openrouter" | "openai" | "replicate";
93
+ type TranscriptionResult = {
94
+ text: string;
95
+ confidence?: number;
96
+ };
97
+ type EntityExtractionResult = EntityResult;
98
+ type CodeGenerationResult = CodeGenerateResult;
99
+ interface ImageAnalysisOptions {
100
+ detail?: "low" | "high" | "auto";
101
+ maxTokens?: number;
102
+ }
103
+ interface ImageGenerateOptions {
104
+ size?: "1024x1024" | "1792x1024" | "1024x1792";
105
+ quality?: "standard" | "hd";
106
+ style?: "vivid" | "natural";
107
+ n?: number;
108
+ }
109
+ interface TranscribeOptions {
110
+ language?: string;
111
+ prompt?: string;
112
+ responseFormat?: "json" | "text" | "srt" | "vtt";
113
+ }
114
+ interface SpeechOptions {
115
+ voice?: Voice;
116
+ speed?: number;
117
+ responseFormat?: "mp3" | "opus" | "aac" | "flac";
118
+ }
119
+ interface SummarizeOptions {
120
+ length?: SummaryLength;
121
+ format?: "paragraph" | "bullets";
122
+ }
123
+ interface TranslateOptions {
124
+ targetLanguage: string;
125
+ sourceLanguage?: string;
126
+ preserveFormatting?: boolean;
127
+ }
128
+ interface SentimentOptions {
129
+ detailed?: boolean;
130
+ includeEmotions?: boolean;
131
+ }
132
+ interface ModerationOptions {
133
+ categories?: string[];
134
+ threshold?: number;
135
+ }
136
+ interface SimplifyOptions {
137
+ targetLevel?: ReadingLevel;
138
+ preserveMeaning?: boolean;
139
+ }
140
+ interface EntityOptions {
141
+ types?: string[];
142
+ includePositions?: boolean;
143
+ }
144
+ interface EmailOptions {
145
+ tone?: EmailTone;
146
+ length?: SummaryLength;
147
+ includeSubject?: boolean;
148
+ }
149
+ interface ProductDescriptionOptions {
150
+ tone?: "professional" | "casual" | "luxury";
151
+ length?: SummaryLength;
152
+ includeBenefits?: boolean;
153
+ }
154
+ interface SEOOptions {
155
+ keywords?: string[];
156
+ targetLength?: number;
157
+ includeMetaTags?: boolean;
158
+ }
159
+ interface CodeGenerateOptions {
160
+ language: string;
161
+ includeComments?: boolean;
162
+ includeTests?: boolean;
163
+ }
164
+ interface CodeReviewOptions {
165
+ focus?: "security" | "performance" | "style" | "all";
166
+ severity?: "all" | "critical" | "high";
167
+ }
168
+ interface SQLQueryOptions {
169
+ dialect?: "mysql" | "postgresql" | "sqlite";
170
+ includeExplanation?: boolean;
171
+ }
172
+ interface RegexOptions {
173
+ flavor?: "javascript" | "python" | "pcre";
174
+ includeExamples?: boolean;
175
+ }
176
+ interface APIDocsOptions {
177
+ format?: "markdown" | "openapi";
178
+ includeExamples?: boolean;
179
+ }
180
+ interface OCROptions {
181
+ language?: string;
182
+ format?: "text" | "structured";
183
+ }
184
+ interface QuizOptions {
185
+ count?: number;
186
+ difficulty?: Difficulty;
187
+ includeExplanations?: boolean;
188
+ }
189
+
190
+ /**
191
+ * AI Service Implementation
192
+ * Multi-provider AI service with OpenRouter, Replicate, and OpenAI support
193
+ */
194
+
195
+ declare class AIService {
196
+ private openai;
197
+ private openrouter;
198
+ private replicate;
199
+ private useOpenRouter;
200
+ private useReplicate;
201
+ private imageModel;
202
+ private ttsModel;
203
+ private whisperModel;
204
+ private moderationModel;
205
+ private chatModel;
206
+ constructor(config?: AIServiceConfig);
207
+ private getChatClient;
208
+ private getChatModel;
209
+ analyzeImage(imageInput: string, question?: string): Promise<string>;
210
+ generateImage(prompt: string, size?: "1024x1024"): Promise<ImageGenerateResult>;
211
+ private generateImageWithReplicate;
212
+ transcribeAudio(audioInput: File | Blob | string): Promise<string>;
213
+ private transcribeWithReplicate;
214
+ synthesizeSpeech(text: string, voice?: Voice): Promise<Buffer>;
215
+ private synthesizeWithReplicate;
216
+ summarizeText(text: string, length?: SummaryLength): Promise<string>;
217
+ translateText(text: string, sourceLang: string, targetLang: string): Promise<TranslationResult>;
218
+ analyzeSentiment(text: string): Promise<SentimentResult>;
219
+ moderateContent(content: string): Promise<ModerationResult>;
220
+ simplifyText(text: string, readingLevel?: ReadingLevel): Promise<string>;
221
+ extractEntities(text: string): Promise<EntityResult>;
222
+ generateEmail(purpose: string, tone: EmailTone | undefined, keyPoints: string[]): Promise<EmailResult>;
223
+ generateProductDescription(productName: string, features: string[], targetAudience?: string): Promise<string>;
224
+ optimizeSEO(content: string, keywords: string[]): Promise<SEOResult>;
225
+ generateCode(description: string, language: string, framework?: string): Promise<CodeGenerateResult>;
226
+ reviewCode(code: string, language: string): Promise<CodeReviewResult>;
227
+ generateSQLQuery(schema: string, query: string): Promise<SQLResult>;
228
+ generateRegex(description: string): Promise<RegexResult>;
229
+ generateAPIDocs(code: string, framework: string): Promise<APIDocsResult>;
230
+ extractTextOCR(image: string): Promise<OCRResult>;
231
+ generateQuiz(topic: string, numQuestions?: number, difficulty?: Difficulty): Promise<QuizResult>;
232
+ }
233
+
234
+ export { type AIProvider, AIService, type AIServiceConfig, type APIDocsOptions, type CodeGenerateOptions, type CodeGenerationResult, type CodeReviewOptions, type CodeReviewResult, type EmailOptions, type EntityExtractionResult, type EntityOptions, type ImageAnalysisOptions, type ImageGenerateOptions, type ImageGenerateResult, type ModerationOptions, type ModerationResult, type OCROptions, type OCRResult, type ProductDescriptionOptions, type QuizOptions, type QuizResult, type RegexOptions, type SEOOptions, type SQLQueryOptions, type SentimentOptions, type SentimentResult, type SimplifyOptions, type SpeechOptions, type SummarizeOptions, type TranscribeOptions, type TranscriptionResult, type TranslateOptions, type TranslationResult };
@@ -0,0 +1,234 @@
1
+ /**
2
+ * AI Service Types
3
+ */
4
+ interface AIServiceConfig {
5
+ openrouterApiKey?: string;
6
+ openaiApiKey?: string;
7
+ replicateApiToken?: string;
8
+ openrouterBaseUrl?: string;
9
+ openrouterReferer?: string;
10
+ openrouterTitle?: string;
11
+ imageModel?: string;
12
+ ttsModel?: string;
13
+ whisperModel?: string;
14
+ moderationModel?: string;
15
+ chatModel?: string;
16
+ }
17
+ interface ImageGenerateResult {
18
+ url?: string;
19
+ base64?: string;
20
+ revisedPrompt?: string;
21
+ }
22
+ interface TranslationResult {
23
+ translation: string;
24
+ confidence: number;
25
+ }
26
+ interface SentimentResult {
27
+ sentiment: "positive" | "negative" | "neutral";
28
+ score: number;
29
+ emotions: string[];
30
+ }
31
+ interface ModerationResult {
32
+ flagged: boolean;
33
+ categories: Record<string, boolean>;
34
+ categoryScores: Record<string, number>;
35
+ reasoning?: string;
36
+ }
37
+ interface EntityResult {
38
+ entities: Array<{
39
+ text: string;
40
+ type: string;
41
+ position: number;
42
+ }>;
43
+ }
44
+ interface EmailResult {
45
+ subject: string;
46
+ body: string;
47
+ }
48
+ interface CodeGenerateResult {
49
+ code: string;
50
+ explanation: string;
51
+ }
52
+ interface CodeReviewResult {
53
+ issues: string[];
54
+ suggestions: string[];
55
+ securityConcerns: string[];
56
+ }
57
+ interface SQLResult {
58
+ query: string;
59
+ explanation: string;
60
+ }
61
+ interface RegexResult {
62
+ pattern: string;
63
+ explanation: string;
64
+ examples: string[];
65
+ }
66
+ interface SEOResult {
67
+ optimizedContent: string;
68
+ analysis: string;
69
+ }
70
+ interface APIDocsResult {
71
+ documentation: string;
72
+ openapi?: string;
73
+ }
74
+ interface OCRResult {
75
+ text: string;
76
+ confidence: number;
77
+ }
78
+ interface QuizQuestion {
79
+ question: string;
80
+ options: string[];
81
+ correctIndex: number;
82
+ explanation: string;
83
+ }
84
+ interface QuizResult {
85
+ questions: QuizQuestion[];
86
+ }
87
+ type SummaryLength = "short" | "medium" | "long";
88
+ type ReadingLevel = "elementary" | "middle" | "high";
89
+ type EmailTone = "formal" | "casual" | "friendly";
90
+ type Difficulty = "easy" | "medium" | "hard";
91
+ type Voice = "alloy" | "echo" | "fable" | "onyx" | "nova" | "shimmer";
92
+ type AIProvider = "openrouter" | "openai" | "replicate";
93
+ type TranscriptionResult = {
94
+ text: string;
95
+ confidence?: number;
96
+ };
97
+ type EntityExtractionResult = EntityResult;
98
+ type CodeGenerationResult = CodeGenerateResult;
99
+ interface ImageAnalysisOptions {
100
+ detail?: "low" | "high" | "auto";
101
+ maxTokens?: number;
102
+ }
103
+ interface ImageGenerateOptions {
104
+ size?: "1024x1024" | "1792x1024" | "1024x1792";
105
+ quality?: "standard" | "hd";
106
+ style?: "vivid" | "natural";
107
+ n?: number;
108
+ }
109
+ interface TranscribeOptions {
110
+ language?: string;
111
+ prompt?: string;
112
+ responseFormat?: "json" | "text" | "srt" | "vtt";
113
+ }
114
+ interface SpeechOptions {
115
+ voice?: Voice;
116
+ speed?: number;
117
+ responseFormat?: "mp3" | "opus" | "aac" | "flac";
118
+ }
119
+ interface SummarizeOptions {
120
+ length?: SummaryLength;
121
+ format?: "paragraph" | "bullets";
122
+ }
123
+ interface TranslateOptions {
124
+ targetLanguage: string;
125
+ sourceLanguage?: string;
126
+ preserveFormatting?: boolean;
127
+ }
128
+ interface SentimentOptions {
129
+ detailed?: boolean;
130
+ includeEmotions?: boolean;
131
+ }
132
+ interface ModerationOptions {
133
+ categories?: string[];
134
+ threshold?: number;
135
+ }
136
+ interface SimplifyOptions {
137
+ targetLevel?: ReadingLevel;
138
+ preserveMeaning?: boolean;
139
+ }
140
+ interface EntityOptions {
141
+ types?: string[];
142
+ includePositions?: boolean;
143
+ }
144
+ interface EmailOptions {
145
+ tone?: EmailTone;
146
+ length?: SummaryLength;
147
+ includeSubject?: boolean;
148
+ }
149
+ interface ProductDescriptionOptions {
150
+ tone?: "professional" | "casual" | "luxury";
151
+ length?: SummaryLength;
152
+ includeBenefits?: boolean;
153
+ }
154
+ interface SEOOptions {
155
+ keywords?: string[];
156
+ targetLength?: number;
157
+ includeMetaTags?: boolean;
158
+ }
159
+ interface CodeGenerateOptions {
160
+ language: string;
161
+ includeComments?: boolean;
162
+ includeTests?: boolean;
163
+ }
164
+ interface CodeReviewOptions {
165
+ focus?: "security" | "performance" | "style" | "all";
166
+ severity?: "all" | "critical" | "high";
167
+ }
168
+ interface SQLQueryOptions {
169
+ dialect?: "mysql" | "postgresql" | "sqlite";
170
+ includeExplanation?: boolean;
171
+ }
172
+ interface RegexOptions {
173
+ flavor?: "javascript" | "python" | "pcre";
174
+ includeExamples?: boolean;
175
+ }
176
+ interface APIDocsOptions {
177
+ format?: "markdown" | "openapi";
178
+ includeExamples?: boolean;
179
+ }
180
+ interface OCROptions {
181
+ language?: string;
182
+ format?: "text" | "structured";
183
+ }
184
+ interface QuizOptions {
185
+ count?: number;
186
+ difficulty?: Difficulty;
187
+ includeExplanations?: boolean;
188
+ }
189
+
190
+ /**
191
+ * AI Service Implementation
192
+ * Multi-provider AI service with OpenRouter, Replicate, and OpenAI support
193
+ */
194
+
195
+ declare class AIService {
196
+ private openai;
197
+ private openrouter;
198
+ private replicate;
199
+ private useOpenRouter;
200
+ private useReplicate;
201
+ private imageModel;
202
+ private ttsModel;
203
+ private whisperModel;
204
+ private moderationModel;
205
+ private chatModel;
206
+ constructor(config?: AIServiceConfig);
207
+ private getChatClient;
208
+ private getChatModel;
209
+ analyzeImage(imageInput: string, question?: string): Promise<string>;
210
+ generateImage(prompt: string, size?: "1024x1024"): Promise<ImageGenerateResult>;
211
+ private generateImageWithReplicate;
212
+ transcribeAudio(audioInput: File | Blob | string): Promise<string>;
213
+ private transcribeWithReplicate;
214
+ synthesizeSpeech(text: string, voice?: Voice): Promise<Buffer>;
215
+ private synthesizeWithReplicate;
216
+ summarizeText(text: string, length?: SummaryLength): Promise<string>;
217
+ translateText(text: string, sourceLang: string, targetLang: string): Promise<TranslationResult>;
218
+ analyzeSentiment(text: string): Promise<SentimentResult>;
219
+ moderateContent(content: string): Promise<ModerationResult>;
220
+ simplifyText(text: string, readingLevel?: ReadingLevel): Promise<string>;
221
+ extractEntities(text: string): Promise<EntityResult>;
222
+ generateEmail(purpose: string, tone: EmailTone | undefined, keyPoints: string[]): Promise<EmailResult>;
223
+ generateProductDescription(productName: string, features: string[], targetAudience?: string): Promise<string>;
224
+ optimizeSEO(content: string, keywords: string[]): Promise<SEOResult>;
225
+ generateCode(description: string, language: string, framework?: string): Promise<CodeGenerateResult>;
226
+ reviewCode(code: string, language: string): Promise<CodeReviewResult>;
227
+ generateSQLQuery(schema: string, query: string): Promise<SQLResult>;
228
+ generateRegex(description: string): Promise<RegexResult>;
229
+ generateAPIDocs(code: string, framework: string): Promise<APIDocsResult>;
230
+ extractTextOCR(image: string): Promise<OCRResult>;
231
+ generateQuiz(topic: string, numQuestions?: number, difficulty?: Difficulty): Promise<QuizResult>;
232
+ }
233
+
234
+ export { type AIProvider, AIService, type AIServiceConfig, type APIDocsOptions, type CodeGenerateOptions, type CodeGenerationResult, type CodeReviewOptions, type CodeReviewResult, type EmailOptions, type EntityExtractionResult, type EntityOptions, type ImageAnalysisOptions, type ImageGenerateOptions, type ImageGenerateResult, type ModerationOptions, type ModerationResult, type OCROptions, type OCRResult, type ProductDescriptionOptions, type QuizOptions, type QuizResult, type RegexOptions, type SEOOptions, type SQLQueryOptions, type SentimentOptions, type SentimentResult, type SimplifyOptions, type SpeechOptions, type SummarizeOptions, type TranscribeOptions, type TranscriptionResult, type TranslateOptions, type TranslationResult };