mindcraft/src/models/qwen.js

87 lines
3 KiB
JavaScript
Raw Normal View History

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