Skip to content

Commit 1f7e0e3

Browse files
authored
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.
1 parent 7997cde commit 1f7e0e3

File tree

10 files changed

+837
-259
lines changed

10 files changed

+837
-259
lines changed

.github/workflows/e2e.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ name: E2E Tests
22
on:
33
push:
44
branches: [main]
5+
paths-ignore:
6+
- "llama-index-server/**"
57
pull_request:
68
branches: [main]
9+
paths-ignore:
10+
- "llama-index-server/**"
711

812
env:
913
POETRY_VERSION: "1.6.1"

llama-index-server/docs/custom_ui_component.md

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,33 @@ Custom UI components are a powerful feature that enables you to:
1414

1515
### Workflow events
1616

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.
2718

2819
```python
29-
from pydantic import BaseModel
20+
from llama_index.server import UIEvent
21+
from pydantic import BaseModel, Field
3022
from typing import Literal, Any
3123

32-
class MyCustomEvent(BaseModel):
33-
type: Literal["<my_custom_event_name>"]
34-
data: dict | Any
35-
36-
def to_response(self):
37-
return self.model_dump()
24+
# Define a Pydantic model for your event data
25+
class DeepResearchEventData(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+
)
3844
```
3945

4046
### Server Setup
@@ -67,3 +73,31 @@ server = LlamaIndexServer(
6773
);
6874
}
6975
```
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+
with open("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.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .api.models import UIEvent
12
from .server import LlamaIndexServer, UIConfig
23

3-
__all__ = ["LlamaIndexServer", "UIConfig"]
4+
__all__ = ["LlamaIndexServer", "UIConfig", "UIEvent"]

llama-index-server/llama_index/server/api/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,14 @@ class ComponentDefinition(BaseModel):
140140
type: str
141141
code: str
142142
filename: str
143+
144+
145+
class UIEvent(Event):
146+
type: str
147+
data: BaseModel
148+
149+
def to_response(self) -> dict:
150+
return {
151+
"type": self.type,
152+
"data": self.data.model_dump(),
153+
}

llama-index-server/llama_index/server/chat_ui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import requests
77

8-
CHAT_UI_VERSION = "0.1.0"
8+
CHAT_UI_VERSION = "0.1.1"
99

1010

1111
def download_chat_ui(

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

Whitespace-only changes.

0 commit comments

Comments
 (0)