-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_call.ts
242 lines (229 loc) · 6.71 KB
/
model_call.ts
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
import Replicate from "replicate";
import chalk from "chalk";
import ModelConfig from "./config/models/main";
import config from "./config/main";
const replicate = new Replicate({
auth: process.env.REPLICATE_API_TOKEN,
});
interface Message {
role: "system" | "user" | "assistant";
content: string;
}
const runReplicateModel = async (
messages: Message[],
): { result: string; context: number[] } => {
const output = await replicate.run(
"antoinelyset/openhermes-2.5-mistral-7b:d7ccd25700fb11c1787c25b580ac8d715d2b677202fe54b77f9b4a1eb7d73e2b",
{
input: {
top_k: 50,
top_p: 0.9,
prompt: JSON.stringify(messages),
temperature: 0.34,
max_new_tokens: 1000,
},
},
);
const result = (Array.isArray(output) ? output.join("") : output) as string;
return { result, context: [] };
};
const chatML = (messages: Message[]): string => {
return messages.map((message) =>
`<|im_start|>${message.role}\n${message.content}<|im_end|>`
).join("\n") + "\n<|im_start|>assistant\n";
};
const runOllamaModel = async (messages: Message[], context?: number[]) => {
const prompt = chatML(messages);
// console.log(chalk.green(prompt));
//console.log(chalk.red(prompt));
// const systemPrompt = messages.find((message) => message.role === "system")
// ?.content;
// const prompt = messages.find((message) => message.role === "user")?.content;
const postData = {
model: "openhermes2.5-mistral",
prompt: prompt,
context: context,
stream: false,
options: {
num_predict: 2000,
temperature: 0.7,
},
};
const response = await fetch("http://localhost:11434/api/generate", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(postData),
});
let result: string;
const tmp = await response.json();
result = tmp.response;
return { result, context: tmp.context };
};
function extractCodeBlockContent(inputString: string): string {
const lines = inputString.split("\n");
const startLine = lines.findIndex((line) =>
line.includes("```ts") || line.includes("```typescript")
);
const endLinesMaybe = lines.slice(startLine + 1).findIndex((line) =>
line.includes("```")
);
const endLine = endLinesMaybe === -1 ? undefined : endLinesMaybe + startLine;
if (startLine === -1 || endLine === undefined) {
throw new Error("No code block found");
}
const codeBlock = lines.slice(startLine + 1, endLine).join("\n");
return codeBlock;
}
const removeUseless = (inputString: string): string => {
const useless = [
"import { fetch }",
"import { Agent }",
"export default {};",
];
let lines = inputString.split("\n");
for (const uselessLine of useless) {
lines = lines.filter((line) => !line.includes(uselessLine));
}
return lines.join("\n").trim();
};
const removeSurroundingCurlyBraces = (inputString: string): string => {
if (inputString.startsWith("{") && inputString.endsWith("}")) {
inputString = inputString.slice(1, -1);
}
return inputString;
};
const addMustHave = (inputString: string): string => {
const mustHaves = [["Agent(", "import Agent from"]];
for (const [condition, mustHave] of mustHaves) {
if (!inputString.includes(condition)) {
continue;
}
const lines = inputString.split("\n");
// check if mustHave is already there among the lines
const alreadyThere = lines.some((line) => line.includes(mustHave));
if (alreadyThere) {
return inputString;
}
// add mustHave at the beginning of the file
lines.unshift(mustHave);
inputString = lines.join("\n").trim();
}
return inputString;
};
// const getModelCall = (
// // instruction: string,
// // jsonSchema?: Record<string, any>,
// systemMessage: string,
// userMessage: string,
// verbose: boolean = false,
// ): () => Promise<any> => {
// if (verbose) {
// console.log(chalk.bgCyan("System message:"));
// console.log(systemMessage);
// console.log(chalk.bgBlue("User message:"));
// console.log(userMessage);
// }
// return async () => {
// let result;
// if (config.model.modelSource === "replicate") {
// result = await runReplicateModel(systemMessage, userMessage);
// } else if (config.model.modelSource === "ollama") {
// result = await runOllamaModel(systemMessage, userMessage);
// } else {
// throw new Error("Invalid model source");
// }
// result = removeUseless(result);
// result = addMustHave(result);
// try {
// const resultString = extractCodeBlockContent(result);
// return resultString;
// } catch (e) {
// return result;
// }
// };
// };
class Model {
systemMessage: string;
userMessage: string;
verbose: boolean;
messages: Message[];
context: number[] | undefined;
constructor(
systemMessage: string,
userMessage: string,
verbose: boolean = false,
) {
this.systemMessage = systemMessage;
this.userMessage = userMessage;
this.verbose = verbose;
this.messages = [];
this.context = undefined;
this.reset();
}
reset() {
this.messages = [
{
role: "system",
content: this.systemMessage,
},
{
role: "user",
content: this.userMessage,
},
];
}
addUserMessage(message: string) {
this.messages.push({
role: "user",
content: message,
});
}
addAssistantMessage(message: string) {
this.messages.push({
role: "assistant",
content: message,
});
}
async run() {
if (this.verbose) {
// console.log(chalk.bgCyan("System message:"));
// console.log(this.systemMessage);
// console.log(chalk.bgBlue("User message:"));
// console.log(this.userMessage);
for (const message of this.messages) {
console.log(chalk.bgBlue(message.role));
console.log(message.content);
}
}
let result;
let callResult;
if (config.model.modelSource === "replicate") {
callResult = await runReplicateModel(this.messages);
} else if (config.model.modelSource === "ollama") {
callResult = await runOllamaModel(
this.messages,
// this.context,
);
} else {
throw new Error("Invalid model source");
}
result = callResult.result;
// result = removeSurroundingCurlyBraces(result);
this.context = callResult.context;
result = removeUseless(result);
result = addMustHave(result);
let resultString = result;
if (result.includes("```ts") || result.includes("```typescript")) {
console.log(chalk.red("EXTRACTING"));
resultString = extractCodeBlockContent(result);
}
this.messages.push({
role: "assistant",
content: resultString,
});
return resultString;
}
}
export { Model };