Skip to content

Commit f24a509

Browse files
committed
feat: implement models() function for OpenAI, Ollama, and Anthropic providers
1 parent e8d0144 commit f24a509

File tree

13 files changed

+155
-60
lines changed

13 files changed

+155
-60
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { anthropic } from "../src";
22

3-
const job = anthropic().listModels();
3+
const job = anthropic().models();
44
const result = await job.run();
55

6-
console.log(result);
6+
console.log(result);
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ollama } from "../src";
22

3-
const job = ollama().listModels();
3+
const job = ollama().models();
44
const result = await job.run();
55

66
console.log(result);
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { openai } from "../src";
22

3-
const job = openai().listModels();
3+
const job = openai().models();
44
const result = await job.run();
55

6-
console.log(result);
6+
console.log(result);

src/jobs/job.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from "zod";
2+
import zodToJsonSchema from "zod-to-json-schema";
23
import { version } from "../../package.json";
34

45
const providerSchema = z.enum([
@@ -146,10 +147,15 @@ const imageJobSchema = baseJobSchema.extend({
146147
params: imageJobParamsSchema,
147148
});
148149

150+
const modelsJobSchema = baseJobSchema.extend({ type: z.literal("models") });
151+
const modelJobSchema = baseJobSchema.extend({ type: z.literal("model") });
152+
149153
export const jobSchema = z.discriminatedUnion("type", [
150154
chatJobSchema,
151155
embeddingJobSchema,
152156
imageJobSchema,
157+
modelsJobSchema,
158+
modelJobSchema,
153159
]);
154160

155161
export class Job {
@@ -180,6 +186,7 @@ export class Job {
180186
}
181187
}
182188

189+
export const jsonSchema = zodToJsonSchema(jobSchema);
183190
export type AIProviderOptions = z.infer<typeof optionsSchema>;
184191
export type ChatJobParams = z.infer<typeof chatJobParamsSchema>;
185192
export type EmbeddingJobParams = z.infer<typeof embeddedJobParamsSchema>;

src/jobs/load.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export function load(obj: AIJob): Job {
3434
if (obj.type === "image" && "image" in provider) {
3535
return provider.image(obj.model)._setParams(obj.params);
3636
}
37+
if (obj.type === "models" && "models" in provider) {
38+
return provider.models();
39+
}
3740

3841
throw new Error("Failed to load job");
3942
}

src/jobs/models.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { Job } from "./job";
22

33
export class ListModelsJob extends Job {
44
constructor() {
5-
super();
6-
this.params = {};
5+
super();
6+
this.params = {};
77
}
88

99
dump() {
1010
const obj = super.dump();
11-
return { ...obj, models: { params: this.params } };
11+
return { ...obj, type: "models", params: this.params };
1212
}
1313
}

src/providers/anthropic.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export function anthropic(options?: AIProviderOptions) {
1010
chat(model: string) {
1111
return new AnthropicChatJob(options, model);
1212
},
13-
listModels() {
13+
models() {
1414
return new AnthropicListModelsJob(options);
15-
}
15+
},
1616
};
1717
}
1818

@@ -79,4 +79,4 @@ export class AnthropicListModelsJob extends ListModelsJob {
7979
const json = await response.json();
8080
return json;
8181
};
82-
}
82+
}

src/providers/ollama.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ export function ollama(options?: AIProviderOptions) {
1010
chat(model: string) {
1111
return new OllamaChatJob(options, model);
1212
},
13-
listModels() {
14-
return new OllamaListModelsJob(options);
15-
},
1613
embedding(model: string) {
1714
return new OllamaEmbeddingJob(options, model);
1815
},
16+
models() {
17+
return new OllamaListModelsJob(options);
18+
},
1919
};
2020
}
2121

src/providers/openai.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ export function openai(options?: AIProviderOptions) {
2121
chat(model: string) {
2222
return new OpenAIChatJob(options, model);
2323
},
24-
listModels() {
25-
return new OpenAIListModelsJob(options);
26-
},
2724
image(model: string) {
2825
return new OpenAIImageJob(options, model);
2926
},
3027
embedding(model: string) {
3128
return new OpenAIEmbeddingJob(options, model);
3229
},
30+
models() {
31+
return new OpenAIListModelsJob(options);
32+
},
3333
};
3434
}
3535

@@ -225,12 +225,11 @@ export class OpenAIChatJob extends ChatJob {
225225
};
226226
}
227227

228-
229228
export class OpenAIListModelsJob extends ListModelsJob {
230229
constructor(options: AIProviderOptions) {
231230
super();
232231
this.provider = "openai";
233-
this.options = options
232+
this.options = options;
234233
}
235234

236235
makeRequest = () => {
@@ -240,7 +239,7 @@ export class OpenAIListModelsJob extends ListModelsJob {
240239
Authorization: `Bearer ${this.options.apiKey}`,
241240
"Content-Type": "application/json",
242241
},
243-
method: "GET"
242+
method: "GET",
244243
});
245244
};
246245

@@ -311,4 +310,4 @@ export class OpenAIEmbeddingJob extends EmbeddingJob {
311310
handleResponse = async (response: Response) => {
312311
return await response.json();
313312
};
314-
}
313+
}

test/__snapshots__/listModels.test.ts.snap renamed to test/__snapshots__/models.snap

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
// Bun Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`listModels 1`] = `
3+
exports[`models 1`] = `
4+
{
5+
"body": "",
6+
"headers": {},
7+
"method": "GET",
8+
"url": "http://localhost:11434/api/tags",
9+
}
10+
`;
11+
12+
exports[`models 2`] = `
413
{
514
"body": "",
615
"headers": {
@@ -12,7 +21,7 @@ exports[`listModels 1`] = `
1221
}
1322
`;
1423

15-
exports[`listModels 2`] = `
24+
exports[`models 3`] = `
1625
{
1726
"body": "",
1827
"headers": {
@@ -27,24 +36,34 @@ exports[`listModels 2`] = `
2736

2837
exports[`dump 1`] = `
2938
{
30-
"models": {
31-
"params": {},
32-
},
39+
"options": {},
40+
"params": {},
41+
"provider": "ollama",
42+
"type": "models",
43+
"version": "0.2.4",
44+
}
45+
`;
46+
47+
exports[`dump 2`] = `
48+
{
3349
"options": {
3450
"apiKey": "<key>",
3551
},
52+
"params": {},
3653
"provider": "openai",
54+
"type": "models",
55+
"version": "0.2.4",
3756
}
3857
`;
3958

40-
exports[`dump 2`] = `
59+
exports[`dump 3`] = `
4160
{
42-
"models": {
43-
"params": {},
44-
},
4561
"options": {
4662
"apiKey": "<key>",
4763
},
64+
"params": {},
4865
"provider": "anthropic",
66+
"type": "models",
67+
"version": "0.2.4",
4968
}
5069
`;

0 commit comments

Comments
 (0)