-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_function_chat.js
285 lines (245 loc) · 10.4 KB
/
create_function_chat.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//import { Ollama } from 'https://esm.run/ollama';
//const ollama = new Ollama();
//import ollama from 'ollama/browser'
import { Ollama } from 'ollama'
const ollama = new Ollama();
//const { default: ollama } = require('ollama');
//const ollama = new Ollama({ host: 'http://127.0.0.1:11434' })
let tools_global=[
{
'type': 'function',
'function': {
'name': 'create_function',
'description': 'Create function',
'parameters': {
'type': 'object',
'properties': {
'name': {
'type': 'string',
'description': 'Function name',
},
'description': {
'type': 'string',
'description': 'Function description',
},
},
'required': ['name', 'description'],
},
},
},
];
let created_functions_global=[];
let temperature_global = 0.0;
let seed_global = 0;
let stream_global = false;
let model_global = "";
let debug = true;
//let debug = false;
//num_ctx_global = 2048;
//num_ctx_global = 4096;
let num_ctx_global = 8192;
let messages_global = [];
let chat = true;
//chat = False;
//chat_in_create_function = True;
let chat_in_create_function = false;
async function create_function(data) {
// console.log('data: ', data);
let name = data['name'];
let description = data['description'];
let messages = [];
if(chat && chat_in_create_function){
messages = messages_global;
}
// client = client_global;
let model = model_global;
let _function = {
'name': 'function_name',
'description': 'function description',
'parameters': {
'type': 'object',
'properties': {
'first_parameter_name': {
'type': 'string',
'description': 'first parameter description',
},
'second_parameter_name': {
'type': 'string',
'description': 'second parameter description',
},
},
'required': ['first_parameter_name', 'second_parameter_name'],
},
};
messages.push({'role': 'user', 'content': 'Write javascript async function '+name+' which makes "'+description+'", gets parameters as named array and returns string type, \n then after end of javascript format block, in separate json format block show how to write which parameters was used to call that function if any, using example "'+JSON.stringify(_function)+'"'});
// messages.push({'role': 'user', 'content': 'Write javascript async function '+name+' which makes "'+description+'", not use "require", gets parameters as named array and returns string type, \n then after end of python format block, in separate json format block show how to write which parameters was used to call that function if any, using example "'+JSON.stringify(_function)+'"'});
// messages.push({'role': 'user', 'content': 'Write javascript, not typescript, async function '+name+' which makes "'+description+'", dets parameters as named array and returns string type, \n then after end of python format block, in separate json format block show how to write which parameters was used to call that function if any, using example "'+JSON.stringify(_function)+'"'});
// messages.push({'role': 'user', 'content': 'Write javascript async function '+name+' which makes "'+description+'" and returns string type, \n then after end of python format block, in separate json format block show how to write which parameters was used to call that function if any, using example "'+JSON.stringify(_function)+'"'});
// First API call: Send the query and function description to the model
let options = {'temperature': temperature_global, 'seed': seed_global, 'num_ctx': num_ctx_global};
let response = await ollama.chat({
model:model,
messages:messages,
// tools=tools_global,
options:options,
stream:stream_global,
});
// Add the model's response to the conversation history
messages.push(response['message']);
if(chat){
messages_global = messages_global + messages;
}
let response_message = response['message'];
if(debug){
console.log('response_message: ', response_message);
console.log("response_message['content']: ", response_message['content']);
}
let created_function = "";
let created_function_parameters = "";
for(let response_message_content_split of response_message['content'].split("```")){
let split_javascript = response_message_content_split.split("javascript\n");
if(((split_javascript.length)>1) && ((split_javascript[0].length)==0) && ((created_function.length)==0)){
created_function = split_javascript[1];
}
let split_json = response_message_content_split.split("json\n");
if(((split_json.length)>1) && ((split_json[0].length)==0) && ((created_function_parameters.length)==0)){
created_function_parameters = split_json[1];
}
}
if(debug){
console.log('created_function: ', created_function);
console.log('created_function_parameters: ', created_function_parameters);
}
let created_function_parameters_json = JSON.parse(created_function_parameters);
if(Array.isArray(created_function_parameters_json)){
created_function_parameters_json = created_function_parameters_json[0];
}
if(debug){
console.log('created_function_parameters_json: ', created_function_parameters_json);
}
if(true){
let created_function_cut = [];
let created_function_def_started = false;
for(let created_function_split of created_function.split("\n")){
if(created_function_split.substring(0,7) == 'import '){
created_function_cut.push(created_function_split);
} else if(created_function_split.substring(0,3) == '/**'){
created_function_cut.push(created_function_split);
} else if(created_function_split.substring(0,2) == ' *'){
created_function_cut.push(created_function_split);
} else if(created_function_split.substring(0,3) == ' */'){
created_function_cut.push(created_function_split);
} else if((created_function_split.length) == 0){
created_function_cut.push(created_function_split);
} else if(created_function_split.substring(0,15) == 'async function '){
created_function_def_started = true;
created_function_cut.push(created_function_split);
} else if((created_function_split.substring(0,1) == ' ') && created_function_def_started){
created_function_cut.push(created_function_split);
} else if((created_function_split.substring(0,1) == '}') && created_function_def_started){
created_function_cut.push(created_function_split);
} else if(created_function_def_started){
break;
}
}
created_function = created_function_cut.join('\n');
// eval(created_function);
tools_global.push(
{
'type': 'function',
'function': created_function_parameters_json,
});
}
created_functions_global.push(created_function);
return created_function;
}
async function run(model, message){
let messages = [];
if(chat){
messages = messages_global;
}
model_global = model;
// client = ollama.Client();
// client_global = ollama;
// console.log("messages: ", messages);
messages.push({'role': 'user', 'content': message});
// First API call: Send the query and function description to the model
let options = {'temperature': temperature_global, 'seed': seed_global, 'num_ctx': num_ctx_global};
// console.log("run() "+model+' '+messages+' '+tools_global+' '+options);
let response = await ollama.chat({
model:model,
messages:messages,
tools:tools_global,
options:options,
stream:stream_global,
});
// Add the model's response to the conversation history
messages.push(response['message'])
// Check if the model decided to use the provided function
if(! response['message']['tool_calls']){
console.log("The model didn't use the function. Its response was:");
console.log(response['message']['content']);
return;
}
// Process function calls made by the model
// console.log("response['message']: ", response['message']);
if(response['message']['tool_calls']){
for(let tool of response['message']['tool_calls']){
if(debug){
// console.log("tool: ", tool);
console.log("tool['function']['name']: ", tool['function']['name']);
console.log("tool['function']['arguments']: ", tool['function']['arguments']);
}
let data = tool['function']['arguments'];
let function_response = await eval(tool['function']['name'])(data);
if(tool['function']['name'] == 'create_function'){
// eval(function_response);
eval.call(null,function_response);
}
if(debug){
console.log("function_response: ", function_response);
}
// Add function response to the conversation
messages.push({'role': 'tool','content': function_response,});
}
}
// Second API call: Get final response from the model
let final_response = await ollama.chat({model:model, messages:messages, stream:stream_global});
console.log(final_response['message']['content']);
if(chat){
messages_global = messages;
// messages_global = messages_global + messages;
}
// console.log("run() end");
}
var args = process.argv.slice(2);
//import sys
if(debug){
console.log ('argument list:', args);
}
import * as readline from 'node:readline/promises'; // This uses the promise-based APIs
import { stdin as input, stdout as output } from 'node:process';
//const rl = readline.createInterface({ input, output });
//const answer = await rl.question('What do you think of Node.js? ');
//console.log(`Thank you for your valuable feedback: ${answer}`);
//const readline = require('readline-sync');
let argument_count = 0;
let message = "";
while(true){
// console.log("args.length: "+args.length);
if(argument_count < (args.length)){
message = args[argument_count];
console.log(">>>", message);
argument_count = argument_count + 1;
} else {
const rl = readline.createInterface({ input, output });
message = await rl.question(">>> ");
rl.close();
}
// console.log("message.length: "+message.length);
if(message == "/exit"){
break;
} else if((message.length) > 0){
await run('mistral-nemo:12b-instruct-2407-q6_K', message);
}
}