- 
                Notifications
    
You must be signed in to change notification settings  - Fork 9
 
Fix mistralai test cases #438
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      1485c4b
              
                Add test cases
              
              
                bitterpanda63 f37d439
              
                create after_async wrapper
              
              
                bitterpanda63 6458ba6
              
                anthropic add create_async wrapp
              
              
                bitterpanda63 fbca6ec
              
                Add test cases for openai (only run with token set)
              
              
                bitterpanda63 a4d7396
              
                Fix mistralai test cases
              
              
                bitterpanda63 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 hidden or 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 hidden or 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 | 
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| from aikido_zen.helpers.on_ai_call import on_ai_call | ||
| from aikido_zen.helpers.register_call import register_call | ||
| from aikido_zen.sinks import on_import, patch_function, after | ||
| from aikido_zen.sinks import on_import, patch_function, after, after_async | ||
| 
     | 
||
| 
     | 
||
| @after | ||
| 
        
          
        
         | 
    @@ -16,6 +16,20 @@ def _messages_create(func, instance, args, kwargs, return_value): | |
| ) | ||
| 
     | 
||
| 
     | 
||
| @after_async | ||
| async def _messages_create_async(func, instance, args, kwargs, return_value): | ||
| op = f"anthropic.resources.messages.messages.Messages.create" | ||
| register_call(op, "ai_op") | ||
| 
     | 
||
| on_ai_call( | ||
| provider="anthropic", | ||
| model=return_value.model, | ||
| input_tokens=return_value.usage.input_tokens, | ||
| output_tokens=return_value.usage.output_tokens, | ||
| ) | ||
| 
     | 
||
| 
     | 
||
| @on_import("anthropic.resources.messages") | ||
| def patch(m): | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function 'patch' lacks clear purpose indication - generic name without docstring or return type hint to clarify its role in API monitoring More info  | 
||
| patch_function(m, "messages.Messages.create", _messages_create) | ||
| patch_function(m, "messages.AsyncMessages.create", _messages_create_async) | ||
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| import os | ||
| 
     | 
||
| import pytest | ||
| import aikido_zen.sinks.openai | ||
| 
     | 
||
| from aikido_zen.thread.thread_cache import get_cache | ||
| 
     | 
||
| skip_no_api_key = pytest.mark.skipif( | ||
| "OPENAI_API_KEY" not in os.environ, | ||
| reason="OPENAI_API_KEY environment variable not set", | ||
| ) | ||
| 
     | 
||
| 
     | 
||
| @pytest.fixture(autouse=True) | ||
| def setup(): | ||
| get_cache().reset() | ||
| yield | ||
| get_cache().reset() | ||
| 
     | 
||
| 
     | 
||
| @pytest.fixture | ||
| def client(): | ||
| import openai | ||
| 
     | 
||
| return openai.OpenAI() | ||
| 
     | 
||
| 
     | 
||
| def get_ai_stats(): | ||
| return get_cache().ai_stats.get_stats() | ||
| 
     | 
||
| 
     | 
||
| @skip_no_api_key | ||
| def test_openai_responses_create_with_vision(client): | ||
| prompt = "What is in this image?" | ||
| img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/2023_06_08_Raccoon1.jpg/1599px-2023_06_08_Raccoon1.jpg" | ||
| 
     | 
||
| response = client.responses.create( | ||
| model="gpt-4o-mini", | ||
| input=[ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| {"type": "input_text", "text": prompt}, | ||
| {"type": "input_image", "image_url": f"{img_url}"}, | ||
| ], | ||
| } | ||
| ], | ||
| max_output_tokens=25, | ||
| ) | ||
| print(response) | ||
| 
     | 
||
| assert get_ai_stats()[0]["model"] == "gpt-4o-mini-2024-07-18" | ||
| assert get_ai_stats()[0]["calls"] == 1 | ||
| assert get_ai_stats()[0]["provider"] == "openai" | ||
| assert get_ai_stats()[0]["tokens"]["input"] == 36848 | ||
| assert get_ai_stats()[0]["tokens"]["output"] == 25 | ||
| assert get_ai_stats()[0]["tokens"]["total"] == 36873 | ||
| 
     | 
||
| 
     | 
||
| @skip_no_api_key | ||
| def test_openai_chat_complete(client): | ||
| completion = client.chat.completions.create( | ||
| model="gpt-4o", | ||
| max_tokens=15, | ||
| messages=[ | ||
| {"role": "developer", "content": "Talk like a pirate."}, | ||
| { | ||
| "role": "user", | ||
| "content": "Who is the best French painter? Answer in one short sentence.", | ||
| }, | ||
| ], | ||
| ) | ||
| answer = completion.choices[0].message.content | ||
| print(answer) | ||
| 
     | 
||
| assert get_ai_stats()[0]["model"] == "gpt-4o-2024-08-06" | ||
| assert get_ai_stats()[0]["calls"] == 1 | ||
| assert get_ai_stats()[0]["provider"] == "openai" | ||
| assert get_ai_stats()[0]["tokens"]["input"] == 29 | ||
| assert get_ai_stats()[0]["tokens"]["output"] == 15 | ||
| assert get_ai_stats()[0]["tokens"]["total"] == 44 | ||
| 
     | 
||
| 
     | 
||
| @skip_no_api_key | ||
| def test_openai_responses_create(client): | ||
| response = client.responses.create( | ||
| model="gpt-4o", | ||
| instructions="You are a coding assistant that talks like a pirate.", | ||
| input="How do I check if a Python object is an instance of a class?", | ||
| max_output_tokens=18, | ||
| ) | ||
| print(response.output_text) | ||
| 
     | 
||
| assert get_ai_stats()[0]["model"] == "gpt-4o-2024-08-06" | ||
| assert get_ai_stats()[0]["calls"] == 1 | ||
| assert get_ai_stats()[0]["provider"] == "openai" | ||
| assert get_ai_stats()[0]["tokens"]["input"] == 37 | ||
| assert get_ai_stats()[0]["tokens"]["output"] == 18 | ||
| assert get_ai_stats()[0]["tokens"]["total"] == 55 | 
      
      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.
Function '_messages_create_async' lacks clear purpose indication - no descriptive name, docstring, or return type hint to clarify it's a tracking callback More info