Skip to main content

Python bindings for https://github.com/openvinotoolkit/openvino.genai

Project description

OpenVINO™ GenAI Library

OpenVINO™ GenAI is a flavor of OpenVINO™, aiming to simplify running inference of generative AI models. It hides the complexity of the generation process and minimizes the amount of code required.

Install OpenVINO™ GenAI

The OpenVINO™ GenAI flavor is available for installation via Archive and PyPI distributions. To install OpenVINO™ GenAI, refer to the Install Guide.

To build OpenVINO™ GenAI library from source, refer to the Build Instructions.

Usage

Prerequisites

  1. Installed OpenVINO™ GenAI

    If OpenVINO GenAI is installed via archive distribution or built from source, you will need to install additional python dependencies (e.g. optimum-cli for simplified model downloading and exporting):

    # (Optional) Clone OpenVINO GenAI repository if it does not exist
    git clone --recursive https://github.com/openvinotoolkit/openvino.genai.git
    cd openvino.genai
    # Install python dependencies
    python -m pip install ./thirdparty/openvino_tokenizers/[transformers] --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/pre-release
    python -m pip install --upgrade-strategy eager -r ./samples/cpp/requirements.txt
    
  2. A model in OpenVINO IR format

    Download and convert a model with optimum-cli:

    optimum-cli export openvino --model "TinyLlama/TinyLlama-1.1B-Chat-v1.0" --trust-remote-code "TinyLlama-1.1B-Chat-v1.0"
    

LLMPipeline is the main object used for decoding. You can construct it straight away from the folder with the converted model. It will automatically load the main model, tokenizer, detokenizer and default generation configuration.

Python

A simple example:

import openvino_genai as ov_genai
pipe = ov_genai.LLMPipeline(model_path, "CPU")
print(pipe.generate("The Sun is yellow because"))

Calling generate with custom generation config parameters, e.g. config for grouped beam search:

import openvino_genai as ov_genai
pipe = ov_genai.LLMPipeline(model_path, "CPU")

result = pipe.generate("The Sun is yellow because", max_new_tokens=30, num_groups=3, group_size=5, diversity_penalty=1.5)
print(result)

output:

'it is made up of carbon atoms. The carbon atoms are arranged in a linear pattern, which gives the yellow color. The arrangement of carbon atoms in'

A simple chat in Python:

import openvino_genai as ov_genai
pipe = ov_genai.LLMPipeline(model_path)

config = {'max_new_tokens': 100, 'num_groups': 3, 'group_size': 5, 'diversity_penalty': 1.5}
pipe.set_generation_config(config)

pipe.start_chat()
while True:
    print('question:')
    prompt = input()
    if prompt == 'Stop!':
        break
    print(pipe(prompt))
pipe.finish_chat()

Test to compare with Huggingface outputs

C++

A simple example:

#include "openvino/genai/llm_pipeline.hpp"
#include <iostream>

int main(int argc, char* argv[]) {
    std::string model_path = argv[1];
    ov::genai::LLMPipeline pipe(model_path, "CPU");
    std::cout << pipe.generate("The Sun is yellow because");
}

Using group beam search decoding:

#include "openvino/genai/llm_pipeline.hpp"
#include <iostream>

int main(int argc, char* argv[]) {
    std::string model_path = argv[1];
    ov::genai::LLMPipeline pipe(model_path, "CPU");

    ov::genai::GenerationConfig config;
    config.max_new_tokens = 256;
    config.num_groups = 3;
    config.group_size = 5;
    config.diversity_penalty = 1.0f;

    std::cout << pipe.generate("The Sun is yellow because", config);
}

A simple chat in C++ using grouped beam search decoding:

#include "openvino/genai/llm_pipeline.hpp"
#include <iostream>

