Skip to content
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

Add unit test for models.py #247

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

mikanfactory
Copy link
Contributor

@mikanfactory mikanfactory commented Apr 17, 2024

Reference Issues/PRs

Fixes #185

What does this implement/fix? Explain your changes.

  • Add unit tests for available models and refactor
  • Refactored to a test format that can be reused even when adding other models or new models
  • Fix bugs for anthropic's claude-2.1

Any other comments?

There is a conflict with #236 , so I will resolve after #236 gets merged.

🧡 Thanks for contributing!

Copy link

codecov bot commented Apr 17, 2024

Codecov Report

Attention: Patch coverage is 83.06452% with 21 lines in your changes missing coverage. Please review.

Project coverage is 78.25%. Comparing base (ad58268) to head (1f62a50).
Report is 437 commits behind head on main.

Files Patch % Lines
sweagent/agent/models.py 67.69% 21 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #247      +/-   ##
==========================================
+ Coverage   76.23%   78.25%   +2.02%     
==========================================
  Files          18       18              
  Lines        2845     2907      +62     
==========================================
+ Hits         2169     2275     +106     
+ Misses        676      632      -44     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@klieret klieret added the 🧪 CI Continuous integration/testing label Apr 17, 2024
@klieret
Copy link
Member

klieret commented Apr 18, 2024

Thanks a lot, this is awesome! Let me know once this is ready for review (it's still marked as a draft)

sweagent/agent/models.py Outdated Show resolved Hide resolved
@@ -429,8 +430,7 @@ class OllamaModel(BaseModel):

def __init__(self, args: ModelArguments, commands: list[Command]):
super().__init__(args, commands)
from ollama import Client
self.client = Client(host=args.host_url)
self.client = OllamaClient(host=args.host_url)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in the comment below, I changed it to a format that can be imported from tests like patch("sweagent.agent.models.OllamaClient").

@pytest.mark.parametrize("model_name", CLAUDE2_MODELS)
def test_anthropic2_model(model_name, mock_anthropic2_response):
with patch("sweagent.agent.models.config.Config"), \
patch("sweagent.agent.models.Anthropic") as mock_anthropic:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch here also works when specified as follows.

patch("anthropic.resources.messages.Messages.create")

While above format works, when we add a model, we need to look up the method file from the library. I thought this was difficult for both the implementer and the reviewer.
That's why I adopted this approach.

}


def split_claude_model_by_version():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Claude-2, we were sending requests with completions.create(), while for Claude-3, we were sending requests with messages.create(), so we needed to separate the test cases and divide the models.

@mikanfactory mikanfactory changed the title [WIP] Add unit test for models.py Add unit test for models.py Apr 19, 2024
@mikanfactory
Copy link
Contributor Author

@klieret
I was planning to mark it as "Ready for review" after a self-review, but It seems that the test I created is broken due to #207.
I will Request review once this is resolved.

@mikanfactory
Copy link
Contributor Author

mikanfactory commented Apr 20, 2024

After merging #207, the AnthropicModel tests broke. To fix this, we have two options: either modify the unit tests or fix the implementation of AnthropicModel. After looking at the implementation of AnthropicModel, I was confident that I could cleanly refactor it, so I decided to fix that this time.

Originally, the implementations of the history_to_messages and query methods in AnthropicModel had branches that executed different code snippets for Claude-2 and Claude-3 (using early returns). This time, an additional branch for Bedrock was added. Therefore:

  • I created mixins for Claude-2 and Claude-3, and split code snippets.
  • The branching for Bedrock or not was mostly commonizable, so I made it common.

I read through the comments on #207, and I think this mixin format will not become redundant when adding other models. (e.g. Add Bedrock to cohere, meta.)

I'm still new here, so I'm not sure if this PR follows the development rules here. If the PR is too large or the changes are too big, please feel free to comment!

@mikanfactory mikanfactory marked this pull request as ready for review April 20, 2024 14:27
@ofirpress
Copy link
Member

@klieret can we merge or close ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🧪 CI Continuous integration/testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unit test models.py
3 participants