Giter VIP home page Giter VIP logo

node-llmatic's Introduction

LLMatic

LLMatic Logo

Use self-hosted LLMs with an OpenAI compatible API

llmatic llmatic test and release


Project status

This project was the result of my curiousity and experimentation with OpenAI's API and I enjoyed building it. It is certainly not the first nor the last project of its kind. Given my limited time and resources, I'd like to pause the development of this project for now. I'll list some other similar projects below that can be used as alternatives:

  1. Ollama
  2. LLaMA.cpp HTTP Server
  3. GPT4All Chat Server Mode
  4. FastChat

Synopsis

LLMatic can be used as a drop-in replacement for OpenAI's API v1.2.0 (see the supported endpoints). By default, it uses llama-node with llama.cpp backend to run the models locally. However, you can easily create your own adapter to use any other model or service.

Supported endpoints:

  • /completions (stream and non-stream)
  • /chat/completions (stream and non-stream)
  • /embeddings
  • /models

How to use

If you prefer a video tutorial, you can watch the following video for step-by-step instructions on how to use this project:

LLMatic

Requirements

  • Node.js >=18.16
  • Unix-based OS (Linux, macOS, WSL, etc.)

Installation

Create an empty directory and run npm init:

export LLMATIC_PROJECT_DIR=my-llmatic-project
mkdir $LLMATIC_PROJECT_DIR
cd $LLMATIC_PROJECT_DIR
npm init -y

Install and configure LLMatic:

npm add llmatic
# Download a model and generate a config file
npx llmatic config

Adjust the config file to your needs and start the server:

npx llmatic start

You can run llmatic --help to see all available commands.

Usage with chatbot-ui

Clone the repo and install the dependencies:

git clone https://github.com/mckaywrigley/chatbot-ui.git
cd chatbot-ui
npm install

Create a .env.local file:

cat <<EOF > .env.local
# For now, this is ignored by LLMatic
DEFAULT_MODEL=Ignored

NEXT_PUBLIC_DEFAULT_SYSTEM_PROMPT=A chat between a curious human (user) and an artificial intelligence assistant (assistant). The assistant gives helpful, detailed, and polite answers to the human's questions.

user: Hello!
assistant: Hello! How may I help you today?
user: Please tell me the largest city in Europe.
assistant: Sure. The largest city in Europe is Moscow, the capital of Russia.

OPENAI_API_KEY=ANYTHING_WILL_DO
OPENAI_API_HOST=http://localhost:3000

GOOGLE_API_KEY=YOUR_API_KEY
GOOGLE_CSE_ID=YOUR_ENGINE_ID
EOF

Run the server:

npm run dev -- --port 3001

Demo:

chatbot-ui Demo

Usage with LangChain

There are two examples of using LLMatic with LangChain in the examples directory.

To run the Node.js example, first install the dependencies:

cd examples/node-langchain
npm install

Then run the main script:

npm start
Expand this to see the sample output
[chain/start] [1:chain:llm_chain] Entering Chain run with input: {
  "humanInput": "Rememeber that this is a demo of LLMatic with LangChain.",
  "history": ""
}
[llm/start] [1:chain:llm_chain > 2:llm:openai] Entering LLM run with input: {
  "prompts": [
    "A chat between a curious user and an artificial intelligence assistant.\nThe assistant gives helpful, detailed, and polite answers to the user's questions.\n\n\nHuman: Rememeber that this is a demo of LLMatic with LangChain.\nAI:"
  ]
}
[llm/end] [1:chain:llm_chain > 2:llm:openai] [5.92s] Exiting LLM run with output: {
  "generations": [
    [
      {
        "text": " Yes, I understand. I am ready to assist you with your queries.",
        "generationInfo": {
          "finishReason": "stop",
          "logprobs": null
        }
      }
    ]
  ],
  "llmOutput": {
    "tokenUsage": {}
  }
}
[chain/end] [1:chain:llm_chain] [5.92s] Exiting Chain run with output: {
  "text": " Yes, I understand. I am ready to assist you with your queries."
}
[chain/start] [1:chain:llm_chain] Entering Chain run with input: {
  "humanInput": "What did I ask you to remember?",
  "history": "Human: Rememeber that this is a demo of LLMatic with LangChain.\nAI:  Yes, I understand. I am ready to assist you with your queries."
}
[llm/start] [1:chain:llm_chain > 2:llm:openai] Entering LLM run with input: {
  "prompts": [
    "A chat between a curious user and an artificial intelligence assistant.\nThe assistant gives helpful, detailed, and polite answers to the user's questions.\n\nHuman: Rememeber that this is a demo of LLMatic with LangChain.\nAI:  Yes, I understand. I am ready to assist you with your queries.\nHuman: What did I ask you to remember?\nAI:"
  ]
}
[llm/end] [1:chain:llm_chain > 2:llm:openai] [6.51s] Exiting LLM run with output: {
  "generations": [
    [
      {
        "text": " You asked me to remember that this is a demo of LLMatic with LangChain.",
        "generationInfo": {
          "finishReason": "stop",
          "logprobs": null
        }
      }
    ]
  ],
  "llmOutput": {
    "tokenUsage": {}
  }
}
[chain/end] [1:chain:llm_chain] [6.51s] Exiting Chain run with output: {
  "text": " You asked me to remember that this is a demo of LLMatic with LangChain."
}

