Spaces:
Running
Running
import { BaseProviderFetcher } from "./base"; | |
import { ProviderEntry, GroqModel } from "./types"; | |
export class GroqFetcher extends BaseProviderFetcher { | |
name = "groq"; | |
constructor(apiKey?: string) { | |
super("https://api.groq.com", apiKey, { | |
requestsPerMinute: 100, // Groq rate limit from spec | |
}); | |
} | |
async fetchModels(): Promise<ProviderEntry[]> { | |
try { | |
const response = await this.fetchWithRetry<{ data: GroqModel[] }>( | |
`${this.baseUrl}/openai/v1/models` | |
); | |
return response.data.map((model) => this.mapModelToProviderEntry(model)); | |
} catch (error) { | |
console.error(`Failed to fetch Groq models: ${error}`); | |
return []; | |
} | |
} | |
async fetchModel(modelId: string): Promise<ProviderEntry | null> { | |
try { | |
const response = await this.fetchWithRetry<GroqModel>( | |
`${this.baseUrl}/openai/v1/models/${encodeURIComponent(modelId)}` | |
); | |
return this.mapModelToProviderEntry(response); | |
} catch (error) { | |
console.error(`Failed to fetch Groq model ${modelId}: ${error}`); | |
return null; | |
} | |
} | |
private mapModelToProviderEntry(model: GroqModel): ProviderEntry { | |
const entry: ProviderEntry = { | |
provider: this.name, | |
context_length: model.context_window, | |
max_completion_tokens: model.max_completion_tokens, | |
status: model.active ? "live" : "offline", | |
owned_by: model.owned_by, | |
}; | |
// Store the model ID for matching | |
(entry as any).id = model.id; | |
// Add static pricing from Groq's website if not provided by API | |
if (!entry.pricing) { | |
const staticPricing = this.getStaticPricing(model.id); | |
if (staticPricing) { | |
entry.pricing = staticPricing; | |
} | |
} | |
return entry; | |
} | |
private getStaticPricing(modelId: string): { input: number; output: number } | null { | |
// Import static pricing data | |
const { getStaticPricing } = require('./static-pricing'); | |
return getStaticPricing('groq', modelId); | |
} | |
} | |