Skip to content

How to use with node-llama-cpp for backend #238

Closed

Description

@TimeLordRaps

Documentation description
Just a relevant example of how to use with node-llama-cpp
Steps include installing node-llama-cpp
installing langchain/community
and here's relevant code:

import { CopilotBackend, LangChainAdapter } from "@copilotkit/backend";
import { ChatLlamaCpp } from "@langchain/community/chat_models/llama_cpp"

const modelPath = ""
const contextLength = 8192
export async function POST(req: Request): Promise<Response> {
  const copilotKit = new CopilotBackend();
  return await copilotKit.response(
    req, 
    new LangChainAdapter(async (forwardedProps) => {
      const model = new ChatLlamaCpp({
        modelPath: modelPath,
        contextSize: contextLength,
        batchSize: contextLength, //weird bug with node-llama-cpp where if batchSize isn't big enough it just crashes.
      });
      return model.stream(
        forwardedProps.messages,
        { tools: forwardedProps.tools }
      );
    })
  );
}

Relevant Context
Where this documentation fits in

  1. Existing documentation (see docs.copilotkit.ai): 'either https://docs.copilotkit.ai/reference/CopilotBackend) or https://docs.copilotkit.ai/getting-started/quickstart-backend

Activity

  1. added
    documentation
    on Feb 22, 2024
  2. mme commented on Feb 23, 2024

    @mme
    Contributor

    @TimeLordRaps that's awesome, thanks for figuring this out!

    Would you mind contributing this to our documentation?

  3. TimeLordRaps commented on Feb 24, 2024

    @TimeLordRaps
    Author

    So I'm still currently testing around with this, it might not be fully functional as the LlamaCpp custom Llm form langchain has trouble with tools. Could someone familiar with the matter explain to me where in the code to look to see the downstream effects of failing tool uses.

  4. ataibarkai commented on Feb 25, 2024

    @ataibarkai
    Contributor

    @TimeLordRaps at a high level the code lives here and here.

    But it can be a bit challenging to come in there cold. It's a complex part of the codebase (and will actually get more complex in the near as we repurpose the underlying function calling capability to do more and more..).

    It might be easiest to test empirically, having 2 identical examples and switching only node-llama-cpp and observing the differences.

    I actually think this should live more than just in documentation, but in ready-made adapter factories / instances.

    I'd love to collaborate / pair program with you to help you make this first addition of its kind of you are interested. And I can also help you get oriented with tool calls if you want to dig deeper.

    btw, cool username

  5. TimeLordRaps commented on Feb 25, 2024

    @TimeLordRaps
    Author

    I'd love the opportunity to pair program an implementation of some contribution to this project. However I'm not sure this issue would be my first choice. I'll reach out on discord though.

  6. added
    good first issue
    on Mar 14, 2024
  7. mme commented on Mar 14, 2024

    @mme
    Contributor

    This would make a great first issue: Add the code to the langchain docs to explain how to use other LLMs.

  8. mitre88 commented on Mar 4, 2026

    @mitre88
    Contributor

    Submitted docs contribution in PR #3359:\nhttps://github.com//pull/3359\n\nWhat was added:\n- New backend guide for using Copilot Runtime with node-llama-cpp via LangChainAdapter.\n- End-to-end setup (dependencies, env, runtime endpoint, frontend runtimeUrl).\n- Added backend nav entry and cross-link from LLM adapters docs.

  9. TimeLordRaps commented on Mar 8, 2026

    @TimeLordRaps
    Author

    Thanks for the memories.

  10. jpr5 commented on Apr 12, 2026

    @jpr5
    Contributor

    The backend API referenced here (CopilotBackend, LangChainAdapter from @copilotkit/backend) has been deprecated. The recommended path forward is AG-UI (Agent-User Interaction protocol). See the AG-UI migration guide for the current approach. Closing as outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions