mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-27 17:33:02 +02:00
Update glhf.js
Fixed reasoning models in glhf.js
This commit is contained in:
parent
a9eadb6ccd
commit
a402753539
1 changed files with 40 additions and 31 deletions
|
@ -1,18 +1,14 @@
|
||||||
|
// glhf-no-logger.js
|
||||||
import OpenAIApi from 'openai';
|
import OpenAIApi from 'openai';
|
||||||
import { getKey } from '../utils/keys.js';
|
import { getKey } from '../utils/keys.js';
|
||||||
|
|
||||||
// glhf doesn't supply an SDK for their models, but fully supports OpenAI SDKs
|
|
||||||
export class glhf {
|
export class glhf {
|
||||||
constructor(model_name, url) {
|
constructor(model_name, url) {
|
||||||
this.model_name = model_name;
|
this.model_name = model_name;
|
||||||
|
|
||||||
// Retrieve the API key from keys.json
|
|
||||||
const apiKey = getKey('GHLF_API_KEY');
|
const apiKey = getKey('GHLF_API_KEY');
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
throw new Error('API key not found. Please check keys.json and ensure GHLF_API_KEY is defined.');
|
throw new Error('API key not found. Please check keys.json and ensure GHLF_API_KEY is defined.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure OpenAIApi with the retrieved API key and base URL
|
|
||||||
this.openai = new OpenAIApi({
|
this.openai = new OpenAIApi({
|
||||||
apiKey,
|
apiKey,
|
||||||
baseURL: url || "https://glhf.chat/api/openai/v1"
|
baseURL: url || "https://glhf.chat/api/openai/v1"
|
||||||
|
@ -20,40 +16,53 @@ export class glhf {
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendRequest(turns, systemMessage, stop_seq = '***') {
|
async sendRequest(turns, systemMessage, stop_seq = '***') {
|
||||||
// Construct the message array for the API request
|
// Construct the message array for the API request.
|
||||||
let messages = [{ 'role': 'system', 'content': systemMessage }].concat(turns);
|
let messages = [{ role: 'system', content: systemMessage }].concat(turns);
|
||||||
|
|
||||||
const pack = {
|
const pack = {
|
||||||
model: this.model_name || "hf:meta-llama/Llama-3.1-405B-Instruct",
|
model: this.model_name || "hf:meta-llama/Llama-3.1-405B-Instruct",
|
||||||
messages,
|
messages,
|
||||||
stop: [stop_seq]
|
stop: [stop_seq]
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = null;
|
const maxAttempts = 5;
|
||||||
try {
|
let attempt = 0;
|
||||||
console.log('Awaiting glhf.chat API response...');
|
let finalRes = null;
|
||||||
// Uncomment the line below if you need to debug the messages
|
|
||||||
// console.log('Messages:', messages);
|
|
||||||
|
|
||||||
let completion = await this.openai.chat.completions.create(pack);
|
while (attempt < maxAttempts) {
|
||||||
if (completion.choices[0].finish_reason === 'length') {
|
attempt++;
|
||||||
throw new Error('Context length exceeded');
|
console.log(`Awaiting glhf.chat API response... (attempt: ${attempt})`);
|
||||||
}
|
try {
|
||||||
|
let completion = await this.openai.chat.completions.create(pack);
|
||||||
console.log('Received.');
|
if (completion.choices[0].finish_reason === 'length') {
|
||||||
res = completion.choices[0].message.content;
|
throw new Error('Context length exceeded');
|
||||||
} catch (err) {
|
}
|
||||||
if ((err.message === 'Context length exceeded' || err.code === 'context_length_exceeded') && turns.length > 1) {
|
let res = completion.choices[0].message.content;
|
||||||
console.log('Context length exceeded, trying again with shorter context.');
|
// If there's an open <think> tag without a corresponding </think>, retry.
|
||||||
return await this.sendRequest(turns.slice(1), systemMessage, stop_seq);
|
if (res.includes("<think>") && !res.includes("</think>")) {
|
||||||
} else {
|
console.warn("Partial <think> block detected. Re-generating...");
|
||||||
console.log(err);
|
continue;
|
||||||
res = 'My brain disconnected, try again.';
|
}
|
||||||
|
// If there's a closing </think> tag but no opening <think>, prepend one.
|
||||||
|
if (res.includes("</think>") && !res.includes("<think>")) {
|
||||||
|
res = "<think>" + res;
|
||||||
|
}
|
||||||
|
finalRes = res.replace(/<\|separator\|>/g, '*no response*');
|
||||||
|
break; // Valid response obtained.
|
||||||
|
} 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.');
|
||||||
|
return await this.sendRequest(turns.slice(1), systemMessage, stop_seq);
|
||||||
|
} else {
|
||||||
|
console.error(err);
|
||||||
|
finalRes = 'My brain disconnected, try again.';
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (finalRes === null) {
|
||||||
// Replace special tokens in the response
|
finalRes = "I thought too hard, sorry, try again";
|
||||||
return res.replace(/<\|separator\|>/g, '*no response*');
|
}
|
||||||
|
return finalRes;
|
||||||
}
|
}
|
||||||
|
|
||||||
async embed(text) {
|
async embed(text) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue