File size: 1,993 Bytes
15a5288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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);
  }
}