Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/config #46

Merged
merged 7 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions protollm_tools/llm-worker/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 85 additions & 12 deletions protollm_tools/llm-worker/protollm_worker/config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,89 @@
import os

REDIS_PREFIX = os.environ.get("REDIS_PREFIX", "llm-api")
REDIS_HOST = os.environ.get("REDIS_HOST", "localhost")
REDIS_PORT = os.environ.get("REDIS_PORT", "6379")

RABBIT_MQ_HOST = os.environ.get("RABBIT_MQ_HOST", "localhost")
RABBIT_MQ_PORT = os.environ.get("RABBIT_MQ_PORT", "5672")
RABBIT_MQ_LOGIN = os.environ.get("RABBIT_MQ_LOGIN", "admin")
RABBIT_MQ_PASSWORD = os.environ.get("RABBIT_MQ_PASSWORD", "admin")
class Config:
"""
Configuration class for setting up Redis, RabbitMQ, and model-specific parameters.

QUEUE_NAME = os.environ.get("QUEUE_NAME", "llm-api-queue")
MODEL_PATH = os.environ.get("MODEL_PATH")
TOKENS_LEN = int(os.environ.get("TOKENS_LEN"))
TENSOR_PARALLEL_SIZE = int(os.environ.get("TENSOR_PARALLEL_SIZE"))
GPU_MEMORY_UTILISATION = float(os.environ.get("GPU_MEMORY_UTILISATION"))
Attributes:
redis_host (str): The hostname of the Redis server. Defaults to "localhost".
redis_port (int): The port number of the Redis server. Defaults to 6379.
redis_prefix (str): Prefix for keys used in Redis. Defaults to "llm-api".
rabbit_host (str): The hostname of the RabbitMQ server. Defaults to "localhost".
rabbit_port (int): The port number of the RabbitMQ server. Defaults to 5672.
rabbit_login (str): The username for RabbitMQ authentication. Defaults to "admin".
rabbit_password (str): The password for RabbitMQ authentication. Defaults to "admin".
queue_name (str): The name of the RabbitMQ queue to use. Defaults to "llm-api-queue".
model_path (str): Path to the model being used. Defaults to None.
token_len (int): The maximum length of tokens for processing by the model. Defaults to None.
tensor_parallel_size (int): The size of tensor parallelism for distributed processing. Defaults to None.
gpu_memory_utilisation (float): The percentage of GPU memory utilization for the model. Defaults to None.
1martin1 marked this conversation as resolved.
Show resolved Hide resolved
"""

def __init__(
self,
redis_host: str = "localhost",
redis_port: int = 6379,
redis_prefix: str = "llm-api",
rabbit_host: str = "localhost",
rabbit_port: int = 5672,
rabbit_login: str = "admin",
rabbit_password: str = "admin",
1martin1 marked this conversation as resolved.
Show resolved Hide resolved
queue_name: str = "llm-api-queue",
model_path: str = None,
token_len: int = None,
tensor_parallel_size: int = None,
gpu_memory_utilisation: float = None,
):
self.redis_host = redis_host
self.redis_port = redis_port
self.redis_prefix = redis_prefix
self.rabbit_host = rabbit_host
self.rabbit_port = rabbit_port
self.rabbit_login = rabbit_login
self.rabbit_password = rabbit_password
self.queue_name = queue_name
self.model_path = model_path,
self.token_len = token_len,
self.tensor_parallel_size = tensor_parallel_size,
self.gpu_memory_utilisation = gpu_memory_utilisation,

@classmethod
def read_from_env(cls) -> 'Config':
return Config(
os.environ.get("REDIS_HOST", "localhost"),
os.environ.get("REDIS_PORT", "6379"),
os.environ.get("REDIS_PREFIX", "llm-api"),
os.environ.get("RABBIT_MQ_HOST", "localhost"),
os.environ.get("RABBIT_MQ_PORT", "5672"),
os.environ.get("RABBIT_MQ_LOGIN", "admin"),
os.environ.get("RABBIT_MQ_PASSWORD", "admin"),
os.environ.get("QUEUE_NAME", "llm-api-queue"),
os.environ.get("MODEL_PATH"),
int(os.environ.get("TOKENS_LEN", "16384")),
int(os.environ.get("TENSOR_PARALLEL_SIZE", "2")),
float(os.environ.get("GPU_MEMORY_UTILISATION", "0.9")),
)

