@node-llm/core 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -25
- package/dist/chat/Chat.d.ts +26 -1
- package/dist/chat/Chat.d.ts.map +1 -1
- package/dist/chat/Chat.js +83 -1
- package/dist/chat/ChatOptions.d.ts +5 -0
- package/dist/chat/ChatOptions.d.ts.map +1 -1
- package/dist/chat/ChatResponse.d.ts +5 -0
- package/dist/chat/ChatResponse.d.ts.map +1 -1
- package/dist/chat/ChatResponse.js +12 -0
- package/dist/constants.d.ts +7 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +6 -0
- package/dist/embedding/Embedding.d.ts +17 -0
- package/dist/embedding/Embedding.d.ts.map +1 -0
- package/dist/embedding/Embedding.js +24 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/llm.d.ts +11 -1
- package/dist/llm.d.ts.map +1 -1
- package/dist/llm.js +55 -28
- package/dist/providers/Embedding.d.ts +20 -0
- package/dist/providers/Embedding.d.ts.map +1 -0
- package/dist/providers/Embedding.js +1 -0
- package/dist/providers/Provider.d.ts +11 -4
- package/dist/providers/Provider.d.ts.map +1 -1
- package/dist/providers/gemini/Capabilities.d.ts +30 -0
- package/dist/providers/gemini/Capabilities.d.ts.map +1 -0
- package/dist/providers/gemini/Capabilities.js +148 -0
- package/dist/providers/gemini/Chat.d.ts +8 -0
- package/dist/providers/gemini/Chat.d.ts.map +1 -0
- package/dist/providers/gemini/Chat.js +69 -0
- package/dist/providers/gemini/ChatUtils.d.ts +9 -0
- package/dist/providers/gemini/ChatUtils.d.ts.map +1 -0
- package/dist/providers/gemini/ChatUtils.js +83 -0
- package/dist/providers/gemini/Embeddings.d.ts +8 -0
- package/dist/providers/gemini/Embeddings.d.ts.map +1 -0
- package/dist/providers/gemini/Embeddings.js +44 -0
- package/dist/providers/gemini/Errors.d.ts +2 -0
- package/dist/providers/gemini/Errors.d.ts.map +1 -0
- package/dist/providers/gemini/Errors.js +34 -0
- package/dist/providers/gemini/GeminiProvider.d.ts +34 -0
- package/dist/providers/gemini/GeminiProvider.d.ts.map +1 -0
- package/dist/providers/gemini/GeminiProvider.js +55 -0
- package/dist/providers/gemini/Image.d.ts +8 -0
- package/dist/providers/gemini/Image.d.ts.map +1 -0
- package/dist/providers/gemini/Image.js +47 -0
- package/dist/providers/gemini/Models.d.ts +8 -0
- package/dist/providers/gemini/Models.d.ts.map +1 -0
- package/dist/providers/gemini/Models.js +38 -0
- package/dist/providers/gemini/Streaming.d.ts +8 -0
- package/dist/providers/gemini/Streaming.d.ts.map +1 -0
- package/dist/providers/gemini/Streaming.js +70 -0
- package/dist/providers/gemini/Transcription.d.ts +9 -0
- package/dist/providers/gemini/Transcription.d.ts.map +1 -0
- package/dist/providers/gemini/Transcription.js +63 -0
- package/dist/providers/gemini/index.d.ts +11 -0
- package/dist/providers/gemini/index.d.ts.map +1 -0
- package/dist/providers/gemini/index.js +24 -0
- package/dist/providers/gemini/types.d.ts +118 -0
- package/dist/providers/gemini/types.d.ts.map +1 -0
- package/dist/providers/gemini/types.js +1 -0
- package/dist/providers/openai/Capabilities.d.ts +7 -2
- package/dist/providers/openai/Capabilities.d.ts.map +1 -1
- package/dist/providers/openai/Capabilities.js +52 -214
- package/dist/providers/openai/Chat.d.ts.map +1 -1
- package/dist/providers/openai/Chat.js +3 -0
- package/dist/providers/openai/Embedding.d.ts +8 -0
- package/dist/providers/openai/Embedding.d.ts.map +1 -0
- package/dist/providers/openai/Embedding.js +48 -0
- package/dist/providers/openai/ModelDefinitions.d.ts +25 -0
- package/dist/providers/openai/ModelDefinitions.d.ts.map +1 -0
- package/dist/providers/openai/ModelDefinitions.js +211 -0
- package/dist/providers/openai/Moderation.d.ts.map +1 -1
- package/dist/providers/openai/Moderation.js +3 -2
- package/dist/providers/openai/OpenAIProvider.d.ts +7 -0
- package/dist/providers/openai/OpenAIProvider.d.ts.map +1 -1
- package/dist/providers/openai/OpenAIProvider.js +10 -0
- package/dist/providers/openai/Transcription.d.ts.map +1 -1
- package/dist/providers/openai/Transcription.js +5 -4
- package/dist/schema/Schema.d.ts +20 -0
- package/dist/schema/Schema.d.ts.map +1 -0
- package/dist/schema/Schema.js +22 -0
- package/dist/schema/to-json-schema.d.ts +3 -0
- package/dist/schema/to-json-schema.d.ts.map +1 -0
- package/dist/schema/to-json-schema.js +10 -0
- package/dist/utils/Binary.d.ts +12 -0
- package/dist/utils/Binary.d.ts.map +1 -0
- package/dist/utils/Binary.js +71 -0
- package/package.json +3 -2
package/dist/llm.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { GeneratedImage } from "./image/GeneratedImage.js";
|
|
|
5
5
|
import { ModelRegistry } from "./models/ModelRegistry.js";
|
|
6
6
|
import { Transcription } from "./transcription/Transcription.js";
|
|
7
7
|
import { Moderation } from "./moderation/Moderation.js";
|
|
8
|
+
import { Embedding } from "./embedding/Embedding.js";
|
|
8
9
|
export interface RetryOptions {
|
|
9
10
|
attempts?: number;
|
|
10
11
|
delayMs?: number;
|
|
@@ -14,19 +15,23 @@ type LLMConfig = {
|
|
|
14
15
|
retry?: RetryOptions;
|
|
15
16
|
defaultTranscriptionModel?: string;
|
|
16
17
|
defaultModerationModel?: string;
|
|
18
|
+
defaultEmbeddingModel?: string;
|
|
17
19
|
} | {
|
|
18
20
|
provider: string;
|
|
19
21
|
retry?: RetryOptions;
|
|
20
22
|
defaultTranscriptionModel?: string;
|
|
21
23
|
defaultModerationModel?: string;
|
|
24
|
+
defaultEmbeddingModel?: string;
|
|
22
25
|
};
|
|
23
26
|
declare class LLMCore {
|
|
24
27
|
readonly models: ModelRegistry;
|
|
25
28
|
private provider?;
|
|
26
29
|
private defaultTranscriptionModelId?;
|
|
27
30
|
private defaultModerationModelId?;
|
|
31
|
+
private defaultEmbeddingModelId?;
|
|
28
32
|
private retry;
|
|
29
33
|
configure(config: LLMConfig): void;
|
|
34
|
+
private ensureProviderSupport;
|
|
30
35
|
chat(model: string, options?: ChatOptions): Chat;
|
|
31
36
|
listModels(): Promise<ModelInfo[]>;
|
|
32
37
|
paint(prompt: string, options?: {
|
|
@@ -43,11 +48,16 @@ declare class LLMCore {
|
|
|
43
48
|
}): Promise<Transcription>;
|
|
44
49
|
get defaultTranscriptionModel(): string | undefined;
|
|
45
50
|
get defaultModerationModel(): string | undefined;
|
|
51
|
+
get defaultEmbeddingModel(): string | undefined;
|
|
46
52
|
getRetryConfig(): Required<RetryOptions>;
|
|
47
53
|
moderate(input: string | string[], options?: {
|
|
48
54
|
model?: string;
|
|
49
55
|
}): Promise<Moderation>;
|
|
56
|
+
embed(input: string | string[], options?: {
|
|
57
|
+
model?: string;
|
|
58
|
+
dimensions?: number;
|
|
59
|
+
}): Promise<Embedding>;
|
|
50
60
|
}
|
|
51
|
-
export { Transcription, Moderation };
|
|
61
|
+
export { Transcription, Moderation, Embedding };
|
|
52
62
|
export declare const LLM: LLMCore;
|
|
53
63
|
//# sourceMappingURL=llm.d.ts.map
|
package/dist/llm.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EACL,QAAQ,EACR,SAAS,EAKV,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EACL,QAAQ,EACR,SAAS,EAKV,MAAM,yBAAyB,CAAC;AAIjC,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;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AAK,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,cAAc,CAAC;IAgB7G,UAAU,CACd,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;QACxB,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;KAC9B,GACA,OAAO,CAAC,aAAa,CAAC;IAiBzB,IAAI,yBAAyB,IAAI,MAAM,GAAG,SAAS,CAElD;IAED,IAAI,sBAAsB,IAAI,MAAM,GAAG,SAAS,CAE/C;IAED,IAAI,qBAAqB,IAAI,MAAM,GAAG,SAAS,CAE9C;IAED,cAAc;IAIR,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,CAAC;IAiBrF,KAAK,CACT,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,EACxB,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,GAChD,OAAO,CAAC,SAAS,CAAC;CAkBtB;AAED,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,CAAC;AAEhD,eAAO,MAAM,GAAG,SAAgB,CAAC"}
|
package/dist/llm.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { Chat } from "./chat/Chat.js";
|
|
2
2
|
import { providerRegistry } from "./providers/registry.js";
|
|
3
3
|
import { ensureOpenAIRegistered } from "./providers/openai/index.js";
|
|
4
|
+
import { registerGeminiProvider } from "./providers/gemini/index.js";
|
|
4
5
|
import { GeneratedImage } from "./image/GeneratedImage.js";
|
|
5
6
|
import { models } from "./models/ModelRegistry.js";
|
|
6
7
|
import { Transcription } from "./transcription/Transcription.js";
|
|
7
8
|
import { Moderation } from "./moderation/Moderation.js";
|
|
9
|
+
import { Embedding } from "./embedding/Embedding.js";
|
|
8
10
|
class LLMCore {
|
|
9
11
|
models = models;
|
|
10
12
|
provider;
|
|
11
13
|
defaultTranscriptionModelId;
|
|
12
14
|
defaultModerationModelId;
|
|
15
|
+
defaultEmbeddingModelId;
|
|
13
16
|
retry = {
|
|
14
17
|
attempts: 1,
|
|
15
18
|
delayMs: 0,
|
|
@@ -21,6 +24,9 @@ class LLMCore {
|
|
|
21
24
|
if (config.defaultModerationModel) {
|
|
22
25
|
this.defaultModerationModelId = config.defaultModerationModel;
|
|
23
26
|
}
|
|
27
|
+
if (config.defaultEmbeddingModel) {
|
|
28
|
+
this.defaultEmbeddingModelId = config.defaultEmbeddingModel;
|
|
29
|
+
}
|
|
24
30
|
if (config.retry) {
|
|
25
31
|
this.retry = {
|
|
26
32
|
attempts: config.retry.attempts ?? 1,
|
|
@@ -31,50 +37,55 @@ class LLMCore {
|
|
|
31
37
|
if (config.provider === "openai") {
|
|
32
38
|
ensureOpenAIRegistered();
|
|
33
39
|
}
|
|
40
|
+
if (config.provider === "gemini") {
|
|
41
|
+
registerGeminiProvider();
|
|
42
|
+
}
|
|
34
43
|
this.provider = providerRegistry.resolve(config.provider);
|
|
35
44
|
}
|
|
36
45
|
else {
|
|
37
46
|
this.provider = config.provider;
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
|
-
|
|
49
|
+
ensureProviderSupport(method) {
|
|
41
50
|
if (!this.provider) {
|
|
42
51
|
throw new Error("LLM provider not configured");
|
|
43
52
|
}
|
|
44
|
-
|
|
53
|
+
if (!this.provider[method]) {
|
|
54
|
+
throw new Error(`Provider does not support ${method}`);
|
|
55
|
+
}
|
|
56
|
+
return this.provider;
|
|
45
57
|
}
|
|
46
|
-
|
|
58
|
+
chat(model, options) {
|
|
47
59
|
if (!this.provider) {
|
|
48
60
|
throw new Error("LLM provider not configured");
|
|
49
61
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
62
|
+
return new Chat(this.provider, model, options);
|
|
63
|
+
}
|
|
64
|
+
async listModels() {
|
|
65
|
+
const provider = this.ensureProviderSupport("listModels");
|
|
66
|
+
return provider.listModels();
|
|
54
67
|
}
|
|
55
68
|
async paint(prompt, options) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
throw new Error(`Provider does not support paint`);
|
|
69
|
+
const provider = this.ensureProviderSupport("paint");
|
|
70
|
+
const model = options?.model;
|
|
71
|
+
if (model && provider.capabilities && !provider.capabilities.supportsImageGeneration(model)) {
|
|
72
|
+
throw new Error(`Model ${model} does not support image generation.`);
|
|
61
73
|
}
|
|
62
|
-
const response = await
|
|
74
|
+
const response = await provider.paint({
|
|
63
75
|
prompt,
|
|
64
76
|
...options,
|
|
65
77
|
});
|
|
66
78
|
return new GeneratedImage(response);
|
|
67
79
|
}
|
|
68
80
|
async transcribe(file, options) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
throw new Error(`Provider does not support transcribe`);
|
|
81
|
+
const provider = this.ensureProviderSupport("transcribe");
|
|
82
|
+
const model = options?.model || this.defaultTranscriptionModelId;
|
|
83
|
+
if (model && provider.capabilities && !provider.capabilities.supportsTranscription(model)) {
|
|
84
|
+
throw new Error(`Model ${model} does not support transcription.`);
|
|
74
85
|
}
|
|
75
|
-
const response = await
|
|
86
|
+
const response = await provider.transcribe({
|
|
76
87
|
file,
|
|
77
|
-
model
|
|
88
|
+
model,
|
|
78
89
|
...options,
|
|
79
90
|
});
|
|
80
91
|
return new Transcription(response);
|
|
@@ -85,23 +96,39 @@ class LLMCore {
|
|
|
85
96
|
get defaultModerationModel() {
|
|
86
97
|
return this.defaultModerationModelId;
|
|
87
98
|
}
|
|
99
|
+
get defaultEmbeddingModel() {
|
|
100
|
+
return this.defaultEmbeddingModelId;
|
|
101
|
+
}
|
|
88
102
|
getRetryConfig() {
|
|
89
103
|
return this.retry;
|
|
90
104
|
}
|
|
91
105
|
async moderate(input, options) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
throw new Error(`Provider does not support moderate`);
|
|
106
|
+
const provider = this.ensureProviderSupport("moderate");
|
|
107
|
+
const model = options?.model || this.defaultModerationModelId;
|
|
108
|
+
if (model && provider.capabilities && !provider.capabilities.supportsModeration(model)) {
|
|
109
|
+
throw new Error(`Model ${model} does not support moderation.`);
|
|
97
110
|
}
|
|
98
|
-
const response = await
|
|
111
|
+
const response = await provider.moderate({
|
|
99
112
|
input,
|
|
100
|
-
model
|
|
113
|
+
model,
|
|
101
114
|
...options,
|
|
102
115
|
});
|
|
103
116
|
return new Moderation(response);
|
|
104
117
|
}
|
|
118
|
+
async embed(input, options) {
|
|
119
|
+
const provider = this.ensureProviderSupport("embed");
|
|
120
|
+
const model = options?.model || this.defaultEmbeddingModelId;
|
|
121
|
+
const request = {
|
|
122
|
+
input,
|
|
123
|
+
model,
|
|
124
|
+
dimensions: options?.dimensions,
|
|
125
|
+
};
|
|
126
|
+
if (request.model && provider.capabilities && !provider.capabilities.supportsEmbeddings(request.model)) {
|
|
127
|
+
throw new Error(`Model ${request.model} does not support embeddings.`);
|
|
128
|
+
}
|
|
129
|
+
const response = await provider.embed(request);
|
|
130
|
+
return new Embedding(response);
|
|
131
|
+
}
|
|
105
132
|
}
|
|
106
|
-
export { Transcription, Moderation };
|
|
133
|
+
export { Transcription, Moderation, Embedding };
|
|
107
134
|
export const LLM = new LLMCore();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface EmbeddingRequest {
|
|
2
|
+
input: string | string[];
|
|
3
|
+
model?: string;
|
|
4
|
+
dimensions?: number;
|
|
5
|
+
user?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface EmbeddingVector {
|
|
8
|
+
embedding: number[];
|
|
9
|
+
index: number;
|
|
10
|
+
}
|
|
11
|
+
export interface EmbeddingResponse {
|
|
12
|
+
vectors: number[][];
|
|
13
|
+
model: string;
|
|
14
|
+
input_tokens: number;
|
|
15
|
+
dimensions: number;
|
|
16
|
+
}
|
|
17
|
+
export interface EmbeddingProvider {
|
|
18
|
+
embed(request: EmbeddingRequest): Promise<EmbeddingResponse>;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=Embedding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Embedding.d.ts","sourceRoot":"","sources":["../../src/providers/Embedding.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,EAAE,EAAE,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,iBAAiB;IAChC,KAAK,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;CAC9D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { Message } from "../chat/Message.js";
|
|
2
2
|
import { Tool, ToolCall } from "../chat/Tool.js";
|
|
3
|
+
import { EmbeddingRequest, EmbeddingResponse } from "./Embedding.js";
|
|
3
4
|
export interface ChatRequest {
|
|
4
5
|
model: string;
|
|
5
6
|
messages: Message[];
|
|
6
7
|
tools?: Tool[];
|
|
7
8
|
temperature?: number;
|
|
8
9
|
max_tokens?: number;
|
|
10
|
+
response_format?: any;
|
|
9
11
|
headers?: Record<string, string>;
|
|
10
12
|
}
|
|
11
13
|
export interface ChatChunk {
|
|
@@ -24,10 +26,14 @@ export interface ChatResponse {
|
|
|
24
26
|
usage?: Usage;
|
|
25
27
|
}
|
|
26
28
|
export interface ProviderCapabilities {
|
|
27
|
-
supportsVision
|
|
28
|
-
supportsTools
|
|
29
|
-
supportsStructuredOutput
|
|
30
|
-
|
|
29
|
+
supportsVision(modelId: string): boolean;
|
|
30
|
+
supportsTools(modelId: string): boolean;
|
|
31
|
+
supportsStructuredOutput(modelId: string): boolean;
|
|
32
|
+
supportsEmbeddings(modelId: string): boolean;
|
|
33
|
+
supportsImageGeneration(modelId: string): boolean;
|
|
34
|
+
supportsTranscription(modelId: string): boolean;
|
|
35
|
+
supportsModeration(modelId: string): boolean;
|
|
36
|
+
getContextWindow(modelId: string): number | null;
|
|
31
37
|
}
|
|
32
38
|
export interface ModelInfo {
|
|
33
39
|
id: string;
|
|
@@ -100,6 +106,7 @@ export interface Provider {
|
|
|
100
106
|
paint?(request: ImageRequest): Promise<ImageResponse>;
|
|
101
107
|
transcribe?(request: TranscriptionRequest): Promise<TranscriptionResponse>;
|
|
102
108
|
moderate?(request: ModerationRequest): Promise<ModerationResponse>;
|
|
109
|
+
embed?(request: EmbeddingRequest): Promise<EmbeddingResponse>;
|
|
103
110
|
capabilities?: ProviderCapabilities;
|
|
104
111
|
}
|
|
105
112
|
//# sourceMappingURL=Provider.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Provider.d.ts","sourceRoot":"","sources":["../../src/providers/Provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,IAAI,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CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACtD,UAAU,CAAC,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAC3E,QAAQ,CAAC,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACnE,KAAK,CAAC,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAC9D,YAAY,CAAC,EAAE,oBAAoB,CAAC;CACrC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare class Capabilities {
|
|
2
|
+
static getContextWindow(modelId: string): number | null;
|
|
3
|
+
static getMaxOutputTokens(modelId: string): number | null;
|
|
4
|
+
static supportsVision(modelId: string): boolean;
|
|
5
|
+
static supportsTools(modelId: string): boolean;
|
|
6
|
+
static supportsStructuredOutput(modelId: string): boolean;
|
|
7
|
+
static supportsJsonMode(modelId: string): boolean;
|
|
8
|
+
static supportsEmbeddings(modelId: string): boolean;
|
|
9
|
+
static supportsImageGeneration(modelId: string): boolean;
|
|
10
|
+
static supportsTranscription(modelId: string): boolean;
|
|
11
|
+
static supportsModeration(modelId: string): boolean;
|
|
12
|
+
static normalizeTemperature(temperature: number | undefined, _modelId: string): number | undefined | null;
|
|
13
|
+
static getFamily(modelId: string): string;
|
|
14
|
+
static getModalities(modelId: string): {
|
|
15
|
+
input: string[];
|
|
16
|
+
output: string[];
|
|
17
|
+
};
|
|
18
|
+
static getCapabilities(modelId: string): string[];
|
|
19
|
+
static getPricing(modelId: string): {
|
|
20
|
+
text_tokens: {
|
|
21
|
+
standard: {
|
|
22
|
+
input_per_million: number;
|
|
23
|
+
output_per_million: number;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
static formatDisplayName(modelId: string): string;
|
|
28
|
+
private static normalizeModelId;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMapping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,OAAO,EAAE,MAAM;;;;;;;;IA0BjC,MAAM,CAAC,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAIjD,OAAO,CAAC,MAAM,CAAC,gBAAgB;CAGhC"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
export class Capabilities {
|
|
2
|
+
static getContextWindow(modelId) {
|
|
3
|
+
const id = this.normalizeModelId(modelId);
|
|
4
|
+
if (id.match(/gemini-2\.5-pro-exp-03-25|gemini-2\.0-flash|gemini-2\.0-flash-lite|gemini-1\.5-flash|gemini-1\.5-flash-8b/)) {
|
|
5
|
+
return 1_048_576;
|
|
6
|
+
}
|
|
7
|
+
if (id.match(/gemini-1\.5-pro/)) {
|
|
8
|
+
return 2_097_152;
|
|
9
|
+
}
|
|
10
|
+
if (id.match(/gemini-embedding-exp/)) {
|
|
11
|
+
return 8_192;
|
|
12
|
+
}
|
|
13
|
+
if (id.match(/text-embedding-004|embedding-001/)) {
|
|
14
|
+
return 2_048;
|
|
15
|
+
}
|
|
16
|
+
if (id.match(/aqa/)) {
|
|
17
|
+
return 7_168;
|
|
18
|
+
}
|
|
19
|
+
return 32_768;
|
|
20
|
+
}
|
|
21
|
+
static getMaxOutputTokens(modelId) {
|
|
22
|
+
const id = this.normalizeModelId(modelId);
|
|
23
|
+
if (id.match(/gemini-2\.5-pro-exp-03-25/)) {
|
|
24
|
+
return 64_000;
|
|
25
|
+
}
|
|
26
|
+
if (id.match(/gemini-2\.0-flash|gemini-2\.0-flash-lite|gemini-1\.5-flash|gemini-1\.5-flash-8b|gemini-1\.5-pro/)) {
|
|
27
|
+
return 8_192;
|
|
28
|
+
}
|
|
29
|
+
if (id.match(/text-embedding-004|embedding-001/)) {
|
|
30
|
+
return 768;
|
|
31
|
+
}
|
|
32
|
+
if (id.match(/imagen-3/)) {
|
|
33
|
+
return 4;
|
|
34
|
+
}
|
|
35
|
+
return 4_096;
|
|
36
|
+
}
|
|
37
|
+
static supportsVision(modelId) {
|
|
38
|
+
const id = this.normalizeModelId(modelId);
|
|
39
|
+
if (id.match(/text-embedding|embedding-001|aqa/)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return !!id.match(/gemini|flash|pro|imagen/);
|
|
43
|
+
}
|
|
44
|
+
static supportsTools(modelId) {
|
|
45
|
+
const id = this.normalizeModelId(modelId);
|
|
46
|
+
if (id.match(/text-embedding|embedding-001|aqa|flash-lite|imagen|gemini-2\.0-flash-lite/)) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return !!id.match(/gemini|pro|flash/);
|
|
50
|
+
}
|
|
51
|
+
static supportsStructuredOutput(modelId) {
|
|
52
|
+
// Explicitly disabled until implementation is validated
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
static supportsJsonMode(modelId) {
|
|
56
|
+
return this.supportsStructuredOutput(modelId);
|
|
57
|
+
}
|
|
58
|
+
static supportsEmbeddings(modelId) {
|
|
59
|
+
const id = this.normalizeModelId(modelId);
|
|
60
|
+
return !!id.match(/text-embedding|embedding|gemini-embedding/);
|
|
61
|
+
}
|
|
62
|
+
static supportsImageGeneration(modelId) {
|
|
63
|
+
const id = this.normalizeModelId(modelId);
|
|
64
|
+
return !!id.match(/imagen/);
|
|
65
|
+
}
|
|
66
|
+
static supportsTranscription(modelId) {
|
|
67
|
+
const id = this.normalizeModelId(modelId);
|
|
68
|
+
return !!id.match(/gemini|flash|pro/);
|
|
69
|
+
}
|
|
70
|
+
static supportsModeration(modelId) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
static normalizeTemperature(temperature, _modelId) {
|
|
74
|
+
return temperature;
|
|
75
|
+
}
|
|
76
|
+
static getFamily(modelId) {
|
|
77
|
+
const id = this.normalizeModelId(modelId);
|
|
78
|
+
if (id.startsWith("gemini-1.5-pro"))
|
|
79
|
+
return "gemini-1.5-pro";
|
|
80
|
+
if (id.startsWith("gemini-1.5-flash"))
|
|
81
|
+
return "gemini-1.5-flash";
|
|
82
|
+
if (id.startsWith("gemini-2.0-flash"))
|
|
83
|
+
return "gemini-2.0-flash";
|
|
84
|
+
if (id.startsWith("gemini-2.0-flash-lite"))
|
|
85
|
+
return "gemini-2.0-flash-lite";
|
|
86
|
+
if (id.startsWith("text-embedding"))
|
|
87
|
+
return "text-embedding";
|
|
88
|
+
if (id.startsWith("imagen"))
|
|
89
|
+
return "imagen";
|
|
90
|
+
return "other";
|
|
91
|
+
}
|
|
92
|
+
static getModalities(modelId) {
|
|
93
|
+
const input = ["text"];
|
|
94
|
+
const output = ["text"];
|
|
95
|
+
const id = this.normalizeModelId(modelId);
|
|
96
|
+
if (this.supportsVision(id))
|
|
97
|
+
input.push("image", "video", "audio", "pdf");
|
|
98
|
+
if (this.supportsImageGeneration(id))
|
|
99
|
+
output.push("image");
|
|
100
|
+
if (this.supportsEmbeddings(id))
|
|
101
|
+
output.push("embeddings");
|
|
102
|
+
return { input, output };
|
|
103
|
+
}
|
|
104
|
+
static getCapabilities(modelId) {
|
|
105
|
+
const caps = ["streaming"];
|
|
106
|
+
const id = this.normalizeModelId(modelId);
|
|
107
|
+
if (this.supportsTools(id))
|
|
108
|
+
caps.push("function_calling");
|
|
109
|
+
if (this.supportsStructuredOutput(id))
|
|
110
|
+
caps.push("structured_output");
|
|
111
|
+
if (this.supportsEmbeddings(id))
|
|
112
|
+
caps.push("embeddings");
|
|
113
|
+
if (this.supportsImageGeneration(id))
|
|
114
|
+
caps.push("image_generation");
|
|
115
|
+
return caps;
|
|
116
|
+
}
|
|
117
|
+
static getPricing(modelId) {
|
|
118
|
+
const id = this.normalizeModelId(modelId);
|
|
119
|
+
let input = 0;
|
|
120
|
+
let output = 0;
|
|
121
|
+
if (id.match(/gemini-1\.5-flash/)) {
|
|
122
|
+
input = 0.075;
|
|
123
|
+
output = 0.3;
|
|
124
|
+
}
|
|
125
|
+
else if (id.match(/gemini-1\.5-pro/)) {
|
|
126
|
+
input = 3.5;
|
|
127
|
+
output = 10.5;
|
|
128
|
+
}
|
|
129
|
+
else if (id.match(/gemini-2\.0-flash/)) {
|
|
130
|
+
input = 0.10;
|
|
131
|
+
output = 0.40;
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
text_tokens: {
|
|
135
|
+
standard: {
|
|
136
|
+
input_per_million: input,
|
|
137
|
+
output_per_million: output
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
static formatDisplayName(modelId) {
|
|
143
|
+
return modelId.replace("models/", "").replace(/-/g, " ").replace(/\b\w/g, l => l.toUpperCase());
|
|
144
|
+
}
|
|
145
|
+
static normalizeModelId(modelId) {
|
|
146
|
+
return modelId.replace("models/", "");
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ChatRequest, ChatResponse } from "../Provider.js";
|
|
2
|
+
export declare class GeminiChat {
|
|
3
|
+
private readonly baseUrl;
|
|
4
|
+
private readonly apiKey;
|
|
5
|
+
constructor(baseUrl: string, apiKey: string);
|
|
6
|
+
execute(request: ChatRequest): Promise<ChatResponse>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=Chat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Chat.d.ts","sourceRoot":"","sources":["../../../src/providers/gemini/Chat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAM3D,qBAAa,UAAU;IACT,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAxC,OAAO,EAAE,MAAM,EAAmB,MAAM,EAAE,MAAM;IAEvE,OAAO,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;CAqE3D"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Capabilities } from "./Capabilities.js";
|
|
2
|
+
import { handleGeminiError } from "./Errors.js";
|
|
3
|
+
import { GeminiChatUtils } from "./ChatUtils.js";
|
|
4
|
+
export class GeminiChat {
|
|
5
|
+
baseUrl;
|
|
6
|
+
apiKey;
|
|
7
|
+
constructor(baseUrl, apiKey) {
|
|
8
|
+
this.baseUrl = baseUrl;
|
|
9
|
+
this.apiKey = apiKey;
|
|
10
|
+
}
|
|
11
|
+
async execute(request) {
|
|
12
|
+
const temperature = Capabilities.normalizeTemperature(request.temperature, request.model);
|
|
13
|
+
const url = `${this.baseUrl}/models/${request.model}:generateContent?key=${this.apiKey}`;
|
|
14
|
+
const { contents, systemInstructionParts } = await GeminiChatUtils.convertMessages(request.messages);
|
|
15
|
+
const payload = {
|
|
16
|
+
contents,
|
|
17
|
+
generationConfig: {
|
|
18
|
+
temperature: temperature ?? undefined,
|
|
19
|
+
maxOutputTokens: request.max_tokens,
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
if (systemInstructionParts.length > 0) {
|
|
23
|
+
payload.systemInstruction = { parts: systemInstructionParts };
|
|
24
|
+
}
|
|
25
|
+
if (request.tools && request.tools.length > 0) {
|
|
26
|
+
payload.tools = [
|
|
27
|
+
{
|
|
28
|
+
functionDeclarations: request.tools.map((t) => ({
|
|
29
|
+
name: t.function.name,
|
|
30
|
+
description: t.function.description,
|
|
31
|
+
parameters: t.function.parameters,
|
|
32
|
+
})),
|
|
33
|
+
},
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
const response = await fetch(url, {
|
|
37
|
+
method: "POST",
|
|
38
|
+
headers: {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
},
|
|
41
|
+
body: JSON.stringify(payload),
|
|
42
|
+
});
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
await handleGeminiError(response, request.model);
|
|
45
|
+
}
|
|
46
|
+
const json = (await response.json());
|
|
47
|
+
const candidate = json.candidates?.[0];
|
|
48
|
+
const content = candidate?.content?.parts
|
|
49
|
+
?.filter(p => p.text)
|
|
50
|
+
.map(p => p.text)
|
|
51
|
+
.join("\n") || null;
|
|
52
|
+
const tool_calls = candidate?.content?.parts
|
|
53
|
+
?.filter((p) => p.functionCall)
|
|
54
|
+
.map((p) => ({
|
|
55
|
+
id: p.functionCall.name,
|
|
56
|
+
type: "function",
|
|
57
|
+
function: {
|
|
58
|
+
name: p.functionCall.name,
|
|
59
|
+
arguments: JSON.stringify(p.functionCall.args),
|
|
60
|
+
},
|
|
61
|
+
}));
|
|
62
|
+
const usage = json.usageMetadata ? {
|
|
63
|
+
input_tokens: json.usageMetadata.promptTokenCount,
|
|
64
|
+
output_tokens: json.usageMetadata.candidatesTokenCount,
|
|
65
|
+
total_tokens: json.usageMetadata.totalTokenCount,
|
|
66
|
+
} : undefined;
|
|
67
|
+
return { content, tool_calls, usage };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Message } from "../../chat/Message.js";
|
|
2
|
+
import { GeminiContent, GeminiPart } from "./types.js";
|
|
3
|
+
export declare class GeminiChatUtils {
|
|
4
|
+
static convertMessages(messages: Message[]): Promise<{
|
|
5
|
+
contents: GeminiContent[];
|
|
6
|
+
systemInstructionParts: GeminiPart[];
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=ChatUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatUtils.d.ts","sourceRoot":"","sources":["../../../src/providers/gemini/ChatUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAGvD,qBAAa,eAAe;WACb,eAAe,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;QAAE,QAAQ,EAAE,aAAa,EAAE,CAAC;QAAC,sBAAsB,EAAE,UAAU,EAAE,CAAA;KAAE,CAAC;CAgFhI"}
|