Skip to content

Commit e0923ea

Browse files
committed
Refine GenUIWorkflow documentation and improve code structure notes; add llm parameter to generate_ui_for_workflow function.
1 parent 38249a1 commit e0923ea

File tree

1 file changed

+20
-19
lines changed
  • llama-index-server/llama_index/server/gen_ui

1 file changed

+20
-19
lines changed

llama-index-server/llama_index/server/gen_ui/main.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import re
32
from typing import Any, Dict, List, Optional, Type
43

@@ -81,8 +80,10 @@ class GenUIWorkflow(Workflow):
8180

8281
code_structure: str = """
8382
```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";
8687
8788
// export the component
8889
export default function Component({ events }) {
@@ -315,7 +316,12 @@ async def refine_code(
315316
{code_structure}
316317
317318
# 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";
319325
- Don't be verbose, only return the code, wrap it in ```jsx <code>```
320326
"""
321327
prompt = PromptTemplate(prompt_template).format(
@@ -344,22 +350,10 @@ async def refine_code(
344350
)
345351

346352

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-
360353
async def generate_ui_for_workflow(
361354
workflow_file: Optional[str] = None,
362355
event_cls: Optional[Type[BaseModel]] = None,
356+
llm: Optional[LLM] = None,
363357
) -> str:
364358
"""
365359
Generate UI component for events from workflow.
@@ -368,6 +362,11 @@ async def generate_ui_for_workflow(
368362
Args:
369363
workflow_file: The path to the workflow file to generate UI from. e.g: `app/workflow.py`.
370364
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
371370
Returns:
372371
The generated UI component code.
373372
"""
@@ -379,8 +378,10 @@ async def generate_ui_for_workflow(
379378
raise ValueError(
380379
"Only one of workflow_file or event_cls can be provided. Please provide only one of them."
381380
)
381+
if llm is None:
382+
from llama_index.llms.anthropic import Anthropic
382383

383-
from llama_index.llms.anthropic import Anthropic
384+
llm = Anthropic(model="claude-3-7-sonnet-latest", max_tokens=8192)
384385

385386
console = Console()
386387

@@ -407,7 +408,7 @@ async def generate_ui_for_workflow(
407408

408409
# Generate UI component from event schemas
409410
console.rule("[bold blue]Generate UI Components[/bold blue]")
410-
llm = Anthropic(model="claude-3-7-sonnet-latest", max_tokens=8192)
411+
411412
workflow = GenUIWorkflow(llm=llm, timeout=500.0)
412413
code = await workflow.run(events=event_schemas)
413414

0 commit comments

Comments
 (0)