@classmethod
def read_from_env_file(cls, path: str) -> 'Config':
with open(path) as file:
lines = file.readlines()
env_vars = {}
for line in lines:
key, value = line.split("=")
env_vars[key] = value
return Config(
env_vars.get("REDIS_HOST", "localhost"),
int(env_vars.get("REDIS_PORT", "6379")),
env_vars.get("REDIS_PREFIX", "llm-api"),
env_vars.get("RABBIT_MQ_HOST", "localhost"),
int(env_vars.get("RABBIT_MQ_PORT", "5672")),
env_vars.get("RABBIT_MQ_LOGIN", "admin"),
env_vars.get("RABBIT_MQ_PASSWORD", "admin"),
env_vars.get("QUEUE_NAME", "llm-api-queue"),
env_vars.get("MODEL_PATH"),
int(env_vars.get("TOKENS_LEN", "16384")),
int(env_vars.get("TENSOR_PARALLEL_SIZE", "2")),
float(env_vars.get("GPU_MEMORY_UTILISATION", "0.9")),
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут аналогично - если такой сетап нужен для тестов, то лучше создать отдельный env-файл с нужными значениями.

22 changes: 7 additions & 15 deletions protollm_tools/llm-worker/protollm_worker/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
from protollm_worker.config import MODEL_PATH, REDIS_HOST, REDIS_PORT, QUEUE_NAME
from protollm_worker.models.vllm_models import VllMModel
from protollm_worker.services.broker import LLMWrap
from protollm_worker.config import (
RABBIT_MQ_HOST, RABBIT_MQ_PORT,
RABBIT_MQ_PASSWORD, RABBIT_MQ_LOGIN,
REDIS_PREFIX
)
from protollm_worker.config import Config

if __name__ == "__main__":
llm_model = VllMModel(model_path=MODEL_PATH)
config = Config.read_from_env()
llm_model = VllMModel(model_path=config.model_path,
tensor_parallel_size=config.tensor_parallel_size,
gpu_memory_utilisation=config.gpu_memory_utilisation,
tokens_len=config.token_len)
llm_wrap = LLMWrap(llm_model=llm_model,
redis_host= REDIS_HOST,
redis_port= REDIS_PORT,
queue_name= QUEUE_NAME,
rabbit_host= RABBIT_MQ_HOST,
rabbit_port= RABBIT_MQ_PORT,
rabbit_login= RABBIT_MQ_LOGIN,
rabbit_password= RABBIT_MQ_PASSWORD,
redis_prefix= REDIS_PREFIX)
config= config)
llm_wrap.start_connection()
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
ChatCompletionTransactionModel, PromptTypes
from vllm import LLM, SamplingParams

from protollm_worker.config import GPU_MEMORY_UTILISATION, TENSOR_PARALLEL_SIZE, TOKENS_LEN
from protollm_worker.models.base import BaseLLM, LocalLLM

logging.basicConfig(level=logging.INFO)
Expand All @@ -17,7 +16,7 @@ class VllMModel(LocalLLM, BaseLLM):
and chat-based completions.
"""

def __init__(self, model_path, n_ctx=8192):
def __init__(self, model_path, tensor_parallel_size, gpu_memory_utilisation, tokens_len, n_ctx=8192):
"""
Initialize the vLLM-based model.

Expand All @@ -30,9 +29,9 @@ def __init__(self, model_path, n_ctx=8192):

