Skip to content

Commit b01fcb7

Browse files
committed
fix: more fixes related to model mapping
1 parent 176128f commit b01fcb7

File tree

8 files changed

+44
-29
lines changed

8 files changed

+44
-29
lines changed

src/ax/ai/anthropic/api.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
AxChatRequest,
66
AxChatResponse,
77
AxChatResponseResult,
8+
AxInternalChatRequest,
89
AxModelConfig
910
} from '../types.js';
1011

@@ -88,9 +89,9 @@ export class AxAIAnthropic extends AxBaseAI<
8889
}
8990

9091
override generateChatReq = (
91-
req: Readonly<AxChatRequest>
92+
req: Readonly<AxInternalChatRequest>
9293
): [API, AxAIAnthropicChatRequest] => {
93-
const model = this.config.model;
94+
const model = req.model;
9495

9596
const apiConfig = {
9697
name: '/messages'

src/ax/ai/base.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import type {
1515
AxChatResponse,
1616
AxEmbedRequest,
1717
AxEmbedResponse,
18+
AxInternalChatRequest,
19+
AxInternalEmbedRequest,
1820
AxModelConfig,
1921
AxModelInfo,
2022
AxModelInfoWithProvider,
@@ -65,10 +67,12 @@ export class AxBaseAI<
6567
> implements AxAIService
6668
{
6769
generateChatReq?: (
68-
req: Readonly<AxChatRequest>,
70+
req: Readonly<AxInternalChatRequest>,
6971
config: Readonly<AxAIPromptConfig>
7072
) => [API, TChatRequest];
71-
generateEmbedReq?: (req: Readonly<AxEmbedRequest>) => [API, TEmbedRequest];
73+
generateEmbedReq?: (
74+
req: Readonly<AxInternalEmbedRequest>
75+
) => [API, TEmbedRequest];
7276
generateChatResp?: (resp: Readonly<TChatResponse>) => AxChatResponse;
7377
generateChatStreamResp?: (
7478
resp: Readonly<TChatResponseDelta>,
@@ -86,10 +90,10 @@ export class AxBaseAI<
8690
private modelInfo: readonly AxModelInfo[];
8791
private modelUsage?: AxTokenUsage;
8892
private embedModelUsage?: AxTokenUsage;
93+
private models: AxBaseAIArgs['models'];
8994

9095
protected apiURL: string;
9196
protected name: string;
92-
protected models: AxBaseAIArgs['models'];
9397
protected headers: Record<string, string>;
9498
protected supportFor: AxBaseAIFeatures;
9599

@@ -266,7 +270,7 @@ export class AxBaseAI<
266270
model,
267271
functions,
268272
modelConfig: { ...chatReq.modelConfig, stream }
269-
} as Readonly<AxChatRequest>;
273+
};
270274

271275
const fn = async () => {
272276
const [apiConfig, reqValue] = reqFn(req, options as AxAIPromptConfig);
@@ -406,7 +410,7 @@ export class AxBaseAI<
406410
const req = {
407411
...embedReq,
408412
embedModel
409-
} as Readonly<AxEmbedRequest>;
413+
};
410414

411415
const fn = async () => {
412416
const [apiConfig, reqValue] = this.generateEmbedReq!(req);

src/ax/ai/cohere/api.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import type {
99
AxAIServiceOptions,
1010
AxChatRequest,
1111
AxChatResponse,
12-
AxEmbedRequest,
1312
AxEmbedResponse,
13+
AxInternalChatRequest,
14+
AxInternalEmbedRequest,
1415
AxModelConfig
1516
} from '../types.js';
1617

@@ -98,11 +99,11 @@ export class AxAICohere extends AxBaseAI<
9899
}
99100

100101
override generateChatReq = (
101-
req: Readonly<AxChatRequest>,
102+
req: Readonly<AxInternalChatRequest>,
102103
// eslint-disable-next-line @typescript-eslint/no-unused-vars
103104
_config: Readonly<AxAIPromptConfig>
104105
): [API, AxAICohereChatRequest] => {
105-
const model = this.config.model;
106+
const model = req.model;
106107

107108
const lastChatMsg = req.chatPrompt.at(-1);
108109
const restOfChat = req.chatPrompt.slice(0, -1);
@@ -187,9 +188,9 @@ export class AxAICohere extends AxBaseAI<
187188
};
188189

189190
override generateEmbedReq = (
190-
req: Readonly<AxEmbedRequest>
191+
req: Readonly<AxInternalEmbedRequest>
191192
): [API, AxAICohereEmbedRequest] => {
192-
const model = this.config.embedModel;
193+
const model = req.embedModel;
193194

194195
if (!model) {
195196
throw new Error('Embed model not set');

src/ax/ai/google-gemini/api.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
} from '../base.js';
77
import type {
88
AxAIServiceOptions,
9-
AxChatRequest,
109
AxChatResponse,
1110
AxChatResponseResult,
12-
AxEmbedRequest,
1311
AxEmbedResponse,
12+
AxInternalChatRequest,
13+
AxInternalEmbedRequest,
1414
AxModelConfig,
1515
AxTokenUsage
1616
} from '../types.js';
@@ -150,9 +150,9 @@ export class AxAIGoogleGemini extends AxBaseAI<
150150
}
151151

152152
override generateChatReq = (
153-
req: Readonly<AxChatRequest>
153+
req: Readonly<AxInternalChatRequest>
154154
): [API, AxAIGoogleGeminiChatRequest] => {
155-
const model = this.config.model;
155+
const model = req.model;
156156
const stream = req.modelConfig?.stream ?? this.config.stream;
157157

158158
if (!req.chatPrompt || req.chatPrompt.length === 0) {
@@ -332,9 +332,9 @@ export class AxAIGoogleGemini extends AxBaseAI<
332332
};
333333

334334
override generateEmbedReq = (
335-
req: Readonly<AxEmbedRequest>
335+
req: Readonly<AxInternalEmbedRequest>
336336
): [API, AxAIGoogleGeminiBatchEmbedRequest] => {
337-
const model = this.config.embedModel;
337+
const model = req.embedModel;
338338

339339
if (!model) {
340340
throw new Error('Embed model not set');

src/ax/ai/huggingface/api.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
import type {
88
AxAIPromptConfig,
99
AxAIServiceOptions,
10-
AxChatRequest,
1110
AxChatResponse,
11+
AxInternalChatRequest,
1212
AxModelConfig
1313
} from '../types.js';
1414

@@ -88,11 +88,11 @@ export class AxAIHuggingFace extends AxBaseAI<
8888
}
8989

9090
override generateChatReq = (
91-
req: Readonly<AxChatRequest>,
91+
req: Readonly<AxInternalChatRequest>,
9292
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9393
_config: Readonly<AxAIPromptConfig>
9494
): [API, AxAIHuggingFaceRequest] => {
95-
const model = this.config.model;
95+
const model = req.model;
9696

9797
const functionsList = req.functions
9898
? `Functions:\n${JSON.stringify(req.functions, null, 2)}\n`

src/ax/ai/openai/api.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import type {
1010
AxChatRequest,
1111
AxChatResponse,
1212
AxChatResponseResult,
13-
AxEmbedRequest,
1413
AxEmbedResponse,
14+
AxInternalChatRequest,
15+
AxInternalEmbedRequest,
1516
AxModelConfig,
1617
AxModelInfo
1718
} from '../types.js';
@@ -121,11 +122,11 @@ export class AxAIOpenAI extends AxBaseAI<
121122
}
122123

123124
override generateChatReq = (
124-
req: Readonly<AxChatRequest>,
125+
req: Readonly<AxInternalChatRequest>,
125126
// eslint-disable-next-line @typescript-eslint/no-unused-vars
126127
_config: Readonly<AxAIPromptConfig>
127128
): [API, AxAIOpenAIChatRequest] => {
128-
const model = this.config.model;
129+
const model = req.model;
129130

130131
if (!req.chatPrompt || req.chatPrompt.length === 0) {
131132
throw new Error('Chat prompt is empty');
@@ -182,9 +183,9 @@ export class AxAIOpenAI extends AxBaseAI<
182183
};
183184

184185
override generateEmbedReq = (
185-
req: Readonly<AxEmbedRequest>
186+
req: Readonly<AxInternalEmbedRequest>
186187
): [API, AxAIOpenAIEmbedRequest] => {
187-
const model = this.config.embedModel;
188+
const model = req.embedModel;
188189

189190
if (!model) {
190191
throw new Error('Embed model not set');

src/ax/ai/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,17 @@ export type AxChatRequest = {
143143
model?: string;
144144
};
145145

146+
export type AxInternalChatRequest = Omit<AxChatRequest, 'model'> &
147+
Required<Pick<AxChatRequest, 'model'>>;
148+
146149
export type AxEmbedRequest = {
147150
texts?: readonly string[];
148151
embedModel?: string;
149152
};
150153

154+
export type AxInternalEmbedRequest = Omit<AxEmbedRequest, 'embedModel'> &
155+
Required<Pick<AxEmbedRequest, 'embedModel'>>;
156+
151157
export type AxRateLimiterFunction = <T = unknown>(
152158
reqFunc: () => Promise<T | ReadableStream<T>>,
153159
info: Readonly<{ modelUsage?: AxTokenUsage; embedModelUsage?: AxTokenUsage }>

src/examples/summarize.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { AxAI, AxAIOpenAIModel, AxChainOfThought } from '@ax-llm/ax';
22

33
const noteText = `The technological singularity—or simply the singularity[1]—is a hypothetical future point in time at which technological growth becomes uncontrollable and irreversible, resulting in unforeseeable changes to human civilization.[2][3] According to the most popular version of the singularity hypothesis, I.J. Good's intelligence explosion model, an upgradable intelligent agent will eventually enter a "runaway reaction" of self-improvement cycles, each new and more intelligent generation appearing more and more rapidly, causing an "explosion" in intelligence and resulting in a powerful superintelligence that qualitatively far surpasses all human intelligence.[4]`;
44

5+
// Example with OpenAI using custom labels in place of model names
56
const ai = new AxAI({
67
name: 'openai',
78
apiKey: process.env.OPENAI_APIKEY as string,
8-
config: { model: AxAIOpenAIModel.GPT35Turbo }
9+
config: { model: 'model-a' },
10+
modelMap: {
11+
'model-a': AxAIOpenAIModel.GPT35Turbo
12+
}
913
});
1014

11-
ai.setOptions({ debug: true });
12-
1315
// const ai = new AxAI({ name: 'ollama', model: 'nous-hermes2' });
1416

1517
const gen = new AxChainOfThought(

0 commit comments

Comments
 (0)