sanatan_ai / tools.py
vikramvasudevan's picture
Upload folder using huggingface_hub
a1180f7 verified
import json
from langchain.agents import Tool
from langchain_core.tools import StructuredTool
from config import SanatanConfig
from nalayiram_helper import (
get_standardized_azhwar_names,
get_standardized_divya_desam_names,
get_standardized_prabandham_names,
)
from push_notifications_helper import push
from serperdev_helper import search as search_web
from sanatan_assistant import (
format_scripture_answer,
query,
)
tool_push = Tool(
name="push", description="Send a push notification to the user", func=push
)
allowed_collections = [s["collection_name"] for s in SanatanConfig.scriptures]
tool_search_db = StructuredTool.from_function(
query,
name="tool_search_db",
description=(
"🚫 use this tool to fetch any data from the database."
"rules for metadata_where_clause:"
"""
- ⚠️ Every time you include a metadata_where_clause argument, you must first call the appropriate standardization tool (tool_get_standardized_divya_desam_names,tool_get_standardized_prabandham_names,tool_get_standardized_azhwar_names). Never insert raw values directly. Even if the input already looks correct, you must still call the tool. If you fail to do this, the query will be invalid.
> Standardization Step 1: Call the standardization tool to get the canonical Divya Desam name.
|--Example:
|----standardized_divya_desams = tool_get_standardized_divya_desam_names()
|----standardized_divya_desam = look for closest match to "Thirukkudandai" in standardized_divya_desams
> Standardization Step 2: Use the standardized name in your DB search argument for metadata_where_clause for the field divya_desams.
- When choosing collection_name argument for the tool_search_db, make sure you choose the exact collection_name from the metadata configuration above
- Always prefer a single tool call with composite filters rather than multiple calls.
- For MetadataWhereClause.filters.$.metadata_search_operator do not use $regex as angument. use semantic search option by using query argument instead.
- If user posts a thematic question, do not ignore the theme when you pass `query` arguments.
- Use `MetadataWhereClause` recursively with `filters` and `groups` to build nested conditions.
"""
"- Always set metadata filters when user mentions a specific divya desam, prabandham, azhwar, or any other known field from the configuration. Example: {\"prabandham_name\": \"Thiruvaimozhi\"}."
"- Multiple metadata filters can be passed at the same time."
"- If passing '$in' as metadata_search_operator, the metadata_value should always be of type array. for instance {'metadata_field': 'divya_desams', 'metadata_search_operator': '$in', 'metadata_value': []'Srirangam']}"
"- Set metadata filters as None if no metadata filter is requested.\n"
"rules for search_type:"
"- use `random` if user does not provide a thematic/semantic search request. For e.g. 'any pasuram' or 'any pasuram from thiruvaimozhi'"
"- use `semantic` if user provides thematic/semantic search request"
"- use `literal` ONLY if user specifically requests for a literal search."
"\n"
f"The collection_name must be one of: {', '.join(allowed_collections)}."
),
)
tool_search_web = Tool(
name="search_web", description="Search the web for information", func=search_web
)
tool_format_scripture_answer = StructuredTool.from_function(
format_scripture_answer,
name="tool_format_scripture_answer",
description=(
"""
Use this tool to generate a custom system prompt based on the scripture title, question, and query_tool_output.
This is especially useful when the user has asked a question about a scripture, and the relevant context has been fetched using the tools like `tool_search_db_by_metadata` or `tool_search_db_by_literal_text` or `tool_semantic_vector_search`.
The generated prompt will guide the assistant to respond using only that scripture’s content, with a clear format including Sanskrit/Tamil verses, English explanations, and source chapters.
Include a santized version of the original native text. Do ensure you dont lose any words from the native text when sanitizing.
"""
),
)
tool_get_standardized_azhwar_names = StructuredTool.from_function(
get_standardized_azhwar_names,
name="tool_get_standardized_azhwar_names",
description=(
"Get a list of standardized azhwar names and the prabandhams they have written. "
"Use this tool to standardize the names of the azhwars when the user asks for pasurams written by a specific azhwar."
"Usually this is followed by passing that standardized azhwar_name for a metadata search using the `tool_search_db_by_metadata` tool."
),
)
tool_get_standardized_prabandham_names = StructuredTool.from_function(
get_standardized_prabandham_names,
name="tool_get_standardized_prabandham_names",
description=(
"Get a list of standardized prabandham names."
"Use this tool to standardize the names of the prabandhams when the user asks for pasurams from a specific prabandham."
"Usually this is followed by passing that `prabandham_name` for a metadata search using the `tool_search_db_by_metadata` tool."
"if you are passing the prabandham_code, make sure you use metadata field `prabandham` and NOT `prabandham_code` as argument to `tool_search_db_by_metadata` tool."
),
)
tool_get_standardized_divya_desam_names = StructuredTool.from_function(
get_standardized_divya_desam_names,
name="tool_get_standardized_divya_desam_names",
description=(
"Get a list of standardized divya desam names. "
"Use this tool to standardize the names of the divya desams when the user asks for pasurams written on a specific divya desam."
"Usually this is followed by passing that standardized divya desam name for a metadata search using the `tool_search_db_by_metadata` tool by using the fiels `divya_desams`."
),
)