Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -145,7 +145,7 @@ async def chat_streaming(debug_mode: bool, message, history, thread_id):
|
|
| 145 |
"debug_mode": debug_mode,
|
| 146 |
"messages": (history or []) + [{"role": "user", "content": message}],
|
| 147 |
}
|
| 148 |
-
config = {"configurable": {"thread_id": thread_id}}
|
| 149 |
start_time = time.time()
|
| 150 |
streamed_response = ""
|
| 151 |
final_response = ""
|
|
@@ -440,7 +440,7 @@ with gr.Blocks(
|
|
| 440 |
scripture["example_labels"], scripture["examples"]
|
| 441 |
):
|
| 442 |
btn = gr.Button(value=f"{example_label}", size="sm")
|
| 443 |
-
btn.click(close_side_bar,outputs=[sidebar]).then(
|
| 444 |
populate_chat_input,
|
| 445 |
inputs=[gr.State(example_text)],
|
| 446 |
outputs=[message_textbox],
|
|
|
|
| 145 |
"debug_mode": debug_mode,
|
| 146 |
"messages": (history or []) + [{"role": "user", "content": message}],
|
| 147 |
}
|
| 148 |
+
config = {"configurable": {"thread_id": thread_id}, "recursion_limit": 15}
|
| 149 |
start_time = time.time()
|
| 150 |
streamed_response = ""
|
| 151 |
final_response = ""
|
|
|
|
| 440 |
scripture["example_labels"], scripture["examples"]
|
| 441 |
):
|
| 442 |
btn = gr.Button(value=f"{example_label}", size="sm")
|
| 443 |
+
btn.click(close_side_bar, outputs=[sidebar]).then(
|
| 444 |
populate_chat_input,
|
| 445 |
inputs=[gr.State(example_text)],
|
| 446 |
outputs=[message_textbox],
|
config.py
CHANGED
|
@@ -178,12 +178,13 @@ class SanatanConfig:
|
|
| 178 |
{
|
| 179 |
"name": "title",
|
| 180 |
"datatype": "str",
|
| 181 |
-
"description": (
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
| 187 |
),
|
| 188 |
},
|
| 189 |
{
|
|
|
|
| 178 |
{
|
| 179 |
"name": "title",
|
| 180 |
"datatype": "str",
|
| 181 |
+
"description": (
|
| 182 |
+
"Title of this pasuram."
|
| 183 |
+
"Use this when a specific prabandham code or name is given along with a relative verse number."
|
| 184 |
+
"for example:\n"
|
| 185 |
+
"- `TVM 1.8.3`\n"
|
| 186 |
+
"- if the user query is 'give me 3rd pasuram in the 8th Thiruvaimozhi of the 1st decade.' - you must convert this representation to the format '{prabandham_code} {nth_decade}.{nth_chapter}.{nth_pasuram}' and pass as filter vaoue to the `title` field. \n"
|
| 187 |
+
"If no decade is provided but a prabandham name is provided, assume decade = 1"
|
| 188 |
),
|
| 189 |
},
|
| 190 |
{
|
graph_helper.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from typing import Annotated, TypedDict
|
| 2 |
from langgraph.graph import StateGraph, START, END
|
| 3 |
from langgraph.checkpoint.memory import MemorySaver
|
|
@@ -5,6 +6,7 @@ from langgraph.graph.message import add_messages
|
|
| 5 |
from langchain_openai import ChatOpenAI
|
| 6 |
from langgraph.graph.state import CompiledStateGraph
|
| 7 |
from langchain_core.messages import AIMessage
|
|
|
|
| 8 |
from tools import (
|
| 9 |
tool_format_scripture_answer,
|
| 10 |
tool_get_standardized_prabandham_names,
|
|
@@ -25,15 +27,17 @@ logger.setLevel(logging.INFO)
|
|
| 25 |
|
| 26 |
|
| 27 |
class ChatState(TypedDict):
|
| 28 |
-
debug_mode
|
| 29 |
messages: Annotated[list[str], add_messages]
|
| 30 |
|
|
|
|
| 31 |
def check_debug_condition(state: ChatState) -> str:
|
| 32 |
if state["debug_mode"]:
|
| 33 |
return "validator"
|
| 34 |
else:
|
| 35 |
return "__end__"
|
| 36 |
|
|
|
|
| 37 |
def branching_condition(state: ChatState) -> str:
|
| 38 |
last_message = state["messages"][-1]
|
| 39 |
if hasattr(last_message, "tool_calls") and last_message.tool_calls:
|
|
@@ -188,6 +192,9 @@ Where:
|
|
| 188 |
|
| 189 |
if not already_has_prompt:
|
| 190 |
messages += [
|
|
|
|
|
|
|
|
|
|
| 191 |
SystemMessage(
|
| 192 |
content="⚠️ Do NOT summarize or compress the output from the `query` tool. It will be passed directly to `format_scripture_answer` tool that formats the answer **AS IS**. DO NOT REMOVE SANSKRIT/TAMIL TEXTS"
|
| 193 |
),
|
|
@@ -197,7 +204,11 @@ Where:
|
|
| 197 |
SystemMessage(
|
| 198 |
content="For general scripture queries, always prefer semantic search (tool_search_db). Use metadata or literal search only if the user specifies an exact verse number, azhwar, divya desam or phrase."
|
| 199 |
),
|
| 200 |
-
SystemMessage(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
When using tools, you may call the same tool multiple times in a single task ONLY if:
|
| 202 |
1. Each call has materially different arguments or targets a different piece of missing information.
|
| 203 |
2. You have a clear reason for another call that is explicitly based on the new results you just received.
|
|
@@ -207,7 +218,8 @@ Where:
|
|
| 207 |
|
| 208 |
Before each new call to the same tool, compare the planned arguments with your previous call(s).
|
| 209 |
If they are essentially the same, do NOT call it again — instead, proceed to generate the final validated output.
|
| 210 |
-
"""
|
|
|
|
| 211 |
]
|
| 212 |
|
| 213 |
return {"messages": messages}
|
|
@@ -223,11 +235,7 @@ Where:
|
|
| 223 |
graph.add_conditional_edges(
|
| 224 |
"llm",
|
| 225 |
branching_condition,
|
| 226 |
-
{
|
| 227 |
-
"tools": "tools",
|
| 228 |
-
"validator": "validator",
|
| 229 |
-
"__end__" : END
|
| 230 |
-
},
|
| 231 |
)
|
| 232 |
graph.add_edge("tools", "llm")
|
| 233 |
graph.add_edge("validator", END)
|
|
|
|
| 1 |
+
import json
|
| 2 |
from typing import Annotated, TypedDict
|
| 3 |
from langgraph.graph import StateGraph, START, END
|
| 4 |
from langgraph.checkpoint.memory import MemorySaver
|
|
|
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
from langgraph.graph.state import CompiledStateGraph
|
| 8 |
from langchain_core.messages import AIMessage
|
| 9 |
+
from config import SanatanConfig
|
| 10 |
from tools import (
|
| 11 |
tool_format_scripture_answer,
|
| 12 |
tool_get_standardized_prabandham_names,
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
class ChatState(TypedDict):
|
| 30 |
+
debug_mode: bool = True
|
| 31 |
messages: Annotated[list[str], add_messages]
|
| 32 |
|
| 33 |
+
|
| 34 |
def check_debug_condition(state: ChatState) -> str:
|
| 35 |
if state["debug_mode"]:
|
| 36 |
return "validator"
|
| 37 |
else:
|
| 38 |
return "__end__"
|
| 39 |
|
| 40 |
+
|
| 41 |
def branching_condition(state: ChatState) -> str:
|
| 42 |
last_message = state["messages"][-1]
|
| 43 |
if hasattr(last_message, "tool_calls") and last_message.tool_calls:
|
|
|
|
| 192 |
|
| 193 |
if not already_has_prompt:
|
| 194 |
messages += [
|
| 195 |
+
SystemMessage(
|
| 196 |
+
content=f"Here is the list of all scriptures along with their metadata configuraiton:\n{json.dumps(SanatanConfig.scriptures, indent=1)}\n"
|
| 197 |
+
),
|
| 198 |
SystemMessage(
|
| 199 |
content="⚠️ Do NOT summarize or compress the output from the `query` tool. It will be passed directly to `format_scripture_answer` tool that formats the answer **AS IS**. DO NOT REMOVE SANSKRIT/TAMIL TEXTS"
|
| 200 |
),
|
|
|
|
| 204 |
SystemMessage(
|
| 205 |
content="For general scripture queries, always prefer semantic search (tool_search_db). Use metadata or literal search only if the user specifies an exact verse number, azhwar, divya desam or phrase."
|
| 206 |
),
|
| 207 |
+
SystemMessage(
|
| 208 |
+
content="you must ALWAYS call one of the standardization tools available to get the correct entity name before using the `tool_search_db_by_metadata` tool."
|
| 209 |
+
),
|
| 210 |
+
SystemMessage(
|
| 211 |
+
content="""
|
| 212 |
When using tools, you may call the same tool multiple times in a single task ONLY if:
|
| 213 |
1. Each call has materially different arguments or targets a different piece of missing information.
|
| 214 |
2. You have a clear reason for another call that is explicitly based on the new results you just received.
|
|
|
|
| 218 |
|
| 219 |
Before each new call to the same tool, compare the planned arguments with your previous call(s).
|
| 220 |
If they are essentially the same, do NOT call it again — instead, proceed to generate the final validated output.
|
| 221 |
+
"""
|
| 222 |
+
),
|
| 223 |
]
|
| 224 |
|
| 225 |
return {"messages": messages}
|
|
|
|
| 235 |
graph.add_conditional_edges(
|
| 236 |
"llm",
|
| 237 |
branching_condition,
|
| 238 |
+
{"tools": "tools", "validator": "validator", "__end__": END},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
)
|
| 240 |
graph.add_edge("tools", "llm")
|
| 241 |
graph.add_edge("validator", END)
|
tools.py
CHANGED
|
@@ -47,10 +47,12 @@ tool_search_db_for_literal = StructuredTool.from_function(
|
|
| 47 |
|
| 48 |
tool_search_db_by_metadata = StructuredTool.from_function(
|
| 49 |
query_by_metadata_field,
|
|
|
|
| 50 |
description=(
|
| 51 |
-
"Use this tool **only when the user provides explicit metadata criteria**, such as: azhwar name, pasuram number, verse number, prabandham name, or divya desam name."
|
| 52 |
" This is not meant for general queries."
|
| 53 |
f" The collection_name must be one of: {', '.join(allowed_collections)}."
|
|
|
|
| 54 |
"If the user asks for a specific azhwar, use `tool_get_standardized_azhwar_names` first."
|
| 55 |
"If the user asks for a specific prabandham, use `tool_get_standardized_prabandham_names` first."
|
| 56 |
"If the user mentions a divya desam, use `tool_get_standardized_divya_desam_names` first."
|
|
@@ -60,7 +62,6 @@ tool_search_db_by_metadata = StructuredTool.from_function(
|
|
| 60 |
For example, if the tool returns Thirumālirum Solai, you must pass that exact string to tool_search_db_by_metadata. Do not change it to Thirumalirum Solai, Tirumalirumsolai, or anything else.
|
| 61 |
🔍 This is critical for the search to return results correctly.
|
| 62 |
🚫 Any deviation will cause the search to fail or miss results."""
|
| 63 |
-
f" Reference config:\n{json.dumps(SanatanConfig.scriptures, indent=1)}\n"
|
| 64 |
),
|
| 65 |
)
|
| 66 |
|
|
|
|
| 47 |
|
| 48 |
tool_search_db_by_metadata = StructuredTool.from_function(
|
| 49 |
query_by_metadata_field,
|
| 50 |
+
name="tool_search_db_by_metadata",
|
| 51 |
description=(
|
| 52 |
+
"Use this tool **only when the user provides explicit metadata criteria**, such as: azhwar name, pasuram number, verse number, decade, prabandham name, or divya desam name."
|
| 53 |
" This is not meant for general queries."
|
| 54 |
f" The collection_name must be one of: {', '.join(allowed_collections)}."
|
| 55 |
+
"you must ALWAYS call one of the standardization tools available to get the correct entity name before using this tool."
|
| 56 |
"If the user asks for a specific azhwar, use `tool_get_standardized_azhwar_names` first."
|
| 57 |
"If the user asks for a specific prabandham, use `tool_get_standardized_prabandham_names` first."
|
| 58 |
"If the user mentions a divya desam, use `tool_get_standardized_divya_desam_names` first."
|
|
|
|
| 62 |
For example, if the tool returns Thirumālirum Solai, you must pass that exact string to tool_search_db_by_metadata. Do not change it to Thirumalirum Solai, Tirumalirumsolai, or anything else.
|
| 63 |
🔍 This is critical for the search to return results correctly.
|
| 64 |
🚫 Any deviation will cause the search to fail or miss results."""
|
|
|
|
| 65 |
),
|
| 66 |
)
|
| 67 |
|