blob: c3a0778b5b58b300d5a51e7b399b76063eae999d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
import fs from 'fs';
import { ToolCall, ChatOptions, ChatMsg } from './api-defs.js';
export type Call = ToolCall;
let toolsList = JSON.parse( fs.readFileSync( "../data/modeltools.json", "utf8" ) );
export function getPromptStr( options: ChatOptions ) {
let toolsArr: any[] = [];
for( let tool of toolsList ) {
for( let [capability, val] of Object.entries( options.model.capabilities ) ) {
if( val && capability.toString().toLowerCase() == tool.function.name.toLowerCase() )
toolsArr.push( tool );
}
}
return JSON.stringify( toolsArr );
}
export function isToolStr( buf: string ) {
let trimmed = buf.replace( /\s+/g, '' ).toLowerCase();
for( let tool of toolsList ) {
let name = tool.function.name.toLowerCase();
let str = `{"name":"${name}"`;
let notMatched = false;
for( let i = 0; i < Math.min( trimmed.length, str.length ); ++i ) {
if( trimmed[i] != str[i] ) {
notMatched = true;
break;
}
}
if( !notMatched )
return true;
}
return false;
}
export function getCall( msg: ChatMsg ) : Call | null {
let firstBracket = msg.content.indexOf( "{" );
let lastBracket = msg.content.lastIndexOf( "}" );
if( firstBracket == -1 || lastBracket == -1 )
return null;
let toolCall = msg.content.substring( firstBracket, lastBracket + 1 );
let json: Call;
try {
json = JSON.parse( toolCall );
} catch( e ) {
return null;
}
for( let tool of toolsList ) {
if( tool.function.name.toLowerCase() == json.name.toLowerCase() ) {
return json;
}
}
return null;
}
|