Update kuzco-cli to allow choice of model on startup & include a timeout function
This commit is contained in:
parent
424a1b5ad1
commit
e5419b0108
|
@ -52,9 +52,27 @@ const askQuestion = (query) => {
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fetchWithTimeout = (url, options, timeout = 3000) => {
|
||||||
|
const timeoutPromise = new Promise((_, reject) => {
|
||||||
|
const id = setTimeout(() => {
|
||||||
|
clearTimeout(id);
|
||||||
|
reject(new Error('Request timed out'));
|
||||||
|
}, timeout);
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.race([
|
||||||
|
fetch(url, options),
|
||||||
|
timeoutPromise
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
let messages = [];
|
let messages = [];
|
||||||
|
|
||||||
|
console.log("Please choose a model: 1 for Mistral, 2 for Llama2");
|
||||||
|
const modelChoice = prompt('Enter your choice (1 or 2): ');
|
||||||
|
const model = modelChoice === '2' ? 'llama2' : 'mistral';
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const user_input = await askQuestion("User: ");
|
const user_input = await askQuestion("User: ");
|
||||||
if (user_input.toLowerCase() === 'exit') {
|
if (user_input.toLowerCase() === 'exit') {
|
||||||
|
@ -63,7 +81,7 @@ async function main() {
|
||||||
messages.push({ 'role': 'user', 'content': user_input + '\n' });
|
messages.push({ 'role': 'user', 'content': user_input + '\n' });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${BASE_URL}/chat/completions`, {
|
const response = await fetchWithTimeout(`${BASE_URL}/chat/completions`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${API_KEY}`,
|
'Authorization': `Bearer ${API_KEY}`,
|
||||||
|
@ -71,17 +89,16 @@ async function main() {
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
messages: messages,
|
messages: messages,
|
||||||
model: 'mistral',
|
model: model
|
||||||
stream: false
|
|
||||||
})
|
})
|
||||||
});
|
}, 15000);;
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
console.log(`\nKuzco (Mistral):\n\n${data.choices[0].message.content.trim()}\n`);
|
console.log(`\nKuzco (${model}):\n\n${data.choices[0].message.content.trim()}\n`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`An error occurred: ${error.message}`);
|
console.error(`An error occurred: ${error.message}`);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue