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

Decompose is_openai_model into separate call for Anyscale #287

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 4 additions & 5 deletions paperqa/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
VectorStore,
VoyageAIEmbeddingModel,
get_score,
is_anyscale_model,
llm_model_factory,
vector_store_factory,
)
Expand Down Expand Up @@ -205,12 +206,10 @@ def set_client(
embedding_client: Any | None = None,
):
if client is None and isinstance(self.llm_model, OpenAILLMModel):
if (api_key := os.environ.get("ANYSCALE_API_KEY")) and (
base_url := os.environ.get("ANYSCALE_BASE_URL")
):
if is_anyscale_model(self.llm_model.name):
client = AsyncOpenAI(
api_key=api_key,
base_url=base_url,
api_key=os.environ["ANYSCALE_API_KEY"],
base_url=os.environ["ANYSCALE_BASE_URL"],
)
else:
client = AsyncOpenAI()
Expand Down
39 changes: 22 additions & 17 deletions paperqa/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@

# return model_name in model_arr or model_name in complete_model_arr

ANYSCALE_MODEL_PREFIXES: tuple[str, ...] = (
"meta-llama/Meta-Llama-3-",
"mistralai/Mistral-",
"mistralai/Mixtral-",
)


def guess_model_type(model_name: str) -> str: # noqa: PLR0911
if model_name.startswith("babbage"):
Expand All @@ -58,13 +64,7 @@ def guess_model_type(model_name: str) -> str: # noqa: PLR0911
if (
os.environ.get("ANYSCALE_API_KEY")
and os.environ.get("ANYSCALE_BASE_URL")
and model_name.startswith("meta-llama/Meta-Llama-3-")
):
return "chat"
if (
os.environ.get("ANYSCALE_API_KEY")
and os.environ.get("ANYSCALE_BASE_URL")
and (model_name.startswith(("mistralai/Mistral-", "mistralai/Mixtral-")))
and (model_name.startswith(ANYSCALE_MODEL_PREFIXES))
):
return "chat"
if "instruct" in model_name:
Expand All @@ -78,17 +78,22 @@ def guess_model_type(model_name: str) -> str: # noqa: PLR0911
return "completion"


def is_openai_model(model_name) -> bool:
open_ai_model_prefixes = {"gpt-", "babbage", "davinci", "ft:gpt-"}
# add special prefixes if the user has anyscale models
def is_anyscale_model(model_name: str) -> bool:
# compares prefixes with anyscale models
# https://docs.anyscale.com/endpoints/text-generation/query-a-model/
if os.environ.get("ANYSCALE_API_KEY") and os.environ.get("ANYSCALE_BASE_URL"):
open_ai_model_prefixes |= {
"meta-llama/Meta-Llama-3-",
"mistralai/Mistral-",
"mistralai/Mixtral-",
}
return model_name.startswith(tuple(open_ai_model_prefixes))
if (
os.environ.get("ANYSCALE_API_KEY")
and os.environ.get("ANYSCALE_BASE_URL")
and model_name.startswith(ANYSCALE_MODEL_PREFIXES)
):
return True
return False


def is_openai_model(model_name: str) -> bool:
return is_anyscale_model(model_name) or model_name.startswith(
("gpt-", "babbage", "davinci", "ft:gpt-")
)


def process_llm_config(
Expand Down