self.model = LLM(
model=model_path,
tensor_parallel_size=TENSOR_PARALLEL_SIZE,
gpu_memory_utilization=GPU_MEMORY_UTILISATION,
max_model_len=TOKENS_LEN
tensor_parallel_size=tensor_parallel_size,
gpu_memory_utilization=gpu_memory_utilisation,
max_model_len=tokens_len
)
self.handlers = {
PromptTypes.SINGLE_GENERATION.value: self.generate,
Expand Down
66 changes: 10 additions & 56 deletions protollm_tools/llm-worker/protollm_worker/services/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import pika
from protollm_sdk.models.job_context_models import PromptModel, ChatCompletionModel, PromptTransactionModel, \
PromptWrapper, ChatCompletionTransactionModel
from protollm_sdk.object_interface import RabbitMQWrapper
from protollm_sdk.object_interface.redis_wrapper import RedisWrapper

from protollm_worker.config import Config
from protollm_worker.models.base import BaseLLM

logging.basicConfig(level=logging.INFO)
Expand All @@ -22,83 +24,35 @@ class LLMWrap:

def __init__(self,
llm_model: BaseLLM,
redis_host: str,
redis_port: str,
queue_name: str,
rabbit_host: str,
rabbit_port: str,
rabbit_login: str,
rabbit_password: str,
redis_prefix: str):
config: Config):
"""
Initialize the LLMWrap class with the necessary configurations.

:param llm_model: The language model to use for processing prompts.
:type llm_model: BaseLLM
:param redis_host: Hostname for the Redis server.
:type redis_host: str
:param redis_port: Port for the Redis server.
:type redis_port: str
:param queue_name: Name of the RabbitMQ queue to consume messages from.
:type queue_name: str
:param rabbit_host: Hostname for the RabbitMQ server.
:type rabbit_host: str
:param rabbit_port: Port for the RabbitMQ server.
:type rabbit_port: str
:param rabbit_login: Login for RabbitMQ authentication.
:type rabbit_login: str
:param rabbit_password: Password for RabbitMQ authentication.
:type rabbit_password: str
:param redis_prefix: Prefix for Redis keys to store results.
:type redis_prefix: str
:param config: Set for setting Redis and RabbitMQ.
:type config: Config
"""
self.llm = llm_model
logger.info('Loaded model')

self.redis_bd = RedisWrapper(redis_host, redis_port)
self.redis_prefix = redis_prefix
self.redis_bd = RedisWrapper(config.redis_host, config.redis_port)
self.rabbitMQ = RabbitMQWrapper(config.rabbit_host, config.rabbit_port, config.rabbit_login, config.rabbit_password)
self.redis_prefix = config.redis_prefix
logger.info('Connected to Redis')

self.models = {
'single_generate': PromptModel,
'chat_completion': ChatCompletionModel,
}

self.queue_name = queue_name
self.rabbit_host = rabbit_host
self.rabbit_port = rabbit_port
self.rabbit_login = rabbit_login
self.rabbit_password = rabbit_password
self.queue_name = config.queue_name

def start_connection(self):
"""
Establish a connection to the RabbitMQ broker and start consuming messages from the specified queue.
"""
connection = pika.BlockingConnection(
pika.ConnectionParameters(
host=self.rabbit_host,
port=self.rabbit_port,
virtual_host='/',
credentials=pika.PlainCredentials(
username=self.rabbit_login,
password=self.rabbit_password
)
)
)

channel = connection.channel()
logger.info('Connected to the broker')

channel.queue_declare(queue=self.queue_name)
logger.info('Queue has been declared')

channel.basic_consume(
on_message_callback=self._callback,
queue=self.queue_name,
auto_ack=True
)

channel.start_consuming()
self.rabbitMQ.consume_messages(self.queue_name, self._callback)
logger.info('Started consuming messages')

def _dump_from_body(self, message_body) -> PromptModel | ChatCompletionModel:
Expand Down
2 changes: 1 addition & 1 deletion protollm_tools/llm-worker/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ python = "^3.10"
redis = "^5.0.5"
pika = "^1.3.2"
pydantic = "^2.7.4"
protollm_sdk = "^1.0.0"
protollm_sdk = "^1.1.0"
vllm = "^0.6.4.post1"

[toll.poetry.llama-cpp]
Expand Down
2 changes: 1 addition & 1 deletion protollm_tools/sdk/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "protollm-sdk"
version = "1.1.0"
version = "1.1.1"
description = ""
authors = ["aimclub"]
readme = "README.md"
Expand Down
Loading