Skip to content

Commit ac5a4bc

Browse files
authored
Fixes issue with context not passing through generate() (#139)
1 parent 25a0c80 commit ac5a4bc

File tree

7 files changed

+90
-1
lines changed

7 files changed

+90
-1
lines changed

js/ai/src/generate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ export async function toGenerateRequest(
410410
messages,
411411
candidates: options.candidates,
412412
config: options.config,
413+
context: options.context,
413414
tools: tools?.map((tool) => toToolDefinition(tool)) || [],
414415
output: {
415416
format:

js/ai/src/model/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ const CONTEXT_ITEM_TEMPLATE = (
198198
out += d.text() + '\n';
199199
return out;
200200
};
201+
201202
export function augmentWithContext(
202203
options?: AugmentWithContextOptions
203204
): ModelMiddleware {
204205
const preface =
205206
typeof options?.preface === 'undefined' ? CONTEXT_PREFACE : options.preface;
206207
const itemTemplate = options?.itemTemplate || CONTEXT_ITEM_TEMPLATE;
207-
const citationKey = options?.citationKey;
208208
return (req, next) => {
209209
// if there is no context in the request, no-op
210210
if (!req.context?.length) return next(req);

js/ai/tests/generate/generate_test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ describe('toGenerateRequest', () => {
260260
],
261261
candidates: undefined,
262262
config: undefined,
263+
context: undefined,
263264
tools: [],
264265
output: { format: 'text' },
265266
},
@@ -278,6 +279,7 @@ describe('toGenerateRequest', () => {
278279
],
279280
candidates: undefined,
280281
config: undefined,
282+
context: undefined,
281283
tools: [
282284
{
283285
name: 'tellAFunnyJoke',
@@ -313,6 +315,7 @@ describe('toGenerateRequest', () => {
313315
],
314316
candidates: undefined,
315317
config: undefined,
318+
context: undefined,
316319
tools: [
317320
{
318321
name: 'tellAFunnyJoke',
@@ -365,6 +368,7 @@ describe('toGenerateRequest', () => {
365368
],
366369
candidates: undefined,
367370
config: undefined,
371+
context: undefined,
368372
tools: [],
369373
output: { format: 'text' },
370374
},
@@ -387,6 +391,25 @@ describe('toGenerateRequest', () => {
387391
],
388392
candidates: undefined,
389393
config: undefined,
394+
context: undefined,
395+
tools: [],
396+
output: { format: 'text' },
397+
},
398+
},
399+
{
400+
should: 'pass context through to the model',
401+
prompt: {
402+
model: 'vertexai/gemini-1.0-pro',
403+
prompt: 'Tell a joke with context.',
404+
context: [{ content: [{ text: 'context here' }] }],
405+
},
406+
expectedOutput: {
407+
messages: [
408+
{ content: [{ text: 'Tell a joke with context.' }], role: 'user' },
409+
],
410+
candidates: undefined,
411+
config: undefined,
412+
context: [{ content: [{ text: 'context here' }] }],
390413
tools: [],
391414
output: { format: 'text' },
392415
},

js/pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/samples/flow-simple-ai/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@genkit-ai/google-cloud": "workspace:*",
2323
"@genkit-ai/googleai": "workspace:*",
2424
"@genkit-ai/vertexai": "workspace:*",
25+
"@genkit-ai/dotprompt": "workspace:*",
2526
"@opentelemetry/sdk-trace-base": "^1.22.0",
2627
"firebase-admin": "^12.1.0",
2728
"zod": "^3.22.4"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
model: vertexai/gemini-1.0-pro
3+
input:
4+
schema:
5+
question: string
6+
output:
7+
format: json
8+
schema:
9+
answer: string, the answer to the question
10+
id: string, the selected id of the saying
11+
reasoning: string, why the saying applies to the question
12+
---
13+
14+
You are a mystic wisdom bot designed to help people with their problems. Use the provided
15+
sayings to answer the question.
16+
17+
Question: {{question}}

js/samples/flow-simple-ai/src/index.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { generate, generateStream, retrieve } from '@genkit-ai/ai';
1818
import { defineTool } from '@genkit-ai/ai/tool';
1919
import { configureGenkit } from '@genkit-ai/core';
20+
import { dotprompt, prompt } from '@genkit-ai/dotprompt';
2021
import { defineFirestoreRetriever, firebase } from '@genkit-ai/firebase';
2122
import { defineFlow, run } from '@genkit-ai/flow';
2223
import { googleCloud } from '@genkit-ai/google-cloud';
@@ -48,6 +49,7 @@ configureGenkit({
4849
metricExportIntervalMillis: 5_000,
4950
},
5051
}),
52+
dotprompt(),
5153
],
5254
flowStateStore: 'firebase',
5355
traceStore: 'firebase',
@@ -230,3 +232,45 @@ Available Options:\n- ${docs.map((d) => `${d.metadata!.name}: ${d.text()}`).join
230232
return result.text();
231233
}
232234
);
235+
236+
export const dotpromptContext = defineFlow(
237+
{
238+
name: 'dotpromptContext',
239+
inputSchema: z.string(),
240+
outputSchema: z.object({
241+
answer: z.string(),
242+
id: z.string(),
243+
reasoning: z.string(),
244+
}),
245+
},
246+
async (question: string) => {
247+
const docs = [
248+
{
249+
content: [{ text: 'an apple a day keeps the doctor away' }],
250+
metadata: { id: 'apple' },
251+
},
252+
{
253+
content: [
254+
{ text: 'those who live in glass houses should not throw stones' },
255+
],
256+
metadata: { id: 'stone' },
257+
},
258+
{
259+
content: [
260+
{
261+
text: "if you don't have anything nice to say, don't say anything at all",
262+
},
263+
],
264+
metadata: { id: 'nice' },
265+
},
266+
];
267+
268+
const result = await (
269+
await prompt('dotpromptContext')
270+
).generate({
271+
input: { question: question },
272+
context: docs,
273+
});
274+
return result.output() as any;
275+
}
276+
);

0 commit comments

Comments
 (0)