You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add GenUIWorkflow for generating UI components from workflow events (#549)
* feat: add GenUIWorkflow for generating UI components from workflow events
* feat: enhance GenUIWorkflow to support event handling and UI generation
* add cache, split code
* use gemini model
* refactor: update GenUIWorkflow to use Anthropic model and add pre-run checks for API key and package installation
* feat: introduce PlanningEvent and enhance GenUIWorkflow for improved UI planning and aggregation function generation
* feat: add gen ui to llamaindexserver
* refactor: remove unused gen_ui.py file
* simplify
* update for tailwindcss
* simplify code and add document
* refine text
* feat: add UIEvent model and update exports in server module
* use default UIEvent
* fix wrong model, update template
* add missing doc
* fix linting
* revert change on template
* fix mypy
* disable e2e for the change from llama-index-server
* remove unused script entry from pyproject.toml and refine UI notice text in GenUIWorkflow
* update workflow, bump chat ui
* Refine GenUIWorkflow documentation and improve code structure notes; add llm parameter to generate_ui_for_workflow function.
Copy file name to clipboardExpand all lines: llama-index-server/docs/custom_ui_component.md
+51-17Lines changed: 51 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -14,27 +14,33 @@ Custom UI components are a powerful feature that enables you to:
14
14
15
15
### Workflow events
16
16
17
-
Your workflow must emit events that fit this structure, allowing the LlamaIndex server to display the right UI components based on the event type.
18
-
19
-
```json
20
-
{
21
-
"type": "<event_name>",
22
-
"data": <data model>
23
-
}
24
-
```
25
-
26
-
In Pydantic, this is equivalent to:
17
+
To display custom UI components, your workflow needs to emit `UIEvent` events with data that conforms to the data model of your custom UI component.
27
18
28
19
```python
29
-
from pydantic import BaseModel
20
+
from llama_index.server import UIEvent
21
+
from pydantic import BaseModel, Field
30
22
from typing import Literal, Any
31
23
32
-
classMyCustomEvent(BaseModel):
33
-
type: Literal["<my_custom_event_name>"]
34
-
data: dict| Any
35
-
36
-
defto_response(self):
37
-
returnself.model_dump()
24
+
# Define a Pydantic model for your event data
25
+
classDeepResearchEventData(BaseModel):
26
+
id: str= Field(description="The unique identifier for the event")
27
+
type: Literal["retrieval", "analysis"] = Field(description="DeepResearch has two main stages: retrieval and analysis")
28
+
status: Literal["pending", "completed", "failed"] = Field(description="The current status of the event")
29
+
content: str= Field(description="The textual content of the event")
30
+
31
+
32
+
# In your workflow, emit the data model with UIEvent
33
+
ctx.write_event_to_stream(
34
+
UIEvent(
35
+
type="deep_research_event",
36
+
data=DeepResearchEventData(
37
+
id="123",
38
+
type="retrieval",
39
+
status="pending",
40
+
content="Retrieving data...",
41
+
),
42
+
)
43
+
)
38
44
```
39
45
40
46
### Server Setup
@@ -67,3 +73,31 @@ server = LlamaIndexServer(
67
73
);
68
74
}
69
75
```
76
+
77
+
### Generate UI Component
78
+
79
+
We provide a `generate_ui_component` function that uses LLMs to automatically generate UI components for your workflow events.
80
+
81
+
> **_Note:_** This feature requires the `ANTHROPIC_API_KEY` to be set in your environment.
82
+
83
+
```python
84
+
from llama_index.server.gen_ui.main import generate_ui_component
85
+
86
+
# Generate a component using the event class you defined in your workflow
87
+
from your_workflow import DeepResearchEvent
88
+
ui_code =await generate_ui_component(
89
+
event_cls=DeepResearchEvent,
90
+
)
91
+
92
+
# Alternatively, generate from your workflow file
93
+
ui_code =await generate_ui_component(
94
+
workflow_file="your_workflow.py",
95
+
)
96
+
print(ui_code)
97
+
98
+
# Save the generated code to a file for use in your project
99
+
withopen("deep_research_event.jsx", "w") as f:
100
+
f.write(ui_code)
101
+
```
102
+
103
+
> **Tip:** For optimal results, add descriptive documentation to each field in your event data class. This helps the LLM better understand your data structure and generate more appropriate UI components.
0 commit comments