|
| 1 | +import type { JSONSchemaType } from "ajv"; |
| 2 | +import { |
| 3 | + BaseTool, |
| 4 | + ChatMessage, |
| 5 | + JSONValue, |
| 6 | + Settings, |
| 7 | + ToolMetadata, |
| 8 | +} from "llamaindex"; |
| 9 | + |
| 10 | +// prompt based on https://github.com/e2b-dev/ai-artifacts |
| 11 | +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 |
| 12 | +
|
| 13 | +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. |
| 14 | +
|
| 15 | +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. |
| 16 | +
|
| 17 | +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. |
| 18 | +
|
| 19 | +4. streamlit-developer: "A streamlit app that reloads automatically.". File: app.py. Dependencies installed: streamlit, pandas, numpy, matplotlib, request, seaborn, plotly. Port: 8501. |
| 20 | +
|
| 21 | +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. |
| 22 | +
|
| 23 | +Provide detail information about the artifact you're about to generate in the following JSON format with the following keys: |
| 24 | + |
| 25 | +commentary: Describe what you're about to do and the steps you want to take for generating the artifact in great detail. |
| 26 | +template: Name of the template used to generate the artifact. |
| 27 | +title: Short title of the artifact. Max 3 words. |
| 28 | +description: Short description of the artifact. Max 1 sentence. |
| 29 | +additional_dependencies: Additional dependencies required by the artifact. Do not include dependencies that are already included in the template. |
| 30 | +has_additional_dependencies: Detect if additional dependencies that are not included in the template are required by the artifact. |
| 31 | +install_dependencies_command: Command to install additional dependencies required by the artifact. |
| 32 | +port: Port number used by the resulted artifact. Null when no ports are exposed. |
| 33 | +file_path: Relative path to the file, including the file name. |
| 34 | +code: Code generated by the artifact. Only runnable code is allowed. |
| 35 | +
|
| 36 | +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. |
| 37 | +`; |
| 38 | + |
| 39 | +// detail information to execute code |
| 40 | +export type CodeArtifact = { |
| 41 | + commentary: string; |
| 42 | + template: string; |
| 43 | + title: string; |
| 44 | + description: string; |
| 45 | + additional_dependencies: string[]; |
| 46 | + has_additional_dependencies: boolean; |
| 47 | + install_dependencies_command: string; |
| 48 | + port: number | null; |
| 49 | + file_path: string; |
| 50 | + code: string; |
| 51 | +}; |
| 52 | + |
| 53 | +export type CodeGeneratorParameter = { |
| 54 | + requirement: string; |
| 55 | + oldCode?: string; |
| 56 | +}; |
| 57 | + |
| 58 | +export type CodeGeneratorToolParams = { |
| 59 | + metadata?: ToolMetadata<JSONSchemaType<CodeGeneratorParameter>>; |
| 60 | +}; |
| 61 | + |
| 62 | +const DEFAULT_META_DATA: ToolMetadata<JSONSchemaType<CodeGeneratorParameter>> = |
| 63 | + { |
| 64 | + name: "artifact", |
| 65 | + 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.`, |
| 66 | + parameters: { |
| 67 | + type: "object", |
| 68 | + properties: { |
| 69 | + requirement: { |
| 70 | + type: "string", |
| 71 | + description: "The description of the application you want to build.", |
| 72 | + }, |
| 73 | + oldCode: { |
| 74 | + type: "string", |
| 75 | + description: "The existing code to be modified", |
| 76 | + nullable: true, |
| 77 | + }, |
| 78 | + }, |
| 79 | + required: ["requirement"], |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | +export class CodeGeneratorTool implements BaseTool<CodeGeneratorParameter> { |
| 84 | + metadata: ToolMetadata<JSONSchemaType<CodeGeneratorParameter>>; |
| 85 | + |
| 86 | + constructor(params?: CodeGeneratorToolParams) { |
| 87 | + this.metadata = params?.metadata || DEFAULT_META_DATA; |
| 88 | + } |
| 89 | + |
| 90 | + async call(input: CodeGeneratorParameter) { |
| 91 | + try { |
| 92 | + const artifact = await this.generateArtifact( |
| 93 | + input.requirement, |
| 94 | + input.oldCode, |
| 95 | + ); |
| 96 | + return artifact as JSONValue; |
| 97 | + } catch (error) { |
| 98 | + return { isError: true }; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Generate artifact (code, environment, dependencies, etc.) |
| 103 | + async generateArtifact( |
| 104 | + query: string, |
| 105 | + oldCode?: string, |
| 106 | + ): Promise<CodeArtifact> { |
| 107 | + const userMessage = ` |
| 108 | + ${query} |
| 109 | + ${oldCode ? `The existing code is: \n\`\`\`${oldCode}\`\`\`` : ""} |
| 110 | + `; |
| 111 | + const messages: ChatMessage[] = [ |
| 112 | + { role: "system", content: CODE_GENERATION_PROMPT }, |
| 113 | + { role: "user", content: userMessage }, |
| 114 | + ]; |
| 115 | + try { |
| 116 | + const response = await Settings.llm.chat({ messages }); |
| 117 | + const content = response.message.content.toString(); |
| 118 | + const jsonContent = content |
| 119 | + .replace(/^```json\s*|\s*```$/g, "") |
| 120 | + .replace(/^`+|`+$/g, "") |
| 121 | + .trim(); |
| 122 | + const artifact = JSON.parse(jsonContent) as CodeArtifact; |
| 123 | + return artifact; |
| 124 | + } catch (error) { |
| 125 | + console.log("Failed to generate artifact", error); |
| 126 | + throw error; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments