- 
                Notifications
    You must be signed in to change notification settings 
- Fork 51
tests: tool runner #327
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
                    tests: tool runner #327
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      b7749ae
              
                chore: enhance logs
              
              
                maciejmajek 4f0ead7
              
                test: ToolRunner
              
              
                maciejmajek 831a81f
              
                chore: add license
              
              
                maciejmajek 40edef5
              
                chore: set pytest verbosity in pyproject.toml
              
              
                maciejmajek 500d338
              
                test: add invalid toolcall test
              
              
                maciejmajek 3a8e765
              
                chore: print minimized tool output
              
              
                maciejmajek 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
    
  
  
    
              
  
    
      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,82 @@ | ||
| # Copyright (C) 2024 Robotec.AI | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|  | ||
| import logging | ||
|  | ||
| from langchain_core.messages import AIMessage, ToolCall, ToolMessage | ||
| from langchain_core.tools import tool | ||
|  | ||
| from rai.agents.tool_runner import ToolRunner | ||
| from rai.messages import HumanMultimodalMessage, ToolMultimodalMessage | ||
| from rai.messages.utils import preprocess_image | ||
| from rai.tools.ros.debugging import ros2_topic | ||
|  | ||
|  | ||
| @tool(response_format="content_and_artifact") | ||
| def get_image(): | ||
| """Get an image from the camera""" | ||
| return "Image retrieved", { | ||
| "images": [preprocess_image("docs/imgs/o3deSimulation.png")] | ||
| } | ||
|  | ||
|  | ||
| def test_tool_runner_invalid_call(): | ||
| runner = ToolRunner(tools=[ros2_topic], logger=logging.getLogger(__name__)) | ||
| tool_call = ToolCall(name="bad_fn", args={"command": "list"}, id="12345") | ||
| state = {"messages": [AIMessage(content="", tool_calls=[tool_call])]} | ||
| output = runner.invoke(state) | ||
| assert isinstance( | ||
| output["messages"][0], AIMessage | ||
| ), "First message is not an AIMessage" | ||
| assert isinstance( | ||
| output["messages"][1], ToolMessage | ||
| ), "Tool output is not a tool message" | ||
| assert output["messages"][1].status == "error" | ||
|  | ||
|  | ||
| def test_tool_runner(): | ||
| runner = ToolRunner(tools=[ros2_topic], logger=logging.getLogger(__name__)) | ||
|  | ||
| tool_call = ToolCall(name="ros2_topic", args={"command": "list"}, id="12345") | ||
| state = {"messages": [AIMessage(content="", tool_calls=[tool_call])]} | ||
| output = runner.invoke(state) | ||
| assert isinstance( | ||
| output["messages"][0], AIMessage | ||
| ), "First message is not an AIMessage" | ||
| assert isinstance( | ||
| output["messages"][1], ToolMessage | ||
| ), "Tool output is not a tool message" | ||
| assert ( | ||
| len(output["messages"][-1].content) > 0 | ||
| ), "Tool output is empty. At least rosout should be visible." | ||
|  | ||
|         
                  maciejmajek marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| def test_tool_runner_multimodal(): | ||
| runner = ToolRunner( | ||
| tools=[ros2_topic, get_image], logger=logging.getLogger(__name__) | ||
| ) | ||
|  | ||
| tool_call = ToolCall(name="get_image", args={}, id="12345") | ||
| state = {"messages": [AIMessage(content="", tool_calls=[tool_call])]} | ||
| output = runner.invoke(state) | ||
|  | ||
| assert isinstance( | ||
| output["messages"][0], AIMessage | ||
| ), "First message is not an AIMessage" | ||
| assert isinstance( | ||
| output["messages"][1], ToolMultimodalMessage | ||
| ), "Tool output is not a multimodal message" | ||
| assert isinstance( | ||
| output["messages"][2], HumanMultimodalMessage | ||
| ), "Human output is not a multimodal message" | ||
|         
                  maciejmajek marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.