Skip to content

Commit

Permalink
Removed features that required python 3.11 (#230)
Browse files Browse the repository at this point in the history
* Removed features that required python 3.11

* Added unit tests
  • Loading branch information
whitead authored Jan 26, 2024
1 parent 87c479b commit 765c602
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 37 deletions.
85 changes: 49 additions & 36 deletions paperqa/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@
import re
from abc import ABC, abstractmethod
from inspect import signature
from typing import (
Any,
AsyncGenerator,
Callable,
Coroutine,
Sequence,
Type,
cast,
get_args,
get_type_hints,
)
from typing import Any, AsyncGenerator, Callable, Coroutine, Sequence, Type, cast

import numpy as np
from openai import AsyncOpenAI
Expand All @@ -23,37 +13,60 @@
from .types import Doc, Embeddable, LLMResult, Text
from .utils import batch_iter, flatten, gather_with_concurrency, is_coroutine_callable

# only works for python 3.11
# def guess_model_type(model_name: str) -> str:
# """Guess the model type from the model name for OpenAI models"""
# import openai

def guess_model_type(model_name: str) -> str:
"""Guess the model type from the model name for OpenAI models"""
import openai

model_type = get_type_hints(
openai.types.chat.completion_create_params.CompletionCreateParamsBase
)["model"]
model_union = get_args(get_args(model_type)[1])
model_arr = list(model_union)
if model_name in model_arr:
return "chat"
return "completion"
# model_type = get_type_hints(
# openai.types.chat.completion_create_params.CompletionCreateParamsBase
# )["model"]
# model_union = get_args(get_args(model_type)[1])
# model_arr = list(model_union)
# if model_name in model_arr:
# return "chat"
# return "completion"

# def is_openai_model(model_name):
# import openai

# model_type = get_type_hints(
# openai.types.chat.completion_create_params.CompletionCreateParamsBase
# )["model"]
# model_union = get_args(get_args(model_type)[1])
# model_arr = list(model_union)

def is_openai_model(model_name):
import openai
# complete_model_types = get_type_hints(
# openai.types.completion_create_params.CompletionCreateParamsBase
# )["model"]
# complete_model_union = get_args(get_args(complete_model_types)[1])
# complete_model_arr = list(complete_model_union)

model_type = get_type_hints(
openai.types.chat.completion_create_params.CompletionCreateParamsBase
)["model"]
model_union = get_args(get_args(model_type)[1])
model_arr = list(model_union)
# return model_name in model_arr or model_name in complete_model_arr


def guess_model_type(model_name: str) -> str:
if model_name.startswith("babbage"):
return "completion"
if model_name.startswith("davinci"):
return "completion"
if "instruct" in model_name:
return "completion"
if model_name.startswith("gpt-4"):
if "base" in model_name:
return "completion"
return "chat"
if model_name.startswith("gpt-3.5"):
return "chat"
return "completion"

complete_model_types = get_type_hints(
openai.types.completion_create_params.CompletionCreateParamsBase
)["model"]
complete_model_union = get_args(get_args(complete_model_types)[1])
complete_model_arr = list(complete_model_union)

return model_name in model_arr or model_name in complete_model_arr
def is_openai_model(model_name) -> bool:
return (
model_name.startswith("gpt-")
or model_name.startswith("babbage")
or model_name.startswith("davinci")
)


def process_llm_config(llm_config: dict) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion paperqa/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "4.0.0-pre.3"
__version__ = "4.0.0-pre.4"
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"html2text",
"tiktoken>=0.4.0",
],
python_requires=">=3.11",
test_suite="tests",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
20 changes: 20 additions & 0 deletions tests/test_paperqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
OpenAIEmbeddingModel,
OpenAILLMModel,
get_score,
guess_model_type,
is_openai_model,
)
from paperqa.readers import read_doc
from paperqa.utils import (
Expand All @@ -37,6 +39,24 @@
)


def test_is_openai_model():
assert is_openai_model("gpt-3.5-turbo")
assert is_openai_model("babbage-002")
assert is_openai_model("gpt-4-1106-preview")
assert is_openai_model("davinci-002")
assert not is_openai_model("llama")
assert not is_openai_model("labgpt")
assert not is_openai_model("mixtral-7B")


def test_guess_model_type():
assert guess_model_type("gpt-3.5-turbo") == "chat"
assert guess_model_type("babbage-002") == "completion"
assert guess_model_type("gpt-4-1106-preview") == "chat"
assert guess_model_type("gpt-3.5-turbo-instruct") == "completion"
assert guess_model_type("davinci-002") == "completion"


def test_iter_citations():
text = (
"Yes, COVID-19 vaccines are effective. Various studies have documented the "
Expand Down

0 comments on commit 765c602

Please sign in to comment.