int main(int argc, char* argv[]) {
    std::string prompt;

    std::string model_path = argv[1];
    ov::genai::LLMPipeline pipe(model_path, "CPU");
    
    ov::genai::GenerationConfig config;
    config.max_new_tokens = 100;
    config.num_groups = 3;
    config.group_size = 5;
    config.diversity_penalty = 1.0f;
    
    pipe.start_chat();
    for (;;;) {
        std::cout << "question:\n";
        std::getline(std::cin, prompt);
        if (prompt == "Stop!")
            break;

        std::cout << "answer:\n";
        auto answer = pipe(prompt, config);
        std::cout << answer << std::endl;
    }
    pipe.finish_chat();
}

Streaming example with lambda function:

#include "openvino/genai/llm_pipeline.hpp"
#include <iostream>

int main(int argc, char* argv[]) {
    std::string model_path = argv[1];
    ov::genai::LLMPipeline pipe(model_path, "CPU");
        
    auto streamer = [](std::string word) { 
        std::cout << word << std::flush; 
        // Return flag correspods whether generation should be stopped.
        // false means continue generation.
        return false;
    };
    std::cout << pipe.generate("The Sun is yellow bacause", streamer);
}

Streaming with a custom class:

#include "openvino/genai/streamer_base.hpp"
#include "openvino/genai/llm_pipeline.hpp"
#include <iostream>

class CustomStreamer: public ov::genai::StreamerBase {
public:
    bool put(int64_t token) {
        bool stop_flag = false; 
        /* 
        custom decoding/tokens processing code
        tokens_cache.push_back(token);
        std::string text = m_tokenizer.decode(tokens_cache);
        ...
        */
        return stop_flag;  // flag whether generation should be stoped, if true generation stops.
    };

    void end() {
        /* custom finalization */
    };
};

int main(int argc, char* argv[]) {
    CustomStreamer custom_streamer;

    std::string model_path = argv[1];
    ov::genai::LLMPipeline pipe(model_path, "CPU");
    std::cout << pipe.generate("The Sun is yellow because", custom_streamer);
}

How It Works

For information on how OpenVINO™ GenAI works, refer to the How It Works Section.

Supported Models

For a list of supported models, refer to the Supported Models Section.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

openvino_genai-2024.2.0.0-305-cp312-cp312-win_amd64.whl (794.4 kB view details)

Uploaded CPython 3.12 Windows x86-64

