mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-26 17:03:45 +02:00
Add mercury.js
This commit is contained in:
parent
d1bb34446f
commit
a100486896
1 changed files with 92 additions and 0 deletions
92
src/models/mercury.js
Normal file
92
src/models/mercury.js
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
import OpenAIApi from 'openai';
|
||||||
|
import { getKey, hasKey } from '../utils/keys.js';
|
||||||
|
import { strictFormat } from '../utils/text.js';
|
||||||
|
|
||||||
|
export class Mercury {
|
||||||
|
constructor(model_name, url, params) {
|
||||||
|
this.model_name = model_name;
|
||||||
|
this.params = params;
|
||||||
|
let config = {};
|
||||||
|
if (url)
|
||||||
|
config.baseURL = url;
|
||||||
|
|
||||||
|
config.apiKey = getKey('MERCURY_API_KEY');
|
||||||
|
|
||||||
|
this.openai = new OpenAIApi(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendRequest(turns, systemMessage, stop_seq='***') {
|
||||||
|
if (typeof stop_seq === 'string') {
|
||||||
|
stop_seq = [stop_seq];
|
||||||
|
} else if (!Array.isArray(stop_seq)) { // 处理其他非字符串非数组的无效输入
|
||||||
|
stop_seq = []; // 或抛出错误,或其他处理方式
|
||||||
|
}
|
||||||
|
let messages = [{'role': 'system', 'content': systemMessage}].concat(turns);
|
||||||
|
messages = strictFormat(messages);
|
||||||
|
const pack = {
|
||||||
|
model: this.model_name || "mercury-coder-small",
|
||||||
|
messages,
|
||||||
|
stop: stop_seq,
|
||||||
|
...(this.params || {})
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
let res = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log('Awaiting mercury api response from model', this.model_name)
|
||||||
|
// console.log('Messages:', messages);
|
||||||
|
let completion = await this.openai.chat.completions.create(pack);
|
||||||
|
if (completion.choices[0].finish_reason == 'length')
|
||||||
|
throw new Error('Context length exceeded');
|
||||||
|
console.log('Received.')
|
||||||
|
res = completion.choices[0].message.content;
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
if ((err.message == 'Context length exceeded' || err.code == 'context_length_exceeded') && turns.length > 1) {
|
||||||
|
console.log('Context length exceeded, trying again with shorter context.');
|
||||||
|
return await this.sendRequest(turns.slice(1), systemMessage, stop_seq);
|
||||||
|
} else if (err.message.includes('image_url')) {
|
||||||
|
console.log(err);
|
||||||
|
res = 'Vision is only supported by certain models.';
|
||||||
|
} else {
|
||||||
|
console.log(err);
|
||||||
|
res = 'My brain disconnected, try again.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendVisionRequest(messages, systemMessage, imageBuffer) {
|
||||||
|
const imageMessages = [...messages];
|
||||||
|
imageMessages.push({
|
||||||
|
role: "user",
|
||||||
|
content: [
|
||||||
|
{ type: "text", text: systemMessage },
|
||||||
|
{
|
||||||
|
type: "image_url",
|
||||||
|
image_url: {
|
||||||
|
url: `data:image/jpeg;base64,${imageBuffer.toString('base64')}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.sendRequest(imageMessages, systemMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
async embed(text) {
|
||||||
|
if (text.length > 8191)
|
||||||
|
text = text.slice(0, 8191);
|
||||||
|
const embedding = await this.openai.embeddings.create({
|
||||||
|
model: this.model_name || "text-embedding-3-small",
|
||||||
|
input: text,
|
||||||
|
encoding_format: "float",
|
||||||
|
});
|
||||||
|
return embedding.data[0].embedding;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue