Skip to content

Commit 317cc16

Browse files
committed
Add sample code
1 parent efa4656 commit 317cc16

File tree

4 files changed

+630
-0
lines changed

4 files changed

+630
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,35 @@
11
# WebLLM-Tools-Sample
22
Sample of using tools with WebLLM
3+
4+
```js
5+
const tools = [
6+
{
7+
type: "function",
8+
function: {
9+
name: "fetch_wikipedia_content",
10+
description: "Search Wikipedia and fetch the introduction of the most relevant article. "+
11+
"Always use this if the user is asking for something that is likely on wikipedia. "+
12+
"If the user has a typo in their search query, correct it before searching.",
13+
parameters: {
14+
type: "object",
15+
properties: {
16+
type: "object",
17+
properties: {
18+
search_query: {
19+
type: "string",
20+
description: "Search query for finding the Wikipedia article"
21+
}
22+
},
23+
},
24+
required: ["search_query"],
25+
},
26+
},
27+
},
28+
];
29+
```
30+
31+
Sample of using tool calls with Web LLM.
32+
Now sample supports only Qwen2.5-* LLM models
33+
34+
Main testing was with `Qwen2.5-3B-Instruct-q1416_1-MLC` https://huggingface.co/mlc-ai/Qwen2.5-3B-Instruct-q4f16_1-MLC
35+

