@evalution/vercel-ai-sdk 1.0.3
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/dist/index.d.ts +176 -0
- package/dist/index.js +243 -0
- package/package.json +183 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexander Corrado
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { AttributeValue, Attributes, Tracer } from "@opentelemetry/api";
|
|
2
|
+
import { generateText, streamText } from "ai";
|
|
3
|
+
import { AmazonBedrockProvider } from "@ai-sdk/amazon-bedrock";
|
|
4
|
+
import { AnthropicProvider } from "@ai-sdk/anthropic";
|
|
5
|
+
import { AssemblyAIProvider } from "@ai-sdk/assemblyai";
|
|
6
|
+
import { AzureOpenAIProvider } from "@ai-sdk/azure";
|
|
7
|
+
import { CerebrasProvider } from "@ai-sdk/cerebras";
|
|
8
|
+
import { CohereProvider } from "@ai-sdk/cohere";
|
|
9
|
+
import { DeepgramProvider } from "@ai-sdk/deepgram";
|
|
10
|
+
import { DeepInfraProvider } from "@ai-sdk/deepinfra";
|
|
11
|
+
import { DeepSeekProvider } from "@ai-sdk/deepseek";
|
|
12
|
+
import { ElevenLabsProvider } from "@ai-sdk/elevenlabs";
|
|
13
|
+
import { FalProvider } from "@ai-sdk/fal";
|
|
14
|
+
import { FireworksProvider } from "@ai-sdk/fireworks";
|
|
15
|
+
import { GatewayProvider } from "@ai-sdk/gateway";
|
|
16
|
+
import { GladiaProvider } from "@ai-sdk/gladia";
|
|
17
|
+
import { GoogleGenerativeAIProvider } from "@ai-sdk/google";
|
|
18
|
+
import { GoogleVertexProvider } from "@ai-sdk/google-vertex";
|
|
19
|
+
import { GroqProvider } from "@ai-sdk/groq";
|
|
20
|
+
import { HumeProvider } from "@ai-sdk/hume";
|
|
21
|
+
import { LMNTProvider } from "@ai-sdk/lmnt";
|
|
22
|
+
import { LumaProvider } from "@ai-sdk/luma";
|
|
23
|
+
import { MistralProvider } from "@ai-sdk/mistral";
|
|
24
|
+
import { OpenAIProvider } from "@ai-sdk/openai";
|
|
25
|
+
import { PerplexityProvider } from "@ai-sdk/perplexity";
|
|
26
|
+
import { ReplicateProvider } from "@ai-sdk/replicate";
|
|
27
|
+
import { RevaiProvider } from "@ai-sdk/revai";
|
|
28
|
+
import { TogetherAIProvider } from "@ai-sdk/togetherai";
|
|
29
|
+
import { VercelProvider } from "@ai-sdk/vercel";
|
|
30
|
+
import { XaiProvider } from "@ai-sdk/xai";
|
|
31
|
+
|
|
32
|
+
//#region ../../src/trace/prompt-tracer.d.ts
|
|
33
|
+
interface PromptsHelperOptions {
|
|
34
|
+
/**
|
|
35
|
+
* Globally-unique identifier for this group of prompts.
|
|
36
|
+
*/
|
|
37
|
+
readonly id: string;
|
|
38
|
+
}
|
|
39
|
+
interface PromptSpanInfo {
|
|
40
|
+
name: string;
|
|
41
|
+
id?: string;
|
|
42
|
+
functionParameters?: any[];
|
|
43
|
+
}
|
|
44
|
+
declare function getPromptSpanAttributes(prompt: PromptSpanInfo, attributes?: Attributes): Record<string, AttributeValue>;
|
|
45
|
+
/**
|
|
46
|
+
* Wraps a {@link Tracer} so that every span it produces is tagged with the
|
|
47
|
+
* attributes that associate it with a prompt — the prompt's name
|
|
48
|
+
* ({@link PROMPT_NAME_ATTRIBUTE}), an optional global prompt ID
|
|
49
|
+
* ({@link PROMPT_ID_ATTRIBUTE}), and an `'LLM'` span kind
|
|
50
|
+
* ({@link SPAN_KIND_ATTRIBUTE}). Attributes set explicitly on an individual
|
|
51
|
+
* span take precedence over these defaults.
|
|
52
|
+
*
|
|
53
|
+
* This depends only on `@opentelemetry/api`, so it can be re-used by SDK
|
|
54
|
+
* adapter packages (e.g. `@evalution/vercel-ai-sdk`) without pulling in the
|
|
55
|
+
* rest of evalution.
|
|
56
|
+
*
|
|
57
|
+
* @param prompt - The prompt to attribute spans to. `name` is a human-readable
|
|
58
|
+
* name; the optional `id` is a globally-unique prompt ID used to resolve
|
|
59
|
+
* runtime traces back to the prompt.
|
|
60
|
+
* @param tracer - Tracer to wrap. Defaults to a tracer from the globally
|
|
61
|
+
* registered tracer provider.
|
|
62
|
+
* @returns A tracer that forwards to `tracer` while attaching the prompt
|
|
63
|
+
* attributes to each span it creates.
|
|
64
|
+
*/
|
|
65
|
+
declare function createTracerForPrompt(prompt: PromptSpanInfo, tracer?: Tracer): Tracer;
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/index.d.ts
|
|
68
|
+
/**
|
|
69
|
+
* Provider instances that can be injected into a {@link prompts} factory.
|
|
70
|
+
* Each field matches the name of the singleton exported by the corresponding
|
|
71
|
+
* `@ai-sdk/*` package (e.g. `openai` from `@ai-sdk/openai`).
|
|
72
|
+
*
|
|
73
|
+
* Only fields that are read are imported, so destructure only the ones you need.
|
|
74
|
+
*/
|
|
75
|
+
interface Providers {
|
|
76
|
+
/** `openai` from `@ai-sdk/openai` */
|
|
77
|
+
openai: OpenAIProvider;
|
|
78
|
+
/** `anthropic` from `@ai-sdk/anthropic` */
|
|
79
|
+
anthropic: AnthropicProvider;
|
|
80
|
+
/** `google` from `@ai-sdk/google` */
|
|
81
|
+
google: GoogleGenerativeAIProvider;
|
|
82
|
+
/** `vertex` from `@ai-sdk/google-vertex` */
|
|
83
|
+
vertex: GoogleVertexProvider;
|
|
84
|
+
/** `azure` from `@ai-sdk/azure` */
|
|
85
|
+
azure: AzureOpenAIProvider;
|
|
86
|
+
/** `bedrock` from `@ai-sdk/amazon-bedrock` */
|
|
87
|
+
bedrock: AmazonBedrockProvider;
|
|
88
|
+
/** `cohere` from `@ai-sdk/cohere` */
|
|
89
|
+
cohere: CohereProvider;
|
|
90
|
+
/** `mistral` from `@ai-sdk/mistral` */
|
|
91
|
+
mistral: MistralProvider;
|
|
92
|
+
/** `groq` from `@ai-sdk/groq` */
|
|
93
|
+
groq: GroqProvider;
|
|
94
|
+
/** `cerebras` from `@ai-sdk/cerebras` */
|
|
95
|
+
cerebras: CerebrasProvider;
|
|
96
|
+
/** `deepinfra` from `@ai-sdk/deepinfra` */
|
|
97
|
+
deepinfra: DeepInfraProvider;
|
|
98
|
+
/** `deepseek` from `@ai-sdk/deepseek` */
|
|
99
|
+
deepseek: DeepSeekProvider;
|
|
100
|
+
/** `fireworks` from `@ai-sdk/fireworks` */
|
|
101
|
+
fireworks: FireworksProvider;
|
|
102
|
+
/** `perplexity` from `@ai-sdk/perplexity` */
|
|
103
|
+
perplexity: PerplexityProvider;
|
|
104
|
+
/** `replicate` from `@ai-sdk/replicate` */
|
|
105
|
+
replicate: ReplicateProvider;
|
|
106
|
+
/** `togetherai` from `@ai-sdk/togetherai` */
|
|
107
|
+
togetherai: TogetherAIProvider;
|
|
108
|
+
/** `xai` from `@ai-sdk/xai` */
|
|
109
|
+
xai: XaiProvider;
|
|
110
|
+
/** `vercel` from `@ai-sdk/vercel` */
|
|
111
|
+
vercel: VercelProvider;
|
|
112
|
+
/** `gateway` from `@ai-sdk/gateway` */
|
|
113
|
+
gateway: GatewayProvider;
|
|
114
|
+
/** `elevenlabs` from `@ai-sdk/elevenlabs` */
|
|
115
|
+
elevenlabs: ElevenLabsProvider;
|
|
116
|
+
/** `assemblyai` from `@ai-sdk/assemblyai` */
|
|
117
|
+
assemblyai: AssemblyAIProvider;
|
|
118
|
+
/** `deepgram` from `@ai-sdk/deepgram` */
|
|
119
|
+
deepgram: DeepgramProvider;
|
|
120
|
+
/** `gladia` from `@ai-sdk/gladia` */
|
|
121
|
+
gladia: GladiaProvider;
|
|
122
|
+
/** `revai` from `@ai-sdk/revai` */
|
|
123
|
+
revai: RevaiProvider;
|
|
124
|
+
/** `luma` from `@ai-sdk/luma` */
|
|
125
|
+
luma: LumaProvider;
|
|
126
|
+
/** `fal` from `@ai-sdk/fal` */
|
|
127
|
+
fal: FalProvider;
|
|
128
|
+
/** `hume` from `@ai-sdk/hume` */
|
|
129
|
+
hume: HumeProvider;
|
|
130
|
+
/** `lmnt` from `@ai-sdk/lmnt` */
|
|
131
|
+
lmnt: LMNTProvider;
|
|
132
|
+
}
|
|
133
|
+
type GenerateTextConfig = Parameters<typeof generateText>[0];
|
|
134
|
+
type StreamTextConfig = Parameters<typeof streamText>[0];
|
|
135
|
+
type Prompt = GenerateTextConfig | StreamTextConfig;
|
|
136
|
+
/**
|
|
137
|
+
* Helper for defining Evalution prompt modules using the Vercel AI SDK.
|
|
138
|
+
*
|
|
139
|
+
* The first argument is a {@link PromptsHelperOptions} containing an `id` that,
|
|
140
|
+
* combined with each prompt's name, forms a globally-unique prompt ID. This ID is
|
|
141
|
+
* attached to every span the prompt produces so runtime traces can be resolved
|
|
142
|
+
* back to the prompt. Choose a stable, unique value for this `id` and do not change it.
|
|
143
|
+
*
|
|
144
|
+
* The second argument is a factory function that receives a {@link Providers} object
|
|
145
|
+
* and returns a record of prompt-building functions. Each key in the returned record
|
|
146
|
+
* is the prompt name, and each value is a function that returns a Vercel AI SDK config object
|
|
147
|
+
* (`generateText` / `streamText` parameters) with `experimental_telemetry`
|
|
148
|
+
* automatically populated. The `Providers` object lazily imports provider singletons
|
|
149
|
+
* (e.g. `openai` from `@ai-sdk/openai`) on first access, so only the providers you
|
|
150
|
+
* destructure need to be installed. You can override individual providers by passing
|
|
151
|
+
* a `Partial<{@link Providers}>` to the function returned by `prompts`.
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* import { prompts } from "@evalution/vercel-ai-sdk";
|
|
156
|
+
*
|
|
157
|
+
* export default prompts(
|
|
158
|
+
* { id: "greeting" }, // <- global, unique ID for this group of prompts
|
|
159
|
+
*
|
|
160
|
+
* // destructure provider functions you need here instead of importing them
|
|
161
|
+
* ({ openai }) => ({
|
|
162
|
+
*
|
|
163
|
+
* // "simple" is the prompt name; result is passed to `generateText`
|
|
164
|
+
* simple: () => ({
|
|
165
|
+
* model: openai('gpt-4o'),
|
|
166
|
+
* system: 'You are a helpful assistant',
|
|
167
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
168
|
+
* }),
|
|
169
|
+
* }));
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
declare const prompts: <Prompts extends Record<string, (...args: any[]) => Prompt>>({
|
|
173
|
+
id
|
|
174
|
+
}: PromptsHelperOptions, factory: (providers: Providers) => Prompts) => (provided?: Partial<Providers>) => Prompts;
|
|
175
|
+
//#endregion
|
|
176
|
+
export { Providers, createTracerForPrompt, getPromptSpanAttributes, prompts };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { trace } from "@opentelemetry/api";
|
|
3
|
+
//#region ../../src/trace/prompt-tracer.ts
|
|
4
|
+
/**
|
|
5
|
+
* Attribute name a span can set to pick one of evalution's span-kind values
|
|
6
|
+
* (`'LLM'`, `'TOOL'`, `'AGENT'`, `'EMBEDDING'`, `'DEFAULT'`). Falls back to
|
|
7
|
+
* `'DEFAULT'` when absent or unrecognised.
|
|
8
|
+
*/
|
|
9
|
+
const SPAN_KIND_ATTRIBUTE = "evalution.span.type";
|
|
10
|
+
/**
|
|
11
|
+
* Attribute name a span can set to give a human-readable name to the prompt.
|
|
12
|
+
*/
|
|
13
|
+
const PROMPT_NAME_ATTRIBUTE = "gen_ai.prompt.name";
|
|
14
|
+
/**
|
|
15
|
+
* Attribute name a span can set to link itself to a specific prompt. The value
|
|
16
|
+
* is a globally-unique prompt ID unless {@link PROMPT_PROVIDER_ID_ATTRIBUTE} is
|
|
17
|
+
* also set, in which case it is scoped to that provider.
|
|
18
|
+
*/
|
|
19
|
+
const PROMPT_ID_ATTRIBUTE = "evalution.prompt.id";
|
|
20
|
+
/**
|
|
21
|
+
* Attribute name a span can set to include an array of input parameters used
|
|
22
|
+
* to construct the final prompt.
|
|
23
|
+
*/
|
|
24
|
+
const PROMPT_INPUTS_ATTRIBUTE = "evalution.prompt.inputs";
|
|
25
|
+
function getPromptSpanAttributes(prompt, attributes = {}) {
|
|
26
|
+
return Object.fromEntries([
|
|
27
|
+
[SPAN_KIND_ATTRIBUTE, "LLM"],
|
|
28
|
+
[PROMPT_NAME_ATTRIBUTE, prompt.name],
|
|
29
|
+
[PROMPT_ID_ATTRIBUTE, prompt.id],
|
|
30
|
+
[PROMPT_INPUTS_ATTRIBUTE, prompt.functionParameters],
|
|
31
|
+
...Object.entries(attributes)
|
|
32
|
+
].filter(([, v]) => v !== void 0 && v !== null));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Wraps a {@link Tracer} so that every span it produces is tagged with the
|
|
36
|
+
* attributes that associate it with a prompt — the prompt's name
|
|
37
|
+
* ({@link PROMPT_NAME_ATTRIBUTE}), an optional global prompt ID
|
|
38
|
+
* ({@link PROMPT_ID_ATTRIBUTE}), and an `'LLM'` span kind
|
|
39
|
+
* ({@link SPAN_KIND_ATTRIBUTE}). Attributes set explicitly on an individual
|
|
40
|
+
* span take precedence over these defaults.
|
|
41
|
+
*
|
|
42
|
+
* This depends only on `@opentelemetry/api`, so it can be re-used by SDK
|
|
43
|
+
* adapter packages (e.g. `@evalution/vercel-ai-sdk`) without pulling in the
|
|
44
|
+
* rest of evalution.
|
|
45
|
+
*
|
|
46
|
+
* @param prompt - The prompt to attribute spans to. `name` is a human-readable
|
|
47
|
+
* name; the optional `id` is a globally-unique prompt ID used to resolve
|
|
48
|
+
* runtime traces back to the prompt.
|
|
49
|
+
* @param tracer - Tracer to wrap. Defaults to a tracer from the globally
|
|
50
|
+
* registered tracer provider.
|
|
51
|
+
* @returns A tracer that forwards to `tracer` while attaching the prompt
|
|
52
|
+
* attributes to each span it creates.
|
|
53
|
+
*/
|
|
54
|
+
function createTracerForPrompt(prompt, tracer) {
|
|
55
|
+
const inner = tracer ?? trace.getTracer("evalution");
|
|
56
|
+
const withPromptAttributes = (options) => ({
|
|
57
|
+
...options,
|
|
58
|
+
attributes: getPromptSpanAttributes(prompt, options?.attributes)
|
|
59
|
+
});
|
|
60
|
+
return {
|
|
61
|
+
startSpan(name, options, context) {
|
|
62
|
+
return inner.startSpan(name, withPromptAttributes(options), context);
|
|
63
|
+
},
|
|
64
|
+
startActiveSpan(name, ...rest) {
|
|
65
|
+
if (typeof rest[0] === "function") return inner.startActiveSpan(name, withPromptAttributes(), rest[0]);
|
|
66
|
+
if (typeof rest[1] === "function") return inner.startActiveSpan(name, withPromptAttributes(rest[0]), rest[1]);
|
|
67
|
+
return inner.startActiveSpan(name, withPromptAttributes(rest[0]), rest[1], rest[2]);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/index.ts
|
|
73
|
+
/**
|
|
74
|
+
* Helpers for integrating Vercel AI SDK prompts with Evalution.
|
|
75
|
+
* @module @evalution/vercel-ai-sdk
|
|
76
|
+
*/
|
|
77
|
+
const require = createRequire(import.meta.url);
|
|
78
|
+
var LazilyImportedProviders = class {
|
|
79
|
+
values;
|
|
80
|
+
constructor(values = {}) {
|
|
81
|
+
this.values = values;
|
|
82
|
+
}
|
|
83
|
+
importProvider(key) {
|
|
84
|
+
return this.values[key] ?? require(`@ai-sdk/${key}`)?.[key] ?? (() => {
|
|
85
|
+
throw new Error(`Unable to import "${key}" from "@ai-sdk/${key}"`);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
get openai() {
|
|
89
|
+
return this.importProvider("openai");
|
|
90
|
+
}
|
|
91
|
+
get anthropic() {
|
|
92
|
+
return this.importProvider("anthropic");
|
|
93
|
+
}
|
|
94
|
+
get google() {
|
|
95
|
+
return this.importProvider("google");
|
|
96
|
+
}
|
|
97
|
+
get vertex() {
|
|
98
|
+
return this.importProvider("vertex");
|
|
99
|
+
}
|
|
100
|
+
get azure() {
|
|
101
|
+
return this.importProvider("azure");
|
|
102
|
+
}
|
|
103
|
+
get bedrock() {
|
|
104
|
+
return this.importProvider("bedrock");
|
|
105
|
+
}
|
|
106
|
+
get cohere() {
|
|
107
|
+
return this.importProvider("cohere");
|
|
108
|
+
}
|
|
109
|
+
get mistral() {
|
|
110
|
+
return this.importProvider("mistral");
|
|
111
|
+
}
|
|
112
|
+
get groq() {
|
|
113
|
+
return this.importProvider("groq");
|
|
114
|
+
}
|
|
115
|
+
get cerebras() {
|
|
116
|
+
return this.importProvider("cerebras");
|
|
117
|
+
}
|
|
118
|
+
get deepinfra() {
|
|
119
|
+
return this.importProvider("deepinfra");
|
|
120
|
+
}
|
|
121
|
+
get deepseek() {
|
|
122
|
+
return this.importProvider("deepseek");
|
|
123
|
+
}
|
|
124
|
+
get fireworks() {
|
|
125
|
+
return this.importProvider("fireworks");
|
|
126
|
+
}
|
|
127
|
+
get perplexity() {
|
|
128
|
+
return this.importProvider("perplexity");
|
|
129
|
+
}
|
|
130
|
+
get replicate() {
|
|
131
|
+
return this.importProvider("replicate");
|
|
132
|
+
}
|
|
133
|
+
get togetherai() {
|
|
134
|
+
return this.importProvider("togetherai");
|
|
135
|
+
}
|
|
136
|
+
get xai() {
|
|
137
|
+
return this.importProvider("xai");
|
|
138
|
+
}
|
|
139
|
+
get vercel() {
|
|
140
|
+
return this.importProvider("vercel");
|
|
141
|
+
}
|
|
142
|
+
get gateway() {
|
|
143
|
+
return this.importProvider("gateway");
|
|
144
|
+
}
|
|
145
|
+
get elevenlabs() {
|
|
146
|
+
return this.importProvider("elevenlabs");
|
|
147
|
+
}
|
|
148
|
+
get assemblyai() {
|
|
149
|
+
return this.importProvider("assemblyai");
|
|
150
|
+
}
|
|
151
|
+
get deepgram() {
|
|
152
|
+
return this.importProvider("deepgram");
|
|
153
|
+
}
|
|
154
|
+
get gladia() {
|
|
155
|
+
return this.importProvider("gladia");
|
|
156
|
+
}
|
|
157
|
+
get revai() {
|
|
158
|
+
return this.importProvider("revai");
|
|
159
|
+
}
|
|
160
|
+
get luma() {
|
|
161
|
+
return this.importProvider("luma");
|
|
162
|
+
}
|
|
163
|
+
get fal() {
|
|
164
|
+
return this.importProvider("fal");
|
|
165
|
+
}
|
|
166
|
+
get hume() {
|
|
167
|
+
return this.importProvider("hume");
|
|
168
|
+
}
|
|
169
|
+
get lmnt() {
|
|
170
|
+
return this.importProvider("lmnt");
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* Helper for defining Evalution prompt modules using the Vercel AI SDK.
|
|
175
|
+
*
|
|
176
|
+
* The first argument is a {@link PromptsHelperOptions} containing an `id` that,
|
|
177
|
+
* combined with each prompt's name, forms a globally-unique prompt ID. This ID is
|
|
178
|
+
* attached to every span the prompt produces so runtime traces can be resolved
|
|
179
|
+
* back to the prompt. Choose a stable, unique value for this `id` and do not change it.
|
|
180
|
+
*
|
|
181
|
+
* The second argument is a factory function that receives a {@link Providers} object
|
|
182
|
+
* and returns a record of prompt-building functions. Each key in the returned record
|
|
183
|
+
* is the prompt name, and each value is a function that returns a Vercel AI SDK config object
|
|
184
|
+
* (`generateText` / `streamText` parameters) with `experimental_telemetry`
|
|
185
|
+
* automatically populated. The `Providers` object lazily imports provider singletons
|
|
186
|
+
* (e.g. `openai` from `@ai-sdk/openai`) on first access, so only the providers you
|
|
187
|
+
* destructure need to be installed. You can override individual providers by passing
|
|
188
|
+
* a `Partial<{@link Providers}>` to the function returned by `prompts`.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* import { prompts } from "@evalution/vercel-ai-sdk";
|
|
193
|
+
*
|
|
194
|
+
* export default prompts(
|
|
195
|
+
* { id: "greeting" }, // <- global, unique ID for this group of prompts
|
|
196
|
+
*
|
|
197
|
+
* // destructure provider functions you need here instead of importing them
|
|
198
|
+
* ({ openai }) => ({
|
|
199
|
+
*
|
|
200
|
+
* // "simple" is the prompt name; result is passed to `generateText`
|
|
201
|
+
* simple: () => ({
|
|
202
|
+
* model: openai('gpt-4o'),
|
|
203
|
+
* system: 'You are a helpful assistant',
|
|
204
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
205
|
+
* }),
|
|
206
|
+
* }));
|
|
207
|
+
* ```
|
|
208
|
+
*/
|
|
209
|
+
const prompts = (({ id }, factory) => (provided) => {
|
|
210
|
+
const definitions = factory(new LazilyImportedProviders(provided));
|
|
211
|
+
const wrapped = {};
|
|
212
|
+
for (const name of Object.keys(definitions)) {
|
|
213
|
+
const define = definitions[name];
|
|
214
|
+
wrapped[name] = (...args) => {
|
|
215
|
+
const config = define(...args);
|
|
216
|
+
if (!isPlainConfig(config)) return config;
|
|
217
|
+
const existing = config.experimental_telemetry;
|
|
218
|
+
return {
|
|
219
|
+
...config,
|
|
220
|
+
experimental_telemetry: {
|
|
221
|
+
isEnabled: true,
|
|
222
|
+
recordInputs: true,
|
|
223
|
+
recordOutputs: true,
|
|
224
|
+
...existing,
|
|
225
|
+
metadata: getPromptSpanAttributes({
|
|
226
|
+
name,
|
|
227
|
+
id: `${id}#${name}`,
|
|
228
|
+
functionParameters: args
|
|
229
|
+
}, existing?.metadata)
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
return wrapped;
|
|
235
|
+
});
|
|
236
|
+
/** True for plain config objects (i.e. not an `Agent` instance or nullish). */
|
|
237
|
+
function isPlainConfig(value) {
|
|
238
|
+
if (value === null || typeof value !== "object") return false;
|
|
239
|
+
const proto = Object.getPrototypeOf(value);
|
|
240
|
+
return proto === Object.prototype || proto === null;
|
|
241
|
+
}
|
|
242
|
+
//#endregion
|
|
243
|
+
export { createTracerForPrompt, getPromptSpanAttributes, prompts };
|
package/package.json
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@evalution/vercel-ai-sdk",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "Helpers for integrating Vercel AI SDK prompts with Evalution",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsdown",
|
|
17
|
+
"prepare": "npm run build",
|
|
18
|
+
"typecheck": "tsc --noEmit"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@opentelemetry/api": "^1.9.0",
|
|
22
|
+
"ai": ">=3.4.0",
|
|
23
|
+
"@ai-sdk/amazon-bedrock": "*",
|
|
24
|
+
"@ai-sdk/anthropic": "*",
|
|
25
|
+
"@ai-sdk/assemblyai": "*",
|
|
26
|
+
"@ai-sdk/azure": "*",
|
|
27
|
+
"@ai-sdk/cerebras": "*",
|
|
28
|
+
"@ai-sdk/cohere": "*",
|
|
29
|
+
"@ai-sdk/deepgram": "*",
|
|
30
|
+
"@ai-sdk/deepinfra": "*",
|
|
31
|
+
"@ai-sdk/deepseek": "*",
|
|
32
|
+
"@ai-sdk/elevenlabs": "*",
|
|
33
|
+
"@ai-sdk/fal": "*",
|
|
34
|
+
"@ai-sdk/fireworks": "*",
|
|
35
|
+
"@ai-sdk/gateway": "*",
|
|
36
|
+
"@ai-sdk/gladia": "*",
|
|
37
|
+
"@ai-sdk/google": "*",
|
|
38
|
+
"@ai-sdk/google-vertex": "*",
|
|
39
|
+
"@ai-sdk/groq": "*",
|
|
40
|
+
"@ai-sdk/hume": "*",
|
|
41
|
+
"@ai-sdk/lmnt": "*",
|
|
42
|
+
"@ai-sdk/luma": "*",
|
|
43
|
+
"@ai-sdk/mistral": "*",
|
|
44
|
+
"@ai-sdk/openai": "*",
|
|
45
|
+
"@ai-sdk/perplexity": "*",
|
|
46
|
+
"@ai-sdk/replicate": "*",
|
|
47
|
+
"@ai-sdk/revai": "*",
|
|
48
|
+
"@ai-sdk/togetherai": "*",
|
|
49
|
+
"@ai-sdk/vercel": "*",
|
|
50
|
+
"@ai-sdk/xai": "*"
|
|
51
|
+
},
|
|
52
|
+
"peerDependenciesMeta": {
|
|
53
|
+
"@ai-sdk/amazon-bedrock": {
|
|
54
|
+
"optional": true
|
|
55
|
+
},
|
|
56
|
+
"@ai-sdk/anthropic": {
|
|
57
|
+
"optional": true
|
|
58
|
+
},
|
|
59
|
+
"@ai-sdk/assemblyai": {
|
|
60
|
+
"optional": true
|
|
61
|
+
},
|
|
62
|
+
"@ai-sdk/azure": {
|
|
63
|
+
"optional": true
|
|
64
|
+
},
|
|
65
|
+
"@ai-sdk/cerebras": {
|
|
66
|
+
"optional": true
|
|
67
|
+
},
|
|
68
|
+
"@ai-sdk/cohere": {
|
|
69
|
+
"optional": true
|
|
70
|
+
},
|
|
71
|
+
"@ai-sdk/deepgram": {
|
|
72
|
+
"optional": true
|
|
73
|
+
},
|
|
74
|
+
"@ai-sdk/deepinfra": {
|
|
75
|
+
"optional": true
|
|
76
|
+
},
|
|
77
|
+
"@ai-sdk/deepseek": {
|
|
78
|
+
"optional": true
|
|
79
|
+
},
|
|
80
|
+
"@ai-sdk/elevenlabs": {
|
|
81
|
+
"optional": true
|
|
82
|
+
},
|
|
83
|
+
"@ai-sdk/fal": {
|
|
84
|
+
"optional": true
|
|
85
|
+
},
|
|
86
|
+
"@ai-sdk/fireworks": {
|
|
87
|
+
"optional": true
|
|
88
|
+
},
|
|
89
|
+
"@ai-sdk/gateway": {
|
|
90
|
+
"optional": true
|
|
91
|
+
},
|
|
92
|
+
"@ai-sdk/gladia": {
|
|
93
|
+
"optional": true
|
|
94
|
+
},
|
|
95
|
+
"@ai-sdk/google": {
|
|
96
|
+
"optional": true
|
|
97
|
+
},
|
|
98
|
+
"@ai-sdk/google-vertex": {
|
|
99
|
+
"optional": true
|
|
100
|
+
},
|
|
101
|
+
"@ai-sdk/groq": {
|
|
102
|
+
"optional": true
|
|
103
|
+
},
|
|
104
|
+
"@ai-sdk/hume": {
|
|
105
|
+
"optional": true
|
|
106
|
+
},
|
|
107
|
+
"@ai-sdk/lmnt": {
|
|
108
|
+
"optional": true
|
|
109
|
+
},
|
|
110
|
+
"@ai-sdk/luma": {
|
|
111
|
+
"optional": true
|
|
112
|
+
},
|
|
113
|
+
"@ai-sdk/mistral": {
|
|
114
|
+
"optional": true
|
|
115
|
+
},
|
|
116
|
+
"@ai-sdk/openai": {
|
|
117
|
+
"optional": true
|
|
118
|
+
},
|
|
119
|
+
"@ai-sdk/perplexity": {
|
|
120
|
+
"optional": true
|
|
121
|
+
},
|
|
122
|
+
"@ai-sdk/replicate": {
|
|
123
|
+
"optional": true
|
|
124
|
+
},
|
|
125
|
+
"@ai-sdk/revai": {
|
|
126
|
+
"optional": true
|
|
127
|
+
},
|
|
128
|
+
"@ai-sdk/togetherai": {
|
|
129
|
+
"optional": true
|
|
130
|
+
},
|
|
131
|
+
"@ai-sdk/vercel": {
|
|
132
|
+
"optional": true
|
|
133
|
+
},
|
|
134
|
+
"@ai-sdk/xai": {
|
|
135
|
+
"optional": true
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
"devDependencies": {
|
|
139
|
+
"@opentelemetry/api": "^1.9.0",
|
|
140
|
+
"ai": "^6.0.185",
|
|
141
|
+
"@ai-sdk/amazon-bedrock": "^4.0.108",
|
|
142
|
+
"@ai-sdk/anthropic": "^3.0.79",
|
|
143
|
+
"@ai-sdk/assemblyai": "^2.0.33",
|
|
144
|
+
"@ai-sdk/azure": "^3.0.66",
|
|
145
|
+
"@ai-sdk/cerebras": "^2.0.54",
|
|
146
|
+
"@ai-sdk/cohere": "^3.0.36",
|
|
147
|
+
"@ai-sdk/deepgram": "^2.0.33",
|
|
148
|
+
"@ai-sdk/deepinfra": "^2.0.52",
|
|
149
|
+
"@ai-sdk/deepseek": "^2.0.35",
|
|
150
|
+
"@ai-sdk/elevenlabs": "^2.0.33",
|
|
151
|
+
"@ai-sdk/fal": "^2.0.34",
|
|
152
|
+
"@ai-sdk/fireworks": "^2.0.53",
|
|
153
|
+
"@ai-sdk/gateway": "^3.0.120",
|
|
154
|
+
"@ai-sdk/gladia": "^2.0.33",
|
|
155
|
+
"@ai-sdk/google": "^3.0.79",
|
|
156
|
+
"@ai-sdk/google-vertex": "^4.0.137",
|
|
157
|
+
"@ai-sdk/groq": "^3.0.39",
|
|
158
|
+
"@ai-sdk/hume": "^2.0.33",
|
|
159
|
+
"@ai-sdk/langchain": "^2.0.197",
|
|
160
|
+
"@ai-sdk/llamaindex": "^2.0.191",
|
|
161
|
+
"@ai-sdk/lmnt": "^2.0.33",
|
|
162
|
+
"@ai-sdk/luma": "^2.0.33",
|
|
163
|
+
"@ai-sdk/mistral": "^3.0.37",
|
|
164
|
+
"@ai-sdk/openai": "^3.0.65",
|
|
165
|
+
"@ai-sdk/perplexity": "^3.0.33",
|
|
166
|
+
"@ai-sdk/replicate": "^2.0.33",
|
|
167
|
+
"@ai-sdk/revai": "^2.0.33",
|
|
168
|
+
"@ai-sdk/togetherai": "^2.0.53",
|
|
169
|
+
"@ai-sdk/valibot": "^2.0.28",
|
|
170
|
+
"@ai-sdk/vercel": "^2.0.50",
|
|
171
|
+
"@ai-sdk/xai": "^3.0.92",
|
|
172
|
+
"@types/json-schema": "^7.0.15",
|
|
173
|
+
"tsdown": "^0.22.1",
|
|
174
|
+
"typescript": "^5.3.3"
|
|
175
|
+
},
|
|
176
|
+
"keywords": [
|
|
177
|
+
"evalution",
|
|
178
|
+
"vercel-ai-sdk",
|
|
179
|
+
"ai",
|
|
180
|
+
"prompt"
|
|
181
|
+
],
|
|
182
|
+
"license": "MIT"
|
|
183
|
+
}
|