@@ -2,7 +2,7 @@ import { createOpenAI } from '@ai-sdk/openai';
2
2
import { store } from './store' ;
3
3
import { generateObject , jsonSchema } from 'ai' ;
4
4
import { Applet } from '@web-applets/sdk' ;
5
- import type { Interaction } from './history-context' ;
5
+ import { historyContext } from './history-context' ;
6
6
import { isEmpty } from '../utils' ;
7
7
8
8
type SchemaResponse = {
@@ -11,18 +11,33 @@ type SchemaResponse = {
11
11
} ;
12
12
13
13
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 ( ' ' ) ;
26
41
}
27
42
28
43
function getResponseSchema ( applet : Applet ) {
@@ -86,14 +101,7 @@ function getResponseSchema(applet: Applet) {
86
101
return jsonSchema < SchemaResponse > ( responseSchema ) ;
87
102
}
88
103
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 ) {
97
105
/**
98
106
* @TODO
99
107
*
@@ -113,7 +121,7 @@ async function getModelResponse(
113
121
114
122
const { object } = await generateObject ( {
115
123
model,
116
- prompt : fullPrompt ,
124
+ prompt : prompt ,
117
125
system : systemPrompt ,
118
126
schema : responseSchema ,
119
127
} ) ;
0 commit comments