-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsimple-moa.js
191 lines (159 loc) · 5.01 KB
/
simple-moa.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
/**
* @file examples/simple-moa.js
* @description Example showing Mixture of Agents (MoA) concept to improve response quality. (https://www.together.ai/blog/together-moa)
*/
const { LLMInterface } = require('llm-interface');
const { simplePrompt } = require('../src/utils/defaults.js');
require('dotenv').config({ path: '../.env' });
// Setup your key and interface
LLMInterface.setApiKey({
huggingface: process.env.HUGGINGFACE_API_KEY,
groq: process.env.GROQ_API_KEY,
gemini: process.env.GEMINI_API_KEY,
});
// Function to get the proposer response
async function getProposerResponse() {
const proposerPrompt = `Given the prompt "${simplePrompt}" explain how you would respond, process wise. Show the process steps you could delegate while compressing the work into 3 steps, only include the brainstorming/research steps, not the answer.
Provide the response as a JSON object; before responding with the object make sure it is valid JSON. Compress the response to save space.
Follow this output format, only responding with the JSON object and nothing else:
[steps[{step}]`;
console.log('Prompt:');
console.log(`> ${proposerPrompt.replaceAll('\n\n', '\n>\n> ')}`);
console.log();
try {
const proposerResponse = await LLMInterface.sendMessage(
'gemini',
proposerPrompt,
{
max_tokens: 1024,
},
{ attemptJsonRepair: true, cacheTimeoutSeconds: 86400 },
);
console.log('Proposer Result:');
const jsonData = proposerResponse.results[1];
const stepsString = jsonData.map((step) => step.step).join('\n\n');
console.log(`> ${stepsString.replaceAll('\n\n', '\n>\n> ')}`);
return stepsString;
} catch (error) {
console.error('Error processing Proposer LLMInterface.sendMessage:', error);
return '';
}
}
// Function to process each MoA query
async function getMoaResponse(moaInterfaceName, stepsString) {
try {
console.log(`- Querying ${moaInterfaceName}.`);
const moaPrompt = `Given the prompt "${simplePrompt}"
${stepsString}
`;
const response = await LLMInterface.sendMessage(
moaInterfaceName,
moaPrompt,
{ max_tokens: 2048, model: 'small' },
{ cacheTimeoutSeconds: 86400 },
);
return response.results;
} catch (error) {
console.error(
`Error processing ${moaInterfaceName} LLMInterface.sendMessage:`,
error,
);
return null;
}
}
// Function to limit concurrency
async function limitConcurrency(tasks, limit) {
const executing = new Set();
const results = [];
for (const task of tasks) {
const p = task().then((result) => {
executing.delete(p);
results.push(result);
});
executing.add(p);
if (executing.size >= limit) {
await Promise.race(executing);
}
}
await Promise.all(executing);
return results;
}
// Function to get all MoA responses with concurrency limit
async function getMoaResponses(moas, stepsString, max_concurrent_moas) {
const moaTasks = moas.map(
(moaInterfaceName) => () => getMoaResponse(moaInterfaceName, stepsString),
);
const moaResponses = await limitConcurrency(moaTasks, max_concurrent_moas);
return moaResponses.filter((response) => response !== null);
}
// Function to get the aggregator response
async function getAggregatorResponse(moaResponses) {
const aggregatorPrompt = `Synthesize a single high quality answer for the prompt "${simplePrompt}" based on:
${moaResponses.join('\n\n')}`;
try {
const aggregatorResponse = await LLMInterface.sendMessage(
'gemini',
aggregatorPrompt,
{
model: 'small',
max_tokens: 1024,
},
{ cacheTimeoutSeconds: 86400 },
);
return aggregatorResponse.results;
} catch (error) {
console.error(
'Error processing Aggregator LLMInterface.sendMessage:',
error,
);
return '';
}
}
// Function to get the control response
async function getControlResponse() {
try {
const controlResponse = await LLMInterface.sendMessage(
'gemini',
simplePrompt,
{
model: 'large',
max_tokens: 1024,
},
{ cacheTimeoutSeconds: 86400 },
);
return controlResponse.results;
} catch (error) {
console.error('Error processing Control LLMInterface.sendMessage:', error);
return '';
}
}
/**
* Main exampleUsage() function.
*/
async function exampleUsage() {
console.log('Mixture of Agents (MoA):');
console.log();
const stepsString = await getProposerResponse();
if (!stepsString) {
return;
}
const moas = ['huggingface', 'groq', 'gemini'];
const max_concurrent_moas = 2;
const moaResponses = await getMoaResponses(
moas,
stepsString,
max_concurrent_moas,
);
console.log('MOA Result:');
const aggregatorResponse = await getAggregatorResponse(moaResponses);
if (aggregatorResponse) {
console.log(`> ${aggregatorResponse.replaceAll('\n\n', '\n>\n> ')}`);
}
console.log();
const controlResponse = await getControlResponse();
if (controlResponse) {
console.log('Control Result:');
console.log(controlResponse);
}
}
exampleUsage();