-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/anthropic #326
Merged
Merged
Feat/anthropic #326
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2704056
anthropic model and asset file added
firojalam 78946d7
fixed formatting issue
firojalam d003705
added anthropic package
firojalam 6602c29
fixed calling ModelBase
firojalam e9f33b0
updated name and info
firojalam a358dcf
Updated cases for exception and test
firojalam ad99ac5
fixed conflict
firojalam c6b4143
Clean up Anthropic error handling by using built-in exceptions
fdalvi fbe253b
Remove unused `api_base` in AnthropicModel
fdalvi af12b30
Expand config tests
fdalvi 2b1ba55
Merge branch 'main' into feat/anthropic
firojalam 2d7b5bc
updated with doc string
firojalam 96925d2
Update image input tests
fdalvi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@firojalam Can you please update the docstring under
def prompt(self, processed_input):
to indicate what the required format is (and also maybe talk about multimodal?).Right now, there are only two ways a user knows what their asset should return, either this doc string or a failing test. The test you have already done, but its nice to have this docstring reflect the correct format as well. Feel free to take a look at the other model docstrings as well.