From 64dd9a97752b3aab0f2be537cfe3bc7bf506b5ee Mon Sep 17 00:00:00 2001 From: "Ninot-Quyi[CN]" Date: Mon, 28 Oct 2024 13:50:39 +0800 Subject: [PATCH] New Model Support: Qwen --- src/models/qwen.js | 56 ++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/src/models/qwen.js b/src/models/qwen.js index 3346735..21e0030 100644 --- a/src/models/qwen.js +++ b/src/models/qwen.js @@ -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) {