@effect/ai 0.26.0 → 0.27.0
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/Chat/package.json +6 -0
- package/EmbeddingModel/package.json +6 -0
- package/IdGenerator/package.json +6 -0
- package/LanguageModel/package.json +6 -0
- package/Model/package.json +6 -0
- package/Prompt/package.json +6 -0
- package/Response/package.json +6 -0
- package/Telemetry/package.json +6 -0
- package/Tool/package.json +6 -0
- package/Toolkit/package.json +6 -0
- package/dist/cjs/AiError.js +575 -11
- package/dist/cjs/AiError.js.map +1 -1
- package/dist/cjs/Chat.js +302 -0
- package/dist/cjs/Chat.js.map +1 -0
- package/dist/cjs/EmbeddingModel.js +184 -0
- package/dist/cjs/EmbeddingModel.js.map +1 -0
- package/dist/cjs/IdGenerator.js +255 -0
- package/dist/cjs/IdGenerator.js.map +1 -0
- package/dist/cjs/LanguageModel.js +584 -0
- package/dist/cjs/LanguageModel.js.map +1 -0
- package/dist/cjs/McpServer.js +12 -4
- package/dist/cjs/McpServer.js.map +1 -1
- package/dist/cjs/Model.js +118 -0
- package/dist/cjs/Model.js.map +1 -0
- package/dist/cjs/Prompt.js +649 -0
- package/dist/cjs/Prompt.js.map +1 -0
- package/dist/cjs/Response.js +635 -0
- package/dist/cjs/Response.js.map +1 -0
- package/dist/cjs/Telemetry.js +176 -0
- package/dist/cjs/Telemetry.js.map +1 -0
- package/dist/cjs/Tokenizer.js +87 -8
- package/dist/cjs/Tokenizer.js.map +1 -1
- package/dist/cjs/Tool.js +556 -0
- package/dist/cjs/Tool.js.map +1 -0
- package/dist/cjs/Toolkit.js +279 -0
- package/dist/cjs/Toolkit.js.map +1 -0
- package/dist/cjs/index.js +21 -19
- package/dist/dts/AiError.d.ts +577 -9
- package/dist/dts/AiError.d.ts.map +1 -1
- package/dist/dts/Chat.d.ts +356 -0
- package/dist/dts/Chat.d.ts.map +1 -0
- package/dist/dts/EmbeddingModel.d.ts +153 -0
- package/dist/dts/EmbeddingModel.d.ts.map +1 -0
- package/dist/dts/IdGenerator.d.ts +272 -0
- package/dist/dts/IdGenerator.d.ts.map +1 -0
- package/dist/dts/LanguageModel.d.ts +458 -0
- package/dist/dts/LanguageModel.d.ts.map +1 -0
- package/dist/dts/McpSchema.d.ts +25 -25
- package/dist/dts/McpServer.d.ts +6 -4
- package/dist/dts/McpServer.d.ts.map +1 -1
- package/dist/dts/Model.d.ts +124 -0
- package/dist/dts/Model.d.ts.map +1 -0
- package/dist/dts/Prompt.d.ts +1119 -0
- package/dist/dts/Prompt.d.ts.map +1 -0
- package/dist/dts/Response.d.ts +1519 -0
- package/dist/dts/Response.d.ts.map +1 -0
- package/dist/dts/Telemetry.d.ts +520 -0
- package/dist/dts/Telemetry.d.ts.map +1 -0
- package/dist/dts/Tokenizer.d.ts +131 -13
- package/dist/dts/Tokenizer.d.ts.map +1 -1
- package/dist/dts/Tool.d.ts +876 -0
- package/dist/dts/Tool.d.ts.map +1 -0
- package/dist/dts/Toolkit.d.ts +310 -0
- package/dist/dts/Toolkit.d.ts.map +1 -0
- package/dist/dts/index.d.ts +498 -13
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/esm/AiError.js +570 -10
- package/dist/esm/AiError.js.map +1 -1
- package/dist/esm/Chat.js +291 -0
- package/dist/esm/Chat.js.map +1 -0
- package/dist/esm/EmbeddingModel.js +173 -0
- package/dist/esm/EmbeddingModel.js.map +1 -0
- package/dist/esm/IdGenerator.js +245 -0
- package/dist/esm/IdGenerator.js.map +1 -0
- package/dist/esm/LanguageModel.js +572 -0
- package/dist/esm/LanguageModel.js.map +1 -0
- package/dist/esm/McpServer.js +12 -4
- package/dist/esm/McpServer.js.map +1 -1
- package/dist/esm/Model.js +108 -0
- package/dist/esm/Model.js.map +1 -0
- package/dist/esm/Prompt.js +633 -0
- package/dist/esm/Prompt.js.map +1 -0
- package/dist/esm/Response.js +619 -0
- package/dist/esm/Response.js.map +1 -0
- package/dist/esm/Telemetry.js +166 -0
- package/dist/esm/Telemetry.js.map +1 -0
- package/dist/esm/Tokenizer.js +87 -8
- package/dist/esm/Tokenizer.js.map +1 -1
- package/dist/esm/Tool.js +534 -0
- package/dist/esm/Tool.js.map +1 -0
- package/dist/esm/Toolkit.js +269 -0
- package/dist/esm/Toolkit.js.map +1 -0
- package/dist/esm/index.js +498 -13
- package/dist/esm/index.js.map +1 -1
- package/package.json +76 -68
- package/src/AiError.ts +739 -9
- package/src/Chat.ts +546 -0
- package/src/EmbeddingModel.ts +311 -0
- package/src/IdGenerator.ts +320 -0
- package/src/LanguageModel.ts +1074 -0
- package/src/McpServer.ts +337 -194
- package/src/Model.ts +155 -0
- package/src/Prompt.ts +1616 -0
- package/src/Response.ts +2131 -0
- package/src/Telemetry.ts +655 -0
- package/src/Tokenizer.ts +145 -24
- package/src/Tool.ts +1267 -0
- package/src/Toolkit.ts +516 -0
- package/src/index.ts +499 -13
- package/AiChat/package.json +0 -6
- package/AiEmbeddingModel/package.json +0 -6
- package/AiInput/package.json +0 -6
- package/AiLanguageModel/package.json +0 -6
- package/AiModel/package.json +0 -6
- package/AiResponse/package.json +0 -6
- package/AiTelemetry/package.json +0 -6
- package/AiTool/package.json +0 -6
- package/AiToolkit/package.json +0 -6
- package/dist/cjs/AiChat.js +0 -122
- package/dist/cjs/AiChat.js.map +0 -1
- package/dist/cjs/AiEmbeddingModel.js +0 -109
- package/dist/cjs/AiEmbeddingModel.js.map +0 -1
- package/dist/cjs/AiInput.js +0 -458
- package/dist/cjs/AiInput.js.map +0 -1
- package/dist/cjs/AiLanguageModel.js +0 -351
- package/dist/cjs/AiLanguageModel.js.map +0 -1
- package/dist/cjs/AiModel.js +0 -37
- package/dist/cjs/AiModel.js.map +0 -1
- package/dist/cjs/AiResponse.js +0 -681
- package/dist/cjs/AiResponse.js.map +0 -1
- package/dist/cjs/AiTelemetry.js +0 -58
- package/dist/cjs/AiTelemetry.js.map +0 -1
- package/dist/cjs/AiTool.js +0 -150
- package/dist/cjs/AiTool.js.map +0 -1
- package/dist/cjs/AiToolkit.js +0 -157
- package/dist/cjs/AiToolkit.js.map +0 -1
- package/dist/cjs/internal/common.js +0 -21
- package/dist/cjs/internal/common.js.map +0 -1
- package/dist/dts/AiChat.d.ts +0 -101
- package/dist/dts/AiChat.d.ts.map +0 -1
- package/dist/dts/AiEmbeddingModel.d.ts +0 -65
- package/dist/dts/AiEmbeddingModel.d.ts.map +0 -1
- package/dist/dts/AiInput.d.ts +0 -590
- package/dist/dts/AiInput.d.ts.map +0 -1
- package/dist/dts/AiLanguageModel.d.ts +0 -302
- package/dist/dts/AiLanguageModel.d.ts.map +0 -1
- package/dist/dts/AiModel.d.ts +0 -25
- package/dist/dts/AiModel.d.ts.map +0 -1
- package/dist/dts/AiResponse.d.ts +0 -863
- package/dist/dts/AiResponse.d.ts.map +0 -1
- package/dist/dts/AiTelemetry.d.ts +0 -242
- package/dist/dts/AiTelemetry.d.ts.map +0 -1
- package/dist/dts/AiTool.d.ts +0 -334
- package/dist/dts/AiTool.d.ts.map +0 -1
- package/dist/dts/AiToolkit.d.ts +0 -96
- package/dist/dts/AiToolkit.d.ts.map +0 -1
- package/dist/dts/internal/common.d.ts +0 -2
- package/dist/dts/internal/common.d.ts.map +0 -1
- package/dist/esm/AiChat.js +0 -111
- package/dist/esm/AiChat.js.map +0 -1
- package/dist/esm/AiEmbeddingModel.js +0 -98
- package/dist/esm/AiEmbeddingModel.js.map +0 -1
- package/dist/esm/AiInput.js +0 -433
- package/dist/esm/AiInput.js.map +0 -1
- package/dist/esm/AiLanguageModel.js +0 -340
- package/dist/esm/AiLanguageModel.js.map +0 -1
- package/dist/esm/AiModel.js +0 -29
- package/dist/esm/AiModel.js.map +0 -1
- package/dist/esm/AiResponse.js +0 -657
- package/dist/esm/AiResponse.js.map +0 -1
- package/dist/esm/AiTelemetry.js +0 -48
- package/dist/esm/AiTelemetry.js.map +0 -1
- package/dist/esm/AiTool.js +0 -134
- package/dist/esm/AiTool.js.map +0 -1
- package/dist/esm/AiToolkit.js +0 -147
- package/dist/esm/AiToolkit.js.map +0 -1
- package/dist/esm/internal/common.js +0 -14
- package/dist/esm/internal/common.js.map +0 -1
- package/src/AiChat.ts +0 -251
- package/src/AiEmbeddingModel.ts +0 -169
- package/src/AiInput.ts +0 -602
- package/src/AiLanguageModel.ts +0 -685
- package/src/AiModel.ts +0 -53
- package/src/AiResponse.ts +0 -986
- package/src/AiTelemetry.ts +0 -333
- package/src/AiTool.ts +0 -579
- package/src/AiToolkit.ts +0 -265
- package/src/internal/common.ts +0 -12
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import * as Context from "effect/Context";
|
|
2
|
+
import * as Effect from "effect/Effect";
|
|
3
|
+
import type { ParseError } from "effect/ParseResult";
|
|
4
|
+
import * as Ref from "effect/Ref";
|
|
5
|
+
import * as Stream from "effect/Stream";
|
|
6
|
+
import type { NoExcessProperties } from "effect/Types";
|
|
7
|
+
import * as LanguageModel from "./LanguageModel.js";
|
|
8
|
+
import * as Prompt from "./Prompt.js";
|
|
9
|
+
import type * as Response from "./Response.js";
|
|
10
|
+
import type * as Tool from "./Tool.js";
|
|
11
|
+
declare const Chat_base: Context.TagClass<Chat, "@effect/ai/Chat", Service>;
|
|
12
|
+
/**
|
|
13
|
+
* The `Chat` service tag for dependency injection.
|
|
14
|
+
*
|
|
15
|
+
* This tag provides access to chat functionality throughout your application,
|
|
16
|
+
* enabling persistent conversational AI interactions with full context
|
|
17
|
+
* management.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { Chat } from "@effect/ai"
|
|
22
|
+
* import { Effect } from "effect"
|
|
23
|
+
*
|
|
24
|
+
* const useChat = Effect.gen(function* () {
|
|
25
|
+
* const chat = yield* Chat
|
|
26
|
+
* const response = yield* chat.generateText({
|
|
27
|
+
* prompt: "Explain quantum computing in simple terms"
|
|
28
|
+
* })
|
|
29
|
+
* return response.content
|
|
30
|
+
* })
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @since 1.0.0
|
|
34
|
+
* @category Context
|
|
35
|
+
*/
|
|
36
|
+
export declare class Chat extends Chat_base {
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Represents the interface that the `Chat` service provides.
|
|
40
|
+
*
|
|
41
|
+
* @since 1.0.0
|
|
42
|
+
* @category Models
|
|
43
|
+
*/
|
|
44
|
+
export interface Service {
|
|
45
|
+
/**
|
|
46
|
+
* Reference to the chat history.
|
|
47
|
+
*
|
|
48
|
+
* Provides direct access to the conversation history for advanced use cases
|
|
49
|
+
* like custom history manipulation or inspection.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { Chat } from "@effect/ai"
|
|
54
|
+
* import { Effect, Ref } from "effect"
|
|
55
|
+
*
|
|
56
|
+
* const inspectHistory = Effect.gen(function* () {
|
|
57
|
+
* const chat = yield* Chat.empty
|
|
58
|
+
* const currentHistory = yield* Ref.get(chat.history)
|
|
59
|
+
* console.log("Current conversation:", currentHistory)
|
|
60
|
+
* return currentHistory
|
|
61
|
+
* })
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
readonly history: Ref.Ref<Prompt.Prompt>;
|
|
65
|
+
/**
|
|
66
|
+
* Exports the chat history into a structured format.
|
|
67
|
+
*
|
|
68
|
+
* Returns the complete conversation history as a structured object
|
|
69
|
+
* that can be stored, transmitted, or processed by other systems.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* import { Chat } from "@effect/ai"
|
|
74
|
+
* import { Effect } from "effect"
|
|
75
|
+
*
|
|
76
|
+
* const saveChat = Effect.gen(function* () {
|
|
77
|
+
* const chat = yield* Chat.empty
|
|
78
|
+
* yield* chat.generateText({ prompt: "Hello!" })
|
|
79
|
+
*
|
|
80
|
+
* const exportedData = yield* chat.export
|
|
81
|
+
*
|
|
82
|
+
* // Save to database or file system
|
|
83
|
+
* return exportedData
|
|
84
|
+
* })
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
readonly export: Effect.Effect<unknown>;
|
|
88
|
+
/**
|
|
89
|
+
* Exports the chat history as a JSON string.
|
|
90
|
+
*
|
|
91
|
+
* Provides a convenient way to serialize the entire conversation
|
|
92
|
+
* for storage or transmission in JSON format.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { Chat } from "@effect/ai"
|
|
97
|
+
* import { Effect } from "effect"
|
|
98
|
+
*
|
|
99
|
+
* const backupChat = Effect.gen(function* () {
|
|
100
|
+
* const chat = yield* Chat.empty
|
|
101
|
+
* yield* chat.generateText({ prompt: "Explain photosynthesis" })
|
|
102
|
+
*
|
|
103
|
+
* const jsonBackup = yield* chat.exportJson
|
|
104
|
+
*
|
|
105
|
+
* yield* Effect.sync(() =>
|
|
106
|
+
* localStorage.setItem("chat-backup", jsonBackup)
|
|
107
|
+
* )
|
|
108
|
+
*
|
|
109
|
+
* return jsonBackup
|
|
110
|
+
* })
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
readonly exportJson: Effect.Effect<string>;
|
|
114
|
+
/**
|
|
115
|
+
* Generate text using a language model for the specified prompt.
|
|
116
|
+
*
|
|
117
|
+
* If a toolkit is specified, the language model will have access to tools
|
|
118
|
+
* for function calling and enhanced capabilities. Both input and output
|
|
119
|
+
* messages are automatically added to the chat history.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* import { Chat } from "@effect/ai"
|
|
124
|
+
* import { Effect } from "effect"
|
|
125
|
+
*
|
|
126
|
+
* const chatWithAI = Effect.gen(function* () {
|
|
127
|
+
* const chat = yield* Chat.empty
|
|
128
|
+
*
|
|
129
|
+
* const response1 = yield* chat.generateText({
|
|
130
|
+
* prompt: "What is the capital of France?"
|
|
131
|
+
* })
|
|
132
|
+
*
|
|
133
|
+
* const response2 = yield* chat.generateText({
|
|
134
|
+
* prompt: "What's the population of that city?",
|
|
135
|
+
* })
|
|
136
|
+
*
|
|
137
|
+
* return [response1.content, response2.content]
|
|
138
|
+
* })
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
readonly generateText: <Options extends NoExcessProperties<LanguageModel.GenerateTextOptions<any>, Options>, Tools extends Record<string, Tool.Any> = {}>(options: Options & LanguageModel.GenerateTextOptions<Tools>) => Effect.Effect<LanguageModel.GenerateTextResponse<Tools>, LanguageModel.ExtractError<Options>, LanguageModel.ExtractContext<Options>>;
|
|
142
|
+
/**
|
|
143
|
+
* Generate text using a language model with streaming output.
|
|
144
|
+
*
|
|
145
|
+
* Returns a stream of response parts that are emitted as soon as they're
|
|
146
|
+
* available from the model. Supports tool calling and maintains chat history.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* import { Chat } from "@effect/ai"
|
|
151
|
+
* import { Effect, Stream, Console } from "effect"
|
|
152
|
+
*
|
|
153
|
+
* const streamingChat = Effect.gen(function* () {
|
|
154
|
+
* const chat = yield* Chat.empty
|
|
155
|
+
*
|
|
156
|
+
* const stream = yield* chat.streamText({
|
|
157
|
+
* prompt: "Write a short story about space exploration"
|
|
158
|
+
* })
|
|
159
|
+
*
|
|
160
|
+
* yield* Stream.runForEach(stream, (part) =>
|
|
161
|
+
* part.type === "text-delta"
|
|
162
|
+
* ? Effect.sync(() => process.stdout.write(part.delta))
|
|
163
|
+
* : Effect.void
|
|
164
|
+
* )
|
|
165
|
+
* })
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
readonly streamText: <Options extends NoExcessProperties<LanguageModel.GenerateTextOptions<any>, Options>, Tools extends Record<string, Tool.Any> = {}>(options: Options & LanguageModel.GenerateTextOptions<Tools>) => Stream.Stream<Response.StreamPart<Tools>, LanguageModel.ExtractError<Options>, LanguageModel.ExtractContext<Options>>;
|
|
169
|
+
/**
|
|
170
|
+
* Generate a structured object using a language model and schema.
|
|
171
|
+
*
|
|
172
|
+
* Forces the model to return data that conforms to the specified schema,
|
|
173
|
+
* enabling structured data extraction and type-safe responses. The
|
|
174
|
+
* conversation history is maintained across calls.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* import { Chat } from "@effect/ai"
|
|
179
|
+
* import { Effect, Schema } from "effect"
|
|
180
|
+
*
|
|
181
|
+
* const ContactSchema = Schema.Struct({
|
|
182
|
+
* name: Schema.String,
|
|
183
|
+
* email: Schema.String,
|
|
184
|
+
* phone: Schema.optional(Schema.String)
|
|
185
|
+
* })
|
|
186
|
+
*
|
|
187
|
+
* const extractContact = Effect.gen(function* () {
|
|
188
|
+
* const chat = yield* Chat.empty
|
|
189
|
+
*
|
|
190
|
+
* const contact = yield* chat.generateObject({
|
|
191
|
+
* prompt: "Extract contact info: John Doe, john@example.com, 555-1234",
|
|
192
|
+
* schema: ContactSchema
|
|
193
|
+
* })
|
|
194
|
+
*
|
|
195
|
+
* console.log(contact.object)
|
|
196
|
+
* // { name: "John Doe", email: "john@example.com", phone: "555-1234" }
|
|
197
|
+
*
|
|
198
|
+
* return contact.object
|
|
199
|
+
* })
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
readonly generateObject: <A, I extends Record<string, unknown>, R, Options extends NoExcessProperties<LanguageModel.GenerateObjectOptions<any, A, I, R>, Options>, Tools extends Record<string, Tool.Any> = {}>(options: Options & LanguageModel.GenerateObjectOptions<Tools, A, I, R>) => Effect.Effect<LanguageModel.GenerateObjectResponse<Tools, A>, LanguageModel.ExtractError<Options>, LanguageModel.LanguageModel | R | LanguageModel.ExtractContext<Options>>;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Creates a new Chat service from an initial prompt.
|
|
206
|
+
*
|
|
207
|
+
* This is the primary constructor for creating chat instances. It initializes
|
|
208
|
+
* a new conversation with the provided prompt as the starting context.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```ts
|
|
212
|
+
* import { Chat, Prompt } from "@effect/ai"
|
|
213
|
+
* import { Effect } from "effect"
|
|
214
|
+
*
|
|
215
|
+
* const chatWithSystemPrompt = Effect.gen(function* () {
|
|
216
|
+
* const chat = yield* Chat.fromPrompt([{
|
|
217
|
+
* role: "system",
|
|
218
|
+
* content: "You are a helpful assistant specialized in mathematics."
|
|
219
|
+
* }])
|
|
220
|
+
*
|
|
221
|
+
* const response = yield* chat.generateText({
|
|
222
|
+
* prompt: "What is 2+2?"
|
|
223
|
+
* })
|
|
224
|
+
*
|
|
225
|
+
* return response.content
|
|
226
|
+
* })
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* import { Chat, Prompt } from "@effect/ai"
|
|
232
|
+
* import { Effect } from "effect"
|
|
233
|
+
*
|
|
234
|
+
* // Initialize with conversation history
|
|
235
|
+
* const existingChat = Effect.gen(function* () {
|
|
236
|
+
* const chat = yield* Chat.fromPrompt([
|
|
237
|
+
* { role: "user", content: [{ type: "text", text: "What's the weather like?" }] },
|
|
238
|
+
* { role: "assistant", content: [{ type: "text", text: "I don't have access to weather data." }] },
|
|
239
|
+
* { role: "user", content: [{ type: "text", text: "Can you help me with coding?" }] }
|
|
240
|
+
* ])
|
|
241
|
+
*
|
|
242
|
+
* const response = yield* chat.generateText({
|
|
243
|
+
* prompt: "I need help with TypeScript"
|
|
244
|
+
* })
|
|
245
|
+
*
|
|
246
|
+
* return response
|
|
247
|
+
* })
|
|
248
|
+
* ```
|
|
249
|
+
*
|
|
250
|
+
* @since 1.0.0
|
|
251
|
+
* @category Constructors
|
|
252
|
+
*/
|
|
253
|
+
export declare const fromPrompt: (prompt: Prompt.RawInput) => Effect.Effect<Service, never, LanguageModel.LanguageModel>;
|
|
254
|
+
/**
|
|
255
|
+
* Creates a new Chat service with empty conversation history.
|
|
256
|
+
*
|
|
257
|
+
* This is the most common way to start a fresh chat session without
|
|
258
|
+
* any initial context or system prompts.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```ts
|
|
262
|
+
* import { Chat } from "@effect/ai"
|
|
263
|
+
* import { Effect } from "effect"
|
|
264
|
+
*
|
|
265
|
+
* const freshChat = Effect.gen(function* () {
|
|
266
|
+
* const chat = yield* Chat.empty
|
|
267
|
+
*
|
|
268
|
+
* const response = yield* chat.generateText({
|
|
269
|
+
* prompt: "Hello! Can you introduce yourself?"
|
|
270
|
+
* })
|
|
271
|
+
*
|
|
272
|
+
* console.log(response.content)
|
|
273
|
+
*
|
|
274
|
+
* return chat
|
|
275
|
+
* })
|
|
276
|
+
* ```
|
|
277
|
+
*
|
|
278
|
+
* @since 1.0.0
|
|
279
|
+
* @category Constructors
|
|
280
|
+
*/
|
|
281
|
+
export declare const empty: Effect.Effect<Service, never, LanguageModel.LanguageModel>;
|
|
282
|
+
/**
|
|
283
|
+
* Creates a Chat service from previously exported chat data.
|
|
284
|
+
*
|
|
285
|
+
* Restores a chat session from structured data that was previously exported
|
|
286
|
+
* using the `export` method. Useful for persisting and restoring conversation
|
|
287
|
+
* state.
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```ts
|
|
291
|
+
* import { Chat } from "@effect/ai"
|
|
292
|
+
* import { Effect } from "effect"
|
|
293
|
+
*
|
|
294
|
+
* declare const loadFromDatabase: (sessionId: string) => Effect.Effect<unknown>
|
|
295
|
+
*
|
|
296
|
+
* const restoreChat = Effect.gen(function* () {
|
|
297
|
+
* // Assume we have previously exported data
|
|
298
|
+
* const savedData = yield* loadFromDatabase("chat-session-123")
|
|
299
|
+
*
|
|
300
|
+
* const restoredChat = yield* Chat.fromExport(savedData)
|
|
301
|
+
*
|
|
302
|
+
* // Continue the conversation from where it left off
|
|
303
|
+
* const response = yield* restoredChat.generateText({
|
|
304
|
+
* prompt: "Let's continue our discussion"
|
|
305
|
+
* })
|
|
306
|
+
* }).pipe(
|
|
307
|
+
* Effect.catchTag("ParseError", (error) => {
|
|
308
|
+
* console.log("Failed to restore chat:", error.message)
|
|
309
|
+
* return Effect.void
|
|
310
|
+
* })
|
|
311
|
+
* )
|
|
312
|
+
* ```
|
|
313
|
+
*
|
|
314
|
+
* @since 1.0.0
|
|
315
|
+
* @category Constructors
|
|
316
|
+
*/
|
|
317
|
+
export declare const fromExport: (data: unknown) => Effect.Effect<Service, ParseError, LanguageModel.LanguageModel>;
|
|
318
|
+
/**
|
|
319
|
+
* Creates a Chat service from previously exported JSON chat data.
|
|
320
|
+
*
|
|
321
|
+
* Restores a chat session from JSON string that was previously exported
|
|
322
|
+
* using the `exportJson` method. This is the most convenient way to
|
|
323
|
+
* persist and restore chat sessions to/from storage systems.
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* ```ts
|
|
327
|
+
* import { Chat } from "@effect/ai"
|
|
328
|
+
* import { Effect } from "effect"
|
|
329
|
+
*
|
|
330
|
+
* const restoreFromJson = Effect.gen(function* () {
|
|
331
|
+
* // Load JSON from localStorage or file system
|
|
332
|
+
* const jsonData = localStorage.getItem("my-chat-backup")
|
|
333
|
+
* if (!jsonData) return yield* Chat.empty
|
|
334
|
+
*
|
|
335
|
+
* const restoredChat = yield* Chat.fromJson(jsonData)
|
|
336
|
+
*
|
|
337
|
+
* // Chat history is now restored
|
|
338
|
+
* const response = yield* restoredChat.generateText({
|
|
339
|
+
* prompt: "What were we talking about?"
|
|
340
|
+
* })
|
|
341
|
+
*
|
|
342
|
+
* return response
|
|
343
|
+
* }).pipe(
|
|
344
|
+
* Effect.catchTag("ParseError", (error) => {
|
|
345
|
+
* console.log("Invalid JSON format:", error.message)
|
|
346
|
+
* return Chat.empty // Fallback to empty chat
|
|
347
|
+
* })
|
|
348
|
+
* )
|
|
349
|
+
* ```
|
|
350
|
+
*
|
|
351
|
+
* @since 1.0.0
|
|
352
|
+
* @category Constructors
|
|
353
|
+
*/
|
|
354
|
+
export declare const fromJson: (data: string) => Effect.Effect<Service, ParseError, LanguageModel.LanguageModel>;
|
|
355
|
+
export {};
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AE,CAAC,EAAE,CAAC,CAAC,KAAK,MAAM,CAAC,MAAM,CACxF,aAAa,CAAC,sBAAsB,CAAC,KAAK,EAAE,CAAC,CAAC,EAC9C,aAAa,CAAC,YAAY,CAAC,OAAO,CAAC,EACnC,aAAa,CAAC,aAAa,GAAG,CAAC,GAAG,aAAa,CAAC,cAAc,CAAC,OAAO,CAAC,CACxE,CAAA;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AACH,eAAO,MAAM,UAAU,yFAoGrB,CAAA;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,aAAa,CAAC,aAAa,CAA4B,CAAA;AAIzG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,UAAU,GAAI,MAAM,OAAO,KAAG,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,UAAU,EAAE,aAAa,CAAC,aAAa,CACxD,CAAA;AAIjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,eAAO,MAAM,QAAQ,GAAI,MAAM,MAAM,KAAG,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,UAAU,EAAE,aAAa,CAAC,aAAa,CACxD,CAAA"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import * as Context from "effect/Context";
|
|
2
|
+
import type * as Duration from "effect/Duration";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import type * as Types from "effect/Types";
|
|
5
|
+
import * as AiError from "./AiError.js";
|
|
6
|
+
declare const EmbeddingModel_base: Context.TagClass<EmbeddingModel, "@effect/ai/EmbeddingModel", Service>;
|
|
7
|
+
/**
|
|
8
|
+
* The `EmbeddingModel` service tag for dependency injection.
|
|
9
|
+
*
|
|
10
|
+
* This tag provides access to vector embedding functionality throughout your application,
|
|
11
|
+
* enabling conversion of text to high-dimensional vectors for semantic analysis.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { EmbeddingModel } from "@effect/ai"
|
|
16
|
+
* import { Effect } from "effect"
|
|
17
|
+
*
|
|
18
|
+
* const useEmbeddings = Effect.gen(function* () {
|
|
19
|
+
* const embedder = yield* EmbeddingModel
|
|
20
|
+
*
|
|
21
|
+
* const documentVector = yield* embedder.embed("This is a sample document")
|
|
22
|
+
* const queryVector = yield* embedder.embed("sample query")
|
|
23
|
+
*
|
|
24
|
+
* const similarity = cosineSimilarity(documentVector, queryVector)
|
|
25
|
+
* return similarity
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @since 1.0.0
|
|
30
|
+
* @category Context
|
|
31
|
+
*/
|
|
32
|
+
export declare class EmbeddingModel extends EmbeddingModel_base {
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The service interface for vector embedding operations.
|
|
36
|
+
*
|
|
37
|
+
* Defines the contract that all embedding model implementations must fulfill.
|
|
38
|
+
* The service provides text-to-vector conversion functionality.
|
|
39
|
+
*
|
|
40
|
+
* @since 1.0.0
|
|
41
|
+
* @category Models
|
|
42
|
+
*/
|
|
43
|
+
export interface Service {
|
|
44
|
+
/**
|
|
45
|
+
* Converts a text string into a vector embedding.
|
|
46
|
+
*/
|
|
47
|
+
readonly embed: (input: string) => Effect.Effect<Array<number>, AiError.AiError>;
|
|
48
|
+
/**
|
|
49
|
+
* Converts a batch of text strings into a chunk of vector embeddings.
|
|
50
|
+
*/
|
|
51
|
+
readonly embedMany: (input: ReadonlyArray<string>, options?: {
|
|
52
|
+
/**
|
|
53
|
+
* The concurrency level to use while batching requests.
|
|
54
|
+
*/
|
|
55
|
+
readonly concurrency?: Types.Concurrency | undefined;
|
|
56
|
+
}) => Effect.Effect<Array<Array<number>>, AiError.AiError>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Represents the result of a batch embedding operation.
|
|
60
|
+
*
|
|
61
|
+
* Used internally by the batching system to associate embeddings with their
|
|
62
|
+
* original request positions in the batch.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* import { EmbeddingModel } from "@effect/ai"
|
|
67
|
+
*
|
|
68
|
+
* const batchResults: EmbeddingModel.Result[] = [
|
|
69
|
+
* { index: 0, embeddings: [0.1, 0.2, 0.3] },
|
|
70
|
+
* { index: 1, embeddings: [0.4, 0.5, 0.6] },
|
|
71
|
+
* { index: 2, embeddings: [0.7, 0.8, 0.9] }
|
|
72
|
+
* ]
|
|
73
|
+
*
|
|
74
|
+
* // Results correspond to input texts at positions 0, 1, 2
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @since 1.0.0
|
|
78
|
+
* @category Models
|
|
79
|
+
*/
|
|
80
|
+
export interface Result {
|
|
81
|
+
/**
|
|
82
|
+
* The position index of this result in the original batch request.
|
|
83
|
+
*/
|
|
84
|
+
readonly index: number;
|
|
85
|
+
/**
|
|
86
|
+
* The vector embedding for the text at this index.
|
|
87
|
+
*/
|
|
88
|
+
readonly embeddings: Array<number>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Creates an EmbeddingModel service with batching and caching capabilities.
|
|
92
|
+
*
|
|
93
|
+
* This is the primary constructor for creating embedding services. It supports
|
|
94
|
+
* automatic batching of requests for efficiency and optional caching to reduce
|
|
95
|
+
* redundant API calls.
|
|
96
|
+
*
|
|
97
|
+
* @since 1.0.0
|
|
98
|
+
* @category Constructors
|
|
99
|
+
*/
|
|
100
|
+
export declare const make: (options: {
|
|
101
|
+
/**
|
|
102
|
+
* A method which processes a batch of text inputs and returns embedding
|
|
103
|
+
* results.
|
|
104
|
+
*/
|
|
105
|
+
readonly embedMany: (input: ReadonlyArray<string>) => Effect.Effect<Array<Result>, AiError.AiError>;
|
|
106
|
+
/**
|
|
107
|
+
* Optional maximum number of text inputs to process in one batch.
|
|
108
|
+
*/
|
|
109
|
+
readonly maxBatchSize?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Optional configuration to control how batch request results are cached.
|
|
112
|
+
*/
|
|
113
|
+
readonly cache?: {
|
|
114
|
+
/**
|
|
115
|
+
* The capacity of the cache.
|
|
116
|
+
*/
|
|
117
|
+
readonly capacity: number;
|
|
118
|
+
/**
|
|
119
|
+
* The time-to-live for items in the cache.
|
|
120
|
+
*/
|
|
121
|
+
readonly timeToLive: Duration.DurationInput;
|
|
122
|
+
};
|
|
123
|
+
}) => Effect.Effect<Service, never, never>;
|
|
124
|
+
/**
|
|
125
|
+
* Creates an EmbeddingModel service with time-window based batching.
|
|
126
|
+
*
|
|
127
|
+
* This constructor creates a service that uses a data loader pattern to batch
|
|
128
|
+
* embedding requests within a specified time window. This is optimal for
|
|
129
|
+
* high-throughput scenarios where you want to automatically batch requests that
|
|
130
|
+
* arrive within a short time period.
|
|
131
|
+
*
|
|
132
|
+
* @since 1.0.0
|
|
133
|
+
* @category Constructors
|
|
134
|
+
*/
|
|
135
|
+
export declare const makeDataLoader: (options: {
|
|
136
|
+
/**
|
|
137
|
+
* A method which processes a batch of text inputs and returns embedding
|
|
138
|
+
* results.
|
|
139
|
+
*/
|
|
140
|
+
readonly embedMany: (input: ReadonlyArray<string>) => Effect.Effect<Array<Result>, AiError.AiError>;
|
|
141
|
+
/**
|
|
142
|
+
* The duration between batch requests during which requests are collected and
|
|
143
|
+
* added to the current batch.
|
|
144
|
+
*/
|
|
145
|
+
readonly window: Duration.DurationInput;
|
|
146
|
+
/**
|
|
147
|
+
* Optional maximum number of requests to add to the batch before a batch
|
|
148
|
+
* request must be sent.
|
|
149
|
+
*/
|
|
150
|
+
readonly maxBatchSize?: number;
|
|
151
|
+
}) => Effect.Effect<Service, never, import("effect/Scope").Scope>;
|
|
152
|
+
export {};
|
|
153
|
+
//# sourceMappingURL=EmbeddingModel.d.ts.map
|
|
@@ -0,0 +1 @@
|
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