mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-06-07 09:45:54 +02:00
Cleaning up the annotations
This commit is contained in:
parent
53e6097ecb
commit
7d4d79b035
1 changed files with 2 additions and 21 deletions
|
@ -1,26 +1,21 @@
|
||||||
// This code uses Dashscope and HTTP to ensure the latest support for the Qwen model.
|
// This code uses Dashscope and HTTP to ensure the latest support for the Qwen model.
|
||||||
// Qwen is also compatible with the OpenAI API format, and the base URL to be configured is: "https://dashscope.aliyuncs.com/compatible-mode/v1".
|
// Qwen is also compatible with the OpenAI API format;
|
||||||
|
|
||||||
https://dashscope.aliyuncs.com/compatible-mode/v1
|
|
||||||
|
|
||||||
import { getKey } from '../utils/keys.js';
|
import { getKey } from '../utils/keys.js';
|
||||||
|
|
||||||
export class Qwen {
|
export class Qwen {
|
||||||
constructor(modelName, url) {
|
constructor(modelName, url) {
|
||||||
// Initialize model name and API URL
|
|
||||||
this.modelName = modelName;
|
this.modelName = modelName;
|
||||||
this.url = url || 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
this.url = url || 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||||
this.apiKey = getKey('QWEN_API_KEY'); // Get API key from utility function
|
this.apiKey = getKey('QWEN_API_KEY');
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendRequest(turns, systemMessage, stopSeq = '***', retryCount = 0) {
|
async sendRequest(turns, systemMessage, stopSeq = '***', retryCount = 0) {
|
||||||
// Limit retry attempts to avoid infinite recursion
|
|
||||||
if (retryCount > 5) {
|
if (retryCount > 5) {
|
||||||
console.error('Maximum retry attempts reached.');
|
console.error('Maximum retry attempts reached.');
|
||||||
return 'Error: Too many retry attempts.';
|
return 'Error: Too many retry attempts.';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build request data
|
|
||||||
const data = {
|
const data = {
|
||||||
model: this.modelName || 'qwen-plus',
|
model: this.modelName || 'qwen-plus',
|
||||||
input: { messages: [{ role: 'system', content: systemMessage }, ...turns] },
|
input: { messages: [{ role: 'system', content: systemMessage }, ...turns] },
|
||||||
|
@ -32,78 +27,65 @@ export class Qwen {
|
||||||
data.input.messages.push({ role: 'user', content: 'hello' });
|
data.input.messages.push({ role: 'user', content: 'hello' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate data format before sending request
|
|
||||||
if (!data.model || !data.input || !data.input.messages || !data.parameters) {
|
if (!data.model || !data.input || !data.input.messages || !data.parameters) {
|
||||||
console.error('Invalid request data format:', data);
|
console.error('Invalid request data format:', data);
|
||||||
throw new Error('Invalid request data format.');
|
throw new Error('Invalid request data format.');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Send request to API
|
|
||||||
const response = await this._makeHttpRequest(this.url, data);
|
const response = await this._makeHttpRequest(this.url, data);
|
||||||
const choice = response?.output?.choices?.[0];
|
const choice = response?.output?.choices?.[0];
|
||||||
|
|
||||||
// Retry request if response is incomplete due to length limit
|
|
||||||
if (choice?.finish_reason === 'length' && turns.length > 0) {
|
if (choice?.finish_reason === 'length' && turns.length > 0) {
|
||||||
return this.sendRequest(turns.slice(1), systemMessage, stopSeq, retryCount + 1);
|
return this.sendRequest(turns.slice(1), systemMessage, stopSeq, retryCount + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return response content or default message
|
|
||||||
return choice?.message?.content || 'No content received.';
|
return choice?.message?.content || 'No content received.';
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Error handling, log error and return error message
|
|
||||||
console.error('Error occurred:', err);
|
console.error('Error occurred:', err);
|
||||||
return 'An error occurred, please try again.';
|
return 'An error occurred, please try again.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async embed(text) {
|
async embed(text) {
|
||||||
// Validate embedding input
|
|
||||||
if (!text || typeof text !== 'string') {
|
if (!text || typeof text !== 'string') {
|
||||||
console.error('Invalid embedding input: text must be a non-empty string.');
|
console.error('Invalid embedding input: text must be a non-empty string.');
|
||||||
return 'Invalid embedding input: text must be a non-empty string.';
|
return 'Invalid embedding input: text must be a non-empty string.';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build embedding request data
|
|
||||||
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' },
|
||||||
};
|
};
|
||||||
|
|
||||||
// Validate data format before sending request
|
|
||||||
if (!data.model || !data.input || !data.input.texts || !data.parameters) {
|
if (!data.model || !data.input || !data.input.texts || !data.parameters) {
|
||||||
console.error('Invalid embedding request data format:', data);
|
console.error('Invalid embedding request data format:', data);
|
||||||
throw new Error('Invalid embedding request data format.');
|
throw new Error('Invalid embedding request data format.');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Send embedding request to API
|
|
||||||
const response = await this._makeHttpRequest(this.url, data);
|
const response = await this._makeHttpRequest(this.url, data);
|
||||||
const embedding = response?.output?.embeddings?.[0]?.embedding;
|
const embedding = response?.output?.embeddings?.[0]?.embedding;
|
||||||
return embedding || 'No embedding result received.';
|
return embedding || 'No embedding result received.';
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Error handling, log error and return error message
|
|
||||||
console.error('Error occurred:', err);
|
console.error('Error occurred:', err);
|
||||||
return 'An error occurred, please try again.';
|
return 'An error occurred, please try again.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _makeHttpRequest(url, data) {
|
async _makeHttpRequest(url, data) {
|
||||||
// Set request headers, including authorization and content type
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'Authorization': `Bearer ${this.apiKey}`,
|
'Authorization': `Bearer ${this.apiKey}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
};
|
};
|
||||||
|
|
||||||
// Send HTTP POST request to API
|
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers,
|
headers,
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check response status code, throw error if not successful
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorText = await response.text();
|
const errorText = await response.text();
|
||||||
console.error(`Request failed, status code ${response.status}: ${response.statusText}`);
|
console.error(`Request failed, status code ${response.status}: ${response.statusText}`);
|
||||||
|
@ -111,7 +93,6 @@ export class Qwen {
|
||||||
throw new Error(`Request failed, status code ${response.status}: ${response.statusText}`);
|
throw new Error(`Request failed, status code ${response.status}: ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse and return response JSON
|
|
||||||
const responseText = await response.text();
|
const responseText = await response.text();
|
||||||
try {
|
try {
|
||||||
return JSON.parse(responseText);
|
return JSON.parse(responseText);
|
||||||
|
|
Loading…
Add table
Reference in a new issue