I have made a proxy server in Node js in my next js app and I am calling the same via fetch. Below is the code to call the proxy server

const output = await fetch("/api/llamax", {
              method: 'POST', // or 'GET' depending on your API
              headers: {
                'Content-Type': 'application/json',
              },
              body: JSON.stringify( { id: "system", role: "system", content: "You are a philosopher." }),
            });

There after I created a Proxy server api/llamax/route.js and have clone model into proxy server. I referred to blog -> https://markus.oberlehner.net/blog/building-a-chatbot-with-nextjs-running-llama-2-locally/.

Below is my code for Proxy Server

import path from "path";
import { spawn } from "child_process";
import { ReadableStream } from "web-streams-polyfill/ponyfill";

const getAnswer = ({ messages }) => {
  const messageString = messages.map((m) => {
    if (m.role === "system") {
      return `<s>[INST] <<SYS>>\n${m.content}\n<</SYS>>\n\n`;
    }
    if (m.role === "assistant") {
      return `${m.content}</s><s>[INST] `;
    }

    return `${m.content} [/INST] `;
  });

  const executablePath = path.resolve(__dirname, 'llama', 'llama');
  console.log('Resolved path:', executablePath); // Update the path accordingly

  return spawn(
    "llama.cpp",
    [
      "-t",
      "8",
      "-ngl",
      "1",
      "-m",
      "llama-2-13b-chat.ggmlv3.q4_0.bin",
      "--color",
      "-c",
      "2048",
      "--temp",
      "0.7",
      "--repeat_penalty",
      "1.1",
      "-n",
      "-1",
      "-p",
      messageString,
    ],
    {
      cwd: path.resolve(process.cwd(), "llama"),
    }
  );
};

const getAnswerStream = ({ messages }) => {
  const encoder = new TextEncoder();
  return new ReadableStream({
    start(controller) {
      const llama = getAnswer({ messages });

      let start = false;
      llama.stdout.on("data", (data) => {
        if (data.includes("[/INST]")) {
          start = true;
          return;
        }
        if (!start) return;

        const chunk = encoder.encode(String(data));
        controller.enqueue(chunk);
      });

      llama.stderr.on("data", (data) => {
        console.error("Error output from llama process:", data.toString());
        // TODO: Handle error as needed
      });

      llama.on("close", (code) => {
        if (code !== 0) {
          console.error(`Llama process exited with code ${code}`);
          // TODO: Handle the non-zero exit code as needed
        }
        controller.close();
      });
    },
  });
};

export async function POST(request) {
  try {
    const { id, role, content } = await request.json();
    const messages = [{ id, role, content }]; // Convert the received data into an array of messages
    console.log('Received messages:', messages);

    if (!messages) {
      return new Response("No message in the request", { status: 400 });
    }

    return new Response(getAnswerStream({ messages }));
  } catch (error) {
    console.error('Error parsing JSON:', error);
    return new Response("Error parsing JSON", { status: 400 });
  }
}

I am getting error Error parsing JSON: Error: spawn UNKNOWN at ChildProcess.spawn (node:internal/child_process:413:11) at spawn (node:child_process:743:9) at getAnswer (webpack-internal:///(rsc)/./app/api/llamax/route.js:26:64) at Object.start (webpack-internal:///(rsc)/./app/api/llamax/route.js:52:27) at reflectCall (webpack-internal:///(rsc)/./node_modules/web-streams-polyfill/dist/ponyfill.js:75:41) at Object.eval [as start] (webpack-internal:///(rsc)/./node_modules/web-streams-polyfill/dist/ponyfill.js:3574:20) at startAlgorithm (webpack-internal:///(rsc)/./node_modules/web-streams-polyfill/dist/ponyfill.js:3179:41) at SetUpReadableStreamDefaultController (webpack-internal:///(rsc)/./node_modules/web-streams-polyfill/dist/ponyfill.js:3158:27) at SetUpReadableStreamDefaultControllerFromUnderlyingSource (webpack-internal:///(rsc)/./node_modules/web-streams-polyfill/dist/ponyfill.js:3192:9) at new ReadableStream (webpack-internal:///(rsc)/./node_modules/web-streams-polyfill/dist/ponyfill.js:3670:17) at getAnswerStream (webpack-internal:///(rsc)/./app/api/llamax/route.js:50:12) at POST (webpack-internal:///(rsc)/./app/api/llamax/route.js:95:29) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:254:37) { errno: -4094, code: 'UNKNOWN', syscall: 'spawn' }

0

There are 0 answers