-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from tigergraph/GML-1767-watsonx-integration
Gml 1767 watsonx integration
- Loading branch information
Showing
8 changed files
with
152 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import os | ||
|
||
from common.llm_services import LLM_Model | ||
from common.logs.log import req_id_cv | ||
from common.logs.logwriter import LogWriter | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class IBMWatsonX(LLM_Model): | ||
def __init__(self, config): | ||
super().__init__(config) | ||
for auth_detail in config["authentication_configuration"].keys(): | ||
os.environ[auth_detail] = config["authentication_configuration"][ | ||
auth_detail | ||
] | ||
|
||
from langchain_ibm import ChatWatsonx | ||
|
||
model_name = config["llm_model"] | ||
self.llm = ChatWatsonx( | ||
params={"temperature": config["model_kwargs"]["temperature"], "max_new_tokens": config["model_kwargs"]["max_new_tokens"]}, | ||
url=config["authentication_configuration"]["WATSONX_URL"], | ||
apikey=config["authentication_configuration"]["WATSONX_APIKEY"], | ||
model_id=model_name, | ||
project_id=config["model_kwargs"]["project_id"] | ||
) | ||
self.prompt_path = config["prompt_path"] | ||
LogWriter.info( | ||
f"request_id={req_id_cv.get()} instantiated WatsonX model_name={model_name}" | ||
) | ||
|
||
@property | ||
def map_question_schema_prompt(self): | ||
return self._read_prompt_file(self.prompt_path + "map_question_to_schema.txt") | ||
|
||
@property | ||
def generate_function_prompt(self): | ||
return self._read_prompt_file(self.prompt_path + "generate_function.txt") | ||
|
||
@property | ||
def entity_relationship_extraction_prompt(self): | ||
return self._read_prompt_file( | ||
self.prompt_path + "entity_relationship_extraction.txt" | ||
) | ||
|
||
@property | ||
def model(self): | ||
return self.llm |
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,59 @@ | ||
import os | ||
import unittest | ||
|
||
import pytest | ||
from fastapi.testclient import TestClient | ||
from test_service import CommonTests | ||
import wandb | ||
import parse_test_config | ||
import sys | ||
|
||
|
||
@pytest.mark.skip(reason="All tests in this class are currently skipped by the pipeline, but used by the LLM regression tests.") | ||
class TestWithWatsonX(CommonTests, unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls) -> None: | ||
from main import app | ||
|
||
cls.client = TestClient(app) | ||
cls.llm_service = "mistralai/mistral-large" | ||
if USE_WANDB: | ||
cls.table = wandb.Table(columns=columns) | ||
|
||
|
||
def test_config_read(self): | ||
resp = self.client.get("/") | ||
self.assertEqual(resp.json()["config"], "mistral-large") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = parse_test_config.create_parser() | ||
|
||
args = parser.parse_known_args()[0] | ||
|
||
USE_WANDB = args.wandb | ||
|
||
schema = args.schema | ||
|
||
if USE_WANDB: | ||
columns = [ | ||
"LLM_Service", | ||
"Dataset", | ||
"Question Type", | ||
"Question Theme", | ||
"Question", | ||
"True Answer", | ||
"True Function Call", | ||
"Retrieved Natural Language Answer", | ||
"Retrieved Answer", | ||
"Answer Source", | ||
"Answer Correct", | ||
"Answered Question", | ||
"Response Time (seconds)", | ||
] | ||
CommonTests.setUpClass(schema) | ||
|
||
# clean up args before unittesting | ||
del sys.argv[1:] | ||
unittest.main() |