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) {
|
||||
this.model_name = model_name;
|
||||
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.apiKey = config.apiKey;
|
||||
}
|
||||
|
||||
async sendRequest(turns, systemMessage, stop_seq = '***') {
|
||||
let messages = [{ role: 'system', content: systemMessage }].concat(turns);
|
||||
const messages = [{ role: 'system', content: systemMessage }, ...turns];
|
||||
const pack = {
|
||||
model: this.model_name || 'qwen-plus',
|
||||
messages,
|
||||
stop: stop_seq,
|
||||
messages: this.model_name.includes('o1') ? strictFormat(messages) : messages,
|
||||
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 {
|
||||
console.log('Awaiting Qwen API response...');
|
||||
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.');
|
||||
const completion = await this.openai.chat.completions.create(pack);
|
||||
const choice = completion.choices[0];
|
||||
|
||||
if (choice.finish_reason === 'length') {
|
||||
console.log('Context length exceeded');
|
||||
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) {
|
||||
|
@ -53,18 +46,15 @@ export class Qwen {
|
|||
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.';
|
||||
}
|
||||
|
||||
const headers = {
|
||||
'Authorization': `Bearer ${this.apiKey}`,
|
||||
'Content-Type': 'application/json'
|
||||
};
|
||||
const data = {
|
||||
model: 'text-embedding-v2',
|
||||
input: {
|
||||
texts: [text]
|
||||
},
|
||||
parameters: {
|
||||
text_type: 'query'
|
||||
}
|
||||
input: { texts: [text] },
|
||||
parameters: { text_type: 'query' }
|
||||
};
|
||||
|
||||
try {
|
||||
|
@ -74,8 +64,10 @@ export class Qwen {
|
|||
body: JSON.stringify(data)
|
||||
});
|
||||
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;
|
||||
} catch (err) {
|
||||
|
|
Loading…
Add table
Reference in a new issue