- Automatic browser geolocation capture on event creation - Reverse geocoding via Nominatim API for place names - Full-text search with SQLite FTS5 - Calendar view for browsing past entries - DateNavigator component for day navigation - SearchModal with Ctrl+K shortcut - QuickAddWidget with Ctrl+J shortcut - Starlight documentation site with GitHub Pages deployment - Multiple AI provider support (Groq, OpenAI, Anthropic, Ollama, LM Studio) - Multi-user registration support BREAKING: Events now include latitude/longitude/placeName fields
92 lines
2.5 KiB
TypeScript
92 lines
2.5 KiB
TypeScript
import type { AIProvider, AIProviderConfig, AIProviderResult } from './provider';
|
|
|
|
export class GroqProvider implements AIProvider {
|
|
provider = 'groq' as const;
|
|
private apiKey: string;
|
|
private model: string;
|
|
private baseUrl: string;
|
|
|
|
constructor(config: AIProviderConfig) {
|
|
this.apiKey = config.apiKey;
|
|
this.model = config.model || 'llama-3.3-70b-versatile';
|
|
this.baseUrl = config.baseUrl || 'https://api.groq.com/openai/v1';
|
|
}
|
|
|
|
async generate(prompt: string, systemPrompt?: string, options?: { jsonMode?: boolean }): Promise<AIProviderResult> {
|
|
const messages: Array<{ role: string; content: string }> = [];
|
|
|
|
if (systemPrompt) {
|
|
messages.push({ role: 'system', content: systemPrompt });
|
|
}
|
|
|
|
messages.push({ role: 'user', content: prompt });
|
|
|
|
const requestBody: Record<string, unknown> = {
|
|
model: this.model,
|
|
messages,
|
|
temperature: 0.7,
|
|
max_tokens: 2000,
|
|
};
|
|
|
|
if (options?.jsonMode) {
|
|
requestBody.response_format = { type: 'json_object' };
|
|
}
|
|
|
|
const response = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${this.apiKey}`,
|
|
},
|
|
body: JSON.stringify(requestBody),
|
|
});
|
|
|
|
const responseText = await response.text();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Groq API error: ${response.status} ${responseText}`);
|
|
}
|
|
|
|
const responseData = JSON.parse(responseText);
|
|
let content = responseData.choices?.[0]?.message?.content || '';
|
|
let title: string | undefined;
|
|
|
|
if (options?.jsonMode) {
|
|
try {
|
|
const parsed = JSON.parse(content);
|
|
title = parsed.title;
|
|
content = parsed.content || content;
|
|
} catch {
|
|
// If JSON parsing fails, use content as-is
|
|
}
|
|
}
|
|
|
|
return {
|
|
content,
|
|
title,
|
|
request: requestBody,
|
|
response: responseData,
|
|
};
|
|
}
|
|
|
|
async validate(): Promise<boolean> {
|
|
try {
|
|
const response = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${this.apiKey}`,
|
|
},
|
|
body: JSON.stringify({
|
|
model: this.model,
|
|
messages: [{ role: 'user', content: 'test' }],
|
|
max_tokens: 5,
|
|
}),
|
|
});
|
|
return response.ok || response.status === 400;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
}
|