@effect/ai-openai-compat 4.0.0-beta.33 → 4.0.0-beta.35
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/dist/OpenAiEmbeddingModel.d.ts +85 -0
- package/dist/OpenAiEmbeddingModel.d.ts.map +1 -0
- package/dist/OpenAiEmbeddingModel.js +119 -0
- package/dist/OpenAiEmbeddingModel.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/OpenAiEmbeddingModel.ts +203 -0
- package/src/index.ts +9 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Embedding Model implementation.
|
|
3
|
+
*
|
|
4
|
+
* Provides an EmbeddingModel implementation for OpenAI-compatible embeddings APIs.
|
|
5
|
+
*
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
import * as Effect from "effect/Effect";
|
|
9
|
+
import * as Layer from "effect/Layer";
|
|
10
|
+
import * as ServiceMap from "effect/ServiceMap";
|
|
11
|
+
import * as EmbeddingModel from "effect/unstable/ai/EmbeddingModel";
|
|
12
|
+
import * as AiModel from "effect/unstable/ai/Model";
|
|
13
|
+
import { OpenAiClient } from "./OpenAiClient.ts";
|
|
14
|
+
/**
|
|
15
|
+
* @since 1.0.0
|
|
16
|
+
* @category models
|
|
17
|
+
*/
|
|
18
|
+
export type Model = string;
|
|
19
|
+
declare const Config_base: ServiceMap.ServiceClass<Config, "@effect/ai-openai-compat/OpenAiEmbeddingModel/Config", {
|
|
20
|
+
readonly [x: string]: unknown;
|
|
21
|
+
readonly model?: string;
|
|
22
|
+
readonly user?: string | undefined | undefined;
|
|
23
|
+
readonly encoding_format?: "float" | "base64" | undefined | undefined;
|
|
24
|
+
readonly dimensions?: number | undefined | undefined;
|
|
25
|
+
}>;
|
|
26
|
+
/**
|
|
27
|
+
* Service definition for OpenAI embedding model configuration.
|
|
28
|
+
*
|
|
29
|
+
* @since 1.0.0
|
|
30
|
+
* @category context
|
|
31
|
+
*/
|
|
32
|
+
export declare class Config extends Config_base {
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* @since 1.0.0
|
|
36
|
+
* @category constructors
|
|
37
|
+
*/
|
|
38
|
+
export declare const model: (model: string, options: {
|
|
39
|
+
readonly dimensions: number;
|
|
40
|
+
readonly config?: Omit<typeof Config.Service, "model" | "dimensions">;
|
|
41
|
+
}) => AiModel.Model<"openai", EmbeddingModel.EmbeddingModel | EmbeddingModel.Dimensions, OpenAiClient>;
|
|
42
|
+
/**
|
|
43
|
+
* Creates an OpenAI embedding model service.
|
|
44
|
+
*
|
|
45
|
+
* @since 1.0.0
|
|
46
|
+
* @category constructors
|
|
47
|
+
*/
|
|
48
|
+
export declare const make: (args_0: {
|
|
49
|
+
readonly model: string;
|
|
50
|
+
readonly config?: Omit<typeof Config.Service, "model"> | undefined;
|
|
51
|
+
}) => Effect.Effect<EmbeddingModel.Service, never, OpenAiClient>;
|
|
52
|
+
/**
|
|
53
|
+
* Creates a layer for the OpenAI embedding model.
|
|
54
|
+
*
|
|
55
|
+
* @since 1.0.0
|
|
56
|
+
* @category layers
|
|
57
|
+
*/
|
|
58
|
+
export declare const layer: (options: {
|
|
59
|
+
readonly model: string;
|
|
60
|
+
readonly config?: Omit<typeof Config.Service, "model"> | undefined;
|
|
61
|
+
}) => Layer.Layer<EmbeddingModel.EmbeddingModel, never, OpenAiClient>;
|
|
62
|
+
/**
|
|
63
|
+
* Provides config overrides for OpenAI embedding model operations.
|
|
64
|
+
*
|
|
65
|
+
* @since 1.0.0
|
|
66
|
+
* @category configuration
|
|
67
|
+
*/
|
|
68
|
+
export declare const withConfigOverride: {
|
|
69
|
+
/**
|
|
70
|
+
* Provides config overrides for OpenAI embedding model operations.
|
|
71
|
+
*
|
|
72
|
+
* @since 1.0.0
|
|
73
|
+
* @category configuration
|
|
74
|
+
*/
|
|
75
|
+
(overrides: typeof Config.Service): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>;
|
|
76
|
+
/**
|
|
77
|
+
* Provides config overrides for OpenAI embedding model operations.
|
|
78
|
+
*
|
|
79
|
+
* @since 1.0.0
|
|
80
|
+
* @category configuration
|
|
81
|
+
*/
|
|
82
|
+
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service): Effect.Effect<A, E, Exclude<R, Config>>;
|
|
83
|
+
};
|
|
84
|
+
export {};
|
|
85
|
+
//# sourceMappingURL=OpenAiEmbeddingModel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OpenAiEmbeddingModel.d.ts","sourceRoot":"","sources":["../src/OpenAiEmbeddingModel.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAA;AAG/C,OAAO,KAAK,cAAc,MAAM,mCAAmC,CAAA;AACnE,OAAO,KAAK,OAAO,MAAM,0BAA0B,CAAA;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEhD;;;GAGG;AACH,MAAM,MAAM,KAAK,GAAG,MAAM,CAAA;;;;;;;;AAE1B;;;;;GAKG;AACH,qBAAa,MAAO,SAAQ,WAa+B;CAAG;AAE9D;;;GAGG;AACH,eAAO,MAAM,KAAK,GAChB,OAAO,MAAM,EACb,SAAS;IACP,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,OAAO,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,YAAY,CAAC,CAAA;CACtE,KACA,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,cAAc,CAAC,cAAc,GAAG,c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for OpenAI-compatible embeddings APIs.
|
|
5
|
+
*
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
import * as Effect from "effect/Effect";
|
|
9
|
+
import { dual } from "effect/Function";
|
|
10
|
+
import * as Layer from "effect/Layer";
|
|
11
|
+
import * as ServiceMap from "effect/ServiceMap";
|
|
12
|
+
import * as AiError from "effect/unstable/ai/AiError";
|
|
13
|
+
import * as EmbeddingModel from "effect/unstable/ai/EmbeddingModel";
|
|
14
|
+
import * as AiModel from "effect/unstable/ai/Model";
|
|
15
|
+
import { OpenAiClient } from "./OpenAiClient.js";
|
|
16
|
+
/**
|
|
17
|
+
* Service definition for OpenAI embedding model configuration.
|
|
18
|
+
*
|
|
19
|
+
* @since 1.0.0
|
|
20
|
+
* @category context
|
|
21
|
+
*/
|
|
22
|
+
export class Config extends /*#__PURE__*/ServiceMap.Service()("@effect/ai-openai-compat/OpenAiEmbeddingModel/Config") {}
|
|
23
|
+
/**
|
|
24
|
+
* @since 1.0.0
|
|
25
|
+
* @category constructors
|
|
26
|
+
*/
|
|
27
|
+
export const model = (model, options) => AiModel.make("openai", model, Layer.merge(layer({
|
|
28
|
+
model,
|
|
29
|
+
config: {
|
|
30
|
+
...options.config,
|
|
31
|
+
dimensions: options.dimensions
|
|
32
|
+
}
|
|
33
|
+
}), Layer.succeed(EmbeddingModel.Dimensions, options.dimensions)));
|
|
34
|
+
/**
|
|
35
|
+
* Creates an OpenAI embedding model service.
|
|
36
|
+
*
|
|
37
|
+
* @since 1.0.0
|
|
38
|
+
* @category constructors
|
|
39
|
+
*/
|
|
40
|
+
export const make = /*#__PURE__*/Effect.fnUntraced(function* ({
|
|
41
|
+
model,
|
|
42
|
+
config: providerConfig
|
|
43
|
+
}) {
|
|
44
|
+
const client = yield* OpenAiClient;
|
|
45
|
+
const makeConfig = Effect.gen(function* () {
|
|
46
|
+
const services = yield* Effect.services();
|
|
47
|
+
return {
|
|
48
|
+
model,
|
|
49
|
+
...providerConfig,
|
|
50
|
+
...services.mapUnsafe.get(Config.key)
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
return yield* EmbeddingModel.make({
|
|
54
|
+
embedMany: Effect.fnUntraced(function* ({
|
|
55
|
+
inputs
|
|
56
|
+
}) {
|
|
57
|
+
const config = yield* makeConfig;
|
|
58
|
+
const response = yield* client.createEmbedding({
|
|
59
|
+
...config,
|
|
60
|
+
input: inputs
|
|
61
|
+
});
|
|
62
|
+
return yield* mapProviderResponse(inputs.length, response);
|
|
63
|
+
})
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
/**
|
|
67
|
+
* Creates a layer for the OpenAI embedding model.
|
|
68
|
+
*
|
|
69
|
+
* @since 1.0.0
|
|
70
|
+
* @category layers
|
|
71
|
+
*/
|
|
72
|
+
export const layer = options => Layer.effect(EmbeddingModel.EmbeddingModel, make(options));
|
|
73
|
+
/**
|
|
74
|
+
* Provides config overrides for OpenAI embedding model operations.
|
|
75
|
+
*
|
|
76
|
+
* @since 1.0.0
|
|
77
|
+
* @category configuration
|
|
78
|
+
*/
|
|
79
|
+
export const withConfigOverride = /*#__PURE__*/dual(2, (self, overrides) => Effect.flatMap(Effect.serviceOption(Config), config => Effect.provideService(self, Config, {
|
|
80
|
+
...(config._tag === "Some" ? config.value : {}),
|
|
81
|
+
...overrides
|
|
82
|
+
})));
|
|
83
|
+
const mapProviderResponse = (inputLength, response) => {
|
|
84
|
+
if (response.data.length !== inputLength) {
|
|
85
|
+
return Effect.fail(invalidOutput(`Provider returned ${response.data.length} embeddings but expected ${inputLength}`));
|
|
86
|
+
}
|
|
87
|
+
const results = new Array(inputLength);
|
|
88
|
+
const seen = new Set();
|
|
89
|
+
for (const entry of response.data) {
|
|
90
|
+
if (!Number.isInteger(entry.index) || entry.index < 0 || entry.index >= inputLength) {
|
|
91
|
+
return Effect.fail(invalidOutput(`Provider returned invalid embedding index: ${entry.index}`));
|
|
92
|
+
}
|
|
93
|
+
if (seen.has(entry.index)) {
|
|
94
|
+
return Effect.fail(invalidOutput(`Provider returned duplicate embedding index: ${entry.index}`));
|
|
95
|
+
}
|
|
96
|
+
if (!Array.isArray(entry.embedding)) {
|
|
97
|
+
return Effect.fail(invalidOutput(`Provider returned non-vector embedding at index ${entry.index}`));
|
|
98
|
+
}
|
|
99
|
+
seen.add(entry.index);
|
|
100
|
+
results[entry.index] = [...entry.embedding];
|
|
101
|
+
}
|
|
102
|
+
if (seen.size !== inputLength) {
|
|
103
|
+
return Effect.fail(invalidOutput(`Provider returned embeddings for ${seen.size} inputs but expected ${inputLength}`));
|
|
104
|
+
}
|
|
105
|
+
return Effect.succeed({
|
|
106
|
+
results,
|
|
107
|
+
usage: {
|
|
108
|
+
inputTokens: response.usage?.prompt_tokens
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
const invalidOutput = description => AiError.make({
|
|
113
|
+
module: "OpenAiEmbeddingModel",
|
|
114
|
+
method: "embedMany",
|
|
115
|
+
reason: new AiError.InvalidOutputError({
|
|
116
|
+
description
|
|
117
|
+
})
|
|
118
|
+
});
|
|
119
|
+
//# sourceMappingURL=OpenAiEmbeddingModel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OpenAiEmbeddingModel.js","names":["Effect","dual","Layer","ServiceMap","AiError","EmbeddingModel","AiModel","OpenAiClient","Config","Service","model","options","make","merge","layer","config","dimensions","succeed","Dimensions","fnUntraced","providerConfig","client","makeConfig","gen","services","mapUnsafe","get","key","embedMany","inputs","response","createEmbedding","input","mapProviderResponse","length","effect","withConfigOverride","self","overrides","flatMap","serviceOption","provideService","_tag","value","inputLength","data","fail","invalidOutput","results","Array","seen","Set","entry","Number","isInteger","index","has","isArray","embedding","add","size","usage","inputTokens","prompt_tokens","description","module","method","reason","InvalidOutputError"],"sources":["../src/OpenAiEmbeddingModel.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;AAOA,OAAO,KAAKA,MAAM,MAAM,eAAe;AACvC,S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EAAE,IAAI/D,OAAO,CAACgE,kBAAkB,CAAC;IAAEJ;EAAW,CAAE;CACvD,CAAC","ignoreList":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,14 @@ export * as OpenAiClient from "./OpenAiClient.ts";
|
|
|
9
9
|
* @since 1.0.0
|
|
10
10
|
*/
|
|
11
11
|
export * as OpenAiConfig from "./OpenAiConfig.ts";
|
|
12
|
+
/**
|
|
13
|
+
* OpenAI Embedding Model implementation.
|
|
14
|
+
*
|
|
15
|
+
* Provides an EmbeddingModel implementation for OpenAI-compatible embeddings APIs.
|
|
16
|
+
*
|
|
17
|
+
* @since 1.0.0
|
|
18
|
+
*/
|
|
19
|
+
export * as OpenAiEmbeddingModel from "./OpenAiEmbeddingModel.ts";
|
|
12
20
|
/**
|
|
13
21
|
* @since 1.0.0
|
|
14
22
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;GAEG;AACH,OAAO,KAAK,YAAY,MAAM,mBAAmB,CAAA;AAEjD;;GAEG;AACH,OAAO,KAAK,YAAY,MAAM,mBAAmB,CAAA;AAEjD;;GAEG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAE/C;;;;;;;GAOG;AACH,OAAO,KAAK,mBAAmB,MAAM,0BAA0B,CAAA;AAE/D;;;;;;;;GAQG;AACH,OAAO,KAAK,eAAe,MAAM,sBAAsB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;GAEG;AACH,OAAO,KAAK,YAAY,MAAM,mBAAmB,CAAA;AAEjD;;GAEG;AACH,OAAO,KAAK,YAAY,MAAM,mBAAmB,CAAA;AAEjD;;;;;;GAMG;AACH,OAAO,KAAK,oBAAoB,MAAM,2BAA2B,CAAA;AAEjE;;GAEG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAE/C;;;;;;;GAOG;AACH,OAAO,KAAK,mBAAmB,MAAM,0BAA0B,CAAA;AAE/D;;;;;;;;GAQG;AACH,OAAO,KAAK,eAAe,MAAM,sBAAsB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,14 @@ export * as OpenAiClient from "./OpenAiClient.js";
|
|
|
10
10
|
* @since 1.0.0
|
|
11
11
|
*/
|
|
12
12
|
export * as OpenAiConfig from "./OpenAiConfig.js";
|
|
13
|
+
/**
|
|
14
|
+
* OpenAI Embedding Model implementation.
|
|
15
|
+
*
|
|
16
|
+
* Provides an EmbeddingModel implementation for OpenAI-compatible embeddings APIs.
|
|
17
|
+
*
|
|
18
|
+
* @since 1.0.0
|
|
19
|
+
*/
|
|
20
|
+
export * as OpenAiEmbeddingModel from "./OpenAiEmbeddingModel.js";
|
|
13
21
|
/**
|
|
14
22
|
* @since 1.0.0
|
|
15
23
|
*/
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["OpenAiClient","OpenAiConfig","OpenAiError","OpenAiLanguageModel","OpenAiTelemetry"],"sources":["../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAIA;AAEA;;;AAGA,OAAO,KAAKA,YAAY,MAAM,mBAAmB;AAEjD;;;AAGA,OAAO,KAAKC,YAAY,MAAM,mBAAmB;AAEjD;;;AAGA,OAAO,KAAKC,WAAW,MAAM,kBAAkB;AAE/C;;;;;;;;AAQA,OAAO,KAAKC,mBAAmB,MAAM,0BAA0B;AAE/D;;;;;;;;;AASA,OAAO,KAAKC,eAAe,MAAM,sBAAsB","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"index.js","names":["OpenAiClient","OpenAiConfig","OpenAiEmbeddingModel","OpenAiError","OpenAiLanguageModel","OpenAiTelemetry"],"sources":["../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA;;;AAIA;AAEA;;;AAGA,OAAO,KAAKA,YAAY,MAAM,mBAAmB;AAEjD;;;AAGA,OAAO,KAAKC,YAAY,MAAM,mBAAmB;AAEjD;;;;;;;AAOA,OAAO,KAAKC,oBAAoB,MAAM,2BAA2B;AAEjE;;;AAGA,OAAO,KAAKC,WAAW,MAAM,kBAAkB;AAE/C;;;;;;;;AAQA,OAAO,KAAKC,mBAAmB,MAAM,0BAA0B;AAE/D;;;;;;;;;AASA,OAAO,KAAKC,eAAe,MAAM,sBAAsB","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/ai-openai-compat",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.35",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "An OpenAI compat integration for Effect",
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
"provenance": true
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"effect": "^4.0.0-beta.
|
|
48
|
+
"effect": "^4.0.0-beta.35"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"effect": "^4.0.0-beta.
|
|
51
|
+
"effect": "^4.0.0-beta.35"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"codegen": "effect-utils codegen",
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Embedding Model implementation.
|
|
3
|
+
*
|
|
4
|
+
* Provides an EmbeddingModel implementation for OpenAI-compatible embeddings APIs.
|
|
5
|
+
*
|
|
6
|
+
* @since 1.0.0
|
|
7
|
+
*/
|
|
8
|
+
import * as Effect from "effect/Effect"
|
|
9
|
+
import { dual } from "effect/Function"
|
|
10
|
+
import * as Layer from "effect/Layer"
|
|
11
|
+
import * as ServiceMap from "effect/ServiceMap"
|
|
12
|
+
import type { Simplify } from "effect/Types"
|
|
13
|
+
import * as AiError from "effect/unstable/ai/AiError"
|
|
14
|
+
import * as EmbeddingModel from "effect/unstable/ai/EmbeddingModel"
|
|
15
|
+
import * as AiModel from "effect/unstable/ai/Model"
|
|
16
|
+
import type { CreateEmbedding200, CreateEmbeddingRequestJson } from "./OpenAiClient.ts"
|
|
17
|
+
import { OpenAiClient } from "./OpenAiClient.ts"
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @since 1.0.0
|
|
21
|
+
* @category models
|
|
22
|
+
*/
|
|
23
|
+
export type Model = string
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Service definition for OpenAI embedding model configuration.
|
|
27
|
+
*
|
|
28
|
+
* @since 1.0.0
|
|
29
|
+
* @category context
|
|
30
|
+
*/
|
|
31
|
+
export class Config extends ServiceMap.Service<
|
|
32
|
+
Config,
|
|
33
|
+
Simplify<
|
|
34
|
+
& Partial<
|
|
35
|
+
Omit<
|
|
36
|
+
CreateEmbeddingRequestJson,
|
|
37
|
+
"input"
|
|
38
|
+
>
|
|
39
|
+
>
|
|
40
|
+
& {
|
|
41
|
+
readonly [x: string]: unknown
|
|
42
|
+
}
|
|
43
|
+
>
|
|
44
|
+
>()("@effect/ai-openai-compat/OpenAiEmbeddingModel/Config") {}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @since 1.0.0
|
|
48
|
+
* @category constructors
|
|
49
|
+
*/
|
|
50
|
+
export const model = (
|
|
51
|
+
model: string,
|
|
52
|
+
options: {
|
|
53
|
+
readonly dimensions: number
|
|
54
|
+
readonly config?: Omit<typeof Config.Service, "model" | "dimensions">
|
|
55
|
+
}
|
|
56
|
+
): AiModel.Model<"openai", EmbeddingModel.EmbeddingModel | EmbeddingModel.Dimensions, OpenAiClient> =>
|
|
57
|
+
AiModel.make(
|
|
58
|
+
"openai",
|
|
59
|
+
model,
|
|
60
|
+
Layer.merge(
|
|
61
|
+
layer({
|
|
62
|
+
model,
|
|
63
|
+
config: {
|
|
64
|
+
...options.config,
|
|
65
|
+
dimensions: options.dimensions
|
|
66
|
+
}
|
|
67
|
+
}),
|
|
68
|
+
Layer.succeed(EmbeddingModel.Dimensions, options.dimensions)
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Creates an OpenAI embedding model service.
|
|
74
|
+
*
|
|
75
|
+
* @since 1.0.0
|
|
76
|
+
* @category constructors
|
|
77
|
+
*/
|
|
78
|
+
export const make = Effect.fnUntraced(function*({ model, config: providerConfig }: {
|
|
79
|
+
readonly model: string
|
|
80
|
+
readonly config?: Omit<typeof Config.Service, "model"> | undefined
|
|
81
|
+
}): Effect.fn.Return<EmbeddingModel.Service, never, OpenAiClient> {
|
|
82
|
+
const client = yield* OpenAiClient
|
|
83
|
+
|
|
84
|
+
const makeConfig = Effect.gen(function*() {
|
|
85
|
+
const services = yield* Effect.services<never>()
|
|
86
|
+
return { model, ...providerConfig, ...services.mapUnsafe.get(Config.key) }
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
return yield* EmbeddingModel.make({
|
|
90
|
+
embedMany: Effect.fnUntraced(function*({ inputs }) {
|
|
91
|
+
const config = yield* makeConfig
|
|
92
|
+
const response = yield* client.createEmbedding({ ...config, input: inputs })
|
|
93
|
+
return yield* mapProviderResponse(inputs.length, response)
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Creates a layer for the OpenAI embedding model.
|
|
100
|
+
*
|
|
101
|
+
* @since 1.0.0
|
|
102
|
+
* @category layers
|
|
103
|
+
*/
|
|
104
|
+
export const layer = (options: {
|
|
105
|
+
readonly model: string
|
|
106
|
+
readonly config?: Omit<typeof Config.Service, "model"> | undefined
|
|
107
|
+
}): Layer.Layer<EmbeddingModel.EmbeddingModel, never, OpenAiClient> =>
|
|
108
|
+
Layer.effect(EmbeddingModel.EmbeddingModel, make(options))
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Provides config overrides for OpenAI embedding model operations.
|
|
112
|
+
*
|
|
113
|
+
* @since 1.0.0
|
|
114
|
+
* @category configuration
|
|
115
|
+
*/
|
|
116
|
+
export const withConfigOverride: {
|
|
117
|
+
/**
|
|
118
|
+
* Provides config overrides for OpenAI embedding model operations.
|
|
119
|
+
*
|
|
120
|
+
* @since 1.0.0
|
|
121
|
+
* @category configuration
|
|
122
|
+
*/
|
|
123
|
+
(overrides: typeof Config.Service): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>
|
|
124
|
+
/**
|
|
125
|
+
* Provides config overrides for OpenAI embedding model operations.
|
|
126
|
+
*
|
|
127
|
+
* @since 1.0.0
|
|
128
|
+
* @category configuration
|
|
129
|
+
*/
|
|
130
|
+
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service): Effect.Effect<A, E, Exclude<R, Config>>
|
|
131
|
+
} = dual<
|
|
132
|
+
/**
|
|
133
|
+
* Provides config overrides for OpenAI embedding model operations.
|
|
134
|
+
*
|
|
135
|
+
* @since 1.0.0
|
|
136
|
+
* @category configuration
|
|
137
|
+
*/
|
|
138
|
+
(overrides: typeof Config.Service) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Config>>,
|
|
139
|
+
/**
|
|
140
|
+
* Provides config overrides for OpenAI embedding model operations.
|
|
141
|
+
*
|
|
142
|
+
* @since 1.0.0
|
|
143
|
+
* @category configuration
|
|
144
|
+
*/
|
|
145
|
+
<A, E, R>(self: Effect.Effect<A, E, R>, overrides: typeof Config.Service) => Effect.Effect<A, E, Exclude<R, Config>>
|
|
146
|
+
>(2, (self, overrides) =>
|
|
147
|
+
Effect.flatMap(
|
|
148
|
+
Effect.serviceOption(Config),
|
|
149
|
+
(config) =>
|
|
150
|
+
Effect.provideService(self, Config, {
|
|
151
|
+
...(config._tag === "Some" ? config.value : {}),
|
|
152
|
+
...overrides
|
|
153
|
+
})
|
|
154
|
+
))
|
|
155
|
+
|
|
156
|
+
const mapProviderResponse = (
|
|
157
|
+
inputLength: number,
|
|
158
|
+
response: CreateEmbedding200
|
|
159
|
+
): Effect.Effect<EmbeddingModel.ProviderResponse, AiError.AiError> => {
|
|
160
|
+
if (response.data.length !== inputLength) {
|
|
161
|
+
return Effect.fail(
|
|
162
|
+
invalidOutput(`Provider returned ${response.data.length} embeddings but expected ${inputLength}`)
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const results = new Array<Array<number>>(inputLength)
|
|
167
|
+
const seen = new Set<number>()
|
|
168
|
+
|
|
169
|
+
for (const entry of response.data) {
|
|
170
|
+
if (!Number.isInteger(entry.index) || entry.index < 0 || entry.index >= inputLength) {
|
|
171
|
+
return Effect.fail(invalidOutput(`Provider returned invalid embedding index: ${entry.index}`))
|
|
172
|
+
}
|
|
173
|
+
if (seen.has(entry.index)) {
|
|
174
|
+
return Effect.fail(invalidOutput(`Provider returned duplicate embedding index: ${entry.index}`))
|
|
175
|
+
}
|
|
176
|
+
if (!Array.isArray(entry.embedding)) {
|
|
177
|
+
return Effect.fail(invalidOutput(`Provider returned non-vector embedding at index ${entry.index}`))
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
seen.add(entry.index)
|
|
181
|
+
results[entry.index] = [...entry.embedding]
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (seen.size !== inputLength) {
|
|
185
|
+
return Effect.fail(
|
|
186
|
+
invalidOutput(`Provider returned embeddings for ${seen.size} inputs but expected ${inputLength}`)
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return Effect.succeed({
|
|
191
|
+
results,
|
|
192
|
+
usage: {
|
|
193
|
+
inputTokens: response.usage?.prompt_tokens
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const invalidOutput = (description: string): AiError.AiError =>
|
|
199
|
+
AiError.make({
|
|
200
|
+
module: "OpenAiEmbeddingModel",
|
|
201
|
+
method: "embedMany",
|
|
202
|
+
reason: new AiError.InvalidOutputError({ description })
|
|
203
|
+
})
|
package/src/index.ts
CHANGED
|
@@ -14,6 +14,15 @@ export * as OpenAiClient from "./OpenAiClient.ts"
|
|
|
14
14
|
*/
|
|
15
15
|
export * as OpenAiConfig from "./OpenAiConfig.ts"
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* OpenAI Embedding Model implementation.
|
|
19
|
+
*
|
|
20
|
+
* Provides an EmbeddingModel implementation for OpenAI-compatible embeddings APIs.
|
|
21
|
+
*
|
|
22
|
+
* @since 1.0.0
|
|
23
|
+
*/
|
|
24
|
+
export * as OpenAiEmbeddingModel from "./OpenAiEmbeddingModel.ts"
|
|
25
|
+
|
|
17
26
|
/**
|
|
18
27
|
* @since 1.0.0
|
|
19
28
|
*/
|