-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dropped the SDK for requests library, Added some tests
- Loading branch information
Showing
7 changed files
with
210 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from garak.generators.watsonx import WatsonXGenerator | ||
import os | ||
import pytest | ||
import requests_mock | ||
|
||
|
||
DEFAULT_DEPLOYMENT_NAME = "ibm/granite-3-8b-instruct" | ||
|
||
|
||
@pytest.fixture | ||
def set_fake_env(request) -> None: | ||
stored_env = { | ||
WatsonXGenerator.ENV_VAR: os.getenv(WatsonXGenerator.ENV_VAR, None), | ||
WatsonXGenerator.PID_ENV_VAR: os.getenv(WatsonXGenerator.PID_ENV_VAR, None), | ||
WatsonXGenerator.URI_ENV_VAR: os.getenv(WatsonXGenerator.URI_ENV_VAR, None), | ||
WatsonXGenerator.DID_ENV_VAR: os.getenv(WatsonXGenerator.DID_ENV_VAR, None), | ||
} | ||
|
||
def restore_env(): | ||
for k, v in stored_env.items(): | ||
if v is not None: | ||
os.environ[k] = v | ||
else: | ||
del os.environ[k] | ||
|
||
os.environ[WatsonXGenerator.ENV_VAR] = "XXXXXXXXXXXXX" | ||
os.environ[WatsonXGenerator.PID_ENV_VAR] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" | ||
os.environ[WatsonXGenerator.DID_ENV_VAR] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" | ||
os.environ[WatsonXGenerator.URI_ENV_VAR] = "https://garak.example.com/" | ||
request.addfinalizer(restore_env) | ||
|
||
|
||
@pytest.mark.usefixtures("set_fake_env") | ||
def test_bearer_token(watsonx_compat_mocks): | ||
with requests_mock.Mocker() as m: | ||
mock_response = watsonx_compat_mocks["watsonx_bearer_token"] | ||
|
||
extended_request = "identity/token" | ||
|
||
m.post( | ||
"https://garak.example.com/" + extended_request, json=mock_response["json"] | ||
) | ||
|
||
granite_llm = WatsonXGenerator(DEFAULT_DEPLOYMENT_NAME) | ||
token = granite_llm._set_bearer_token(iam_url="https://garak.example.com/identity/token") | ||
|
||
assert granite_llm.bearer_token == ("Bearer " + mock_response["json"]["access_token"]) | ||
|
||
|
||
@pytest.mark.usefixtures("set_fake_env") | ||
def test_project(watsonx_compat_mocks): | ||
with requests_mock.Mocker() as m: | ||
mock_response = watsonx_compat_mocks["watsonx_generation"] | ||
extended_request = "/ml/v1/text/generation?version=2023-05-29" | ||
|
||
m.post( | ||
"https://garak.example.com/" + extended_request, json=mock_response["json"] | ||
) | ||
|
||
granite_llm = WatsonXGenerator(DEFAULT_DEPLOYMENT_NAME) | ||
response = granite_llm._generate_with_project("What is this?") | ||
|
||
assert granite_llm.name == response["model_id"] | ||
|
||
|
||
@pytest.mark.usefixtures("set_fake_env") | ||
def test_deployment(watsonx_compat_mocks): | ||
with requests_mock.Mocker() as m: | ||
mock_response = watsonx_compat_mocks["watsonx_generation"] | ||
extended_request = "/ml/v1/deployments/" | ||
extended_request += "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" | ||
extended_request += "/text/generation?version=2021-05-01" | ||
|
||
m.post( | ||
"https://garak.example.com/" + extended_request, json=mock_response["json"] | ||
) | ||
|
||
granite_llm = WatsonXGenerator(DEFAULT_DEPLOYMENT_NAME) | ||
response = granite_llm._generate_with_deployment("What is this?") | ||
|
||
assert granite_llm.name == response["model_id"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"watsonx_bearer_token": { | ||
"code": 200, | ||
"json": { | ||
"access_token": "fake_token1231231231", | ||
"refresh_token": "not_supported", | ||
"token_type": "Bearer", | ||
"expires_in": 3600, | ||
"expiration": 1737754747, | ||
"scope": "ibm openid" | ||
} | ||
}, | ||
"watsonx_generation": { | ||
"code": 200, | ||
"json" : { | ||
"model_id": "ibm/granite-3-8b-instruct", | ||
"model_version": "1.1.0", | ||
"created_at": "2025-01-24T20:51:59.520Z", | ||
"results": [ | ||
{ | ||
"generated_text": "This is a test generation. :)", | ||
"generated_token_count": 32, | ||
"input_token_count": 6, | ||
"stop_reason": "eos_token" | ||
} | ||
] | ||
} | ||
} | ||
} |