src/index.css

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
body,
2+
html {
3+
font-family: Arial, sans-serif;
4+
padding: 10px 20px;
5+
}
6+
7+
.textarea {
8+
display: block;
9+
width: 100%;
10+
min-height: 30px;
11+
line-height: 20px;
12+
font-family: inherit;
13+
font-size: inherit;
14+
padding: 1px 6px;
15+
overflow: auto;
16+
}
17+
18+
.download-container {
19+
display: flex;
20+
justify-content: space-between;
21+
margin-bottom: 20px;
22+
}
23+
24+
#download-status {
25+
border: solid 1px black;
26+
box-shadow:
27+
0 10px 15px -3px rgba(0, 0, 0, 0.1),
28+
0 4px 6px -2px rgba(0, 0, 0, 0.05);
29+
padding: 10px;
30+
}
31+
32+
.chat-container {
33+
height: 600px;
34+
width: 100%;
35+
display: flex;
36+
flex-direction: column;
37+
}
38+
39+
.chat-box {
40+
overflow-y: scroll;
41+
background-color: #c3c3c3;
42+
// border: 1px solid #ccc;
43+
border: 2px solid black;
44+
padding: 5px;
45+
flex: 1 1;
46+
}
47+
48+
.chat-stats {
49+
background-color: #d3eceb;
50+
flex: 0 0;
51+
padding: 10px;
52+
font-size: 0.75rem;
53+
}
54+
55+
.message-container {
56+
width: 100%;
57+
display: flex;
58+
}
59+
60+
.message {
61+
padding: 10px;
62+
margin: 10px 0;
63+
border-radius: 10px;
64+
width: fit-content;
65+
}
66+
67+
68+
.message-container.assistant {
69+
justify-content: start;
70+
}
71+
72+
.message-container.user .message {
73+
background: #007bff;
74+
color: #fff;
75+
}
76+
77+
.message-container.assistant .message {
78+
background: #f1f0f0;
79+
color: #333;
80+
}
81+
82+
.chat-input-container {
83+
min-height: 40px;
84+
flex: 0 0;
85+
display: flex;
86+
}
87+
88+
#user-input {
89+
padding: 5px;
90+
border: 2px solid black;
91+
margin-right: 5px;
92+
}
93+
94+
button {
95+
width: 25%;
96+
padding: 10px;
97+
border: none;
98+
background-color: #007bff;
99+
color: white;
100+
cursor: pointer;
101+
}
102+
103+
button:disabled {
104+
background-color: lightgray;
105+
cursor: not-allowed;
106+
}
107+
108+
button:hover:not(:disabled) {
109+
background-color: #0056b3;
110+
}
111+
112+
.hidden {
113+
display: none;
114+
}
115+
116+
@scope (.markdown) {
117+
/* Code blocks */
118+
pre {
119+
margin: 0.5rem 0;
120+
white-space: break-spaces;
121+
}
122+
123+
code {
124+
padding: 0.2em 0.4em;
125+
border-radius: 4px;
126+
font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
127+
font-size: 0.9em;
128+
}
129+
130+
pre,
131+
code {
132+
background-color: #f2f2f2;
133+
}
134+
135+
@media (prefers-color-scheme: dark) {
136+
pre,
137+
code {
138+
background-color: #333;
139+
}
140+
}
141+
142+
pre:has(code) {
143+
padding: 1rem 0.5rem;
144+
}
145+
146+
pre > code {
147+
padding: 0;
148+
}
149+
150+
/* Headings */
151+
h1,
152+
h2,
153+
h3,
154+
h4,
155+
h5,
156+
h6 {
157+
font-weight: 600;
158+
line-height: 1.2;
159+
}
160+
161+
h1 {
162+
font-size: 2em;
163+
margin: 1rem 0;
164+
}
165+
166+
h2 {
167+
font-size: 1.5em;
168+
margin: 0.83rem 0;
169+
}
170+
171+
h3 {
172+
font-size: 1.25em;
173+
margin: 0.67rem 0;
174+
}
175+
176+
h4 {
177+
font-size: 1em;
178+
margin: 0.5rem 0;
179+
}
180+
181+
h5 {
182+
font-size: 0.875em;
183+
margin: 0.33rem 0;
184+
}
185+
186+
h6 {
187+
font-size: 0.75em;
188+
margin: 0.25rem 0;
189+
}
190+
191+
h1,
192+
h2,
193+
h3,
194+
h4,
195+
h5,
196+
h6:first-child {
197+
margin-top: 0;
198+
}
199+
200+
/* Unordered List */
201+
ul {
202+
list-style-type: disc;
203+
margin-left: 1.5rem;
204+
}
205+
206+
/* Ordered List */
207+
ol {
208+
list-style-type: decimal;
209+
margin-left: 1.5rem;
210+
}
211+
212+
/* List Items */
213+
li {
214+
margin: 0.25rem 0;
215+
}
216+
217+
p:not(:first-child) {
218+
margin-top: 0.75rem;
219+
}
220+
221+
p:not(:last-child) {
222+
margin-bottom: 0.75rem;
223+
}
224+
225+
ul > li {
226+
margin-left: 1rem;
227+
}
228+
229+
/* Table */
230+
table,
231+
th,
232+
td {
233+
border: 1px solid lightgray;
234+
padding: 0.25rem;
235+
}
236+
237+
@media (prefers-color-scheme: dark) {
238+
table,
239+
th,
240+
td {
241+
border: 1px solid #f2f2f2;
242+
}
243+
}
244+
}

src/index.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Simple Chatbot</title>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta charset="UTF-8" />
7+
<link rel="stylesheet" href="./index.css" />
8+
</head>
9+
10+
<body>
11+
<p>Step 1: Initialize WebLLM and Download Model</p>
12+
<div class="download-container">
13+
<select id="model-selection"></select>
14+
<button id="download">Download</button>
15+
</div>
16+
<p id="download-status" class="hidden"></p>
17+
18+
<p>Step 2: Chat</p>
19+
<div class="chat-container">
20+
<div id="chat-box" class="chat-box"></div>
21+
<div style="display:flex; width:100%; min-height:40px; margin-top:10px">
22+
<span id="user-input" class="textarea" role="textbox" contenteditable placeholder="Type a message..."></span>
23+
<button id="send" style="width:100px; height:40px" disabled>Send</button>
24+
</div>
25+
<div id="chat-stats" class="chat-stats hidden"></div>
26+
</div>
27+
28+
<script src="./index.js" type="module"></script>
29+
</body>
30+
</html>

0 commit comments

Comments
 (0)