2024-10-28 13:29:16 +08:00
|
|
|
import OpenAIApi from 'openai';
|
2024-10-28 13:41:20 +08:00
|
|
|
import { getKey } from '../utils/keys.js';
|
2024-10-28 13:29:16 +08:00
|
|
|
import { strictFormat } from '../utils/text.js';
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
this.openai = new OpenAIApi(config);
|
|
|
|
this.apiKey = config.apiKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendRequest(turns, systemMessage, stop_seq = '***') {
|
|
|
|
let messages = [{ role: 'system', content: systemMessage }].concat(turns);
|
|
|
|
const pack = {
|
|
|
|
model: this.model_name || 'qwen-plus',
|
|
|
|
messages,
|
|
|
|
stop: 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);
|
2024-10-28 13:41:20 +08:00
|
|
|
if (completion.choices[0].finish_reason === 'length')
|
2024-10-28 13:29:16 +08:00
|
|
|
throw new Error('Context length exceeded');
|
|
|
|
console.log('Received.');
|
|
|
|
res = completion.choices[0].message.content;
|
|
|
|
} catch (err) {
|
2024-10-28 13:41:20 +08:00
|
|
|
if ((err.message === 'Context length exceeded' || err.code === 'context_length_exceeded') && turns.length > 1) {
|
2024-10-28 13:29:16 +08:00
|
|
|
console.log('Context length exceeded, trying again with shorter context.');
|
|
|
|
return await this.sendRequest(turns.slice(1), systemMessage, stop_seq);
|
|
|
|
} else {
|
|
|
|
console.log(err);
|
|
|
|
res = 'My brain disconnected, try again.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
async embed(text) {
|
|
|
|
if (!text || typeof text !== '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.';
|
|
|
|
}
|
|
|
|
const headers = {
|
|
|
|
'Authorization': `Bearer ${this.apiKey}`,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
};
|
|
|
|
const data = {
|
|
|
|
model: 'text-embedding-v2',
|
|
|
|
input: {
|
|
|
|
texts: [text]
|
|
|
|
},
|
|
|
|
parameters: {
|
|
|
|
text_type: 'query'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2024-10-28 13:41:20 +08:00
|
|
|
const response = await fetch(this.url, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: headers,
|
|
|
|
body: JSON.stringify(data)
|
|
|
|
});
|
|
|
|
const responseData = await response.json();
|
|
|
|
if (!responseData || !responseData.output || !responseData.output.embeddings) {
|
2024-10-28 13:29:16 +08:00
|
|
|
throw new Error('Invalid response from embedding API');
|
|
|
|
}
|
2024-10-28 13:41:20 +08:00
|
|
|
return responseData.output.embeddings[0].embedding;
|
2024-10-28 13:29:16 +08:00
|
|
|
} catch (err) {
|
|
|
|
console.error('Error occurred:', err);
|
|
|
|
return 'An error occurred while processing your embedding request. Please try again.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|