Skip to content

Commit

Permalink
Enhance custom tool doc
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanSallami committed Jun 12, 2024
1 parent cb9e62d commit 85d363b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
59 changes: 49 additions & 10 deletions docs/api/tools/custom-tools.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
# Custom Tools

In HybridAGI, each tool is a DSPy module that is optimized in the background when the interpreter call it during optimization.
To add a new tool to HybridAGI, you have to create an object that inherit from `BaseTool`:
In HybridAGI, each tool is a module that is optimized in the background when the interpreter calls it during optimization. Each tool is a DSPy module that is optimized in the background when the interpreter calls it during optimization.

To add a new tool to HybridAGI, there are two ways to do it. The first way is easier, while the other gives you more control but involves more code.

Let's see the easiest way first:

```python

def greet(**kwargs):
"""
This function greets the person passed in as a parameter
"""
name = kwargs["name"] # The function inputs need to use python **kwargs
# Here goes your tool logic
message = f"Hello, {name}. Nice to meet you!"
print(message)
# You have then to returns the output of your function as a dict
output = {}
output["message"] = message
return output

my_custom_tool = Tool(
name = "Greet", # The name of the tool
signature = "name -> message", # The function signature (multi input/output are possible like in DSPy signature)
instructions = "Greet a person", # The tool instruction
func = greet, # And the callable function
lm = my_lm # You can optionally use another LM to infer the inputs of the tool
)

```

The other way to create a custom tool gives you better control, for that you have to create an object that inherit from `BaseTool`:

```python
import abc
Expand Down Expand Up @@ -31,7 +61,8 @@ To implement your own tool, here is a toy example:
```python

import dspy
from hybridagi.tools.base import BaseTool
from typing import Optional
from hybridagi.tools import BaseTool
from hybridagi.output_parsers.prediction import PredictionOutputParser

def greet(name):
Expand All @@ -53,7 +84,10 @@ class GreetSignature(dspy.Signature):

class GreetTool(BaseTool):

def __init__(self):
def __init__(
self,
lm = Optional[dspy.LM] = None,
):
super().__init__(name = "Greet")
self.predict = dspy.Predict(GreetSignature)
self.prediction_parser = PredictionOutputParser()
Expand All @@ -69,12 +103,15 @@ class GreetTool(BaseTool):
"""Method to perform DSPy forward prediction"""
if not disable_inference:
# Perform a DSPy prediction
pred = self.predict(
objective = objective,
context = context,
purpose = purpose,
prompt = prompt,
)
with dspy.context(lm=self.lm if self.lm is not None else dspy.settings.lm):
# This allow the tool to use a different LM than the one you configured in DSPy
# Allowing you to choose the best LM to infer the tool inputs
pred = self.predict(
objective = objective,
context = context,
purpose = purpose,
prompt = prompt,
)
# Some parsing to clean up the prediction
pred.name = self.prediction_parser.parse(pred.name, prefix = "Name:", stop = ["\n"])
# Then we can call our python function
Expand All @@ -92,4 +129,6 @@ class GreetTool(BaseTool):
answer = prompt
message = message
)

my_custom_tool = GreetTool()
```
2 changes: 2 additions & 0 deletions static/robot.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-Agent: GPTBot
Disallow: /

0 comments on commit 85d363b

Please sign in to comment.