|
| 1 | +""" |
| 2 | +Example demonstrating custom tools in RLM. |
| 3 | +
|
| 4 | +Custom tools allow you to provide domain-specific functions and data |
| 5 | +that the RLM can use in its REPL environment. |
| 6 | +
|
| 7 | +Run with: python -m examples.custom_tools_example |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +from typing import Any |
| 12 | + |
| 13 | +from dotenv import load_dotenv |
| 14 | + |
| 15 | +from rlm import RLM |
| 16 | + |
| 17 | +load_dotenv() |
| 18 | + |
| 19 | + |
| 20 | +# ============================================================================= |
| 21 | +# Define Custom Tools |
| 22 | +# ============================================================================= |
| 23 | + |
| 24 | + |
| 25 | +def fetch_stock_price(symbol: str) -> dict[str, Any]: |
| 26 | + """ |
| 27 | + Fetch stock price data for a given symbol. |
| 28 | + In a real application, this would call a financial API. |
| 29 | + """ |
| 30 | + # Mock data for demonstration |
| 31 | + prices = { |
| 32 | + "AAPL": {"price": 178.50, "change": 2.3, "volume": 52_000_000}, |
| 33 | + "GOOGL": {"price": 141.25, "change": -0.8, "volume": 21_000_000}, |
| 34 | + "MSFT": {"price": 378.90, "change": 1.5, "volume": 18_000_000}, |
| 35 | + "AMZN": {"price": 178.75, "change": 0.2, "volume": 35_000_000}, |
| 36 | + } |
| 37 | + return prices.get(symbol.upper(), {"error": f"Symbol {symbol} not found"}) |
| 38 | + |
| 39 | + |
| 40 | +def calculate_portfolio_value(holdings: dict[str, int]) -> float: |
| 41 | + """ |
| 42 | + Calculate total portfolio value given holdings. |
| 43 | + holdings: dict mapping symbol to number of shares |
| 44 | + """ |
| 45 | + total = 0.0 |
| 46 | + for symbol, shares in holdings.items(): |
| 47 | + data = fetch_stock_price(symbol) |
| 48 | + if "price" in data: |
| 49 | + total += data["price"] * shares |
| 50 | + return total |
| 51 | + |
| 52 | + |
| 53 | +def format_currency(amount: float) -> str: |
| 54 | + """Format a number as USD currency.""" |
| 55 | + return f"${amount:,.2f}" |
| 56 | + |
| 57 | + |
| 58 | +# Configuration data (non-callable values become variables) |
| 59 | +MARKET_CONFIG = { |
| 60 | + "trading_hours": {"open": "09:30", "close": "16:00"}, |
| 61 | + "currency": "USD", |
| 62 | + "exchange": "NYSE", |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +# ============================================================================= |
| 67 | +# Example 1: Basic Custom Tools |
| 68 | +# ============================================================================= |
| 69 | + |
| 70 | + |
| 71 | +def example_basic_tools(): |
| 72 | + """Demonstrate basic custom tools usage with descriptions.""" |
| 73 | + print("=" * 60) |
| 74 | + print("Example 1: Basic Custom Tools with Descriptions") |
| 75 | + print("=" * 60) |
| 76 | + |
| 77 | + # Tools can be provided with descriptions using dict format: |
| 78 | + # {"name": {"tool": callable_or_value, "description": "..."}} |
| 79 | + # The description will be included in the system prompt so the model |
| 80 | + # knows what each tool does. |
| 81 | + |
| 82 | + rlm = RLM( |
| 83 | + backend="portkey", |
| 84 | + backend_kwargs={ |
| 85 | + "model_name": "@openai/gpt-5-nano", |
| 86 | + "api_key": os.getenv("PORTKEY_API_KEY"), |
| 87 | + }, |
| 88 | + environment="local", |
| 89 | + custom_tools={ |
| 90 | + # Callable functions with descriptions (dict format) |
| 91 | + "fetch_stock_price": { |
| 92 | + "tool": fetch_stock_price, |
| 93 | + "description": "Fetch current stock price data for a symbol (AAPL, GOOGL, MSFT, AMZN)", |
| 94 | + }, |
| 95 | + "calculate_portfolio_value": { |
| 96 | + "tool": calculate_portfolio_value, |
| 97 | + "description": "Calculate total portfolio value from a dict of {symbol: shares}", |
| 98 | + }, |
| 99 | + "format_currency": { |
| 100 | + "tool": format_currency, |
| 101 | + "description": "Format a number as USD currency string", |
| 102 | + }, |
| 103 | + # Data values with descriptions |
| 104 | + "MARKET_CONFIG": { |
| 105 | + "tool": MARKET_CONFIG, |
| 106 | + "description": "Market configuration including trading hours and exchange info", |
| 107 | + }, |
| 108 | + }, |
| 109 | + verbose=True, |
| 110 | + ) |
| 111 | + |
| 112 | + # The model can now use these tools to answer questions |
| 113 | + result = rlm.completion( |
| 114 | + "What's the current price of AAPL and GOOGL? " |
| 115 | + "Calculate the total value of a portfolio with 100 shares of each. " |
| 116 | + "Format the result as currency." |
| 117 | + ) |
| 118 | + |
| 119 | + print(f"\nFinal Answer: {result.response}") |
| 120 | + |
| 121 | + |
| 122 | +# ============================================================================= |
| 123 | +# Main |
| 124 | +# ============================================================================= |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + if not os.getenv("OPENAI_API_KEY"): |
| 129 | + print("Please set OPENAI_API_KEY environment variable") |
| 130 | + print("You can also modify this example to use a different backend") |
| 131 | + exit(1) |
| 132 | + |
| 133 | + example_basic_tools() |
0 commit comments