community[patch] : Tidy up and update Clarifai SDK functions (#18314)

Description :
* Tidy up, add missing docstring and fix unused params
* Enable using session token
This commit is contained in:
Phat Vo
2024-03-08 10:47:44 +07:00
committed by GitHub
parent 93b87f2bfb
commit 3ecb903d49
3 changed files with 117 additions and 128 deletions

View File

@@ -4,8 +4,7 @@ from typing import Any, Dict, List, Optional
from langchain_core.callbacks import CallbackManagerForLLMRun
from langchain_core.language_models.llms import LLM
from langchain_core.outputs import Generation, LLMResult
from langchain_core.pydantic_v1 import Extra, root_validator
from langchain_core.utils import get_from_dict_or_env
from langchain_core.pydantic_v1 import Extra, Field, root_validator
from langchain_community.llms.utils import enforce_stop_tokens
@@ -42,8 +41,11 @@ class Clarifai(LLM):
"""Clarifai application id to use."""
user_id: Optional[str] = None
"""Clarifai user id to use."""
pat: Optional[str] = None
pat: Optional[str] = Field(default=None, exclude=True) #: :meta private:
"""Clarifai personal access token to use."""
token: Optional[str] = Field(default=None, exclude=True) #: :meta private:
"""Clarifai session token to use."""
model: Any = Field(default=None, exclude=True) #: :meta private:
api_base: str = "https://api.clarifai.com"
class Config:
@@ -55,21 +57,32 @@ class Clarifai(LLM):
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that we have all required info to access Clarifai
platform and python package exists in environment."""
values["pat"] = get_from_dict_or_env(values, "pat", "CLARIFAI_PAT")
try:
from clarifai.client.model import Model
except ImportError:
raise ImportError(
"Could not import clarifai python package. "
"Please install it with `pip install clarifai`."
)
user_id = values.get("user_id")
app_id = values.get("app_id")
model_id = values.get("model_id")
model_version_id = values.get("model_version_id")
model_url = values.get("model_url")
api_base = values.get("api_base")
pat = values.get("pat")
token = values.get("token")
if model_url is not None and model_id is not None:
raise ValueError("Please provide either model_url or model_id, not both.")
if model_url is None and model_id is None:
raise ValueError("Please provide one of model_url or model_id.")
if model_url is None and model_id is not None:
if user_id is None or app_id is None:
raise ValueError("Please provide a user_id and app_id.")
values["model"] = Model(
url=model_url,
app_id=app_id,
user_id=user_id,
model_version=dict(id=model_version_id),
pat=pat,
token=token,
model_id=model_id,
base_url=api_base,
)
return values
@@ -117,28 +130,10 @@ class Clarifai(LLM):
response = clarifai_llm("Tell me a joke.")
"""
# If version_id None, Defaults to the latest model version
try:
from clarifai.client.model import Model
except ImportError:
raise ImportError(
"Could not import clarifai python package. "
"Please install it with `pip install clarifai`."
)
if self.pat is not None:
pat = self.pat
if self.model_url is not None:
_model_init = Model(url=self.model_url, pat=pat)
else:
_model_init = Model(
model_id=self.model_id,
user_id=self.user_id,
app_id=self.app_id,
pat=pat,
)
try:
(inference_params := {}) if inference_params is None else inference_params
predict_response = _model_init.predict_by_bytes(
predict_response = self.model.predict_by_bytes(
bytes(prompt, "utf-8"),
input_type="text",
inference_params=inference_params,
@@ -165,27 +160,15 @@ class Clarifai(LLM):
# TODO: add caching here.
try:
from clarifai.client.input import Inputs
from clarifai.client.model import Model
except ImportError:
raise ImportError(
"Could not import clarifai python package. "
"Please install it with `pip install clarifai`."
)
if self.pat is not None:
pat = self.pat
if self.model_url is not None:
_model_init = Model(url=self.model_url, pat=pat)
else:
_model_init = Model(
model_id=self.model_id,
user_id=self.user_id,
app_id=self.app_id,
pat=pat,
)
generations = []
batch_size = 32
input_obj = Inputs(pat=pat)
input_obj = Inputs.from_auth_helper(self.model.auth_helper)
try:
for i in range(0, len(prompts), batch_size):
batch = prompts[i : i + batch_size]
@@ -196,7 +179,7 @@ class Clarifai(LLM):
(
inference_params := {}
) if inference_params is None else inference_params
predict_response = _model_init.predict(
predict_response = self.model.predict(
inputs=input_batch, inference_params=inference_params
)