@spendgraph/prompt 0.2.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/README.md +240 -0
- package/dist/budget/budget.d.ts +31 -0
- package/dist/budget/budget.js +53 -0
- package/dist/budget/errors.d.ts +12 -0
- package/dist/budget/errors.js +18 -0
- package/dist/budget/index.d.ts +2 -0
- package/dist/budget/index.js +2 -0
- package/dist/build/build.d.ts +19 -0
- package/dist/build/build.js +73 -0
- package/dist/build/index.d.ts +2 -0
- package/dist/build/index.js +1 -0
- package/dist/cache/cache.d.ts +118 -0
- package/dist/cache/cache.js +198 -0
- package/dist/cache/index.d.ts +1 -0
- package/dist/cache/index.js +1 -0
- package/dist/client.d.ts +147 -0
- package/dist/client.js +230 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +5 -0
- package/dist/internals.d.ts +17 -0
- package/dist/internals.js +13 -0
- package/dist/prompt/bind.d.ts +60 -0
- package/dist/prompt/bind.js +21 -0
- package/dist/prompt/factory.d.ts +49 -0
- package/dist/prompt/factory.js +118 -0
- package/dist/prompt/index.d.ts +4 -0
- package/dist/prompt/index.js +2 -0
- package/dist/pull/index.d.ts +1 -0
- package/dist/pull/index.js +1 -0
- package/dist/pull/pull.d.ts +12 -0
- package/dist/pull/pull.js +31 -0
- package/dist/render/index.d.ts +4 -0
- package/dist/render/index.js +3 -0
- package/dist/render/messages.d.ts +12 -0
- package/dist/render/messages.js +24 -0
- package/dist/render/system.d.ts +8 -0
- package/dist/render/system.js +19 -0
- package/dist/render/types.d.ts +15 -0
- package/dist/render/types.js +1 -0
- package/dist/render/variables.d.ts +11 -0
- package/dist/render/variables.js +29 -0
- package/dist/run/across-models.d.ts +7 -0
- package/dist/run/across-models.js +6 -0
- package/dist/run/concurrency.d.ts +7 -0
- package/dist/run/concurrency.js +18 -0
- package/dist/run/id.d.ts +2 -0
- package/dist/run/id.js +4 -0
- package/dist/run/index.d.ts +5 -0
- package/dist/run/index.js +5 -0
- package/dist/run/once.d.ts +4 -0
- package/dist/run/once.js +22 -0
- package/dist/run/sample.d.ts +17 -0
- package/dist/run/sample.js +39 -0
- package/dist/types/dataset.d.ts +22 -0
- package/dist/types/dataset.js +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.js +1 -0
- package/dist/types/payload.d.ts +16 -0
- package/dist/types/payload.js +1 -0
- package/dist/types/prompt.d.ts +73 -0
- package/dist/types/prompt.js +1 -0
- package/dist/types/result.d.ts +13 -0
- package/dist/types/result.js +1 -0
- package/dist/types/rollout.d.ts +8 -0
- package/dist/types/rollout.js +1 -0
- package/dist/types/run.d.ts +18 -0
- package/dist/types/run.js +1 -0
- package/dist/types/trace.d.ts +71 -0
- package/dist/types/trace.js +1 -0
- package/dist/types/version.d.ts +21 -0
- package/dist/types/version.js +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FieldSpec } from "@spendgraph/sdk";
|
|
2
|
+
import type { Block } from "../render/index.js";
|
|
3
|
+
/** What `GET /api/v1/prompts/:id` returns, narrowed to what this package uses. */
|
|
4
|
+
export interface PromptPayload {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
/** The other handle `pull` accepts. Null on a row the backfill could not slug. */
|
|
8
|
+
slug?: string | null;
|
|
9
|
+
blocks: Block[];
|
|
10
|
+
question: string;
|
|
11
|
+
models: string[];
|
|
12
|
+
temperature: number;
|
|
13
|
+
maxTokens: number;
|
|
14
|
+
fieldSpec: FieldSpec[];
|
|
15
|
+
currentVersionId: string | null;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { FieldSpec } from "@spendgraph/sdk";
|
|
2
|
+
import type { Chain, Model } from "../prompt/bind.js";
|
|
3
|
+
import type { History, Message } from "../render/index.js";
|
|
4
|
+
import type { Recordable, TraceHandle, TraceOptions, TraceOutcome } from "./trace.js";
|
|
5
|
+
/**
|
|
6
|
+
* Where a prompt came from, and so what a rollout may reference.
|
|
7
|
+
*
|
|
8
|
+
* `server` has a row and a version behind it. `custom` was written in code and
|
|
9
|
+
* has neither, which is why its `trace` records usage and not a rollout.
|
|
10
|
+
*/
|
|
11
|
+
export type PromptSource = "server" | "custom";
|
|
12
|
+
/** A prompt: its fields, its version, and how to run it. */
|
|
13
|
+
export interface Prompt {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
/** What `pullPrompt` accepts besides the id. Null when there is none. */
|
|
17
|
+
slug: string | null;
|
|
18
|
+
fields: FieldSpec[];
|
|
19
|
+
/** What a rollout recorded from this prompt should reference. Null when custom. */
|
|
20
|
+
versionId: string | null;
|
|
21
|
+
models: string[];
|
|
22
|
+
source: PromptSource;
|
|
23
|
+
/**
|
|
24
|
+
* Validates and substitutes, throwing on a missing required field.
|
|
25
|
+
*
|
|
26
|
+
* `history` is placed between the system turn and the question, verbatim.
|
|
27
|
+
*/
|
|
28
|
+
format(values?: Record<string, unknown>, opts?: RenderOptions): Message[];
|
|
29
|
+
/** The serialised field map that `render` used — recorded on the rollout. */
|
|
30
|
+
serialize(values?: Record<string, unknown>): Record<string, string>;
|
|
31
|
+
/**
|
|
32
|
+
* Renders, runs your model call, and records what it cost around it.
|
|
33
|
+
*
|
|
34
|
+
* This package never sees the provider call, so handing it the call is what
|
|
35
|
+
* gives it a completion signal. It fills in the version, fields, messages,
|
|
36
|
+
* latency and status; you return the text, model and tokens.
|
|
37
|
+
*
|
|
38
|
+
* A `server` prompt records a rollout. A `custom` one has no row to hang a
|
|
39
|
+
* rollout on, so it records usage instead — the spend still lands, the
|
|
40
|
+
* wording is simply not versioned. A callback that throws is recorded as
|
|
41
|
+
* failed, then rethrown.
|
|
42
|
+
*/
|
|
43
|
+
call<T extends Recordable = Recordable>(values: Record<string, unknown>, run: (handle: TraceHandle<T>) => Promise<TraceOutcome> | TraceOutcome, opts?: TraceOptions<T>): Promise<TraceOutcome & {
|
|
44
|
+
rolloutId: string;
|
|
45
|
+
pricing?: Promise<number | undefined>;
|
|
46
|
+
}>;
|
|
47
|
+
/**
|
|
48
|
+
* Attaches a model, so the prompt can run itself.
|
|
49
|
+
*
|
|
50
|
+
* `chain.invoke(values)` is then the whole call — rendered, run and recorded.
|
|
51
|
+
*/
|
|
52
|
+
bind(model: Model): Chain;
|
|
53
|
+
}
|
|
54
|
+
export interface RenderOptions {
|
|
55
|
+
/** Prior turns, oldest first. A `system` turn here is dropped — the prompt owns that. */
|
|
56
|
+
history?: History;
|
|
57
|
+
}
|
|
58
|
+
/** What `buildCustomPrompt` takes. Everything but `name` has a sane default. */
|
|
59
|
+
export interface PromptSpec {
|
|
60
|
+
name: string;
|
|
61
|
+
/** Titled sections, rendered into the system turn in order. */
|
|
62
|
+
blocks?: {
|
|
63
|
+
title: string;
|
|
64
|
+
body: string;
|
|
65
|
+
}[];
|
|
66
|
+
/** The user turn. */
|
|
67
|
+
question?: string;
|
|
68
|
+
/** Declared placeholders. Undeclared `{names}` are left alone. */
|
|
69
|
+
fields?: FieldSpec[];
|
|
70
|
+
models?: string[];
|
|
71
|
+
/** Substituted into blocks and question before field validation. */
|
|
72
|
+
variables?: Record<string, string>;
|
|
73
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** A rollout as the run route returns it. `id` is null for a dry run. */
|
|
2
|
+
export interface RunResult {
|
|
3
|
+
id: string | null;
|
|
4
|
+
model: string;
|
|
5
|
+
status: "completed" | "failed";
|
|
6
|
+
output: string;
|
|
7
|
+
error?: string | null;
|
|
8
|
+
costMicros: number;
|
|
9
|
+
latencyMs: number;
|
|
10
|
+
inputTokens: number;
|
|
11
|
+
outputTokens: number;
|
|
12
|
+
seed?: number;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RolloutInput, RolloutRecord } from "@spendgraph/sdk";
|
|
2
|
+
/**
|
|
3
|
+
* Prompt-shaped names for the shared rollout types.
|
|
4
|
+
*
|
|
5
|
+
* The shapes live in core because a graph run is a rollout too.
|
|
6
|
+
*/
|
|
7
|
+
export type ReportInput = RolloutInput;
|
|
8
|
+
export type Rollout = RolloutRecord;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Run configuration. Field values are the second argument, not part of this. */
|
|
2
|
+
export interface RunOptions {
|
|
3
|
+
/** Overrides the prompt's first saved model. Never silently defaulted. */
|
|
4
|
+
model?: string;
|
|
5
|
+
temperature?: number;
|
|
6
|
+
maxTokens?: number;
|
|
7
|
+
/** Which repetition this is. Metadata for pass^k, not a provider parameter. */
|
|
8
|
+
seed?: number;
|
|
9
|
+
/** Links the run to a dataset case; the server resolves the split from it. */
|
|
10
|
+
caseId?: string;
|
|
11
|
+
candidateId?: string;
|
|
12
|
+
parentId?: string;
|
|
13
|
+
generation?: number;
|
|
14
|
+
/** Supply to make your own retry idempotent. Generated when omitted. */
|
|
15
|
+
rolloutId?: string;
|
|
16
|
+
/** false runs the model but writes nothing — for debugging, not datasets. */
|
|
17
|
+
record?: boolean;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { RolloutStep } from "@spendgraph/sdk";
|
|
2
|
+
import type { History, Message } from "../render/index.js";
|
|
3
|
+
/** What a turn contributes to the rollout once the call is done. */
|
|
4
|
+
export interface TurnRecord {
|
|
5
|
+
offeredTools: string[];
|
|
6
|
+
steps: RolloutStep[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Structurally what `@spendgraph/tools`'s `ToolTrace` is, narrowed to the one
|
|
10
|
+
* method a rollout needs.
|
|
11
|
+
*
|
|
12
|
+
* Declared here rather than imported so this package depends on nothing but the
|
|
13
|
+
* SDK. A real `ToolTrace` satisfies it, and the generic below hands the callback
|
|
14
|
+
* back the full thing rather than this narrowing.
|
|
15
|
+
*/
|
|
16
|
+
export interface Recordable {
|
|
17
|
+
record(): TurnRecord;
|
|
18
|
+
}
|
|
19
|
+
/** Anything that can hand out a turn — a `toolbus()`, or a test double. */
|
|
20
|
+
export interface TurnSource<T extends Recordable = Recordable> {
|
|
21
|
+
trace(query?: string, limit?: number): T;
|
|
22
|
+
}
|
|
23
|
+
/** What the callback is handed. */
|
|
24
|
+
export interface TraceHandle<T extends Recordable = Recordable> {
|
|
25
|
+
messages: Message[];
|
|
26
|
+
/** Present only when `tools` was given. Already selected for this call. */
|
|
27
|
+
turn?: T;
|
|
28
|
+
}
|
|
29
|
+
/** What the callback returns — only what this package cannot see for itself. */
|
|
30
|
+
export interface TraceOutcome {
|
|
31
|
+
output: string;
|
|
32
|
+
model: string;
|
|
33
|
+
inputTokens?: number;
|
|
34
|
+
outputTokens?: number;
|
|
35
|
+
cacheReadTokens?: number;
|
|
36
|
+
cacheWriteTokens?: number;
|
|
37
|
+
/** Billed on their own by search-grounded providers. */
|
|
38
|
+
citationTokens?: number;
|
|
39
|
+
reasoningTokens?: number;
|
|
40
|
+
/**
|
|
41
|
+
* What the call cost, in micro-USD, once the server has priced it.
|
|
42
|
+
*
|
|
43
|
+
* Set by a caller that already knows it. `call` does not fill this in — the
|
|
44
|
+
* price is computed server-side and does not exist until the rollout has been
|
|
45
|
+
* written, so `call` hands back `pricing`, a promise, and never stalls the
|
|
46
|
+
* call waiting for it. Never guessed here: a number this package made up
|
|
47
|
+
* would be indistinguishable from one the pricing table produced.
|
|
48
|
+
*/
|
|
49
|
+
costMicros?: number;
|
|
50
|
+
/** Defaults to "completed". A run that answered badly is not a failure. */
|
|
51
|
+
status?: "completed" | "failed";
|
|
52
|
+
error?: string;
|
|
53
|
+
/** Overrides the turn's steps. A `GraphResult` already carries them. */
|
|
54
|
+
steps?: RolloutStep[];
|
|
55
|
+
offeredTools?: string[];
|
|
56
|
+
/** Set only for evaluation rollouts. Production traffic leaves it unset. */
|
|
57
|
+
caseId?: string;
|
|
58
|
+
seed?: number;
|
|
59
|
+
}
|
|
60
|
+
export interface TraceOptions<T extends Recordable = Recordable> {
|
|
61
|
+
/** Prior turns, oldest first. Rendered between the system turn and the question. */
|
|
62
|
+
history?: History;
|
|
63
|
+
/** A bus to select from. The callback gets the turn; the rollout gets both. */
|
|
64
|
+
tools?: TurnSource<T>;
|
|
65
|
+
/** What to select tools on. Defaults to the rendered user message. */
|
|
66
|
+
query?: string;
|
|
67
|
+
/** False records nothing. The callback still runs and still returns. */
|
|
68
|
+
report?: boolean;
|
|
69
|
+
/** Supply your own to make a retry safe. Generated otherwise. */
|
|
70
|
+
rolloutId?: string;
|
|
71
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { FieldSpec } from "@spendgraph/sdk";
|
|
2
|
+
import type { Block } from "../render/index.js";
|
|
3
|
+
/** One wording a prompt has had. */
|
|
4
|
+
export interface PromptVersion {
|
|
5
|
+
id: string;
|
|
6
|
+
promptId: string;
|
|
7
|
+
hash: string;
|
|
8
|
+
blocks: Block[];
|
|
9
|
+
question: string;
|
|
10
|
+
fieldSpec: FieldSpec[] | null;
|
|
11
|
+
/** "user" is a wording a person wrote; "assay" is one the optimizer proposed. */
|
|
12
|
+
origin: "user" | "assay";
|
|
13
|
+
candidateId: string | null;
|
|
14
|
+
parentVersionId: string | null;
|
|
15
|
+
/** Last time this wording was put live; null means it never has been. */
|
|
16
|
+
servedAt?: string | null;
|
|
17
|
+
/** How many stints it has had live. */
|
|
18
|
+
servedCount?: number;
|
|
19
|
+
createdAt: string;
|
|
20
|
+
isCurrent: boolean;
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spendgraph/prompt",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Pull a stored prompt or build one in code, render it, and record what it cost.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/fnLog0/spendgraph.git",
|
|
9
|
+
"directory": "packages/prompt"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/fnLog0/spendgraph#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/fnLog0/spendgraph/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"llm",
|
|
17
|
+
"prompt",
|
|
18
|
+
"versioning",
|
|
19
|
+
"evaluation",
|
|
20
|
+
"observability"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./render": {
|
|
31
|
+
"types": "./dist/render/index.d.ts",
|
|
32
|
+
"import": "./dist/render/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json",
|
|
35
|
+
"./internals": {
|
|
36
|
+
"types": "./dist/internals.d.ts",
|
|
37
|
+
"import": "./dist/internals.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"README.md"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsc -p tsconfig.json",
|
|
46
|
+
"test": "npm run build && vitest run",
|
|
47
|
+
"pretest": "npm run build --workspace @spendgraph/tools"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@spendgraph/sdk": "^0.2.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@spendgraph/llms": "^0.2.0",
|
|
54
|
+
"@spendgraph/tools": "^0.2.0",
|
|
55
|
+
"typescript": "^5"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=18"
|
|
59
|
+
},
|
|
60
|
+
"publishConfig": {
|
|
61
|
+
"access": "public"
|
|
62
|
+
}
|
|
63
|
+
}
|