-
Notifications
You must be signed in to change notification settings - Fork 184
feat: implement artifact tool in TS #328
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
Merged
Merged
Changes from 41 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
f98b0ae
feat: implement artifact tool in TS
thucpn cf09c6d
Create modern-cars-travel.md
thucpn 526c342
feat: add loading for iframe
thucpn 1b9e656
fix: typo
thucpn b3d6b2a
add license
thucpn 8e90fd2
Merge branch 'main' into feat/implement-artifact-tool-in-ts
thucpn f7ee7eb
fix: license
thucpn 017ca72
support python execution in code interpreter
thucpn 6ce4cb5
support display version artifact with side panel
thucpn d940311
auto open panel when having tool data
thucpn d9bf348
update prompt
thucpn 1650f17
enhance prompt and UI
thucpn e07c149
show code tab as default
thucpn e0fd6c6
add border for log
thucpn 2813c82
make image collapsible
thucpn 143deec
fix: rel noopener
thucpn 5d96576
remove collapsible from image
thucpn d643392
enhance UI
thucpn c5f00e4
fix: harden JSON parser
marcusschiesser aaeb0be
feat: enhance version and UI
thucpn 7dadfbd
remove versionId uuid
thucpn 8279832
fix scroll code panel and add animation
thucpn 2b2f41d
feat: use textarea for input
thucpn 93d8558
return error in artifact tool
thucpn f01c871
fix: responsive for small screen
thucpn 42886a4
fix: update e2e for textarea
thucpn c0848c0
Merge branch 'main' into feat/implement-artifact-tool-in-ts
marcusschiesser c82a7f4
changes:
marcusschiesser 12890a3
enhance UI UX, handling error, log
thucpn 91d56c9
feat: append latest code to user message
thucpn 5a98581
refactor: output, dom handling and action to trigger
thucpn 5b84589
define CodeArtifact type in sandbox url
thucpn 193fd8f
refactor UI code
thucpn 0f60dc8
rename function
thucpn 3eac490
add copy for runtime error
thucpn fb019e4
Merge branch 'main' into feat/implement-artifact-tool-in-ts
thucpn 28576ed
feat: add sandbox route for express
thucpn 961b220
fix: /sandbox instead of /chat/sandbox
thucpn 896da0d
fix: response json for express
thucpn 4eb982d
simplify chat route.ts
marcusschiesser 912fcdc
small fixes
marcusschiesser 6933cbb
remove dead code
marcusschiesser e5ec77a
fix express
marcusschiesser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"create-llama": patch | ||
--- | ||
|
||
feat: implement artifact tool in TS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
templates/components/engines/typescript/agent/tools/code-generator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import type { JSONSchemaType } from "ajv"; | ||
import { | ||
BaseTool, | ||
ChatMessage, | ||
JSONValue, | ||
Settings, | ||
ToolMetadata, | ||
} from "llamaindex"; | ||
|
||
// prompt based on https://github.com/e2b-dev/ai-artifacts | ||
const CODE_GENERATION_PROMPT = `You are a skilled software engineer. You do not make mistakes. Generate an artifact. You can install additional dependencies. You can use one of the following templates:\n | ||
|
||
1. code-interpreter-multilang: "Runs code as a Jupyter notebook cell. Strong data analysis angle. Can use complex visualisation to explain results.". File: script.py. Dependencies installed: python, jupyter, numpy, pandas, matplotlib, seaborn, plotly. Port: none. | ||
|
||
2. nextjs-developer: "A Next.js 13+ app that reloads automatically. Using the pages router.". File: pages/index.tsx. Dependencies installed: [email protected], typescript, @types/node, @types/react, @types/react-dom, postcss, tailwindcss, shadcn. Port: 3000. | ||
|
||
3. vue-developer: "A Vue.js 3+ app that reloads automatically. Only when asked specifically for a Vue app.". File: app.vue. Dependencies installed: vue@latest, [email protected], tailwindcss. Port: 3000. | ||
|
||
4. streamlit-developer: "A streamlit app that reloads automatically.". File: app.py. Dependencies installed: streamlit, pandas, numpy, matplotlib, request, seaborn, plotly. Port: 8501. | ||
|
||
5. gradio-developer: "A gradio app. Gradio Blocks/Interface should be called demo.". File: app.py. Dependencies installed: gradio, pandas, numpy, matplotlib, request, seaborn, plotly. Port: 7860. | ||
|
||
Provide detail information about the artifact you're about to generate in the following JSON format with the following keys: | ||
|
||
commentary: Describe what you're about to do and the steps you want to take for generating the artifact in great detail. | ||
template: Name of the template used to generate the artifact. | ||
title: Short title of the artifact. Max 3 words. | ||
description: Short description of the artifact. Max 1 sentence. | ||
additional_dependencies: Additional dependencies required by the artifact. Do not include dependencies that are already included in the template. | ||
has_additional_dependencies: Detect if additional dependencies that are not included in the template are required by the artifact. | ||
install_dependencies_command: Command to install additional dependencies required by the artifact. | ||
port: Port number used by the resulted artifact. Null when no ports are exposed. | ||
file_path: Relative path to the file, including the file name. | ||
code: Code generated by the artifact. Only runnable code is allowed. | ||
|
||
Make sure to use the correct syntax for the programming language you're using. Make sure to generate only one code file. If you need to use CSS, make sure to include the CSS in the code file using Tailwind CSS syntax. | ||
`; | ||
|
||
// detail information to execute code | ||
export type CodeArtifact = { | ||
commentary: string; | ||
template: string; | ||
title: string; | ||
description: string; | ||
additional_dependencies: string[]; | ||
has_additional_dependencies: boolean; | ||
install_dependencies_command: string; | ||
port: number | null; | ||
file_path: string; | ||
code: string; | ||
}; | ||
|
||
export type CodeGeneratorParameter = { | ||
requirement: string; | ||
oldCode?: string; | ||
}; | ||
|
||
export type CodeGeneratorToolParams = { | ||
metadata?: ToolMetadata<JSONSchemaType<CodeGeneratorParameter>>; | ||
}; | ||
|
||
const DEFAULT_META_DATA: ToolMetadata<JSONSchemaType<CodeGeneratorParameter>> = | ||
{ | ||
name: "artifact", | ||
description: `Generate a code artifact based on the input. Don't call this tool if the user has not asked for code generation. E.g. if the user asks to write a description or specification, don't call this tool.`, | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
requirement: { | ||
type: "string", | ||
description: "The description of the application you want to build.", | ||
}, | ||
oldCode: { | ||
type: "string", | ||
description: "The existing code to be modified", | ||
nullable: true, | ||
}, | ||
}, | ||
required: ["requirement"], | ||
}, | ||
}; | ||
|
||
export class CodeGeneratorTool implements BaseTool<CodeGeneratorParameter> { | ||
metadata: ToolMetadata<JSONSchemaType<CodeGeneratorParameter>>; | ||
|
||
constructor(params?: CodeGeneratorToolParams) { | ||
this.metadata = params?.metadata || DEFAULT_META_DATA; | ||
} | ||
|
||
async call(input: CodeGeneratorParameter) { | ||
thucpn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
const artifact = await this.generateArtifact( | ||
input.requirement, | ||
input.oldCode, | ||
); | ||
return artifact as JSONValue; | ||
} catch (error) { | ||
return { isError: true }; | ||
} | ||
thucpn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
marcusschiesser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Generate artifact (code, environment, dependencies, etc.) | ||
async generateArtifact( | ||
query: string, | ||
oldCode?: string, | ||
): Promise<CodeArtifact> { | ||
const userMessage = ` | ||
${query} | ||
${oldCode ? `The existing code is: \n\`\`\`${oldCode}\`\`\`` : ""} | ||
`; | ||
const messages: ChatMessage[] = [ | ||
{ role: "system", content: CODE_GENERATION_PROMPT }, | ||
{ role: "user", content: userMessage }, | ||
]; | ||
try { | ||
const response = await Settings.llm.chat({ messages }); | ||
const content = response.message.content.toString(); | ||
const jsonContent = content | ||
thucpn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.replace(/^```json\s*|\s*```$/g, "") | ||
.replace(/^`+|`+$/g, "") | ||
.trim(); | ||
const artifact = JSON.parse(jsonContent) as CodeArtifact; | ||
thucpn marked this conversation as resolved.
Show resolved
Hide resolved
marcusschiesser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return artifact; | ||
} catch (error) { | ||
console.log("Failed to generate artifact", error); | ||
thucpn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw error; | ||
} | ||
marcusschiesser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.