-
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.
* anthropic model and asset file added * fixed formatting issue * added anthropic package * fixed calling ModelBase * updated name and info * Updated cases for exception and test * Clean up Anthropic error handling by using built-in exceptions * Remove unused `api_base` in AnthropicModel * Expand config tests * updated with doc string * Update image input tests --------- Co-authored-by: Fahim Imaduddin Dalvi <faimaduddin@hbku.edu.qa>
- Loading branch information
Showing
2 changed files
with
127 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from llmebench import Benchmark | ||
from llmebench.models import AnthropicModel | ||
|
||
from llmebench.utils import is_fewshot_asset | ||
|
||
|
||
class TestAssetsForAnthropicPrompts(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
# Load the benchmark assets | ||
benchmark = Benchmark(benchmark_dir="assets") | ||
all_assets = benchmark.find_assets() | ||
|
||
# Filter out assets not using the Petals model | ||
cls.assets = [ | ||
asset | ||
for asset in all_assets | ||
if asset["config"]["model"] in [AnthropicModel] | ||
] | ||
|
||
def test_anthropic_prompts(self): | ||
"Test if all assets using this model return data in an appropriate format for prompting" | ||
# self.test_openai_prompts() | ||
n_shots = 3 # Sample for few shot prompts | ||
|
||
for asset in self.assets: | ||
with self.subTest(msg=asset["name"]): | ||
config = asset["config"] | ||
dataset_args = config.get("dataset_args", {}) | ||
dataset_args["data_dir"] = "" | ||
dataset = config["dataset"](**dataset_args) | ||
data_sample = dataset.get_data_sample() | ||
if is_fewshot_asset(config, asset["module"].prompt): | ||
prompt = asset["module"].prompt( | ||
data_sample["input"], | ||
[data_sample for _ in range(n_shots)], | ||
) | ||
else: | ||
prompt = asset["module"].prompt(data_sample["input"]) | ||
|
||
self.assertIsInstance(prompt, list) | ||
|
||
for message in prompt: | ||
self.assertIsInstance(message, dict) | ||
self.assertIn("role", message) | ||
self.assertIsInstance(message["role"], str) | ||
self.assertIn("content", message) | ||
self.assertIsInstance(message["content"], (str, list)) | ||
|
||
# Multi-modal input | ||
if isinstance(message["content"], list): | ||
for elem in message["content"]: | ||
self.assertIsInstance(elem, dict) | ||
self.assertIn("type", elem) | ||
|
||
if elem["type"] == "text": | ||
self.assertIn("text", elem) | ||
self.assertIsInstance(elem["text"], str) | ||
elif elem["type"] == "image": | ||
self.assertIn("source", elem) | ||
self.assertIsInstance(elem["source"], dict) | ||
|
||
# Current support is for base64 | ||
self.assertIn("type", elem["source"]) | ||
self.assertIsInstance(elem["source"]["type"], str) | ||
self.assertIn("data", elem["source"]) | ||
self.assertIsInstance(elem["source"]["data"], str) | ||
|
||
self.assertIn("media_type", elem["source"]) | ||
self.assertIsInstance(elem["source"]["media_type"], str) | ||
|
||
|
||
class TestAnthropicConfig(unittest.TestCase): | ||
def test_anthropic_config(self): | ||
"Test if model config parameters passed as arguments are used" | ||
model = AnthropicModel(api_key="secret-key", model_name="private-model") | ||
self.assertEqual(model.api_key, "secret-key") | ||
self.assertEqual(model.model, "private-model") | ||
|
||
@patch.dict( | ||
"os.environ", | ||
{ | ||
"ANTHROPIC_API_KEY": "secret-env-key", | ||
"ANTHROPIC_MODEL": "private-env-model", | ||
}, | ||
) | ||
def test_anthropic_config_env_var(self): | ||
"Test if model config parameters passed as environment variables are used" | ||
model = AnthropicModel() | ||
|
||
self.assertEqual(model.api_key, "secret-env-key") | ||
self.assertEqual(model.model, "private-env-model") | ||
|
||
@patch.dict( | ||
"os.environ", | ||
{ | ||
"ANTHROPIC_API_KEY": "secret-env-key", | ||
"ANTHROPIC_MODEL": "private-env-model", | ||
}, | ||
) | ||
def test_anthropic_config_priority(self): | ||
"Test if model config parameters passed as environment variables are used" | ||
model = AnthropicModel(api_key="secret-key", model_name="private-model") | ||
|
||
self.assertEqual(model.api_key, "secret-key") | ||
self.assertEqual(model.model, "private-model") |