Skip to content

Commit 60e5d5f

Browse files
committed
docs: add website to java path
1 parent 118023a commit 60e5d5f

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

docs/_workshop-java-quarkus.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ include::sections/java-quarkus/06-chat-api.md[]
3030

3131
---
3232

33+
include::sections/java-quarkus/08-website.md[]
34+
35+
---
36+
3337
include::sections/09-azure.md[]
3438

3539
---
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<div class="info" data-title="Skip notice">
2+
3+
> If you want to skip the Chat website implementation and jump directly to the next section, run this command in the terminal **at the root of the project** to get the completed code directly:
4+
> ```bash
5+
> curl -fsSL https://github.com/Azure-Samples/azure-openai-rag-workshop/releases/download/latest/frontend.tar.gz | tar -xvz
6+
> ```
7+
8+
<div>
9+
10+
## Chat website
11+
12+
Now that we have our Chat API, it's time to complete the website that will use it.
13+
14+
### Introducing Vite and Lit
15+
16+
We'll use [Vite](https://vitejs.dev/) as a frontend build tool, and [Lit](https://lit.dev/) as a Web components library.
17+
18+
This frontend will be built as a Single Page Application (SPA), which will be similar to the well-known ChatGPT website. The main difference is that it will get its data from the Chat API that we described in the previous section.
19+
20+
The project is available in the `src/frontend` folder. From the project directory, you can run this command to start the development server:
21+
22+
```bash
23+
npm run dev
24+
```
25+
26+
This will start the application in development mode. Open [http://localhost:8000](http://localhost:8000) to view it in the browser.
27+
28+
<div class="tip" data-title="Tip">
29+
30+
> In development mode, the Web page will automatically reload when you make any change to the code. We recommend you to keep this command running in the background, and then have two windows side-by-side: one with your IDE where you will edit the code, and one with your Web browser where you can see the final result.
31+
32+
</div>
33+
34+
### The chat Web component
35+
36+
We already built a chat Web component for you, so you can focus on connecting the chat API. The nice thing about Web components is that they are just HTML elements, so you can use them in any framework, or even without a framework, just like we do in this workshop.
37+
38+
As a result, you can re-use this component in your own projects, and customize it if needed.
39+
40+
The component is located in the `src/frontend/src/components/chat.ts` file, if you're curious about how it works.
41+
42+
If you want to customize the component, you can do it by editing the `src/frontend/src/components/chat.ts` file. The various HTML rendering methods are called `renderXxx`, for example here's the `renderLoader` method that is used to display the spinner while the answer is loading:
43+
44+
```ts
45+
protected renderLoader = () => {
46+
return this.isLoading && !this.isStreaming
47+
? html`
48+
<div class="message assistant loader">
49+
<div class="message-body">
50+
<slot name="loader"><div class="loader-animation"></div></slot>
51+
<div class="message-role">${this.options.strings.assistant}</div>
52+
</div>
53+
</div>
54+
`
55+
: nothing;
56+
};
57+
```
58+
59+
### Calling the chat API
60+
61+
Now we need to call the chat API we created earlier. For this, we need to edit the `src/frontend/src/api.ts` file and complete the code where the `TODO` comment is:
62+
63+
```ts
64+
// TODO: complete call to Chat API here
65+
// const response =
66+
```
67+
68+
Here you can use the [Fetch Web API](https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch) to call your chat API. The URL of the API is already available in the `apiUrl` property.
69+
70+
In the body of the request, you should pass a JSON string containing the messages located in the `options.messages` property.
71+
72+
Now it's your turn to complete the code! 🙂
73+
74+
<details>
75+
<summary>Click here to see an example solution</summary>
76+
77+
```ts
78+
const response = await fetch(`${apiUrl}/chat`, {
79+
method: 'POST',
80+
headers: {'Content-Type': 'application/json' },
81+
body: JSON.stringify({
82+
messages: options.messages,
83+
}),
84+
});
85+
```
86+
87+
</details>
88+
89+
This method will be called from the Web component, in the `onSendClicked` method.
90+
91+
### Testing the completed website
92+
93+
Once you completed the code, you also need to run the backend to be able to test the application. Keep your frontend server running, and make sure that your Qdrant database and chat backend are running.
94+
95+
Run this command from the project root if you need to restart the backend services:
96+
97+
```bash
98+
docker compose up qdrant
99+
cd src/backend
100+
./mvnw quarkus:dev
101+
```
102+
103+
Now go back to your browser, and send a question to the chatbot. You should see the answer appear in the chat window.
104+
105+
![Screenshot of the chatbot answer](./assets/chatbot-answer.png)

0 commit comments

Comments
 (0)