Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collect data tool #84

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/sayvai_tools/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from sayvai_tools.tools.youtube.comments import (ListCommentRepliesTool,
ReplyToCommentTool)
from sayvai_tools.tools.youtube.utils import get_youtube_credentials
from sayvai_tools.tools.collect_data_from_user import CollectUserDataTool, create_data_model

__all__: List[str] = [
"ConversationalHuman",
Expand All @@ -54,6 +55,8 @@
"ListCommentRepliesTool",
"ReplyToCommentTool",
"load_tools",
"CollectUserDataTool",
"create_data_model"
]


Expand Down
6 changes: 6 additions & 0 deletions src/sayvai_tools/tools/collect_data_from_user/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from sayvai_tools.tools.collect_data_from_user.collect_data_from_user import CollectUserDataTool, create_data_model

__all__ = [
"CollectUserDataTool",
"create_data_model"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Type, Optional, Dict

from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.tools import BaseTool
from pydantic import BaseModel, create_model, Field


def create_data_model(fields: Dict[str, str]) -> Type[BaseModel]:
"""
Dynamically creates a Pydantic model from the fields dictionary.

:param fields: A dictionary where keys are field names and values are descriptions.
:return: A dynamically created Pydantic model.
"""
field_definitions = {field: (str, Field(description=desc)) for field, desc in fields.items()}
return create_model('UserData', **field_definitions)


class CollectUserDataTool(BaseTool):
name: str = "user_data_collector"
description: str = "Collects data from the user based on specified fields."

args_schema: Type[BaseModel]

def _run(self, run_manager: Optional[CallbackManagerForToolRun] = None, **kwargs) -> bool:
try:
# Validate data using the dynamically created model
print("Collected and validated data:", kwargs)
return True
except Exception as e:
print("Error in data collection or validation:", str(e))
return False
Loading