1
- import os
2
1
import re
3
2
from typing import Any , Dict , List , Optional , Type
4
3
@@ -81,8 +80,10 @@ class GenUIWorkflow(Workflow):
81
80
82
81
code_structure : str = """
83
82
```jsx
84
- // Note: Only shadcn/ui (@/components/ui/<component_name>) and lucide-react and tailwind css are allowed (but cn() is not supported yet).
85
- e.g: import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
83
+ // Note: Only shadcn/ui and lucide-react and tailwind css are allowed (but cn() is not supported yet).
84
+ // shadcn import pattern: import { ComponentName } from "@/components/ui/<component_path>";
85
+ // e.g: import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
86
+ // import { Button } from "@/components/ui/button";
86
87
87
88
// export the component
88
89
export default function Component({ events }) {
@@ -315,7 +316,12 @@ async def refine_code(
315
316
{code_structure}
316
317
317
318
# Requirements:
318
- - Refine the code if needed to ensure there are no potential bugs. Be careful on code placement, make sure it doesn't call any undefined code.
319
+ - Refine the code if needed to ensure there are no potential bugs.
320
+ - Be careful on code placement, make sure it doesn't call any undefined code.
321
+ - Make sure the import statements are correct.
322
+ e.g: import { Button, Card, Accordion } from "@/components/ui" is correct because Button, Card are defined in different shadcn/ui components.
323
+ -> correction: import { Button } from "@/components/ui/button";
324
+ import { Card } from "@/components/ui/card";
319
325
- Don't be verbose, only return the code, wrap it in ```jsx <code>```
320
326
"""
321
327
prompt = PromptTemplate (prompt_template ).format (
@@ -344,22 +350,10 @@ async def refine_code(
344
350
)
345
351
346
352
347
- def pre_run_checks () -> None :
348
- if not os .getenv ("ANTHROPIC_API_KEY" ):
349
- raise ValueError (
350
- "Anthropic API key is not set. Please set the ANTHROPIC_API_KEY environment variable."
351
- )
352
- try :
353
- from llama_index .llms .anthropic import Anthropic # noqa: F401
354
- except ImportError :
355
- raise ValueError (
356
- "Anthropic package is not installed. Please install it with `poetry add llama-index-llms-anthropic` or `pip install llama-index-llms-anthropic`."
357
- )
358
-
359
-
360
353
async def generate_ui_for_workflow (
361
354
workflow_file : Optional [str ] = None ,
362
355
event_cls : Optional [Type [BaseModel ]] = None ,
356
+ llm : Optional [LLM ] = None ,
363
357
) -> str :
364
358
"""
365
359
Generate UI component for events from workflow.
@@ -368,6 +362,11 @@ async def generate_ui_for_workflow(
368
362
Args:
369
363
workflow_file: The path to the workflow file to generate UI from. e.g: `app/workflow.py`.
370
364
event_cls: A Pydantic class to generate UI for. e.g: `DeepResearchEvent`.
365
+ llm: The LLM to use for the generation. Default is Anthropic's Claude 3.7 Sonnet.
366
+ We recommend using these LLMs:
367
+ - Anthropic's Claude 3.7 Sonnet
368
+ - OpenAI's GPT-4.1
369
+ - Google Gemini 2.5 Pro
371
370
Returns:
372
371
The generated UI component code.
373
372
"""
@@ -379,8 +378,10 @@ async def generate_ui_for_workflow(
379
378
raise ValueError (
380
379
"Only one of workflow_file or event_cls can be provided. Please provide only one of them."
381
380
)
381
+ if llm is None :
382
+ from llama_index .llms .anthropic import Anthropic
382
383
383
- from llama_index . llms . anthropic import Anthropic
384
+ llm = Anthropic ( model = "claude-3-7-sonnet-latest" , max_tokens = 8192 )
384
385
385
386
console = Console ()
386
387
@@ -407,7 +408,7 @@ async def generate_ui_for_workflow(
407
408
408
409
# Generate UI component from event schemas
409
410
console .rule ("[bold blue]Generate UI Components[/bold blue]" )
410
- llm = Anthropic ( model = "claude-3-7-sonnet-latest" , max_tokens = 8192 )
411
+
411
412
workflow = GenUIWorkflow (llm = llm , timeout = 500.0 )
412
413
code = await workflow .run (events = event_schemas )
413
414
0 commit comments