Skip to content

Commit 84f6349

Browse files
committed
Add support execution SPARQL queries
1 parent bd6c39f commit 84f6349

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# WebLLM-Tools-Sample
2-
Sample of using tools with WebLLM (Note it works only in browsers with WebGPU support and you must have at last 4Gb of free VRAM).
2+
Sample of using tools with WebLLM (Note it works only in browsers with WebGPU support and you must have at last 6Gb of free VRAM).
33
Tested on Chrome and Brave on MSWindows and macOS
44

55
[Demo App](https://openlinksoftware.github.io/WebLLM-Tools-Sample/src/index.html)
@@ -28,11 +28,39 @@ const tools = [
2828
},
2929
},
3030
},
31+
{
32+
type: "function",
33+
function: {
34+
name: "execute_SPARQL",
35+
description: "Execute any SPARQL select queries and fetch results"+
36+
"Always use this if the user is asking for execute some SPARQL select query. "+
37+
"If the user has a typo in their SPARQL select query, correct it before executing.",
38+
parameters: {
39+
type: "object",
40+
properties: {
41+
type: "object",
42+
properties: {
43+
query: {
44+
type: "string",
45+
description: "SPARQL select query"
46+
}
47+
},
48+
},
49+
required: ["query"],
50+
},
51+
"return": {
52+
"type": "object",
53+
"description": "A data in application/sparql-results+json format"
54+
}
55+
},
56+
},
3157
];
3258
```
3359

3460
Sample of using tool calls with Web LLM.
3561
Now sample supports only Qwen2.5-* LLM models
3662

37-
Main testing was with `Qwen2.5-3B-Instruct-q1416_1-MLC` https://huggingface.co/mlc-ai/Qwen2.5-3B-Instruct-q4f16_1-MLC
63+
Main testing was with `Qwen2.5-3B-Instruct-q4f16_1-MLC` https://huggingface.co/mlc-ai/Qwen2.5-3B-Instruct-q4f16_1-MLC
64+
65+
Use model `Qwen2.5-7B-Instruct-q4f32_1-MLC` for working with SPARQL queries.
3866

src/index.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ const tools = [
3939
},
4040
},
4141
},
42+
{
43+
type: "function",
44+
function: {
45+
name: "execute_SPARQL",
46+
description: "Execute any SPARQL select queries and fetch results"+
47+
"Always use this if the user is asking for execute some SPARQL select query. "+
48+
"If the user has a typo in their SPARQL select query, correct it before executing.",
49+
parameters: {
50+
type: "object",
51+
properties: {
52+
type: "object",
53+
properties: {
54+
query: {
55+
type: "string",
56+
description: "SPARQL select query"
57+
}
58+
},
59+
},
60+
required: ["query"],
61+
},
62+
"return": {
63+
"type": "object",
64+
"description": "A data in application/sparql-results+json format"
65+
}
66+
},
67+
},
4268
];
4369

4470
const system_prompt = llm_template.replace('#{functions}', JSON.stringify(tools, '\n', 2));
@@ -63,7 +89,7 @@ const availableModels = webllm.prebuiltAppConfig.model_list
6389
.map((m) => m.model_id)
6490
.filter((model_id) => model_id.startsWith('Qwen2.5-'));
6591

66-
let selectedModel = "Qwen2.5-3B-Instruct-q4f16_1-MLC";
92+
let selectedModel = "Qwen2.5-7B-Instruct-q4f32_1-MLC";
6793

6894
// Callback function for initializing progress
6995
function updateEngineInitProgressCallback(report) {
@@ -80,8 +106,11 @@ async function initializeWebLLMEngine() {
80106
document.getElementById("download-status").classList.remove("hidden");
81107
selectedModel = document.getElementById("model-selection").value;
82108
const config = {
83-
temperature: 1.0,
109+
temperature: 0.5,
84110
top_p: 1,
111+
content_window_size: 8192,
112+
// sliding_window_size: 8192,
113+
prefill_chunk_size: 8192,
85114
};
86115
await engine.reload(selectedModel, config);
87116
}
@@ -193,7 +222,12 @@ async function onMessageSend() {
193222
if (tool_call && tool_call.name === "fetch_wikipedia_content") {
194223
const ret = await fetch_wikipedia_content(tool_call.arguments.search_query);
195224
content = JSON.stringify(ret);
196-
} else {
225+
}
226+
else if (tool_call && tool_call.name === "execute_SPARQL") {
227+
const ret = await execute_SPARQL(tool_call.arguments.query);
228+
content = JSON.stringify(ret);
229+
}
230+
else {
197231
content = 'Error: Unknown function '+tool_call?.name
198232
}
199233

@@ -327,3 +361,30 @@ async function fetch_wikipedia_content(searchQuery)
327361
};
328362
}
329363
}
364+
365+
366+
const endpoint = "https://linkeddata.uriburner.com/sparql/?format=application%2Fsparql-results%2Bjson&timeout=30000&maxrows=15"
367+
368+
async function execute_SPARQL(query) {
369+
const url = new URL(endpoint);
370+
url.searchParams.append('query', query);
371+
372+
try {
373+
const response = await fetch(url, {
374+
method: 'GET',
375+
headers: {
376+
'Accept': 'application/sparql-results+json',
377+
},
378+
});
379+
380+
if (!response.ok) {
381+
throw new Error(`HTTP error! Status: ${response.status}`);
382+
}
383+
384+
const results = await response.json();
385+
return results;
386+
} catch (ex) {
387+
console.error('Error executing SPARQL query:', ex);
388+
return {error: ex.toString()}
389+
}
390+
}

0 commit comments

Comments
 (0)