Spaces:
Build error
Build error
Romain Lembo
commited on
Commit
·
1ab73a3
1
Parent(s):
765902b
Add mathematical operation tools: add, multiply, and divide; implement python code parser tool
Browse files- agent.py +8 -0
- tools/add.py +12 -0
- tools/divide.py +14 -0
- tools/multiply.py +12 -0
- tools/python_code_parser.py +35 -0
agent.py
CHANGED
|
@@ -16,6 +16,10 @@ from supabase.client import Client, create_client
|
|
| 16 |
from tools.wiki_search import wiki_search
|
| 17 |
from tools.web_search import web_search
|
| 18 |
from tools.arvix_search import arvix_search
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# from tools.audio_transcription import audio_transcription
|
| 20 |
|
| 21 |
load_dotenv()
|
|
@@ -48,6 +52,10 @@ tools = [
|
|
| 48 |
wiki_search,
|
| 49 |
web_search,
|
| 50 |
arvix_search,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# audio_transcription,
|
| 52 |
]
|
| 53 |
|
|
|
|
| 16 |
from tools.wiki_search import wiki_search
|
| 17 |
from tools.web_search import web_search
|
| 18 |
from tools.arvix_search import arvix_search
|
| 19 |
+
from tools.python_code_parser import python_code_parser
|
| 20 |
+
from tools.multiply import multiply
|
| 21 |
+
from tools.add import add
|
| 22 |
+
from tools.divide import divide
|
| 23 |
# from tools.audio_transcription import audio_transcription
|
| 24 |
|
| 25 |
load_dotenv()
|
|
|
|
| 52 |
wiki_search,
|
| 53 |
web_search,
|
| 54 |
arvix_search,
|
| 55 |
+
python_code_parser,
|
| 56 |
+
add,
|
| 57 |
+
multiply,
|
| 58 |
+
divide,
|
| 59 |
# audio_transcription,
|
| 60 |
]
|
| 61 |
|
tools/add.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
@tool
|
| 4 |
+
def add(a: int, b: int) -> int:
|
| 5 |
+
"""Add two numbers.
|
| 6 |
+
Args:
|
| 7 |
+
a: first int
|
| 8 |
+
b: second int
|
| 9 |
+
Returns:
|
| 10 |
+
The sum of a and b.
|
| 11 |
+
"""
|
| 12 |
+
return a + b
|
tools/divide.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
@tool
|
| 4 |
+
def divide(a: int, b: int) -> int:
|
| 5 |
+
"""Divide two numbers.
|
| 6 |
+
Args:
|
| 7 |
+
a: first int
|
| 8 |
+
b: second int
|
| 9 |
+
Returns:
|
| 10 |
+
The result of dividing a by b.
|
| 11 |
+
"""
|
| 12 |
+
if b == 0:
|
| 13 |
+
raise ValueError("Impossible to divide by zero.")
|
| 14 |
+
return a / b
|
tools/multiply.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
@tool
|
| 4 |
+
def multiply(a: int, b: int) -> int:
|
| 5 |
+
"""Multiply two numbers.
|
| 6 |
+
Args:
|
| 7 |
+
a: first int
|
| 8 |
+
b: second int
|
| 9 |
+
Returns:
|
| 10 |
+
The product of a and b.
|
| 11 |
+
"""
|
| 12 |
+
return a * b
|
tools/python_code_parser.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tool to parse Python code."""
|
| 2 |
+
from langchain_core.tools import tool
|
| 3 |
+
from langchain_community.document_loaders.generic import GenericLoader
|
| 4 |
+
from langchain_community.document_loaders.parsers import LanguageParser
|
| 5 |
+
|
| 6 |
+
@tool
|
| 7 |
+
def python_code_parser(file_path: str) -> str:
|
| 8 |
+
"""
|
| 9 |
+
Parse Python code to extract function names and their docstrings.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
code: The Python code to parse.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
A string containing the function names and their docstrings.
|
| 16 |
+
"""
|
| 17 |
+
if not file_path:
|
| 18 |
+
return "0"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
loader = GenericLoader.from_filesystem(
|
| 22 |
+
file_path,
|
| 23 |
+
glob="**/*",
|
| 24 |
+
suffixes=[".py"],
|
| 25 |
+
parser=LanguageParser()
|
| 26 |
+
)
|
| 27 |
+
search_docs = loader.load()
|
| 28 |
+
|
| 29 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 30 |
+
[
|
| 31 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 32 |
+
for doc in search_docs
|
| 33 |
+
])
|
| 34 |
+
|
| 35 |
+
return {"audio_results": formatted_search_docs}
|