openvino_genai-2024.2.0.0-305-cp312-cp312-manylinux_2_31_aarch64.whl (984.9 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.31+ ARM64

openvino_genai-2024.2.0.0-305-cp312-cp312-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

openvino_genai-2024.2.0.0-305-cp311-cp311-win_amd64.whl (794.1 kB view details)

Uploaded CPython 3.11 Windows x86-64

openvino_genai-2024.2.0.0-305-cp311-cp311-manylinux_2_31_aarch64.whl (985.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.31+ ARM64

openvino_genai-2024.2.0.0-305-cp311-cp311-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

openvino_genai-2024.2.0.0-305-cp311-cp311-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

openvino_genai-2024.2.0.0-305-cp311-cp311-macosx_10_15_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.11 macOS 10.15+ x86-64

openvino_genai-2024.2.0.0-305-cp310-cp310-win_amd64.whl (792.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

openvino_genai-2024.2.0.0-305-cp310-cp310-manylinux_2_31_aarch64.whl (984.5 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.31+ ARM64

openvino_genai-2024.2.0.0-305-cp310-cp310-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

openvino_genai-2024.2.0.0-305-cp310-cp310-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

openvino_genai-2024.2.0.0-305-cp310-cp310-macosx_10_15_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.10 macOS 10.15+ x86-64

openvino_genai-2024.2.0.0-305-cp39-cp39-win_amd64.whl (793.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

openvino_genai-2024.2.0.0-305-cp39-cp39-manylinux_2_31_aarch64.whl (984.9 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.31+ ARM64

openvino_genai-2024.2.0.0-305-cp39-cp39-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

openvino_genai-2024.2.0.0-305-cp39-cp39-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

openvino_genai-2024.2.0.0-305-cp39-cp39-macosx_10_15_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

openvino_genai-2024.2.0.0-305-cp38-cp38-win_amd64.whl (793.1 kB view details)

Uploaded CPython 3.8 Windows x86-64

openvino_genai-2024.2.0.0-305-cp38-cp38-manylinux_2_31_aarch64.whl (984.3 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.31+ ARM64

openvino_genai-2024.2.0.0-305-cp38-cp38-manylinux_2_17_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

openvino_genai-2024.2.0.0-305-cp38-cp38-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

openvino_genai-2024.2.0.0-305-cp38-cp38-macosx_10_15_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

File details

Details for the file openvino_genai-2024.2.0.0-305-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 02172dd8e6e6638623c323208a0eba2147cd9f43d588b836848870ad3bf8a471
MD5 54d025a8dd9f3215104c35ab31e227ff
BLAKE2b-256 bc5ad3d168d738676f7c4e2d6c61d6c5ceb614331495130a1c972e40a94cc856

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp312-cp312-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp312-cp312-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 d9ad88273c4b705030a240aee595b7f00802160bb2231ca2a3ea8b1066909f33
MD5 aadbc5db0189ed9b22fa6e99a9c67477
BLAKE2b-256 adff1b38d48f9fe1b5c2c012b38048007b05892a6e256d83a0a80892ae4cae73

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp312-cp312-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp312-cp312-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a1fa65e380526ebc0ea462c1c4b98c72f6b6d7282515f77eff0d3b988c100af9
MD5 fc08f3a01731c39f9b61f6714e7a193a
BLAKE2b-256 64cfe04cc16cf7cd2fcf945cda7e87de19819b73154e84815ab9234e32736216

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7ec48980cb61aa3308c15586d09cb86ea204cf0ed165678b3f594f6cfec4b827
MD5 e385fe4bb30c134142e1386a34289932
BLAKE2b-256 277386a8ca834d5b5522226320a981f70ace208e8aefa2434f0a1b99f1596f37

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp311-cp311-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp311-cp311-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 87512f8f1a62b1addd287ecd2c9a97ba13409528682ae688b69d4c841ee44ffc
MD5 403720413c8e490a803ab9c5ae2e0434
BLAKE2b-256 7a934c1db9be826f609f4023b8521b0f7eef5b43035dbc3adb998a4d762dfc20

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp311-cp311-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp311-cp311-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 1d763dec72245e7340208ec11300d0f2f8681877bd99273f05f8e752c06ab24d
MD5 f0ff2c545759ca17b942d96d30162146
BLAKE2b-256 f0842897f91d948b282578505e42183b58ca1c09a270d8aba1737a64ca9555c2

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d0b399167b1a27acf267ca8d35fb7b38180645a3f266cd86267433a6347caf27
MD5 755e5b8e32469a707bb26b66919db9db
BLAKE2b-256 6798dcc8633372bb138b303e0d88e8d4cab4dd27f71412f11952a1eb2d9ea976

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp311-cp311-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp311-cp311-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 83eefc5aeee58da29a279262c1da7f34419a666517cbf4699a7be3301f7a7a41
MD5 51d481f9b87ad715e001f910020e0608
BLAKE2b-256 72f814efb0c9640c654bd230118b8cc82aacb8be82d43e6b52304a43d538b74a

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 58ad2c5f7141bd78d9dbbadc658384349534fbb71b19d7e22831f40e0095db78
MD5 1be6423e57f5e14ab2fd01e5516046e2
BLAKE2b-256 87f57e150c5249b44a1129e71ca0740ede2d9cc897f098610d50bda2964b5cf1

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp310-cp310-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp310-cp310-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 3879e241335e5821787a596ef1aabc63f870d4aa823c65c520bd5979ae125c5c
MD5 a5dce21c05d674d171738a5e54634467
BLAKE2b-256 c61f512a8bc8d4435dcda973b3ce2646a14131998f18ca2d3b6d3c1b4cb597e0

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp310-cp310-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp310-cp310-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 5dd2a08ba5d23d86d7c3f25b1446f5d91fb3e07de8a569ff73d78692a3d61c08
MD5 eecd0966af1c2e74c6b8f2dca2c57b7a
BLAKE2b-256 405963caa88e9eac036fb247afff7352f7a3abb8390356cde77df061746bc643

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7886d7ceca24d3cbc7f470977744924c79fa2a98c8de0e82e750bc012eaa75e1
MD5 c511182c947b1d07598555c3e1d54e55
BLAKE2b-256 e8ae60df172c22c70ea18d6ad38a94a0247b2709399815704c1bc427e29d3e29

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 a77516b11115a3c25d71c3193c4998c5aab4770f0120bd71ecd4ed80dba9432b
MD5 f93a8624354f9e3c78d622d7c1edf529
BLAKE2b-256 8234409cea1db43ceedb41a5febc9339ac45555e8c02380285d175922317e30c

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5a5030149bcd4c3b8dad61b475808cfd816bb1b44c649ec23e28bc3731998c4c
MD5 a98798e3a13f4f8c275873dafb0b67b1
BLAKE2b-256 70e204e90661d8fd349f86118715caecf15f44889398cb0f5328cd798a87a4cc

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp39-cp39-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp39-cp39-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 29bb8b198edcb9739c661fdedf9ad7ad57c75a2b7e379900e8195f6edf456b3f
MD5 7750739fe3342b82bcb067b8ed5465db
BLAKE2b-256 87610925f3dbde7dd42a30e143dc4d1dc4c1746488631de715e19def99d5d4b1

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp39-cp39-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp39-cp39-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 2fe301d9f20cbe434cca3e88960ad038170c6a626be50fb7c86c520ce5ab906e
MD5 21f67fd14ec7ec9f38b9ce8a729b56bd
BLAKE2b-256 33b2f5a3fd0ccd97f4fa14f0866cc8f8a0cec646cc4b8bd2af23dcc73e20d7e2

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5a3d508677f946e2d3bbe0ec9e27c855448f7692a0d935c917ad0f945e6a8415
MD5 90e4c9a2992fda6f70c3fea928716c5c
BLAKE2b-256 0b90fa66309a24907187b72f362314e9269bb7d21d926040dfc69b64c880fd96

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 886861d3432f3e3c608944773597f3c87bf4e72246446f2289da618e63da869b
MD5 ad7fbd41aa6bfee8f00c1b4a307a916c
BLAKE2b-256 54a93ffd09579a106524b15671beef58844091ef638cd7e8565e61489eeaff47

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 2f314ee20d2d8b81c3dc4eb9534d6dbc572d387c723489e3b3950b692cfe9db6
MD5 bd0f984bffe3c2722a15efdb10759d7a
BLAKE2b-256 3b5df8bf6c1de32a172f6829b5b43de1d30626c87e766a0d4ff18d34508fdc04

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp38-cp38-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp38-cp38-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 92376694db73e86a850c8aaaedbfa8ac042418226a86d3629fcaa5df8f23ec24
MD5 4b307e08d35ac4b90aac342bea519f19
BLAKE2b-256 d5e6e950aa70be1b71893119a77e4a1d3723e7437bc4704fc11ae5c4610024ba

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp38-cp38-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp38-cp38-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 482a79d65235d04fefa320e03a8628467e9ed117488313fcbfa8c8a91f8b35de
MD5 b14bd737dd58af522aa4824fdf902888
BLAKE2b-256 f17638f12e58c3bd7f749c80bd899724bdab02908fe49572648b5ae8a3cf278b

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a2722180832d9ba21268c851319d5b38742dcac354d92683fad00a8ec386df41
MD5 d16cc2855dfee648eb1fa8295f93980c
BLAKE2b-256 d6b1832b164adb1c3961711a1593ee30ca754bcc9692531ee8c996c97dd0458b

See more details on using hashes here.

File details

Details for the file openvino_genai-2024.2.0.0-305-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for openvino_genai-2024.2.0.0-305-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 dd5d8cc883ae968123359dbb91d7a0e0152c8936ef91327c005fe563fb2de818
MD5 d66fb36699fec8db10f624f226c0bed7
BLAKE2b-256 72ca3c5ba483b52654aa7184186643a9b4099faa815eb89dc22e2131344f33c3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page