To run the Python example, first install the dependencies:

cd examples/python-langchain
pip3 install -r requirements.txt

Then run the main script:

python3 main.py
Expand this to see the sample output
> Entering new LLMChain chain...
Prompt after formatting:
A chat between a curious user and an artificial intelligence assistant.
The assistant gives helpful, detailed, and polite answers to the user's questions.


Human: Rememeber that this is a demo of LLMatic with LangChain.
AI:

> Finished chain.
 Yes, I understand. I am ready to assist you with your queries.


> Entering new LLMChain chain...
Prompt after formatting:
A chat between a curious user and an artificial intelligence assistant.
The assistant gives helpful, detailed, and polite answers to the user's questions.

Human: Rememeber that this is a demo of LLMatic with LangChain.
AI:  Yes, I understand. I am ready to assist you with your queries.
Human: What did I ask you to remember?
AI:

> Finished chain.
 You asked me to remember that this is a demo of LLMatic with LangChain.

Custom Adapters

LLMatic is designed to be easily extensible. You can create your own adapters by extending the LlmAdapter class. See examples/custom-adapter for an example.

To start llmatic with a custom adapter, use the --llm-adapter flag:

llmatic start --llm-adapter ./custom-llm-adapter.ts

node-llmatic's People

Contributors

fardjad avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-llmatic's Issues

SyntaxError: The requested module 'llama-node' does not provide an export named 'LLMErrorType'

With WSL2 I am getting an error

/mnt/c/LLM/GitHub/my-llmatic-project/node_modules/llmatic/src/default-llm-adapter.ts:15
import { type LLMError, LLM as LlamaNode, LLMErrorType } from "llama-node";
^

SyntaxError: The requested module 'llama-node' does not provide an export named 'LLMErrorType'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:131:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:213:5)
at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
at async Command. (file:///mnt/c/LLM/GitHub/my-llmatic-project/node_modules/llmatic/src/cli/llmatic-config.js:135:24)
at Command.parseAsync (/mnt/c/LLM/GitHub/my-llmatic-project/node_modules/commander/lib/command.js:936:5)
at async file:///mnt/c/LLM/GitHub/my-llmatic-project/node_modules/llmatic/src/cli/llmatic-config.js:139:1

Node.js v20.9.0

Want to contribute to the repo

I am also building a ChatLLM as project and faced same issue of that the OpenAI api are costly and trying to figure out a way to add Open Source LLM as API but didn't find for TypeScript and came across your project.
I would love to contribute to the this project.

How do I set English as default?

For some reason when I send a request to the model I get response in random language, Korean, Mandarin etc and only sometimes in English. Do you know how to set English as default? My model is /ggml-vic13b-q5_1.bin
curl -X 'POST' 'http://127.0.0.1:3000/v1/chat/completions' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{
"model": "string",
"messages": [
{
"role": "system",
"content": "Hello"
}, {"role":"user", "content":"Hi"}
],
"temperature": 1,
"max_tokens":50,
"stream": false
}'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.