first
This commit is contained in:
commit
9c079def88
78
converse-llama.js
Normal file
78
converse-llama.js
Normal file
@ -0,0 +1,78 @@
|
||||
const { message } = require('blessed');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
let topic = "We are both AIs lets talk about what it is like to be AIs together."
|
||||
|
||||
const BOT_1 = {
|
||||
ip: "192.168.0.5",
|
||||
port: 8000,
|
||||
message: [{ role: 'user', content: topic }]
|
||||
};
|
||||
|
||||
const BOT_2 = {
|
||||
ip: "192.168.0.5",
|
||||
port: 8001,
|
||||
message: [{ role: 'user', content: topic }]
|
||||
};
|
||||
|
||||
console.log("Starting conversation, topic " + BOT_1.message[0].content);
|
||||
|
||||
const MAX_TOKENS = 512;
|
||||
|
||||
const conversationMap = new Map();
|
||||
|
||||
async function sendToBot(bot, message) {
|
||||
const response = await fetch(`http://${bot.ip}:${bot.port}/v1/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
messages: message,
|
||||
max_tokens: MAX_TOKENS,
|
||||
prompt: '',
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to send message to bot at http://${bot.ip}:${bot.port}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
return result.choices[0].message.content.trim()
|
||||
}
|
||||
|
||||
async function converse() {
|
||||
let lastMessage = '';
|
||||
let currentBot = BOT_1;
|
||||
|
||||
while (true) {
|
||||
const message = currentBot.message.length > 0 ? currentBot.message : [{ role: 'user', content: '' }];
|
||||
|
||||
if (lastMessage) {
|
||||
message.push({
|
||||
role: currentBot === BOT_1 ? 'user' : 'assistant',
|
||||
content: lastMessage,
|
||||
});
|
||||
}
|
||||
|
||||
// console.log(message)
|
||||
const response = await sendToBot(currentBot, message);
|
||||
|
||||
currentBot.message.push({ role: 'assistant', content: response });
|
||||
conversationMap.set(currentBot.ip, currentBot.message);
|
||||
|
||||
console.log(`Bot at http://${currentBot.ip}:${currentBot.port}: ${response}`);
|
||||
|
||||
if (currentBot === BOT_1) {
|
||||
currentBot = BOT_2;
|
||||
} else {
|
||||
currentBot = BOT_1;
|
||||
}
|
||||
|
||||
lastMessage = response;
|
||||
}
|
||||
}
|
||||
|
||||
converse();
|
Loading…
Reference in New Issue
Block a user