From 2248705c4a70c8c749079a7cf5df9d7be526d2b6 Mon Sep 17 00:00:00 2001 From: kingbri Date: Thu, 16 Nov 2023 01:26:46 -0500 Subject: [PATCH] Requirements: Don't force fastchat installation Fastchat requires a lot of dependencies such as transformers, peft, and accelerate which are heavy. This is not useful unless a user wants to add a shim for the chat completion endpoint. Instead, try importing fastchat and notify the console of the error. Signed-off-by: kingbri --- OAI/utils.py | 25 ++++++++++++++++++++++++- requirements.txt | Bin 122 -> 108 bytes 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/OAI/utils.py b/OAI/utils.py index 87ce8be..769991b 100644 --- a/OAI/utils.py +++ b/OAI/utils.py @@ -1,5 +1,4 @@ import pathlib -from fastchat.model.model_adapter import get_conversation_template, Conversation from OAI.types.completion import CompletionResponse, CompletionRespChoice from OAI.types.chat_completion import ( ChatCompletionMessage, @@ -10,8 +9,17 @@ from OAI.types.chat_completion import ( ) from OAI.types.common import UsageStats from OAI.types.model import ModelList, ModelCard +from packaging import version from typing import Optional, List +# Check fastchat +try: + import fastchat + from fastchat.model.model_adapter import get_conversation_template + _fastchat_available = True +except ImportError: + _fastchat_available = False + def create_completion_response(text: str, model_name: Optional[str]): # TODO: Add method to get token amounts in model for UsageStats @@ -78,6 +86,21 @@ def get_model_list(model_path: pathlib.Path): return model_card_list def get_chat_completion_prompt(model_path: str, messages: List[ChatCompletionMessage]): + # Check if fastchat is available + + if not _fastchat_available: + raise ModuleNotFoundError( + "Fastchat must be installed to parse these chat completion messages.\n" + "Please run the following command: pip install fschat[model_worker]" + ) + if version.parse(fastchat.__version__) < version.parse("0.2.23"): + raise ImportError( + "Parsing these chat completion messages requires fastchat 0.2.23 or greater. " + f"Current version: {fastchat.__version__}\n" + "Please upgrade fastchat by running the following command: " + "pip install -U fschat[model_worker]" + ) + conv = get_conversation_template(model_path) for message in messages: msg_role = message.role diff --git a/requirements.txt b/requirements.txt index 7dd575d1ae64974f918f9006ba70bbb7a525ce8d..2db8867c89f5809158b98a6f1e9595db455a0971 100644 GIT binary patch delta 4 Lcmb=*nUDhj1iS&` delta 19 acmd0)nvlbn#!$?V%#gv5$WX$-#Q*>}4Fq%m