-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapi-sniffer.ts
More file actions
153 lines (145 loc) · 4.48 KB
/
api-sniffer.ts
File metadata and controls
153 lines (145 loc) · 4.48 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
export interface ApiInfo {
provider:
| "openai"
| "anthropic"
| "deepseek"
| "openrouter"
| "hyperbolic"
| "vllm"
| "kobold-cpp"
| "llama-server"
| "nous"
| "unknown";
supportsLogprobs: "yes" | "no" | "unknown";
supportsPrefill: "yes" | "no" | "unknown";
prefillStyle?: { kind: "trailing" } | { kind: "flags"; flags: Record<string, any>; target: "body" | "message" };
needsTemperature?: number;
onlySupportsModels?: string[];
extraWarning?: string;
}
const UNKNOWN_API: ApiInfo = {
provider: "unknown",
supportsLogprobs: "unknown",
supportsPrefill: "unknown",
};
export async function sniffApi(baseUrl: string, apiKey: string): Promise<ApiInfo> {
baseUrl = baseUrl.replace(/\/+$/, "");
// walk up baseUrl in case /models is hosted on a higher path (e.g. deepseek has api.deepseek.com/models but not /beta/models)
for (let i = 0; i < 3; i++) {
try {
const info = await _sniffApi(baseUrl, apiKey);
if (info.provider !== "unknown") {
return info;
}
} catch (e) {
console.log(`/models error for ${baseUrl}:`, e);
}
baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf("/"));
if (!baseUrl.includes("://")) {
break;
}
}
return UNKNOWN_API;
}
async function _sniffApi(baseUrl: string, apiKey: string): Promise<ApiInfo> {
// we could just check baseUrl here, but it might be proxied.
const response = await fetch(`${baseUrl}/models`, {
headers: {
Authorization: `Bearer ${apiKey}`,
"x-api-key": apiKey,
"anthropic-dangerous-direct-browser-access": "true",
},
redirect: "follow",
});
console.log(`/models response for ${baseUrl}:`, response);
if (response.status === 200) {
let json;
try {
json = await response.json();
} catch (e) {
console.error(`Getting /models from ${baseUrl}: 200, but not JSON.`);
return UNKNOWN_API;
}
const models = (json.data ?? []).map((m: any) => m.id);
const owners = (json.data ?? []).map((m: any) => m.owned_by);
if (owners.includes("koboldcpp")) {
return {
provider: "kobold-cpp",
supportsLogprobs: "yes",
supportsPrefill: "unknown",
prefillStyle: { kind: "trailing" },
};
} else if (owners.includes("llamacpp")) {
return {
provider: "llama-server",
supportsLogprobs: "yes",
supportsPrefill: "yes",
prefillStyle: { kind: "trailing" },
};
} else if (owners.includes("vllm")) {
return {
provider: "vllm",
supportsLogprobs: "yes",
supportsPrefill: "yes",
prefillStyle: {
kind: "flags",
flags: { continue_final_message: true, add_generation_prompt: false },
target: "body",
},
extraWarning:
"Relaunch VLLM with VLLM_USE_V1=0 if you notice tokens like 'Ġhello'. See vllm-project/vllm#16838",
};
} else if (models.includes("openrouter/auto")) {
return {
provider: "openrouter",
supportsLogprobs: "unknown",
supportsPrefill: "unknown",
prefillStyle: { kind: "trailing" },
};
} else if (owners.includes("Hyperbolic")) {
return {
provider: "hyperbolic",
supportsLogprobs: "unknown", // yes for some models, not for others?
supportsPrefill: "unknown", // same
prefillStyle: { kind: "trailing" },
};
} else if (models.includes("chatgpt-4o-latest")) {
return {
provider: "openai",
supportsLogprobs: "yes",
supportsPrefill: "no",
};
} else if (models.includes("deepseek-chat")) {
return {
provider: "deepseek",
supportsLogprobs: "yes",
supportsPrefill: "yes",
prefillStyle: { kind: "flags", flags: { prefix: true }, target: "message" },
needsTemperature: 1.0,
onlySupportsModels: ["deepseek-chat"],
};
} else if (models.includes("DeepHermes-3-Mistral-24B-Preview")) {
return {
provider: "nous",
supportsLogprobs: "yes",
supportsPrefill: "yes",
prefillStyle: {
kind: "flags",
flags: { continue_final_message: true, add_generation_prompt: false },
target: "body",
},
};
}
} else {
const error = await response.text();
if (error.includes("anthropic-version")) {
return {
provider: "anthropic",
supportsLogprobs: "no",
supportsPrefill: "yes",
prefillStyle: { kind: "trailing" },
};
}
}
return UNKNOWN_API;
}