Skip to content

Commit 3bbca45

Browse files
author
Phil Hodgson
committed
fix/move history into system prompt
1 parent 48d8c11 commit 3bbca45

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

inspector/web/components/app-prompt.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,10 @@ export class AppPrompt extends LitElement {
3434
const userMessage = input.value.trim();
3535
if (!userMessage) return;
3636

37-
// Retrieve the last 10 interactions (default value) to provide context to the model.
38-
const recentHistory = historyContext.getRecentInteractions();
39-
4037
this.promptState = 'thinking';
4138
input.value = '';
4239

43-
const response = await model.getModelResponse(
44-
userMessage,
45-
recentHistory,
46-
window.applet
47-
);
40+
const response = await model.getModelResponse(userMessage, window.applet);
4841

4942
// contruct array for storing outputs from model
5043
const outputs: InteractionOutput[] = [];

inspector/web/lib/model.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createOpenAI } from '@ai-sdk/openai';
22
import { store } from './store';
33
import { generateObject, jsonSchema } from 'ai';
44
import { Applet } from '@web-applets/sdk';
5-
import type { Interaction } from './history-context';
5+
import { historyContext } from './history-context';
66
import { isEmpty } from '../utils';
77

88
type SchemaResponse = {
@@ -11,18 +11,33 @@ type SchemaResponse = {
1111
};
1212

1313
function getSystemPrompt(applet: Applet) {
14-
if (!applet) return;
15-
16-
const prompt = `\
17-
In this environment you have access to a set of tools. Here are the functions available in JSONSchema format:
18-
${JSON.stringify(applet.actions)}
19-
This is the start data that the tools have provided: ${JSON.stringify(
20-
applet.data
21-
)}
22-
Choose to respond as text and/or one or more functions to call to respond to the user's query.
23-
`;
24-
25-
return prompt;
14+
// Retrieve the last 10 interactions (default value) to provide context to the model.
15+
const recentHistory = historyContext.getRecentInteractions();
16+
17+
const baseSystemPrompt = [
18+
'In this environment you have access to a set of tools and a set of the last ten historical messages and tools used.',
19+
];
20+
if (recentHistory.length > 0)
21+
baseSystemPrompt.push(
22+
`Here is a a history of the last ten messages and tools used for context: ${JSON.stringify(
23+
recentHistory
24+
)}.`
25+
);
26+
27+
if (applet) {
28+
baseSystemPrompt.push(
29+
`Here are the functions available in JSONSchema format:
30+
${JSON.stringify(applet.actions)}
31+
This is the start data that the tools have provided:
32+
${JSON.stringify(applet.data)}.`
33+
);
34+
}
35+
36+
baseSystemPrompt.push(
37+
`Choose to respond as text and/or one or more functions to call to respond to the user's query.`
38+
);
39+
40+
return baseSystemPrompt.join(' ');
2641
}
2742

2843
function getResponseSchema(applet: Applet) {
@@ -86,14 +101,7 @@ function getResponseSchema(applet: Applet) {
86101
return jsonSchema<SchemaResponse>(responseSchema);
87102
}
88103

89-
async function getModelResponse(
90-
prompt: string,
91-
history: Interaction[],
92-
applet: Applet
93-
) {
94-
const contextText = JSON.stringify(history);
95-
const fullPrompt = contextText ? `${contextText}\n${prompt}` : prompt;
96-
104+
async function getModelResponse(prompt: string, applet: Applet) {
97105
/**
98106
* @TODO
99107
*
@@ -113,7 +121,7 @@ async function getModelResponse(
113121

114122
const { object } = await generateObject({
115123
model,
116-
prompt: fullPrompt,
124+
prompt: prompt,
117125
system: systemPrompt,
118126
schema: responseSchema,
119127
});

0 commit comments

Comments
 (0)