### Quickstart Monte Carlo Agent with Multiplication Tool (Python) Source: https://github.com/shobrook/saplings/blob/master/README.md This snippet demonstrates the basic setup of a Saplings MonteCarloAgent. It initializes the agent with a custom MultiplicationTool, a Model wrapper for LiteLLM, and an Evaluator, then runs a sample arithmetic query. ```python from saplings.examples import MultiplicationTool from saplings import MonteCarloAgent, Evaluator, Model model = Model(model="openai/gpt-4o") # Wraps LiteLLM evaluator = Evaluator(model) tools = [MultiplicationTool()] agent = MonteCarloAgent(tools, model, evaluator) messages, _, _ = agent.run("Let x = 9418.343 * 8.11 and y = 2x. Calculate (xy)(x^2).") ``` -------------------------------- ### Installing Saplings (Bash) Source: https://github.com/shobrook/saplings/blob/master/README.md This command installs the Saplings library using the pip package manager. It is the standard way to get the library installed in your Python environment. ```bash $ pip install saplings ``` -------------------------------- ### Creating a Saplings Tool (Python) Source: https://github.com/shobrook/saplings/blob/master/README.md Demonstrates how to create a custom tool for a Saplings agent by extending the `Tool` base class. It shows the required instance variables (`name`, `description`, `parameters`, `is_terminal`) and the asynchronous `run` method that executes the tool's logic. The example implements a simple multiplication tool. ```Python from saplings.abstract import Tool class MultiplicationTool(Tool): def __init__(self, **kwargs): self.name = "multiply" self.description = "Multiplies two numbers and returns the result number." self.parameters = { "type": "object", "properties": { "a": { "type": "number", "description": "The number to multiply." }, "b": { "type": "number", "description": "The number to multiply by." } }, "required": ["a", "b"], "additionalProperties": False } self.is_terminal = False async def run(self, a, b, **kwargs): return a * b ``` -------------------------------- ### Setting up Saplings Evaluator (Python) Source: https://github.com/shobrook/saplings/blob/master/README.md Illustrates how to set up the evaluator component for a Saplings agent. The evaluator guides the search process by scoring trajectories. The default evaluator uses an LLM (the provided `model`) to perform this scoring. ```Python from saplings import Evaluator evaluator = Evaluator(model) ``` -------------------------------- ### Example Saplings Agent Message Structure Source: https://github.com/shobrook/saplings/blob/master/README.md Illustrates the basic structure of message objects within an agent's trajectory, highlighting the inclusion of a `raw_output` property specifically for tool responses, which differs from the `content` property shown to the model. ```JSON [{"role": "user", "content": "This is my prompt!"}, ..., {"role": "assistant", "content": "This is a response!"}] ``` -------------------------------- ### Initializing Saplings Model (Python) Source: https://github.com/shobrook/saplings/blob/master/README.md Shows how to select a language model using LiteLLM and create a `Model` object for use with a Saplings agent. Additional keyword arguments passed to the `Model` constructor will be forwarded to LiteLLM completion calls. ```Python from saplings import Model model = Model("openai/gpt-4o") ``` -------------------------------- ### Initializing Saplings Agent (Python) Source: https://github.com/shobrook/saplings/blob/master/README.md Demonstrates how to initialize a Saplings agent instance, such as the `MonteCarloAgent`, by providing the list of available tools, the configured model, and the evaluator. This sets up the agent with its core components. ```Python from saplings import MonteCarloAgent agent = MonteCarloAgent(tools, model, evaluator) ``` -------------------------------- ### Running Saplings Agent (Python) Source: https://github.com/shobrook/saplings/blob/master/README.md Shows how to execute the configured Saplings agent on a given input prompt using the `run` method for synchronous execution or `run_async` for asynchronous execution. The output includes the best message trajectory, the final score, and whether the trajectory was considered a solution. ```Python messages, score, is_solution = agent.run("What's 2 * 2?") # await agent.run_async("What's 2 * 2?") ``` -------------------------------- ### Running Saplings Agent and Converting Messages (Python) Source: https://github.com/shobrook/saplings/blob/master/README.md This snippet demonstrates how to run a Saplings agent with a given prompt and then convert the resulting list of Message objects into a list of OpenAI-compatible message dictionaries using the to_openai_response() method. It then prints the converted messages. Requires an initialized agent object. ```python messages, _, _ = agent.run("This is my prompt!") messages = [message.to_openai_response() for message in messages] print(messages) ``` -------------------------------- ### Formatting Tool Output in Saplings Python Tool Source: https://github.com/shobrook/saplings/blob/master/README.md Demonstrates how to implement the optional `format_output` method in a Saplings `Tool` subclass. This method allows customizing the string representation of the tool's output that is presented to the agent model, while the original raw output is still stored separately. ```Python from saplings.abstract import Tool class MultiplicationTool(Tool): ... async def run(self, a, b, **kwargs): return {"a": a, "b": b, "result": a * b} def format_output(self, output): a, b = output['a'], output['b'] result = output['result'] return f"{a} * {b} = {result}" ``` -------------------------------- ### Implementing a Custom Saplings Evaluator in Python Source: https://github.com/shobrook/saplings/blob/master/README.md This Python snippet demonstrates the basic structure required to create a custom evaluator in saplings. It shows how to inherit from the `Evaluator` base class and define the asynchronous `run` method, which is responsible for evaluating a search trajectory (list of `Message` objects) and returning an `Evaluation` object containing a score and optional reasoning. Users must replace the placeholder implementation within the `run` method with their specific evaluation logic. ```python from saplings.abstract import Evaluator from saplings.dtos import Evaluation from typing import List # Assuming Message is imported or defined elsewhere, e.g., from saplings.dtos # from saplings.dtos import Message class CustomEvaluator(Evaluator): def __init__(self): pass async def run(self, trajectory: List[Message]) -> Evaluation: # Implement this return Evaluation(score=1.0, reasoning="Justification goes here.") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.