mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-09 00:35:33 +02:00
New Model Support: Qwen
This commit is contained in:
parent
379ce2d5e7
commit
64dd9a9775
1 changed files with 24 additions and 32 deletions
|
@ -6,46 +6,39 @@ export class Qwen {
|
||||||
constructor(model_name, url) {
|
constructor(model_name, url) {
|
||||||
this.model_name = model_name;
|
this.model_name = model_name;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
let config = {};
|
|
||||||
if (this.url)
|
|
||||||
config.baseURL = this.url;
|
|
||||||
|
|
||||||
config.apiKey = getKey('QWEN_API_KEY');
|
const config = {
|
||||||
|
baseURL: this.url,
|
||||||
|
apiKey: getKey('QWEN_API_KEY'),
|
||||||
|
};
|
||||||
|
|
||||||
this.openai = new OpenAIApi(config);
|
this.openai = new OpenAIApi(config);
|
||||||
this.apiKey = config.apiKey;
|
this.apiKey = config.apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendRequest(turns, systemMessage, stop_seq = '***') {
|
async sendRequest(turns, systemMessage, stop_seq = '***') {
|
||||||
let messages = [{ role: 'system', content: systemMessage }].concat(turns);
|
const messages = [{ role: 'system', content: systemMessage }, ...turns];
|
||||||
const pack = {
|
const pack = {
|
||||||
model: this.model_name || 'qwen-plus',
|
model: this.model_name || 'qwen-plus',
|
||||||
messages,
|
messages: this.model_name.includes('o1') ? strictFormat(messages) : messages,
|
||||||
stop: stop_seq,
|
stop: this.model_name.includes('o1') ? undefined : stop_seq,
|
||||||
};
|
};
|
||||||
if (this.model_name.includes('o1')) {
|
|
||||||
pack.messages = strictFormat(messages);
|
|
||||||
delete pack.stop;
|
|
||||||
}
|
|
||||||
|
|
||||||
let res = null;
|
|
||||||
try {
|
try {
|
||||||
console.log('Awaiting Qwen API response...');
|
console.log('Awaiting Qwen API response...');
|
||||||
let completion = await this.openai.chat.completions.create(pack);
|
const completion = await this.openai.chat.completions.create(pack);
|
||||||
if (completion.choices[0].finish_reason === 'length')
|
const choice = completion.choices[0];
|
||||||
throw new Error('Context length exceeded');
|
|
||||||
console.log('Received.');
|
if (choice.finish_reason === 'length') {
|
||||||
res = completion.choices[0].message.content;
|
console.log('Context length exceeded');
|
||||||
} 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);
|
return await this.sendRequest(turns.slice(1), systemMessage, stop_seq);
|
||||||
} else {
|
|
||||||
console.log(err);
|
|
||||||
res = 'My brain disconnected, try again.';
|
|
||||||
}
|
}
|
||||||
|
console.log('Received.');
|
||||||
|
return choice.message.content;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error occurred:', err);
|
||||||
|
return 'My brain disconnected, try again.';
|
||||||
}
|
}
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async embed(text) {
|
async embed(text) {
|
||||||
|
@ -53,18 +46,15 @@ export class Qwen {
|
||||||
console.error('Invalid input for embedding: text must be a non-empty string.');
|
console.error('Invalid input for embedding: text must be a non-empty string.');
|
||||||
return 'Invalid input for embedding: text must be a non-empty string.';
|
return 'Invalid input for embedding: text must be a non-empty string.';
|
||||||
}
|
}
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'Authorization': `Bearer ${this.apiKey}`,
|
'Authorization': `Bearer ${this.apiKey}`,
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
};
|
};
|
||||||
const data = {
|
const data = {
|
||||||
model: 'text-embedding-v2',
|
model: 'text-embedding-v2',
|
||||||
input: {
|
input: { texts: [text] },
|
||||||
texts: [text]
|
parameters: { text_type: 'query' }
|
||||||
},
|
|
||||||
parameters: {
|
|
||||||
text_type: 'query'
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -74,8 +64,10 @@ export class Qwen {
|
||||||
body: JSON.stringify(data)
|
body: JSON.stringify(data)
|
||||||
});
|
});
|
||||||
const responseData = await response.json();
|
const responseData = await response.json();
|
||||||
if (!responseData || !responseData.output || !responseData.output.embeddings) {
|
|
||||||
throw new Error('Invalid response from embedding API');
|
if (!responseData?.output?.embeddings) {
|
||||||
|
console.error('Invalid response from embedding API');
|
||||||
|
return 'An error occurred while processing your embedding request. Please try again.';
|
||||||
}
|
}
|
||||||
return responseData.output.embeddings[0].embedding;
|
return responseData.output.embeddings[0].embedding;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue