Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.1 kB
11
Indexable
Never
const claude_url = 'https://anthropic.api.stdlib.com/playground@dev/complete/'
//const claude_url = 'https://api.anthropic.com/v1/complete'

//**************Kobold api
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
const needle_1 = __importDefault(require("needle"));

app.post("/generate", jsonParser, function (request, response_generate_novel = response) {
    if (!request.body) return response_generate_novel.sendStatus(400);
    const stops = new Set(['System:', "Human:", "${HUMAN_PROMPT}", request.body.username])

    console.log(request.body);
    var data = {
        prompt: request.body.prompt,
        model: "claude-v1.2",
        max_context_length: request.body.max_context_length,
        max_tokens_to_sample: request.body.max_length,
        temperature: request.body.temperature,
        stop_sequences: Array.from(stops),

    };
    console.log("REQUEST")
    console.log(data)
    api_server = request.body.api_server;
    sendClaude(request.body.claude_api)

    async function sendClaude(key) {
        const header = {
            'Content-Type': 'application/json',
            'x-api-key': key
        }
        console.log(header)
        const resp = await (0, needle_1.default)('post', claude_url, JSON.stringify(data), {
            json: true,
            headers: header
        }).catch((err) => ({ error: err }));
        if ('error' in resp) {
            console.log({ error: resp.error }, 'Claude request failed to send');
            console.log(`Claude request failed: ${resp.error?.message || resp.error}`)
            response_generate_novel.send(`Error: Claude request failed: ${resp.error?.message || resp.error}`);
            //sendClaude()
            return;
        }
        if (resp.statusCode && resp.statusCode >= 400) {
            console.log({ body: resp.statusCode }, `Claude request failed (${resp.statusCode})`);
            console.log(`Claude request failed: ${resp.body.detail}`)
            response_generate_novel.send(`Error: Claude request failed: ${resp.body.detail}`);
            //sendClaude()
            return;
        }
        try {
            const completion = resp.body.completion;
            if (!completion) {
                console.log({ body: resp.body }, 'Claude request failed: Empty response');
                setTimeout(function () {
                    sendClaude(key);
                }, 2000);
                return;
            }
            else {

                console.log(completion);
                response_generate_novel.send(completion);
            }
        }
        catch (ex) {
            log.error({ err: ex }, 'Claude failed to parse');
            response_generate_novel.send(`Error: Claude request failed: ${ex}`);
            return;
        }
    }
});
function randomHash() {
    const letters = 'abcdefghijklmnopqrstuvwxyz0123456789';
    let result = '';
    for (let i = 0; i < 9; i++) {
        result += letters.charAt(Math.floor(Math.random() * letters.length));
    }
    return result;
}

function textGenProcessStartedHandler(websocket, content, session, prompt, fn_index) {
    switch (content.msg) {
        case "send_hash":
            const send_hash = JSON.stringify({ "session_hash": session, "fn_index": fn_index });
            websocket.send(send_hash);
            break;
        case "estimation":
            break;
        case "send_data":
            const send_data = JSON.stringify({ "session_hash": session, "fn_index": fn_index, "data": prompt.data });
            console.log(send_data);
            websocket.send(send_data);
            break;
        case "process_starts":
            break;
        case "process_generating":
            return { text: content.output.data[0], completed: false };
        case "process_completed":
            try {
                return { text: content.output.data[0], completed: true };
            }
            catch {
                return { text: '', completed: true };
            }
    }

    return { text: '', completed: false };
}