Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js/ai): pass chat messages to premable prompt to correctly render history #1326

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/ai/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class Session<S = any> {
input: renderOptions?.input,
model: (renderOptions as BaseGenerateOptions)?.model,
config: (renderOptions as BaseGenerateOptions)?.config,
messages: (renderOptions as BaseGenerateOptions)?.messages,
})
.then((rb) => {
return {
Expand Down
124 changes: 124 additions & 0 deletions js/genkit/tests/chat_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { MessageData } from '@genkit-ai/ai';
import { z } from '@genkit-ai/core';
import assert from 'node:assert';
import { beforeEach, describe, it } from 'node:test';
import { Genkit, genkit } from '../src/genkit';
Expand Down Expand Up @@ -535,4 +537,126 @@ describe('preamble', () => {
},
]);
});

it('initializes chat with history', async () => {
const history: MessageData[] = [
{
role: 'user',
content: [{ text: 'hi' }],
},
{
role: 'model',
content: [{ text: 'bye' }],
},
];

const chat = ai.chat({
model: 'echoModel',
system: 'system instructions',
messages: history,
});

const response = await chat.send('hi again');
assert.deepStrictEqual(response.messages, [
{
role: 'system',
content: [{ text: 'system instructions' }],
metadata: { preamble: true },
},
{
role: 'user',
content: [{ text: 'hi' }],
metadata: { preamble: true },
},
{
role: 'model',
content: [{ text: 'bye' }],
metadata: { preamble: true },
},
{
role: 'user',
content: [{ text: 'hi again' }],
},
{
role: 'model',
content: [
{ text: 'Echo: system: system instructions,hi,bye,hi again' },
{ text: '; config: {}' },
],
},
]);
});

it('initializes chat with history in preamble', async () => {
const hi = ai.definePrompt(
{
name: 'hi',
model: 'echoModel',
input: {
schema: z.object({
name: z.string(),
}),
},
},
'{{ role "system"}}system instructions{{ history}}hi {{ name }}'
);

const history: MessageData[] = [
{
role: 'user',
content: [{ text: 'hi' }],
},
{
role: 'model',
content: [{ text: 'bye' }],
},
];

const chat = ai.chat(hi, { input: { name: 'Genkit' }, messages: history });

const response = await chat.send('hi again');
assert.deepStrictEqual(response.messages, [
{
role: 'system',
content: [{ text: 'system instructions' }],
metadata: { preamble: true },
},
{
role: 'user',
content: [{ text: 'hi' }],
metadata: {
preamble: true,
purpose: 'history',
},
},
{
role: 'model',
content: [{ text: 'bye' }],
metadata: {
preamble: true,
purpose: 'history',
},
},
{
role: 'model',
content: [{ text: 'hi Genkit' }],
metadata: {
preamble: true,
},
},
{
role: 'user',
content: [{ text: 'hi again' }],
},
{
role: 'model',
content: [
{
text: 'Echo: system: system instructions,hi,bye,hi Genkit,hi again',
},
{ text: '; config: {}' },
],
},
]);
});
});
Loading