@appweaver/ai 0.0.0-stage → 2.9.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/LICENSE +21 -0
- package/README.md +62 -2
- package/config.d.ts +49 -0
- package/config.js +58 -0
- package/driver/ai-driver.d.ts +42 -0
- package/driver/ai-driver.js +31 -0
- package/driver/mock-driver.d.ts +27 -0
- package/driver/mock-driver.js +147 -0
- package/driver/model-registry.d.ts +53 -0
- package/driver/model-registry.js +244 -0
- package/driver/presets.d.ts +23 -0
- package/driver/presets.js +126 -0
- package/index.d.ts +14 -0
- package/index.js +30 -0
- package/module.d.ts +7 -0
- package/module.js +48 -0
- package/package.json +54 -3
- package/service/ai-object-stream.d.ts +35 -0
- package/service/ai-object-stream.js +58 -0
- package/service/ai-service.d.ts +118 -0
- package/service/ai-service.js +769 -0
- package/service/ai-stream.d.ts +48 -0
- package/service/ai-stream.js +133 -0
- package/service/prompt.d.ts +100 -0
- package/service/prompt.js +2 -0
- package/tools/create-tool.d.ts +53 -0
- package/tools/create-tool.js +57 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/types.d.ts +201 -0
- package/types.js +2 -0
- package/utils/async-queue.d.ts +13 -0
- package/utils/async-queue.js +59 -0
- package/utils/json-util.d.ts +27 -0
- package/utils/json-util.js +127 -0
- package/utils/stream-util.d.ts +53 -0
- package/utils/stream-util.js +90 -0
- package/utils/telemetry.d.ts +38 -0
- package/utils/telemetry.js +136 -0
- package/utils/vector-util.d.ts +24 -0
- package/utils/vector-util.js +43 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { TSchema } from '@sinclair/typebox';
|
|
2
|
+
import { HEALTH_CHECK, HealthCheckConfig, HealthCheckResult, IHealthCheck, LIFECYCLE, OnInit, QueueJobOptions } from '@appweaver/common';
|
|
3
|
+
import { AiCapability } from '../types';
|
|
4
|
+
import { AiObjectStream } from './ai-object-stream';
|
|
5
|
+
import { AiStream } from './ai-stream';
|
|
6
|
+
import { AiEmbedOptions, AiEmbedResult, AiObjectPrompt, AiObjectResult, AiPrompt, AiQueuedPrompt, AiResult } from './prompt';
|
|
7
|
+
/**
|
|
8
|
+
* Calls the models of the endpoints of the `ai` config: a whole response, a stream, an object matching a schema, a
|
|
9
|
+
* classification or embeddings, directly or through the `ai` queue, running the tools the model calls.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const ai = inject(AiService);
|
|
13
|
+
* const { text } = await ai.generate({ model: 'fast', prompt: 'Summarize: ...' });
|
|
14
|
+
*/
|
|
15
|
+
export declare class AiService implements OnInit, IHealthCheck {
|
|
16
|
+
static [LIFECYCLE]: boolean;
|
|
17
|
+
static [HEALTH_CHECK]: boolean;
|
|
18
|
+
constructor();
|
|
19
|
+
/**
|
|
20
|
+
* Checks that the default models and the aliases name configured endpoints.
|
|
21
|
+
*
|
|
22
|
+
* @throws {ConfigurationError} `CONFIGURATION_INVALID` listing the invalid models.
|
|
23
|
+
*/
|
|
24
|
+
onInit(): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Calls a model and returns its whole response, after running the tools it calls. The models of an alias are
|
|
27
|
+
* called in turn while one fails with a rate limit, a timeout or an unavailable provider.
|
|
28
|
+
*
|
|
29
|
+
* @param {AiPrompt} prompt - The model, the messages and the parameters.
|
|
30
|
+
* @return {Promise<AiResult>} The response, its steps and the conversation.
|
|
31
|
+
* @throws {AiError} The error of the request or of the driver.
|
|
32
|
+
*/
|
|
33
|
+
generate(prompt: AiPrompt): Promise<AiResult>;
|
|
34
|
+
/**
|
|
35
|
+
* Calls a model and streams its response, running the tools it calls between its steps. The models of an alias are
|
|
36
|
+
* called in turn while one fails before streaming anything.
|
|
37
|
+
*
|
|
38
|
+
* @param {AiPrompt} prompt - The model, the messages and the parameters.
|
|
39
|
+
* @return {AiStream} The text deltas, with the result once it ends.
|
|
40
|
+
*/
|
|
41
|
+
stream(prompt: AiPrompt): AiStream;
|
|
42
|
+
/**
|
|
43
|
+
* Calls a model for an object matching a schema, in the JSON schema mode of the endpoint when it has one. An object
|
|
44
|
+
* not matching the schema is sent back with its errors for the model to repair, up to `repairAttempts` times.
|
|
45
|
+
*
|
|
46
|
+
* @param {AiObjectPrompt} prompt - The model, the messages, the parameters and the schema.
|
|
47
|
+
* @return {Promise<AiObjectResult>} The object and the response.
|
|
48
|
+
* @throws {AiError} `AI_OUTPUT_INVALID` when the object still does not match the schema.
|
|
49
|
+
*/
|
|
50
|
+
generateObject<T extends TSchema>(prompt: AiObjectPrompt<T>): Promise<AiObjectResult<T>>;
|
|
51
|
+
/**
|
|
52
|
+
* Calls a model for an object matching a schema, streaming the object while it is generated. The object is checked
|
|
53
|
+
* once it ends, without a repair.
|
|
54
|
+
*
|
|
55
|
+
* @param {AiObjectPrompt} prompt - The model, the messages, the parameters and the schema.
|
|
56
|
+
* @return {AiObjectStream} The partial objects, with the object once it ends.
|
|
57
|
+
*/
|
|
58
|
+
streamObject<T extends TSchema>(prompt: AiObjectPrompt<T>): AiObjectStream<T>;
|
|
59
|
+
/**
|
|
60
|
+
* Classifies a text into one of the labels.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} input - The text to classify.
|
|
63
|
+
* @param {string[]} labels - The labels, at least two.
|
|
64
|
+
* @param {AiPrompt} [prompt] - The model, the instructions in `system`, and the parameters.
|
|
65
|
+
* @return {Promise<string>} The label.
|
|
66
|
+
* @throws {AiError} `AI_REQUEST_INVALID` for fewer than two labels.
|
|
67
|
+
*/
|
|
68
|
+
classify<L extends string>(input: string, labels: readonly L[], prompt?: Omit<AiPrompt, 'prompt' | 'responseFormat'>): Promise<L>;
|
|
69
|
+
/**
|
|
70
|
+
* Embeds texts into vectors, with `ai.defaultEmbeddingModel` when no model is given.
|
|
71
|
+
*
|
|
72
|
+
* @param {string | string[]} input - A text or the texts.
|
|
73
|
+
* @param {AiEmbedOptions} [options] - The model, the dimensions and the parameters.
|
|
74
|
+
* @return {Promise<AiEmbedResult>} A vector per text.
|
|
75
|
+
* @throws {AiError} `AI_CAPABILITY_UNSUPPORTED` for an endpoint without embeddings.
|
|
76
|
+
*/
|
|
77
|
+
embed(input: string | string[], options?: AiEmbedOptions): Promise<AiEmbedResult>;
|
|
78
|
+
/**
|
|
79
|
+
* Tells whether a model has a capability, i.e. to send images only to a model reading them.
|
|
80
|
+
*
|
|
81
|
+
* @param {string | undefined} model - A model reference or an alias, the default model when none is given.
|
|
82
|
+
* @param {AiCapability} capability - The capability.
|
|
83
|
+
* @return {Promise<boolean>} `true` when the first model of the alias has it.
|
|
84
|
+
*/
|
|
85
|
+
supports(model: string | undefined, capability: AiCapability): Promise<boolean>;
|
|
86
|
+
/**
|
|
87
|
+
* Adds a call to the `ai` queue, run by a worker of any instance with the retries of the queue.
|
|
88
|
+
*
|
|
89
|
+
* @param {AiQueuedPrompt} prompt - The prompt, a JSON value naming its tools.
|
|
90
|
+
* @param {QueueJobOptions} [options] - The options of the job.
|
|
91
|
+
* @return {Promise<string | undefined>} The ID of the job.
|
|
92
|
+
* @throws {ConfigurationError} `CONFIGURATION_INVALID` without a queue.
|
|
93
|
+
*/
|
|
94
|
+
enqueue(prompt: AiQueuedPrompt, options?: QueueJobOptions): Promise<string | undefined>;
|
|
95
|
+
/**
|
|
96
|
+
* Runs a call through the `ai` queue and waits for its result, on whichever instance its worker runs.
|
|
97
|
+
*
|
|
98
|
+
* @param {AiQueuedPrompt} prompt - The prompt, a JSON value naming its tools.
|
|
99
|
+
* @param {QueueJobOptions} [options] - The options of the job.
|
|
100
|
+
* @return {Promise<AiResult>} The result.
|
|
101
|
+
* @throws {AiError} The error of the last attempt.
|
|
102
|
+
*/
|
|
103
|
+
generateQueued(prompt: AiQueuedPrompt, options?: QueueJobOptions): Promise<AiResult>;
|
|
104
|
+
/**
|
|
105
|
+
* Runs a call through the `ai` queue and streams it from the worker, the parts relayed through the `PubSub` in
|
|
106
|
+
* batches of `ai.streamFlushInterval`. The job is attempted once, and aborting the stream aborts the worker's call.
|
|
107
|
+
*
|
|
108
|
+
* @param {AiQueuedPrompt} prompt - The prompt, a JSON value naming its tools.
|
|
109
|
+
* @param {QueueJobOptions} [options] - The options of the job, and the signal aborting the stream.
|
|
110
|
+
* @return {AiStream} The stream.
|
|
111
|
+
* @throws {ConfigurationError} `CONFIGURATION_INVALID` without a queue or a pub/sub.
|
|
112
|
+
*/
|
|
113
|
+
streamQueued(prompt: AiQueuedPrompt, options?: Omit<QueueJobOptions, 'attempts'> & {
|
|
114
|
+
signal?: AbortSignal;
|
|
115
|
+
}): AiStream;
|
|
116
|
+
checkHealth(): Promise<HealthCheckResult>;
|
|
117
|
+
checkHealthConfig(): HealthCheckConfig;
|
|
118
|
+
}
|