Sign inSign up

hyperonym/basaran

By hyperonym

โ€ขUpdated about 3 years ago

Open-source alternative to the OpenAI text completion API.

Image
Machine learning & AI
Data science
7

10K+

hyperonym/basaran repository overview

โ Basaran

Python codecov PyPI Status

Basaran is an open-source alternative to the OpenAI text completion APIโ . It provides a compatible streaming API for your Hugging Face Transformersโ -based text generation modelsโ .

The open source community will eventually witness the Stable Diffusionโ  moment for large language models (LLMs), and Basaran allows you to replace OpenAI's service with the latest open-source model to power your application without modifying a single line of codeโ .

The key features of Basaran are:

  • Streaming generation using various decoding strategies.
  • Support for both decoder-only and encoder-decoder models.
  • Detokenizer that handles surrogates and whitespace.
  • Multi-GPU support with optional quantization.
  • Real-time partial progress using server-sent eventsโ .
  • Compatibility with OpenAI API and client libraries.
  • Comes with a fancy web-based playground!

โ Quick Start

โ TL;DR

Replace user/repo with your selected modelโ  and X.Y.Z with the latest versionโ , then run:

docker run -p 80:80 -e MODEL=user/repo hyperonym/basaran:X.Y.Z

And you're good to go! ๐Ÿš€

Playground: http://127.0.0.1/
API:        http://127.0.0.1/v1/completions
โ Installation

Docker images are available on Docker Hubโ  and GitHub Packagesโ .

For GPU acceleration, you also need to install the NVIDIA Driverโ  and NVIDIA Container Runtimeโ . Basaran's image already comes with related libraries such as CUDA and cuDNN, so there is no need to install them manually.

Basaran's image can be used in three ways:

  • Run directly: By specifying the MODEL="user/repo" environment variable, the corresponding model can be downloaded from Hugging Face Hub during the first startup.
  • Bundling: Create a new Dockerfile to preload a public modelโ  or bundle a private modelโ .
  • Bind mount: Mount a model from the local file system into the container and point the MODEL environment variable to the corresponding path.

For the above use cases, you can find sample Dockerfilesโ  and docker-compose filesโ  in the deployments directoryโ .

โ Using pip

Basaran is tested on Python 3.8+ and PyTorch 1.13+. You should create a virtual environmentโ  with the version of Python you want to use, and activate it before proceeding.

  1. Install with pip:
pip install basaran
  1. Install dependencies required for GPU acceleration (optional):
pip install accelerate bitsandbytes
  1. Replace user/repo with the selected model and run Basaran:
MODEL=user/repo PORT=80 python -m basaran

For a complete list of environment variables, see __init__.pyโ .

โ Running From Source

If you want to access the latest features or hack it yourself, you can choose to run from source using git.

  1. Clone the repository:
git clone https://github.com/hyperonym/basaran.git && cd basaran
  1. Install dependencies:
pip install -r requirements.txt
  1. Replace user/repo with the selected model and run Basaran:
MODEL=user/repo PORT=80 python -m basaran
โ Basic Usage
โ cURL

Basaran's HTTP request and response formats are consistent with the OpenAI APIโ .

Taking text completionโ  as an example:

curl http://127.0.0.1/v1/completions \
    -H 'Content-Type: application/json' \
    -d '{ "prompt": "once upon a time,", "echo": true }'
Example response
{
    "id": "cmpl-e08c701b4ba032c09ef080e1",
    "object": "text_completion",
    "created": 1678003509,
    "model": "bigscience/bloomz-560m",
    "choices": [
        {
            "text": "once upon a time, the human being faces a complicated situation and he needs to find a new life.",
            "index": 0,
            "logprobs": null,
            "finish_reason": "length"
        }
    ],
    "usage": {
        "prompt_tokens": 5,
        "completion_tokens": 21,
        "total_tokens": 26
    }
}
โ OpenAI Client Library

If your application uses client librariesโ  provided by OpenAI, you only need to modify the OPENAI_API_BASE environment variable to match Basaran's endpoint:

OPENAI_API_BASE="http://127.0.0.1/v1" python your_app.py

The examplesโ  directory contains examples of using the OpenAI Python libraryโ .

โ Using as a Python Library

Basaran is also available as a library on PyPIโ . It can be used directly in Python without the need to start a separate API server.

  1. Install with pip:
pip install basaran
  1. Use the load_model function to load a model:
from basaran.model import load_model

model = load_model("user/repo")
  1. Generate streaming output by calling the model:
for choice in model("once upon a time"):
    print(choice)

The examplesโ  directory contains examples of using Basaran as a libraryโ .

โ Compatibility

Basaran's API format is consistent with OpenAI's, with differences in compatibility mainly in terms of parameter support and response fields. The following sections provide detailed information on the compatibility of each endpoint.

โ Models

Each Basaran process serves only one model, so the result will only contain that model.

โ Completions

Although Basaran does not support the model parameter, the OpenAI client library requires it to be present. Therefore, you can enter any random model name.

ParameterBasaranOpenAIDefault ValueMaximum Value
modelโ—‹โ—--
promptโ—โ—""COMPLETION_MAX_PROMPT
suffixโ—‹โ—--
min_tokensโ—โ—‹0COMPLETION_MAX_TOKENS
max_tokensโ—โ—16COMPLETION_MAX_TOKENS
temperatureโ—โ—1.0-
top_pโ—โ—1.0-
nโ—โ—1COMPLETION_MAX_N
streamโ—โ—false-
logprobsโ—โ—0COMPLETION_MAX_LOGPROBS
echoโ—โ—false-
stopโ—‹โ—--
presence_penaltyโ—‹โ—--
frequency_penaltyโ—‹โ—--
best_ofโ—‹โ—--
logit_biasโ—‹โ—--
userโ—‹โ—--
โ Chat

Providing a unified chat API is currently difficult because each model has a different format for chat history.

Therefore, it is recommended to pre-format the chat history based on the requirements of the specific model and use it as the prompt for the completion API.

โ GPT-NeoXT-Chat-Base-20Bโ 
**Summarize a long document into a single sentence and ...**

<human>: Last year, the travel industry saw a big ...

<bot>: If you're traveling this spring break, ...

<human>: But ...

<bot>:
โ chatglm-6bโ 
[Round 0]
้—ฎ๏ผšไฝ ๅฅฝ
็ญ”๏ผšไฝ ๅฅฝ!ๆœ‰ไป€ไนˆๆˆ‘ๅฏไปฅๅธฎๅŠฉไฝ ็š„ๅ—?
[Round 1]
้—ฎ๏ผšไฝ ๆ˜ฏ่ฐ๏ผŸ
็ญ”๏ผš

โ Roadmap

  • API
    • Models
      • List models
      • Retrieve model
    • Completions
      • Create completion
    • Chat
      • Create chat completion
  • Model
    • Architectures
      • Encoder-decoder
      • Decoder-only
    • Decoding strategies
      • Random sampling with temperature
      • Nucleus-sampling (top-p)
      • Stop sequences
      • Presence and frequency penalties

See the open issuesโ  for a full list of proposed features.

โ Contributing

This project is open-source. If you have any ideas or questions, please feel free to reach out by creating an issue!

Contributions are greatly appreciated, please refer to CONTRIBUTING.mdโ  for more information.

โ License

Basaran is available under the MIT Licenseโ .


ยฉ 2023 Hyperonymโ 

Tag summary

Content type

Image

Digest

sha256:8853a1494โ€ฆ

Size

8.1 GB

Last updated

about 3 years ago

docker pull hyperonym/basaran:0.21.1