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: jac/support/jac-lang.org/docs/learn/tutorial/2_building-a-rag-chatbot.md
+45-23Lines changed: 45 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -1,37 +1,42 @@
1
1
# Building a RAG Chatbot with Jac Cloud and Streamlit (Part 2/3)
2
2
3
-
Now that we have a jac application served up, let's build a simple chatbot using Retrieval Augmented Generation (RAG) with Jac Cloudand Streamlit as our frontend interface.
3
+
Now that we have a jac application served up, In this section, we'll build a simple chatbot using **Retrieval Augmented Generation (RAG)** with **Jac Cloud** for the backend and **Streamlit** for the frontend interface.
4
4
5
5
### Preparation / Installation
6
6
7
-
There are a couple of additional dependenices we need here
7
+
Before we start, let's install a few additional dependencies. Run the following command in your terminal:
-`mtllm`: A toolkit for multi-task learning with language models.
14
+
-`jac-streamlit`: A plugin that integrates jaclang with Streamlit.
15
+
-`langchain`, `langchain_community`: Essential tools for handling the language generation logic in your chatbot.
16
+
-`chromadb`: A vector database to store and retrieve embeddings.
17
+
-`pypdf`: A Python library to handle PDF documents.
18
+
19
+
13
20
## Building a Streamlit Interface
14
21
15
22
Before we begin building out our chatbot, let's first build a simple GUI to interact with the chatbot. Streamlit offers several Chat elements, enabling you to build Graphical User Interfaces (GUIs) for conversational agents or chatbots. Leveraging session state along with these elements allows you to construct anything from a basic chatbot to a more advanced, ChatGPT-like experience using purely Python code.
16
23
17
24
Luckily for us, jaclang has a plugin for streamlit that allows us to build web applications with streamlit using jaclang. In this part of the tutorial, we will build a frontend for our conversational agent using streamlit. You can find more information about the `jac-streamlit` plugin [here](https://github.com/Jaseci-Labs/jaclang/blob/main/support/plugins/streamlit/README.md).
18
25
19
-
First, let's create a new file called`client.jac`. This file will contain the code for the frontend chat interface.
26
+
Begin by creating a file named`client.jac`. This file will define the frontend logic for interacting with users and displaying responses from the chatbot.
20
27
21
-
We start by importing the necessary modules in Jac:
28
+
First, import the necessary Python libraries that will handle the user interface (UI) and API communication:
22
29
23
-
-`streamlit` (for frontend UI components)
24
-
-`requests` (for making API calls)
25
30
26
31
```jac
27
32
import:py streamlit as st;
28
33
import:py requests;
29
34
```
30
35
31
-
-`streamlit` will handle the user interface (UI) of the chatbot.
32
-
-`requests` will handle API calls to our backend.
36
+
-`streamlit`: Handle the user interface (UI) of the chatbot.
37
+
-`requests`: Handle API calls to our backend.
33
38
34
-
Now let's define a function bootstrap_frontend, which accepts a token for authentication and builds the chat interface.
39
+
Now let's define a function `bootstrap_frontend`, which is the core of the user interface. It is responsible for rendering the welcome message, initializing the chat history, and interacting with the backend.
35
40
36
41
```jac
37
42
can bootstrap_frontend (token: str) {
@@ -44,25 +49,35 @@ can bootstrap_frontend (token: str) {
44
49
}
45
50
```
46
51
47
-
-`st.write()` adds a welcome message to the app.
48
-
-`st.session_state`is used to persist data across user interactions. Here, we're using it to store the chat history (`messages`).
52
+
-`st.write("Welcome to your Demo Agent!")`: This displays a welcome message on the page.
53
+
-`st.session_state`: Streamlit's `session_state`is used to persist the data (in this case, the chat history) across multiple interactions. Here, we initialize `st.session_state.messages` to store the messages between the user and the assistant.
49
54
50
-
Now, let's update the function such that when the page reloads or updates, the previous chat messages are reloaded from `st.session_state.messages`. Add the following to `bootstrap_frontend`
55
+
Next, we need to ensure that the chat history is reloaded when the page is refreshed or updated. The following code snippet does that by iterating through the stored messages and rendering them on the interface:
51
56
52
57
```jac
58
+
can bootstrap_frontend(token:str){
59
+
//
60
+
//
61
+
53
62
for message in st.session_state.messages {
54
63
with st.chat_message(message["role"]) {
55
64
st.markdown(message["content"]);
56
65
}
57
66
}
67
+
}
58
68
```
59
69
60
-
- This block loops through the stored messages in the session state.
61
-
- For each message, we use `st.chat_message()` to display the message by its role (either `"user"` or `"assistant"`).
70
+
-`for message in st.session_state.messages`: This loop goes through each message stored in `st.session_state.messages`.
71
+
-`st.chat_message(message["role"])`: Displays each message by its role (either `"user"` or `"assistant"`)
72
+
62
73
63
74
Next, let's capture user input using `st.chat_input()`. This is where users can type their message to the chatbot.
@@ -72,16 +87,23 @@ Next, let's capture user input using `st.chat_input()`. This is where users can
72
87
st.markdown(prompt);
73
88
}
74
89
}
90
+
}
75
91
```
76
92
77
-
-`st.chat_input()` waits for the user to type a message and submit it.
78
-
- Once the user submits a message, it's appended to the session state's message history and immediately displayed on the screen.
93
+
-`st.chat_input("What is up?")`: This waits for the user to type a message. The text typed by the user is stored in the variable `prompt`.
94
+
-`st.session_state.messages.append(...)`: The user’s message is added to the session's chat history. Each message is stored as a dictionary with two keys: "role" (indicating whether the message is from the user or the assistant) and "content" (the actual text).
95
+
-`with st.chat_message("user")`: The message is displayed in the chat box under the "user" role.
79
96
80
-
Now we handle the interaction with the backend server. After the user submits a message, the assistant responds. This involves sending the user's message to the backend, receiving a response from the backend and displaying it.
97
+
Now we handle the interaction with the backend server. After capturing the user's input, it will send to the backend, retrieve the assistant's response, and display it,
@@ -109,14 +131,14 @@ Add the following to `bootstrap_frontend`.
109
131
}
110
132
}
111
133
}
134
+
}
112
135
```
113
136
114
-
- The user's input (`prompt`) is sent to the backend using a POST request to the `/walker/interact` endpoint.
115
-
- The `interact` walker, as we created in the last chapter, just returns `Hello World!` for now. This will change as we build out our chatbot.
116
-
-`message` and `session_id` are not yet utilized at this point. They will come into play later in this chapter.
137
+
-`requests.post(...)`: This sends the user’s message (stored in `prompt`) to the backend using an HTTP POST request. The message is sent as JSON with a "message" key, and a "session_id" is also included, but `message` and `session_id` are not yet utilized at this point. They will come into play later in this chapter.
138
+
- The backend is the `interact` walker, which we created in the last chapter, it just returns `Hello World!` for now. This will change as we build out our chatbot.
117
139
- The response from the backend is then displayed using `st.write()`, and the assistant's message is stored in the session state.
118
140
119
-
Lastly, we'll define the entry point of `client.jac`. Think `main` function of a python program. We authenticates the user and retrieves the token needed for the `bootstrap_frontend` function.
141
+
Lastly, we'll define the entry point of our program. Think about the `main` function of a python program. Here we authenticates the user and retrieves the token needed for the `bootstrap_frontend` function. Add the following code block to the `client.jac`
120
142
121
143
```jac
122
144
with entry {
@@ -169,7 +191,7 @@ Now you can run the frontend using the following command:
169
191
jac streamlit client.jac
170
192
```
171
193
172
-
If your server is still running, you can chat with your assistant using the streamlit interface. The response will only be "Hello, world!" for now, but we will update it to be a fully working chatbot next.
194
+
If your server is still running, you can chat with your assistant using the streamlit interface which is opening in your browser. The response will only be "Hello, world!" for now, but we will update it to be a fully working chatbot next.
0 commit comments