@ohos-ports/genkit 1.42.0-beta.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 +203 -0
- package/README.md +434 -0
- package/lib/beta.d.mts +18 -0
- package/lib/beta.d.ts +18 -0
- package/lib/beta.js +88 -0
- package/lib/beta.js.map +1 -0
- package/lib/beta.mjs +64 -0
- package/lib/beta.mjs.map +1 -0
- package/lib/client/agent.d.mts +52 -0
- package/lib/client/agent.d.ts +52 -0
- package/lib/client/agent.js +90 -0
- package/lib/client/agent.js.map +1 -0
- package/lib/client/agent.mjs +68 -0
- package/lib/client/agent.mjs.map +1 -0
- package/lib/client/client.d.mts +80 -0
- package/lib/client/client.d.ts +80 -0
- package/lib/client/client.js +164 -0
- package/lib/client/client.js.map +1 -0
- package/lib/client/client.mjs +139 -0
- package/lib/client/client.mjs.map +1 -0
- package/lib/client/index.d.mts +6 -0
- package/lib/client/index.d.ts +6 -0
- package/lib/client/index.js +55 -0
- package/lib/client/index.js.map +1 -0
- package/lib/client/index.mjs +31 -0
- package/lib/client/index.mjs.map +1 -0
- package/lib/client/types.d.mts +17 -0
- package/lib/client/types.d.ts +17 -0
- package/lib/client/types.js +17 -0
- package/lib/client/types.js.map +1 -0
- package/lib/client/types.mjs +1 -0
- package/lib/client/types.mjs.map +1 -0
- package/lib/common.d.mts +3 -0
- package/lib/common.d.ts +3 -0
- package/lib/common.js +151 -0
- package/lib/common.js.map +1 -0
- package/lib/common.mjs +128 -0
- package/lib/common.mjs.map +1 -0
- package/lib/context.d.mts +1 -0
- package/lib/context.d.ts +1 -0
- package/lib/context.js +50 -0
- package/lib/context.js.map +1 -0
- package/lib/context.mjs +28 -0
- package/lib/context.mjs.map +1 -0
- package/lib/embedder.d.mts +1 -0
- package/lib/embedder.d.ts +1 -0
- package/lib/embedder.js +48 -0
- package/lib/embedder.js.map +1 -0
- package/lib/embedder.mjs +26 -0
- package/lib/embedder.mjs.map +1 -0
- package/lib/evaluator.d.mts +1 -0
- package/lib/evaluator.d.ts +1 -0
- package/lib/evaluator.js +60 -0
- package/lib/evaluator.js.map +1 -0
- package/lib/evaluator.mjs +38 -0
- package/lib/evaluator.mjs.map +1 -0
- package/lib/extract.d.mts +1 -0
- package/lib/extract.d.ts +1 -0
- package/lib/extract.js +48 -0
- package/lib/extract.js.map +1 -0
- package/lib/extract.mjs +23 -0
- package/lib/extract.mjs.map +1 -0
- package/lib/formats.d.mts +1 -0
- package/lib/formats.d.ts +1 -0
- package/lib/formats.js +34 -0
- package/lib/formats.js.map +1 -0
- package/lib/formats.mjs +18 -0
- package/lib/formats.mjs.map +1 -0
- package/lib/genkit-beta.d.mts +183 -0
- package/lib/genkit-beta.d.ts +183 -0
- package/lib/genkit-beta.js +178 -0
- package/lib/genkit-beta.js.map +1 -0
- package/lib/genkit-beta.mjs +159 -0
- package/lib/genkit-beta.mjs.map +1 -0
- package/lib/genkit.d.mts +10 -0
- package/lib/genkit.d.ts +10 -0
- package/lib/genkit.js +471 -0
- package/lib/genkit.js.map +1 -0
- package/lib/genkit.mjs +489 -0
- package/lib/genkit.mjs.map +1 -0
- package/lib/index-BSkuxdwl.d.mts +311 -0
- package/lib/index-BSkuxdwl.d.ts +311 -0
- package/lib/index.d.mts +10 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +51 -0
- package/lib/index.js.map +1 -0
- package/lib/index.mjs +24 -0
- package/lib/index.mjs.map +1 -0
- package/lib/logging.d.mts +1 -0
- package/lib/logging.d.ts +1 -0
- package/lib/logging.js +46 -0
- package/lib/logging.js.map +1 -0
- package/lib/logging.mjs +22 -0
- package/lib/logging.mjs.map +1 -0
- package/lib/middleware.d.mts +1 -0
- package/lib/middleware.d.ts +1 -0
- package/lib/middleware.js +56 -0
- package/lib/middleware.js.map +1 -0
- package/lib/middleware.mjs +34 -0
- package/lib/middleware.mjs.map +1 -0
- package/lib/model.d.mts +1 -0
- package/lib/model.d.ts +1 -0
- package/lib/model.js +92 -0
- package/lib/model.js.map +1 -0
- package/lib/model.mjs +70 -0
- package/lib/model.mjs.map +1 -0
- package/lib/plugin.d.mts +10 -0
- package/lib/plugin.d.ts +10 -0
- package/lib/plugin.js +143 -0
- package/lib/plugin.js.map +1 -0
- package/lib/plugin.mjs +113 -0
- package/lib/plugin.mjs.map +1 -0
- package/lib/registry.d.mts +1 -0
- package/lib/registry.d.ts +1 -0
- package/lib/registry.js +46 -0
- package/lib/registry.js.map +1 -0
- package/lib/registry.mjs +24 -0
- package/lib/registry.mjs.map +1 -0
- package/lib/reranker.d.mts +1 -0
- package/lib/reranker.d.ts +1 -0
- package/lib/reranker.js +54 -0
- package/lib/reranker.js.map +1 -0
- package/lib/reranker.mjs +32 -0
- package/lib/reranker.mjs.map +1 -0
- package/lib/retriever.d.mts +1 -0
- package/lib/retriever.d.ts +1 -0
- package/lib/retriever.js +58 -0
- package/lib/retriever.js.map +1 -0
- package/lib/retriever.mjs +36 -0
- package/lib/retriever.mjs.map +1 -0
- package/lib/schema.d.mts +1 -0
- package/lib/schema.d.ts +1 -0
- package/lib/schema.js +54 -0
- package/lib/schema.js.map +1 -0
- package/lib/schema.mjs +32 -0
- package/lib/schema.mjs.map +1 -0
- package/lib/testing.d.mts +1 -0
- package/lib/testing.d.ts +1 -0
- package/lib/testing.js +50 -0
- package/lib/testing.js.map +1 -0
- package/lib/testing.mjs +28 -0
- package/lib/testing.mjs.map +1 -0
- package/lib/tool.d.mts +1 -0
- package/lib/tool.d.ts +1 -0
- package/lib/tool.js +54 -0
- package/lib/tool.js.map +1 -0
- package/lib/tool.mjs +32 -0
- package/lib/tool.mjs.map +1 -0
- package/lib/tracing.d.mts +1 -0
- package/lib/tracing.d.ts +1 -0
- package/lib/tracing.js +82 -0
- package/lib/tracing.js.map +1 -0
- package/lib/tracing.mjs +60 -0
- package/lib/tracing.mjs.map +1 -0
- package/package.json +233 -0
- package/src/beta.ts +85 -0
- package/src/client/agent.ts +160 -0
- package/src/client/client.ts +237 -0
- package/src/client/index.ts +72 -0
- package/src/client/types.ts +41 -0
- package/src/common.ts +175 -0
- package/src/context.ts +37 -0
- package/src/embedder.ts +41 -0
- package/src/evaluator.ts +50 -0
- package/src/extract.ts +30 -0
- package/src/formats.ts +30 -0
- package/src/genkit-beta.ts +275 -0
- package/src/genkit.ts +818 -0
- package/src/index.ts +29 -0
- package/src/logging.ts +30 -0
- package/src/middleware.ts +38 -0
- package/src/model.ts +87 -0
- package/src/plugin.ts +172 -0
- package/src/registry.ts +36 -0
- package/src/reranker.ts +42 -0
- package/src/retriever.ts +55 -0
- package/src/schema.ts +40 -0
- package/src/testing.ts +47 -0
- package/src/tool.ts +40 -0
- package/src/tracing.ts +55 -0
- package/tests/agent_client_test.ts +497 -0
- package/tests/embed_test.ts +142 -0
- package/tests/evaluate_test.ts +52 -0
- package/tests/flow_test.ts +94 -0
- package/tests/formats_test.ts +168 -0
- package/tests/generate_test.ts +1631 -0
- package/tests/genkit_test.ts +33 -0
- package/tests/helpers.ts +176 -0
- package/tests/mock-model_test.ts +490 -0
- package/tests/plugins_test.ts +259 -0
- package/tests/prompts/badSchemaRef.prompt +9 -0
- package/tests/prompts/chat_preamble.prompt +5 -0
- package/tests/prompts/kitchensink.prompt +25 -0
- package/tests/prompts/output.prompt +11 -0
- package/tests/prompts/schemaRef.prompt +9 -0
- package/tests/prompts/sub/test.prompt +5 -0
- package/tests/prompts/test.prompt +5 -0
- package/tests/prompts/test.variant.prompt +8 -0
- package/tests/prompts/toolPrompt.prompt +6 -0
- package/tests/prompts_test.ts +1442 -0
- package/tests/session_test.ts +17 -0
- package/typedoc.json +22 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2024 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { z } from '@genkit-ai/core';
|
|
18
|
+
import * as assert from 'assert';
|
|
19
|
+
import { beforeEach, describe, it } from 'node:test';
|
|
20
|
+
import { genkit, type Genkit } from '../src/index.js';
|
|
21
|
+
|
|
22
|
+
describe('flow', () => {
|
|
23
|
+
let ai: Genkit;
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
ai = genkit({
|
|
27
|
+
context: { something: 'extra' },
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('calls simple flow', async () => {
|
|
32
|
+
const bananaFlow = ai.defineFlow('banana', () => 'banana');
|
|
33
|
+
|
|
34
|
+
assert.strictEqual(await bananaFlow(), 'banana');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('streams simple chunks (no schema defined)', async () => {
|
|
38
|
+
const streamingBananaFlow = ai.defineFlow(
|
|
39
|
+
'banana',
|
|
40
|
+
(input: string, sendChunk) => {
|
|
41
|
+
for (let i = 0; i < input.length; i++) {
|
|
42
|
+
sendChunk(input.charAt(i));
|
|
43
|
+
}
|
|
44
|
+
return input;
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const { stream, output } = streamingBananaFlow.stream('banana');
|
|
49
|
+
const chunks: string[] = [];
|
|
50
|
+
for await (const chunk of stream) {
|
|
51
|
+
chunks.push(chunk as string);
|
|
52
|
+
}
|
|
53
|
+
assert.strictEqual(await output, 'banana');
|
|
54
|
+
assert.deepStrictEqual(chunks, ['b', 'a', 'n', 'a', 'n', 'a']);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('streams simple chunks with schema defined', async () => {
|
|
58
|
+
const streamingBananaFlow = ai.defineFlow(
|
|
59
|
+
{
|
|
60
|
+
name: 'banana',
|
|
61
|
+
inputSchema: z.string(),
|
|
62
|
+
streamSchema: z.string(),
|
|
63
|
+
},
|
|
64
|
+
(input, sendChunk) => {
|
|
65
|
+
for (let i = 0; i < input.length; i++) {
|
|
66
|
+
sendChunk(input.charAt(i));
|
|
67
|
+
}
|
|
68
|
+
return input;
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const { stream, output } = streamingBananaFlow.stream('banana');
|
|
73
|
+
const chunks: string[] = [];
|
|
74
|
+
for await (const chunk of stream) {
|
|
75
|
+
chunks.push(chunk);
|
|
76
|
+
}
|
|
77
|
+
assert.deepStrictEqual(chunks, ['b', 'a', 'n', 'a', 'n', 'a']);
|
|
78
|
+
assert.strictEqual(await output, 'banana');
|
|
79
|
+
|
|
80
|
+
// a "streaming" flow can be invoked in non-streaming mode.
|
|
81
|
+
assert.strictEqual(await streamingBananaFlow('banana2'), 'banana2');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('passes through the context', async () => {
|
|
85
|
+
const bananaFlow = ai.defineFlow('banana', (_, { context }) =>
|
|
86
|
+
JSON.stringify(context)
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
assert.strictEqual(
|
|
90
|
+
await bananaFlow(undefined, { context: { foo: 'bar' } }),
|
|
91
|
+
'{"something":"extra","foo":"bar"}'
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2024 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { stripUndefinedProps } from '@genkit-ai/core';
|
|
18
|
+
import * as assert from 'assert';
|
|
19
|
+
import { beforeEach, describe, it } from 'node:test';
|
|
20
|
+
import { genkit, type GenkitBeta } from '../src/beta';
|
|
21
|
+
import { defineEchoModel } from './helpers';
|
|
22
|
+
|
|
23
|
+
describe('formats', () => {
|
|
24
|
+
let ai: GenkitBeta;
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
ai = genkit({});
|
|
28
|
+
ai.defineFormat(
|
|
29
|
+
{
|
|
30
|
+
name: 'banana',
|
|
31
|
+
format: 'banana',
|
|
32
|
+
constrained: true,
|
|
33
|
+
},
|
|
34
|
+
(schema) => {
|
|
35
|
+
let instructions: string | undefined;
|
|
36
|
+
|
|
37
|
+
if (schema) {
|
|
38
|
+
instructions = `Output should be in banana format`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
parseChunk: (chunk) => {
|
|
43
|
+
return `banana: ${chunk.content[0].text}`;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
parseMessage: (message) => {
|
|
47
|
+
return `banana: ${message.content[0].text}`;
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
instructions,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('lets you define and use a custom output format with native constrained generation', async () => {
|
|
57
|
+
defineEchoModel(ai, { supports: { constrained: 'all' } });
|
|
58
|
+
|
|
59
|
+
const { output } = await ai.generate({
|
|
60
|
+
model: 'echoModel',
|
|
61
|
+
prompt: 'hi',
|
|
62
|
+
output: { format: 'banana' },
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
assert.strictEqual(output, 'banana: Echo: hi');
|
|
66
|
+
|
|
67
|
+
const { response, stream } = await ai.generateStream({
|
|
68
|
+
model: 'echoModel',
|
|
69
|
+
prompt: 'hi',
|
|
70
|
+
output: { format: 'banana' },
|
|
71
|
+
});
|
|
72
|
+
const chunks: string[] = [];
|
|
73
|
+
for await (const chunk of stream) {
|
|
74
|
+
chunks.push(`${chunk.output}`);
|
|
75
|
+
}
|
|
76
|
+
assert.deepStrictEqual(chunks, ['banana: 3', 'banana: 2', 'banana: 1']);
|
|
77
|
+
assert.strictEqual((await response).output, 'banana: Echo: hi');
|
|
78
|
+
assert.deepStrictEqual(stripUndefinedProps((await response).request), {
|
|
79
|
+
config: {},
|
|
80
|
+
messages: [
|
|
81
|
+
{
|
|
82
|
+
content: [{ text: 'hi' }],
|
|
83
|
+
role: 'user',
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
output: {
|
|
87
|
+
constrained: true,
|
|
88
|
+
format: 'banana',
|
|
89
|
+
},
|
|
90
|
+
tools: [],
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('lets you define and use a custom output format with simulated constrained generation', async () => {
|
|
95
|
+
defineEchoModel(ai, { supports: { constrained: 'none' } });
|
|
96
|
+
|
|
97
|
+
const { output } = await ai.generate({
|
|
98
|
+
model: 'echoModel',
|
|
99
|
+
prompt: 'hi',
|
|
100
|
+
output: { format: 'banana' },
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
assert.strictEqual(output, 'banana: Echo: hi');
|
|
104
|
+
|
|
105
|
+
const { response, stream } = await ai.generateStream({
|
|
106
|
+
model: 'echoModel',
|
|
107
|
+
prompt: 'hi',
|
|
108
|
+
output: { format: 'banana' },
|
|
109
|
+
});
|
|
110
|
+
const chunks: string[] = [];
|
|
111
|
+
for await (const chunk of stream) {
|
|
112
|
+
chunks.push(`${chunk.output}`);
|
|
113
|
+
}
|
|
114
|
+
assert.deepStrictEqual(chunks, ['banana: 3', 'banana: 2', 'banana: 1']);
|
|
115
|
+
assert.strictEqual((await response).output, 'banana: Echo: hi');
|
|
116
|
+
assert.deepStrictEqual(stripUndefinedProps((await response).request), {
|
|
117
|
+
config: {},
|
|
118
|
+
messages: [
|
|
119
|
+
{
|
|
120
|
+
content: [{ text: 'hi' }],
|
|
121
|
+
role: 'user',
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
output: {
|
|
125
|
+
constrained: true,
|
|
126
|
+
format: 'banana',
|
|
127
|
+
},
|
|
128
|
+
tools: [],
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('used explicitly specified output options overriding format options', async () => {
|
|
133
|
+
defineEchoModel(ai, { supports: { constrained: 'all' } });
|
|
134
|
+
const response = await ai.generate({
|
|
135
|
+
model: 'echoModel',
|
|
136
|
+
prompt: 'hi',
|
|
137
|
+
output: {
|
|
138
|
+
format: 'banana',
|
|
139
|
+
// Explicitly specified, should ignore whatever format sets
|
|
140
|
+
constrained: false,
|
|
141
|
+
jsonSchema: { type: 'string' },
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
assert.deepStrictEqual(stripUndefinedProps(response.request), {
|
|
145
|
+
config: {},
|
|
146
|
+
messages: [
|
|
147
|
+
{
|
|
148
|
+
content: [
|
|
149
|
+
{ text: 'hi' },
|
|
150
|
+
{
|
|
151
|
+
text: 'Output should be in banana format',
|
|
152
|
+
metadata: { purpose: 'output' },
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
role: 'user',
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
output: {
|
|
159
|
+
constrained: false,
|
|
160
|
+
format: 'banana',
|
|
161
|
+
schema: {
|
|
162
|
+
type: 'string',
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
tools: [],
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|