modsysML icon indicating copy to clipboard operation
modsysML copied to clipboard

update provider interface for client in `__init__.py`

Open cloudguruab opened this issue 1 year ago • 0 comments

# todo: integrate into client
def get_provider_client(provider_path: str, *args, **kwargs):
    secret = kwargs["secret"] if "secret" in kwargs else None
    api_user = kwargs["api_user"] if "api_user" in kwargs else None

    if provider_path.startswith("openai:"):
        client = import_string(OPENAI_CLIENT_CLASS)
        path_parts = provider_path.split(":")
        model_name = path_parts[0]
        model_type = path_parts[1]
        if model_type == "chat":
            raise NotImplementedError
        elif model_type == "completion":
            return client("text-davinci-003")
        else:
            raise ValueError(f"Unknown OpenAI model type: {model_type}")
    elif provider_path.startswith("google_perspective:"):
        client = import_string(GOOGLE_PERSPECTIVE_CLIENT_CLASS)
        path_parts = provider_path.split(":")
        model_name = path_parts[0]
        model_type = path_parts[1]
        if model_type == "analyze":
            return client(model_type, secret)
        elif model_type == "suggest":
            raise NotImplementedError
        else:
            raise ValueError(f"Unknown OpenAI model type: {model_type}")
    elif provider_path.startswith("sightengine:"):
        client = import_string(SIGHTENGINE_CLIENT_CLASS)
        path_parts = provider_path.split(":")
        model_name = path_parts[0]
        model_type = ast.literal_eval(path_parts[1])

        if len(model_type) > 0:
            return client(model_type, secret=secret, api_user=api_user)
        else:
            raise ValueError(f"No model type set for Sightengine: {model_name}")
        return path_parts
    else:
        return importlib.import_module(provider_path).default()

cloudguruab avatar Sep 10 '23 23:09 cloudguruab