Skip to content

Commit

Permalink
Add support for passing token to initialise ChatWatsonx on cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszOssGit committed Nov 22, 2024
1 parent ec5b5f0 commit a3d4fe3
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 110 deletions.
17 changes: 15 additions & 2 deletions libs/ibm/langchain_ibm/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ class ChatWatsonx(BaseChatModel):
"""ID of the Watson Studio space."""

url: SecretStr = Field(
alias="url", default_factory=secret_from_env("WATSONX_URL", default=None)
alias="url",
default_factory=secret_from_env("WATSONX_URL", default=None), # type: ignore[assignment]
)
"""URL to the Watson Machine Learning or CPD instance."""

Expand Down Expand Up @@ -619,7 +620,19 @@ def validate_environment(self) -> Self:
check_for_attribute(self.url, "url", "WATSONX_URL")

if "cloud.ibm.com" in self.url.get_secret_value():
check_for_attribute(self.apikey, "apikey", "WATSONX_APIKEY")
if not self.token and not self.apikey:
raise ValueError(
"Did not find 'apikey' or 'token',"
" please add an environment variable"
" `WATSONX_APIKEY` or 'WATSONX_TOKEN' "
"which contains it,"
" or pass 'apikey' or 'token'"
" as a named parameter."
)
elif self.apikey:
check_for_attribute(self.apikey, "apikey", "WATSONX_APIKEY")
elif self.token:
check_for_attribute(self.token, "token", "WATSONX_TOKEN")
else:
if not self.token and not self.password and not self.apikey:
raise ValueError(
Expand Down
3 changes: 2 additions & 1 deletion libs/ibm/langchain_ibm/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class WatsonxEmbeddings(BaseModel, LangChainEmbeddings):
"""ID of the Watson Studio space."""

url: SecretStr = Field(
alias="url", default_factory=secret_from_env("WATSONX_URL", default=None)
alias="url",
default_factory=secret_from_env("WATSONX_URL", default=None), # type: ignore[assignment]
)
"""URL to the Watson Machine Learning or CPD instance."""

Expand Down
3 changes: 2 additions & 1 deletion libs/ibm/langchain_ibm/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class WatsonxLLM(BaseLLM):
"""ID of the Watson Studio space."""

url: SecretStr = Field(
alias="url", default_factory=secret_from_env("WATSONX_URL", default=None)
alias="url",
default_factory=secret_from_env("WATSONX_URL", default=None), # type: ignore[assignment]
)
"""URL to the Watson Machine Learning or CPD instance."""

Expand Down
3 changes: 2 additions & 1 deletion libs/ibm/langchain_ibm/rerank.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class WatsonxRerank(BaseDocumentCompressor):
"""ID of the Watson Studio space."""

url: SecretStr = Field(
alias="url", default_factory=secret_from_env("WATSONX_URL", default=None)
alias="url",
default_factory=secret_from_env("WATSONX_URL", default=None), # type: ignore[assignment]
)
"""URL to the Watson Machine Learning or CPD instance."""

Expand Down
212 changes: 110 additions & 102 deletions libs/ibm/poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libs/ibm/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langchain-ibm"
version = "0.3.4"
version = "0.3.5"
description = "An integration package connecting IBM watsonx.ai and LangChain"
authors = ["IBM"]
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions libs/ibm/tests/unit_tests/test_chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def test_initialize_chat_watsonx_cloud_bad_path() -> None:
try:
ChatWatsonx(model_id=MODEL_ID, url="https://us-south.ml.cloud.ibm.com") # type: ignore[arg-type]
except ValueError as e:
assert "apikey" in e.__str__()
assert "WATSONX_APIKEY" in e.__str__()
assert "apikey" in e.__str__() and "token" in e.__str__()
assert "WATSONX_APIKEY" in e.__str__() and "WATSONX_TOKEN" in e.__str__()


def test_initialize_chat_watsonx_cpd_bad_path_without_all() -> None:
Expand Down

0 comments on commit a3d4fe3

Please sign in to comment.