Skip to content

Commit

Permalink
Fixes issue with context not passing through generate() (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbleigh authored May 13, 2024
1 parent 25a0c80 commit ac5a4bc
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions js/ai/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export async function toGenerateRequest(
messages,
candidates: options.candidates,
config: options.config,
context: options.context,
tools: tools?.map((tool) => toToolDefinition(tool)) || [],
output: {
format:
Expand Down
2 changes: 1 addition & 1 deletion js/ai/src/model/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ const CONTEXT_ITEM_TEMPLATE = (
out += d.text() + '\n';
return out;
};

export function augmentWithContext(
options?: AugmentWithContextOptions
): ModelMiddleware {
const preface =
typeof options?.preface === 'undefined' ? CONTEXT_PREFACE : options.preface;
const itemTemplate = options?.itemTemplate || CONTEXT_ITEM_TEMPLATE;
const citationKey = options?.citationKey;
return (req, next) => {
// if there is no context in the request, no-op
if (!req.context?.length) return next(req);
Expand Down
23 changes: 23 additions & 0 deletions js/ai/tests/generate/generate_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ describe('toGenerateRequest', () => {
],
candidates: undefined,
config: undefined,
context: undefined,
tools: [],
output: { format: 'text' },
},
Expand All @@ -278,6 +279,7 @@ describe('toGenerateRequest', () => {
],
candidates: undefined,
config: undefined,
context: undefined,
tools: [
{
name: 'tellAFunnyJoke',
Expand Down Expand Up @@ -313,6 +315,7 @@ describe('toGenerateRequest', () => {
],
candidates: undefined,
config: undefined,
context: undefined,
tools: [
{
name: 'tellAFunnyJoke',
Expand Down Expand Up @@ -365,6 +368,7 @@ describe('toGenerateRequest', () => {
],
candidates: undefined,
config: undefined,
context: undefined,
tools: [],
output: { format: 'text' },
},
Expand All @@ -387,6 +391,25 @@ describe('toGenerateRequest', () => {
],
candidates: undefined,
config: undefined,
context: undefined,
tools: [],
output: { format: 'text' },
},
},
{
should: 'pass context through to the model',
prompt: {
model: 'vertexai/gemini-1.0-pro',
prompt: 'Tell a joke with context.',
context: [{ content: [{ text: 'context here' }] }],
},
expectedOutput: {
messages: [
{ content: [{ text: 'Tell a joke with context.' }], role: 'user' },
],
candidates: undefined,
config: undefined,
context: [{ content: [{ text: 'context here' }] }],
tools: [],
output: { format: 'text' },
},
Expand Down
3 changes: 3 additions & 0 deletions js/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions js/samples/flow-simple-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@genkit-ai/google-cloud": "workspace:*",
"@genkit-ai/googleai": "workspace:*",
"@genkit-ai/vertexai": "workspace:*",
"@genkit-ai/dotprompt": "workspace:*",
"@opentelemetry/sdk-trace-base": "^1.22.0",
"firebase-admin": "^12.1.0",
"zod": "^3.22.4"
Expand Down
17 changes: 17 additions & 0 deletions js/samples/flow-simple-ai/prompts/dotpromptContext.prompt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
model: vertexai/gemini-1.0-pro
input:
schema:
question: string
output:
format: json
schema:
answer: string, the answer to the question
id: string, the selected id of the saying
reasoning: string, why the saying applies to the question
---

You are a mystic wisdom bot designed to help people with their problems. Use the provided
sayings to answer the question.

Question: {{question}}
44 changes: 44 additions & 0 deletions js/samples/flow-simple-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { generate, generateStream, retrieve } from '@genkit-ai/ai';
import { defineTool } from '@genkit-ai/ai/tool';
import { configureGenkit } from '@genkit-ai/core';
import { dotprompt, prompt } from '@genkit-ai/dotprompt';
import { defineFirestoreRetriever, firebase } from '@genkit-ai/firebase';
import { defineFlow, run } from '@genkit-ai/flow';
import { googleCloud } from '@genkit-ai/google-cloud';
Expand Down Expand Up @@ -48,6 +49,7 @@ configureGenkit({
metricExportIntervalMillis: 5_000,
},
}),
dotprompt(),
],
flowStateStore: 'firebase',
traceStore: 'firebase',
Expand Down Expand Up @@ -230,3 +232,45 @@ Available Options:\n- ${docs.map((d) => `${d.metadata!.name}: ${d.text()}`).join
return result.text();
}
);

export const dotpromptContext = defineFlow(
{
name: 'dotpromptContext',
inputSchema: z.string(),
outputSchema: z.object({
answer: z.string(),
id: z.string(),
reasoning: z.string(),
}),
},
async (question: string) => {
const docs = [
{
content: [{ text: 'an apple a day keeps the doctor away' }],
metadata: { id: 'apple' },
},
{
content: [
{ text: 'those who live in glass houses should not throw stones' },
],
metadata: { id: 'stone' },
},
{
content: [
{
text: "if you don't have anything nice to say, don't say anything at all",
},
],
metadata: { id: 'nice' },
},
];

const result = await (
await prompt('dotpromptContext')
).generate({
input: { question: question },
context: docs,
});
return result.output() as any;
}
);

0 comments on commit ac5a4bc

Please sign in to comment.