Updated the usage of Qwen api

This commit is contained in:
Ninot-Quyi[CN] 2024-10-28 19:06:51 +08:00
parent 360db0cbcb
commit 0f926727f5
2 changed files with 37 additions and 29 deletions

View file

@ -3,7 +3,7 @@
"model": { "model": {
"api": "qwen", "api": "qwen",
"url": "https://dashscope.aliyuncs.com/compatible-mode/v1", "url": "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation",
"model": "qwen-max" "model": "qwen-max"
}, },

View file

@ -1,40 +1,44 @@
import OpenAIApi from 'openai';
import { getKey } from '../utils/keys.js'; import { getKey } from '../utils/keys.js';
import { strictFormat } from '../utils/text.js';
export class Qwen { 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 || 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
this.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 = '***') { async sendRequest(turns, systemMessage, stop_seq = '***') {
const messages = [{ role: 'system', content: systemMessage }, ...turns]; const data = {
const pack = {
model: this.model_name || 'qwen-plus', model: this.model_name || 'qwen-plus',
messages: this.model_name.includes('o1') ? strictFormat(messages) : messages, input: { messages: [{ role: 'system', content: systemMessage }, ...turns] },
stop: this.model_name.includes('o1') ? undefined : stop_seq, parameters: { result_format: 'message', stop: stop_seq },
};
const headers = {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
}; };
try { try {
console.log('Awaiting Qwen API response...'); console.log('Awaiting Qwen API response...');
const completion = await this.openai.chat.completions.create(pack); const response = await fetch(this.url, {
const choice = completion.choices[0]; method: 'POST',
headers,
body: JSON.stringify(data),
});
if (choice.finish_reason === 'length') { if (!response.ok) throw new Error(`Request failed with status ${response.status}: ${response.statusText}`);
const responseData = await response.json();
const choice = responseData?.output?.choices?.[0];
if (choice?.finish_reason === 'length') {
console.log('Context length exceeded'); console.log('Context length exceeded');
return await this.sendRequest(turns.slice(1), systemMessage, stop_seq); return this.sendRequest(turns.slice(1), systemMessage, stop_seq);
} }
console.log('Received.'); console.log('Received.');
return choice.message.content; return choice?.message?.content || 'No content received.';
} catch (err) { } catch (err) {
console.error('Error occurred:', err); console.error('Error occurred:', err);
return 'My brain disconnected, try again.'; return 'My brain disconnected, try again.';
@ -47,28 +51,32 @@ export class Qwen {
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 = {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
};
const data = { const data = {
model: 'text-embedding-v2', model: 'text-embedding-v2',
input: { texts: [text] }, input: { texts: [text] },
parameters: { text_type: 'query' } parameters: { text_type: 'query' },
};
const headers = {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
}; };
try { try {
const response = await fetch(this.url, { const response = await fetch(this.url, {
method: 'POST', method: 'POST',
headers: headers, headers,
body: JSON.stringify(data) body: JSON.stringify(data),
}); });
const responseData = await response.json();
if (!response.ok) throw new Error(`Request failed with status ${response.status}: ${response.statusText}`);
const responseData = await response.json();
if (!responseData?.output?.embeddings) { if (!responseData?.output?.embeddings) {
console.error('Invalid response from embedding API'); console.error('Invalid response from embedding API');
return 'An error occurred while processing your embedding request. Please try again.'; 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) {
console.error('Error occurred:', err); console.error('Error occurred:', err);