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
Copy file name to clipboardExpand all lines: blog/2024-04-16-custom-components.md
-73Lines changed: 0 additions & 73 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,79 +65,6 @@ def zoom():
65
65
)
66
66
```
67
67
68
-
## High-Level Components
69
-
70
-
You can also create custom components that don't wrap React, but instead are built out of existing Reflex components. For example, you can define custom navigation bars, modals, or even entire pages as custom components.
71
-
72
-
In our [intro chatapp tutorial]({getting_started.chatapp_tutorial.path}) we share the code for creating a chat component in Reflex. Having the full code gives you full flexibility in how your chat component works and appears, but sometimes you just want to drop in a chat component and not worry about the details.
73
-
74
-
With custom components, we now have a [reflex-chat](https://github.com/picklelo/reflex-chat/) package that you can install with `pip` and use in your app.
75
-
76
-
```bash
77
-
pip install reflex-chat
78
-
```
79
-
80
-
You can then import the chat component into your app and use it like any other Reflex component. All the styling and logic is encapsulated, you only need to pass in the actual LLM logic.
81
-
82
-
```python exec
83
-
import reflex as rx
84
-
from openai import OpenAI
85
-
from reflex_chat import chat
86
-
87
-
try:
88
-
client = OpenAI()
89
-
except:
90
-
client =None
91
-
92
-
# Only define your logic, the chat component handles the rest.
93
-
asyncdefrun_llm(chat_state):
94
-
# Start a new session to answer the question.
95
-
session = client.chat.completions.create(
96
-
model="gpt-4o-mini",
97
-
messages=chat_state.get_messages(),
98
-
stream=True,
99
-
)
100
-
101
-
# Stream the results, yielding after every word.
102
-
for item in session:
103
-
ifhasattr(item.choices[0].delta, "content"):
104
-
answer_text = item.choices[0].delta.content
105
-
# Ensure answer_text is not None before concatenation
106
-
chat_state.append_to_response(answer_text)
107
-
yield
108
-
```
109
-
110
-
```python demo
111
-
rx.box(
112
-
chat(process=run_llm),
113
-
height="500px",
114
-
width="100%"
115
-
)
116
-
```
117
-
118
-
```md alert info
119
-
# Just set `process` prop of the chat component to connect to your LLM.
120
-
121
-
\```python
122
-
async def run_llm(chat_state):
123
-
# Start a new session to answer the question.
124
-
session = client.chat.completions.create(
125
-
model="gpt-4o-mini",
126
-
messages=chat_state.get_messages(),
127
-
stream=True,
128
-
)
129
-
130
-
# Stream the results, yielding after every word.
131
-
for item in session:
132
-
if hasattr(item.choices[0].delta, "content"):
133
-
answer_text = item.choices[0].delta.content
134
-
chat_state.append_to_response(answer_text)
135
-
yield
136
-
\```
137
-
```
138
-
139
-
Depending on how much control you want, you can either use a high level component directly or create your own component from scratch.
140
-
141
68
### Component State
142
69
143
70
A new feature we've added in 0.4.6 is the `rx.ComponentState` class. This class allows you to encapsulate state and UI for a component in a single class. In fact, this is what allows the `chat` component to work without having the user having to define a state - the component will dynamically create and handle its own state internally.
0 commit comments