Skip to main content

A dependency-free, code-less socket-based solution for resolving (Python / conda) environment incompatibilities

Project description

A dependency-free, code-less socket-based solution for resolving (Python / conda) environment incompatibilities.

Usage Guidelines

Installation:

To install the inincompatibility package, run:

pip install inincompatibility

Example: Making Your LLMs Callable Like an API:

First, make your LLMs (e.g., meta-llama/Meta-Llama-3.1-8B-Instruct) callable functions:

# your_llm.py
import torch
import transformers

assert str(torch.__version__).startswith('2.')

pipeline = transformers.pipeline(
    "text-generation",
    model="meta-llama/Meta-Llama-3.1-8B-Instruct",
    model_kwargs={"torch_dtype": torch.bfloat16},
    device="cuda"
)


def llm_qa(msg: list) -> dict:
    res = pipeline(
        msg,
        max_new_tokens=512,
    )
    return res[0]["generated_text"][-1]

Next, create another Python file (e.g., to_import_ori.py) to import the LLM function:

# to_import_ori.py
from your_llm import llm_qa

Then, use the inincompatibility CLI to generate the necessary importable code and then run the LLM in its (Python / conda) environment:

conda activate llama
pip install inincompatibility
py -m inincompatibility -i to_import_ori.py -o to_import.py

A file named to_import.py (specified by the -o argument) will be generated as follows:

# Generated by `inincompatibility`.
# But **not** depend on `incompatibility`. :)
# Please use this file instead of the original file to `import`.
import socket
import pickle

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 23333))

BUFFER_SIZE = 4096


def _func_eval(func, args, kwargs):
    data = pickle.dumps((func, args, kwargs))
    client.sendall(data)
    return pickle.loads(client.recv(BUFFER_SIZE))


def _inincompatibility_remote_eval(*args, **kwargs):
    return _func_eval("_inincompatibility_remote_eval", args, kwargs)


def _inincompatibility_remote_exec(*args, **kwargs):
    return _func_eval("_inincompatibility_remote_exec", args, kwargs)


def llm_qa(*args, **kwargs):
    return _func_eval("llm_qa", args, kwargs)

Now, you can directly import the generated code in another (Python / conda) environment:

# main.py
import torch
from to_import import llm_qa

assert str(torch.__version__).startswith('1.')

msg = [
    {"role": "system", "content": "You are a cat girl!"},
    {"role": "user", "content": "Who are you?"},
]

res = llm_qa(msg)
print(res["content"])

Run your main script (e.g., main.py) in the target environment:

conda activate black_box_prompt_optimizer
python main.py

For more details, check out the sample-llama directory on GitHub.

Example: Additional Samples:

For more usage examples, visit the sample1 directory on GitHub.

Project details


Download files

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

Source Distribution

inincompatibility-0.0.3.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inincompatibility-0.0.3-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file inincompatibility-0.0.3.tar.gz.

File metadata

  • Download URL: inincompatibility-0.0.3.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.7

File hashes

Hashes for inincompatibility-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a74e5a99583ec7cdcd1c3503238bfac9c7733180abd84d192b7e41cdba2d09df
MD5 b842761d4ebb6d9e985d26eaafe53ec2
BLAKE2b-256 3d580d79d96427310674e1d494b16c61dbb8fc6af218a058897bab8c75c2fc8a

See more details on using hashes here.

File details

Details for the file inincompatibility-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for inincompatibility-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e0ca297bf10eb6ae1c43448dd17dfc068d20292973a1ec7c97985fafc7493665
MD5 3d7abf549e40ba467e120e1e92ce4495
BLAKE2b-256 aff26d8ab1019c62babd2542c66f59588c42d6101d2add9f0f0aa078ff3e6552

See more details on using hashes here.

Supported by

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