File size: 1,106 Bytes
f415c95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { MaxTokensCache } from "./index.js";

const COHERE_API_URL = "https://api.cohere.ai/v1/models";

// Accept apiKey as an argument
export async function fetchCohereData(apiKey: string | undefined): Promise<MaxTokensCache["cohere"]> {
	if (!apiKey) {
		console.warn("Cohere API key not provided. Skipping Cohere fetch.");
		return {};
	}
	try {
		const response = await fetch(COHERE_API_URL, {
			headers: {
				Authorization: `Bearer ${apiKey}`, // Use passed-in apiKey
			},
		});
		if (!response.ok) {
			throw new Error(`Cohere API request failed: ${response.status} ${response.statusText}`);
		}
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		const data: any = await response.json();
		const modelsData: MaxTokensCache["cohere"] = {};
		if (data?.models && Array.isArray(data.models)) {
			for (const model of data.models) {
				if (model.name && typeof model.context_length === "number") {
					modelsData[model.name] = model.context_length;
				}
			}
		}
		return modelsData;
	} catch (error) {
		console.error("Error fetching Cohere data:", error);
		return {};
	}
}