Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix prompt editing for new model. Improve default prompt.

Tomas Valenta667c1217fa69
diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.py
index 467ce63eb..7fbde3336 100644
--- a/api-service/codegen/codegen.py
+++ b/api-service/codegen/codegen.py
@@ -71,8 +71,8 @@ class Codegen(BaseModel):
llm=self._llm,
tools=tools,
prefix=self.get_prompt_part("system", "prefix"),
- format_instructions=self.get_prompt_part("system", "instructionsFormat"),
- suffix=self.get_prompt_part("system", "suffix"),
+ format_instructions=self.get_prompt_part("system", "suffix"),
+ suffix="",
input_variables=Codegen.input_variables,
callback_manager=self._callback_manager,
)
diff --git a/components/Button.tsx b/components/Button.tsx
index 11047d1ac..08291db3a 100644
--- a/components/Button.tsx
+++ b/components/Button.tsx
@@ -77,4 +77,5 @@ function Button({
Button.variant = Variant
Button.iconPosition = IconPosition
+
export default Button
diff --git a/components/Editor/Sidebar/Prompt/Editor.tsx b/components/Editor/Sidebar/Prompt/Editor.tsx
new file mode 100644
index 000000000..8cc6ef053
--- /dev/null
+++ b/components/Editor/Sidebar/Prompt/Editor.tsx
@@ -0,0 +1,72 @@
+import { EditorContent } from '@tiptap/react'
+import Fuse from 'fuse.js'
+
+import Text from 'components/Text'
+import useDocEditor from 'hooks/useDocEditor'
+import { Reference } from 'editor/referenceType'
+import { PromptPart, SelectedModel } from 'state/store'
+import { useCallback } from 'react'
+import { useStateStore } from 'state/StoreProvider'
+
+export interface Props {
+ title?: string
+ placeholder?: string
+ onChange?: (value: string) => void
+ content: string
+ referenceSearch?: Fuse<Reference>
+ model: SelectedModel
+ templateID: string
+ idx: number
+ promptPart: PromptPart
+}
+
+function Editor({
+ title,
+ idx,
+ promptPart,
+ templateID,
+ model,
+ content,
+ placeholder,
+ referenceSearch,
+}: Props) {
+ const [selectors] = useStateStore()
+ const setPrompt = selectors.use.setPrompt()
+
+ const onChange = useCallback((content: string) => {
+ setPrompt(templateID, model.provider, model.name, idx, { content, role: promptPart.role, type: promptPart.type })
+ }, [setPrompt, templateID, model.provider, model.name, idx, promptPart.role, promptPart.type])
+
+
+ const editor = useDocEditor({
+ initialContent: content,
+ onContentChange: onChange,
+ placeholder,
+ referenceSearch,
+ })
+
+ return (
+ <div className="
+ flex
+ flex-col
+ space-y-2
+ self-stretch
+ pb-6
+ border-b
+ border-slate-200
+ ">
+ {title &&
+ <Text
+ className="font-bold text-slate-400"
+ size={Text.size.S2}
+ text={title}
+ />
+ }
+ <EditorContent
+ editor={editor}
+ />
+ </div>
+ )
+}
+
+export default Editor
diff --git a/components/Editor/Sidebar/Prompt.tsx b/components/Editor/Sidebar/Prompt/index.tsx
similarity index 63%
rename from components/Editor/Sidebar/Prompt.tsx
rename to components/Editor/Sidebar/Prompt/index.tsx
index bac04b865..05b27f85b 100644
--- a/components/Editor/Sidebar/Prompt.tsx
+++ b/components/Editor/Sidebar/Prompt/index.tsx
@@ -1,14 +1,13 @@
import Text from 'components/Text'
import { useStateStore } from 'state/StoreProvider'
-import { defaultPromptTemplate } from 'state/prompt'
+import { defaultPromptTemplate, getDescription } from 'state/prompt'
import { defaultTemplateID } from 'state/store'
-import PromptEditor from '../RouteEditor/PromptEditor'
+import Editor from './Editor'
function Prompt() {
const [selectors] = useStateStore()
const model = selectors.use.model()
- const setPrompt = selectors.use.setPrompt()
const modelSetup = selectors.use.modelSetups().find(p =>
p.templateID === defaultTemplateID &&
p.provider === model.provider &&
@@ -66,17 +65,31 @@ function Prompt() {
<div
key={i}
className="
- flex
- flex-col
- "
+ flex
+ space-y-4
+ flex-col
+ "
>
- <PromptEditor
- title={`${p.role} ${p.type.replace(/([A-Z])/g, ' $1')}`.toUpperCase()}
+ <div className="flex flex-col">
+ <Text
+ className="font-bold text-slate-400"
+ size={Text.size.S2}
+ text={`${p.role} ${p.type.replace(/([A-Z])/g, ' $1')}`.toUpperCase()}
+ />
+ <Text
+ className="font-medium text-slate-400"
+ size={Text.size.S3}
+ text={getDescription(p)}
+ />
+ </div>
+ <Editor
+ // title={`${p.role} ${p.type.replace(/([A-Z])/g, ' $1')}`.toUpperCase()}
placeholder={`Specify ${p.role} ${p.type.replace(/([A-Z])/g, ' $1').toLowerCase()} prompt here`}
content={p.content}
- onChange={(content) => {
- setPrompt(defaultTemplateID, model.provider, model.name, i, { ...p, content, })
- }}
+ model={model}
+ templateID={defaultTemplateID}
+ idx={i}
+ promptPart={p}
/>
</div>
)}
diff --git a/components/Text.tsx b/components/Text.tsx
index 7d0c457e3..52b7eade2 100644
--- a/components/Text.tsx
+++ b/components/Text.tsx
@@ -35,6 +35,7 @@ function Text({
'flex-row',
'items-center',
'text-left',
+ 'whitespace-pre-wrap',
Sizes[size],
className,
)
diff --git a/hooks/useDocEditor.ts b/hooks/useDocEditor.ts
index 02bc281a3..fcb2c9c12 100644
--- a/hooks/useDocEditor.ts
+++ b/hooks/useDocEditor.ts
@@ -51,7 +51,7 @@ function useDocEditor({
onContentChange(editor.getHTML())
}
},
- }, [])
+ }, [onContentChange])
return editor
}
diff --git a/state/prompt.ts b/state/prompt.ts
index f62649634..d5d8d5651 100644
--- a/state/prompt.ts
+++ b/state/prompt.ts
@@ -4,26 +4,35 @@ import Mustache from 'mustache'
import { Block, PromptPart } from 'state/store'
import { identity } from 'utils/identity'
+export function getDescription(part: PromptPart) {
+ if (part.role === 'system' && part.type === 'prefix') {
+ return 'Specify model\'s behavior for the whole run'
+ }
+ if (part.role === 'system' && part.type === 'suffix') {
+ return 'Specify how the model should call tools (like WriteJavaScriptCode)'
+ }
+ if (part.role === 'user' && part.type === 'prefix') {
+ return 'Describe the context and the start of the task the model should do.\nTo use information from the user prompt (like the steps from "Step-by-step instructions") add them like this: `{{Instructions}}`.\nYou can add Description, RequestBody, Instructions, Method, and Route this way.'
+ }
+
+ return ''
+}
+
export const defaultPromptTemplate: PromptPart[] = [
{
role: 'system',
type: 'prefix',
content: '<p>You are an AI JavaScript developer assistant:</p><p>You are building an Express server that handles REST API.</p><p>The <code>express</code> package is already installed.</p><p>Follow the user\'s instructions carefully &amp; to the letter.</p><p>Minimize any other prose.</p><p>You have access to the following tools:\n</p>',
},
- {
- role: 'system',
- type: 'instructionsFormat',
- content: '<p>The way you use the tools is by specifying a XML snippet.</p><p>The XML snippet MUST have a <code>&lt;action tool="$TOOL_NAME"&gt;$INPUT&lt;/action&gt;</code> element with the name of the tool in the <code>tool</code> attribute and input for the tool inside the XML tag.</p><p></p><p>Here is an example of a valid XML snippet:</p><p>&lt;action tool="$TOOL_NAME"&gt;</p><p>$INPUT</p><p>&lt;/action&gt;</p><p></p><p>ALWAYS use the following format:</p><p>Thought: you should always think about what to do</p><p>Action:</p><p>&lt;action tool="$TOOL_NAME"&gt;</p><p>$INPUT</p><p>&lt;/action&gt;</p><p>Observation: the result of the action</p><p>... (this Thought/Action/Observation can repeat N times)</p><p>Thought: I now know the final server code and can show it.</p><p>Final Answer: the final answer</p>',
- },
{
role: 'system',
type: 'suffix',
- content: '',
+ content: '<p>The way you use the tools is by specifying a XML snippet.</p><p>The XML snippet MUST have a <code>&lt;action tool="$TOOL_NAME"&gt;$INPUT&lt;/action&gt;</code> element with the name of the tool in the <code>tool</code> attribute and input for the tool inside the XML tag.</p><p></p><p>Here is an example of a valid XML snippet:</p><p>&lt;action tool="$TOOL_NAME"&gt;</p><p>$INPUT</p><p>&lt;/action&gt;</p><p></p><p>ALWAYS use the following format:</p><p>Thought: you should always think about what to do</p><p>Action:</p><p>&lt;action tool="$TOOL_NAME"&gt;</p><p>$INPUT</p><p>&lt;/action&gt;</p><p>Observation: the result of the action</p><p>... (this Thought/Action/Observation can repeat N times)</p><p>Thought: I now know the final server code and can show it.</p><p>Final Answer: the final answer</p>',
},
{
role: 'user',
type: 'prefix',
- content: '<p>The handler you are building should do the following:</p><p>{{Description}}</p><p>Do not try to come up with solutions and code if you do not know. Instead, use the tool <code>AskHuman</code> to ask for help.</p><p>If you think there might be multiple paths forward, use the tool <code>LetHumanChoose</code> to choose from them.</p><p>{{#RequestBody}}</p><p>The incoming request body is JSON that looks like this:</p><pre><code>{\n{{RequestBody}}\n}</code></pre><p>{{/RequestBody}}</p><p>Use this starting template:</p><pre><code>import express from \'express\';\nconst app = express();\nconst port = 3000;\napp.use(express.json())\n\n// TODO: Implement the {{Method}} handler here\n\napp.listen(port, async () =&gt; {\n console.log(`Server listening on port ${ port }`)\n})</code></pre><p>The HTTP request handler is of type {{Method}}</p><p>The request handler MUST be on the route <code>{{Route}}</code></p><p>Do not forget to use async and await</p><p>Always test that the generated server works without bugs and errors as required by running the code and making mock <code>curl</code> requests to the server</p><p>Generate the full required server code</p><p>{{#Instructions}}</p><p>Here are the required implementation instructions:</p><p>{{Instructions}}</p><p>{{/Instructions}}</p><p>Thought: Here is the plan of how I will go about solving this:</p><p></p>',
+ content: '<p>The handler you are building should do the following:</p><p>{{Description}}</p><p></p><p>{{#RequestBody}}</p><p>The incoming request body is JSON that looks like this:</p><pre><code>{\n{{RequestBody}}\n}</code></pre><p>{{/RequestBody}}</p><p></p><p>Use this starting template:</p><pre><code>import express from \'express\';\nconst app = express();\nconst port = 3000;\napp.use(express.json())\n\n// TODO: Implement the {{Method}} handler here\n\napp.listen(port, async () =&gt; {\n console.log(`Server listening on port ${ port }`)\n})</code></pre><p>The HTTP request handler is of type {{Method}}.</p><p>The request handler MUST be on the route <code>{{Route}}</code>.</p><p>Do not forget to use async and await.</p><p>Always test that the generated server works without bugs and errors as required by running the code and making mock <code>curl</code> requests to the server.</p><p>Generate the full required server code.</p><p></p><p>{{#Instructions}}</p><p>Here are the required implementation instructions:</p><p>{{Instructions}}</p><p>{{/Instructions}}</p><p></p><p>Thought: Here is the plan of how I will go about solving this:</p>',
},
]
diff --git a/state/store.ts b/state/store.ts
index 7a6bd6354..ce43970b7 100644
--- a/state/store.ts
+++ b/state/store.ts
@@ -115,7 +115,7 @@ function getDefaultModel(): SelectedModel {
}
}
-function getDefaultPrompts(): ModelSetup[] {
+function getDefaultModelSetup(): ModelSetup[] {
return [{
provider: ModelProvider.OpenAI,
modelName: defaultModelName,
@@ -129,7 +129,7 @@ function getDefaultState(): SerializedState {
envs: [{ key: '', value: '' }],
routes: [getDefaultRoute()],
model: getDefaultModel(),
- modelSetups: getDefaultPrompts(),
+ modelSetups: getDefaultModelSetup(),
}
}
@@ -153,8 +153,8 @@ export function createStore(project: projects, client?: SupabaseClient<Database>
initialState.envs.push({ key: '', value: '' })
}
- if (!initialState.modelSetups || !Array.isArray(initialState.modelSetups)) {
- initialState.modelSetups = getDefaultPrompts()
+ if (!initialState.modelSetups) {
+ initialState.modelSetups = getDefaultModelSetup()
}
// TEMPORARY: We are checking .model === string for backwards compatibility.
@@ -193,6 +193,20 @@ export function createStore(project: projects, client?: SupabaseClient<Database>
state.envs = envs
}),
setModel: (model) => set(state => {
+ const modelSetupIdx = state.modelSetups.findIndex(p =>
+ p.templateID === defaultTemplateID &&
+ p.provider === model.provider &&
+ p.modelName === model.name,
+ )
+
+ if (modelSetupIdx === -1) {
+ state.modelSetups.push({
+ provider: model.provider,
+ templateID: defaultTemplateID,
+ prompt: defaultPromptTemplate,
+ modelName: model.name,
+ })
+ }
state.model = model
}),
changeEnv: (pair, idx) => set(state => {