revamping crewai tool

This commit is contained in:
João Moura
2024-02-25 21:11:09 -03:00
parent 7c99e9ab50
commit 50bae27948
21 changed files with 100 additions and 144 deletions

View File

View File

@@ -1,67 +0,0 @@
from typing import Callable
from chromadb import Documents, EmbeddingFunction, Embeddings
from embedchain import App
from embedchain.config import AppConfig, ChromaDbConfig
from embedchain.embedder.base import BaseEmbedder
from embedchain.vectordb.chroma import ChromaDB
from crewai_tools.adapters.embedchain_adapter import EmbedchainAdapter
class MockEmbeddingFunction(EmbeddingFunction):
fn: Callable
def __init__(self, embedding_fn: Callable):
self.fn = embedding_fn
def __call__(self, input: Documents) -> Embeddings:
return self.fn(input)
def test_embedchain_adapter(helpers):
embedding_function = MockEmbeddingFunction(
embedding_fn=helpers.get_embedding_function()
)
embedder = BaseEmbedder()
embedder.set_embedding_fn(embedding_function) # type: ignore
db = ChromaDB(
config=ChromaDbConfig(
dir="tests/data/chromadb",
collection_name="requirements",
)
)
app = App(
config=AppConfig(
id="test",
),
db=db,
embedding_model=embedder,
)
adapter = EmbedchainAdapter(
dry_run=True,
embedchain_app=app,
)
assert (
adapter.query("What are the requirements for the task?")
== """
Use the following pieces of context to answer the query at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Technical requirements
The system should be able to process 1000 transactions per second. The code must be written in Ruby. | Problem
Currently, we are not able to find out palindromes in a given string. We need a solution to this problem. | Solution
We need a function that takes a string as input and returns true if the string is a palindrome, otherwise false.
Query: What are the requirements for the task?
Helpful Answer:
"""
)

View File

@@ -1,22 +0,0 @@
from crewai_tools.adapters.lancedb_adapter import LanceDBAdapter
def test_lancedb_adapter(helpers):
adapter = LanceDBAdapter(
uri="tests/data/lancedb",
table_name="requirements",
embedding_function=helpers.get_embedding_function(),
top_k=2,
vector_column_name="vector",
text_column_name="text",
)
assert (
adapter.query("What are the requirements for the task?")
== """Technical requirements
The system should be able to process 1000 transactions per second. The code must be written in Ruby.
Problem
Currently, we are not able to find out palindromes in a given string. We need a solution to this problem."""
)

46
tests/base_tool_test.py Normal file
View File

@@ -0,0 +1,46 @@
import json
import pydantic_core
import pytest
from crewai_tools import BaseTool, tool
def test_creating_a_tool_using_annotation():
@tool("Name of my tool")
def my_tool(question: str) -> str:
"""Clear description for what this tool is useful for, you agent will need this information to use it."""
return question
# Assert all the right attributes were defined
assert my_tool.name == "Name of my tool"
assert my_tool.description == "Clear description for what this tool is useful for, you agent will need this information to use it."
assert my_tool.args_schema.schema()["properties"] == {'question': {'title': 'Question', 'type': 'string'}}
assert my_tool.func("What is the meaning of life?") == "What is the meaning of life?"
# Assert the langchain tool conversion worked as expected
converted_tool = my_tool.to_langchain()
assert converted_tool.name == "Name of my tool"
assert converted_tool.description == "Clear description for what this tool is useful for, you agent will need this information to use it."
assert converted_tool.args_schema.schema()["properties"] == {'question': {'title': 'Question', 'type': 'string'}}
assert converted_tool.func("What is the meaning of life?") == "What is the meaning of life?"
def test_creating_a_tool_using_baseclass():
class MyCustomTool(BaseTool):
name: str = "Name of my tool"
description: str = "Clear description for what this tool is useful for, you agent will need this information to use it."
def _run(self, question: str) -> str:
return question
my_tool = MyCustomTool()
# Assert all the right attributes were defined
assert my_tool.name == "Name of my tool"
assert my_tool.description == "Clear description for what this tool is useful for, you agent will need this information to use it."
assert my_tool.args_schema.schema()["properties"] == {'question': {'title': 'Question', 'type': 'string'}}
assert my_tool.run("What is the meaning of life?") == "What is the meaning of life?"
# Assert the langchain tool conversion worked as expected
converted_tool = my_tool.to_langchain()
assert converted_tool.name == "Name of my tool"
assert converted_tool.description == "Clear description for what this tool is useful for, you agent will need this information to use it."
assert converted_tool.args_schema.schema()["properties"] == {'question': {'title': 'Question', 'type': 'string'}}
assert converted_tool.run("What is the meaning of life?") == "What is the meaning of life?"

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@@ -1 +0,0 @@
$d2c46569-d173-4b3f-b589-f8f00eddc371²Vtext ÿÿÿÿÿÿÿÿÿ*string085vector ÿÿÿÿÿÿÿÿÿ*fixed_size_list:float:153608

View File

@@ -1,21 +0,0 @@
from crewai_tools.tools.rag.rag_tool import Adapter, RagTool
class MockAdapter(Adapter):
answer: str
def query(self, question: str) -> str:
return self.answer
def test_rag_tool():
adapter = MockAdapter(answer="42")
rag_tool = RagTool(adapter=adapter)
assert rag_tool.name == "Knowledge base"
assert (
rag_tool.description == "A knowledge base that can be used to answer questions."
)
assert (
rag_tool.run("What is the answer to life, the universe and everything?") == "42"
)