@@ -6,7 +6,7 @@ import DOMPurify from "https://unpkg.com/
[email protected] /dist/purify.es.mjs";
66const llm_template =
77 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.\n\n'
88+ '# Tools\n\n'
9- + 'You may call one or more functions to assist with the user query.\n\n'
9+ + 'You may call one or two functions to assist with the user query.\n\n'
1010+ 'You are provided with function signatures within <tools></tools> XML tags:\n'
1111+ '<tools>\n'
1212+ '#{functions}\n'
@@ -42,9 +42,9 @@ const tools = [
4242 {
4343 type : "function" ,
4444 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. " +
45+ name : "sparql_exec " ,
46+ description : "Execute a SPARQL select query and fetch results" +
47+ "Always use this if the user is asking for execute a SPARQL select query. " +
4848 "If the user has a typo in their SPARQL select query, correct it before executing." ,
4949 parameters : {
5050 type : "object" ,
@@ -106,7 +106,7 @@ async function initializeWebLLMEngine() {
106106 document . getElementById ( "download-status" ) . classList . remove ( "hidden" ) ;
107107 selectedModel = document . getElementById ( "model-selection" ) . value ;
108108 const config = {
109- temperature : 0.5 ,
109+ temperature : 1.0 ,
110110 top_p : 1 ,
111111 content_window_size : 8192 ,
112112// sliding_window_size: 8192,
@@ -149,6 +149,16 @@ async function streamingGenerating(messages, onUpdate, onFinish, onError) {
149149 console . log ( e ) ;
150150 }
151151 }
152+ else if ( finalMessage . startsWith ( "```json" ) ) {
153+ const tool_call = finalMessage . replace ( / ^ ` ` ` j s o n \n ? \s * / , "" ) . replace ( / \s * \n ? ` ` ` $ / , "" ) ;
154+ try {
155+ const func = JSON . parse ( tool_call ) ;
156+ onFinish ( "**func call:** " + tool_call , usage ) ;
157+ return { done : false , tool_call : func , tool_role :"user" } ; // Qwen2-Coder role=tool
158+ } catch ( e ) {
159+ console . log ( e ) ;
160+ }
161+ }
152162
153163 onFinish ( finalMessage , usage ) ;
154164 } catch ( err ) {
@@ -223,20 +233,21 @@ async function onMessageSend() {
223233 const ret = await fetch_wikipedia_content ( tool_call . arguments . search_query ) ;
224234 content = JSON . stringify ( ret ) ;
225235 }
226- else if ( tool_call && tool_call . name === "execute_SPARQL " ) {
227- const ret = await execute_SPARQL ( tool_call . arguments . query ) ;
236+ else if ( tool_call && tool_call . name === "sparql_exec " ) {
237+ const ret = await sparql_exec ( tool_call . arguments . query ) ;
228238 content = JSON . stringify ( ret ) ;
229239 }
230240 else {
231241 content = 'Error: Unknown function ' + tool_call ?. name
232242 }
233243
244+ const ret_data = `<tool_response>\n{name:${ tool_call . name } , content:${ content } }\n</tool_response>` ;
234245 messages . push ( {
235- content : `<tool_response> ${ content } </tool_response>` ,
246+ content : ret_data ,
236247 tool_call_id,
237248 role :tool_role } ) ;
238249 tool_call_id ++ ;
239- updateLastMessage ( "**func result:** " + content ) ;
250+ updateLastMessage ( "**func result:** " + ret_data ) ;
240251 }
241252// console.log(messages);
242253// console.log("------")
@@ -287,6 +298,7 @@ document.getElementById("download").addEventListener("click", async function ()
287298 }
288299 await initializeWebLLMEngine ( ) ;
289300 document . getElementById ( "send" ) . disabled = false ;
301+ document . getElementById ( "download" ) . disabled = true ;
290302} ) ;
291303document . getElementById ( "send" ) . addEventListener ( "click" , function ( ) {
292304 onMessageSend ( ) ;
@@ -365,7 +377,7 @@ async function fetch_wikipedia_content(searchQuery)
365377
366378const endpoint = "https://linkeddata.uriburner.com/sparql/?format=application%2Fsparql-results%2Bjson&timeout=30000&maxrows=15"
367379
368- async function execute_SPARQL ( query ) {
380+ async function sparql_exec ( query ) {
369381 const url = new URL ( endpoint ) ;
370382 url . searchParams . append ( 'query' , query ) ;
371383
0 commit comments