Andrei Sferdian commited on
Commit ·
88ae537
1
Parent(s): 254c111
Add filter_md_table_count tool
Browse files- agent.py +5 -3
- app.py +9 -9
- tools/__init__.py +16 -14
- tools/wikipedia.py +46 -0
agent.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from typing import Annotated, TypedDict
|
| 2 |
|
| 3 |
-
from langchain_community.tools import GoogleSerperResults
|
| 4 |
from langchain_core.messages import AnyMessage, HumanMessage
|
| 5 |
from langchain_together import ChatTogether
|
| 6 |
from langgraph.graph import START, StateGraph
|
|
@@ -13,6 +13,7 @@ from tools import (
|
|
| 13 |
add,
|
| 14 |
divide,
|
| 15 |
execute_python_code,
|
|
|
|
| 16 |
find_wiki_page,
|
| 17 |
floor_divide,
|
| 18 |
get_text_file,
|
|
@@ -39,12 +40,12 @@ tools = [
|
|
| 39 |
power,
|
| 40 |
modulus,
|
| 41 |
floor_divide,
|
|
|
|
| 42 |
]
|
| 43 |
|
| 44 |
model = ChatTogether(
|
| 45 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 46 |
temperature=0.3,
|
| 47 |
-
max_tokens=2000,
|
| 48 |
max_retries=3,
|
| 49 |
).bind_tools(tools)
|
| 50 |
|
|
@@ -59,6 +60,7 @@ prompt = (
|
|
| 59 |
" b. If the required information is not found in the search results, use the find_wiki_page tool to locate the relevant Wikipedia page.\n"
|
| 60 |
" The response will be a JSON string with the page's summary and sections.\n"
|
| 61 |
" c. If the Wikipedia page exists, and the summary does not contain the required info, use get_wiki_page_section_by_title tool to get the content of the desired section.\n"
|
|
|
|
| 62 |
"If the user asks for an Excel file, use the load_excel_file tool.\n"
|
| 63 |
"When you need to perform calculations, use the provided calculator tools (add, subtract, multiply, divide).\n"
|
| 64 |
"If the user provides a Python code snippet, follow this sequence:\n"
|
|
@@ -88,7 +90,7 @@ def detect_reverse(state: GraphState):
|
|
| 88 |
question = state["messages"][-1]
|
| 89 |
model_ = ChatTogether(
|
| 90 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 91 |
-
temperature=0.
|
| 92 |
max_tokens=2000,
|
| 93 |
max_retries=3,
|
| 94 |
)
|
|
|
|
| 1 |
from typing import Annotated, TypedDict
|
| 2 |
|
| 3 |
+
from langchain_community.tools.google_serper import GoogleSerperResults
|
| 4 |
from langchain_core.messages import AnyMessage, HumanMessage
|
| 5 |
from langchain_together import ChatTogether
|
| 6 |
from langgraph.graph import START, StateGraph
|
|
|
|
| 13 |
add,
|
| 14 |
divide,
|
| 15 |
execute_python_code,
|
| 16 |
+
filter_md_table_count,
|
| 17 |
find_wiki_page,
|
| 18 |
floor_divide,
|
| 19 |
get_text_file,
|
|
|
|
| 40 |
power,
|
| 41 |
modulus,
|
| 42 |
floor_divide,
|
| 43 |
+
filter_md_table_count,
|
| 44 |
]
|
| 45 |
|
| 46 |
model = ChatTogether(
|
| 47 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 48 |
temperature=0.3,
|
|
|
|
| 49 |
max_retries=3,
|
| 50 |
).bind_tools(tools)
|
| 51 |
|
|
|
|
| 60 |
" b. If the required information is not found in the search results, use the find_wiki_page tool to locate the relevant Wikipedia page.\n"
|
| 61 |
" The response will be a JSON string with the page's summary and sections.\n"
|
| 62 |
" c. If the Wikipedia page exists, and the summary does not contain the required info, use get_wiki_page_section_by_title tool to get the content of the desired section.\n"
|
| 63 |
+
" d. If the section contains a table, apply the filter_md_table_count tool to count the number of rows that match specific criteria.\n"
|
| 64 |
"If the user asks for an Excel file, use the load_excel_file tool.\n"
|
| 65 |
"When you need to perform calculations, use the provided calculator tools (add, subtract, multiply, divide).\n"
|
| 66 |
"If the user provides a Python code snippet, follow this sequence:\n"
|
|
|
|
| 90 |
question = state["messages"][-1]
|
| 91 |
model_ = ChatTogether(
|
| 92 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 93 |
+
temperature=0.5,
|
| 94 |
max_tokens=2000,
|
| 95 |
max_retries=3,
|
| 96 |
)
|
app.py
CHANGED
|
@@ -263,21 +263,21 @@ def run_locally():
|
|
| 263 |
# print("No questions available to run.")
|
| 264 |
# return
|
| 265 |
questions = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
# {
|
| 267 |
-
# "task_id": "
|
| 268 |
-
# "question": "
|
| 269 |
# "file_name": "",
|
| 270 |
# },
|
| 271 |
# {
|
| 272 |
-
# "task_id": "
|
| 273 |
-
# "question": "
|
| 274 |
# "file_name": "",
|
| 275 |
# },
|
| 276 |
-
{
|
| 277 |
-
"task_id": "9d191bce-651d-4746-be2d-7ef8ecadb9c2",
|
| 278 |
-
"question": "Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec.\n\nWhat does Teal'c say in response to the question \"Isn't that hot?\"",
|
| 279 |
-
"file_name": "",
|
| 280 |
-
},
|
| 281 |
# {
|
| 282 |
# "task_id": "2d83110e-a098-4ebb-9987-066c06fa42d0",
|
| 283 |
# "question": '.rewsna eht sa "tfel" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI',
|
|
|
|
| 263 |
# print("No questions available to run.")
|
| 264 |
# return
|
| 265 |
questions = [
|
| 266 |
+
{
|
| 267 |
+
"task_id": "8e867cd7-cff9-4e6c-867a-ff5ddc2550be",
|
| 268 |
+
"question": "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.",
|
| 269 |
+
"file_name": "",
|
| 270 |
+
},
|
| 271 |
# {
|
| 272 |
+
# "task_id": "cf106601-ab4f-4af9-b045-5295fe67b37d",
|
| 273 |
+
# "question": "What country had the least number of athletes at the 1928 Summer Olympics? If there's a tie for a number of athletes, return the first in alphabetical order. Give the IOC country code as your answer.",
|
| 274 |
# "file_name": "",
|
| 275 |
# },
|
| 276 |
# {
|
| 277 |
+
# "task_id": "9d191bce-651d-4746-be2d-7ef8ecadb9c2",
|
| 278 |
+
# "question": "Examine the video at https://www.youtube.com/watch?v=1htKBjuUWec.\n\nWhat does Teal'c say in response to the question \"Isn't that hot?\"",
|
| 279 |
# "file_name": "",
|
| 280 |
# },
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
# {
|
| 282 |
# "task_id": "2d83110e-a098-4ebb-9987-066c06fa42d0",
|
| 283 |
# "question": '.rewsna eht sa "tfel" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI',
|
tools/__init__.py
CHANGED
|
@@ -11,22 +11,24 @@ from .execute_code import execute_python_code
|
|
| 11 |
from .files import get_text_file, load_excel_file
|
| 12 |
from .get_youtube_transcript import GetYouTubeTranscript
|
| 13 |
from .wikipedia import (
|
|
|
|
| 14 |
find_wiki_page,
|
| 15 |
get_wiki_page_section_by_title,
|
| 16 |
)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
]
|
|
|
|
| 11 |
from .files import get_text_file, load_excel_file
|
| 12 |
from .get_youtube_transcript import GetYouTubeTranscript
|
| 13 |
from .wikipedia import (
|
| 14 |
+
filter_md_table_count,
|
| 15 |
find_wiki_page,
|
| 16 |
get_wiki_page_section_by_title,
|
| 17 |
)
|
| 18 |
|
| 19 |
+
Tools = [
|
| 20 |
+
GetYouTubeTranscript,
|
| 21 |
+
find_wiki_page,
|
| 22 |
+
get_wiki_page_section_by_title,
|
| 23 |
+
load_excel_file,
|
| 24 |
+
get_text_file,
|
| 25 |
+
add,
|
| 26 |
+
subtract,
|
| 27 |
+
multiply,
|
| 28 |
+
divide,
|
| 29 |
+
power,
|
| 30 |
+
modulus,
|
| 31 |
+
floor_divide,
|
| 32 |
+
execute_python_code,
|
| 33 |
+
filter_md_table_count,
|
| 34 |
]
|
tools/wikipedia.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import re
|
|
|
|
| 2 |
from typing import List
|
| 3 |
from urllib.parse import quote
|
| 4 |
|
| 5 |
import markdownify as md
|
|
|
|
| 6 |
import requests
|
| 7 |
import wikipedia
|
| 8 |
import wikipediaapi
|
|
@@ -199,3 +201,47 @@ def get_wiki_page_section_by_title(page_title: str, section_title: str) -> str |
|
|
| 199 |
section_md = md.markdownify(section_html)
|
| 200 |
print(f"Section content: {section_md}")
|
| 201 |
return section_md
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
+
from io import StringIO
|
| 3 |
from typing import List
|
| 4 |
from urllib.parse import quote
|
| 5 |
|
| 6 |
import markdownify as md
|
| 7 |
+
import pandas as pd
|
| 8 |
import requests
|
| 9 |
import wikipedia
|
| 10 |
import wikipediaapi
|
|
|
|
| 201 |
section_md = md.markdownify(section_html)
|
| 202 |
print(f"Section content: {section_md}")
|
| 203 |
return section_md
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
@tool(parse_docstring=True)
|
| 207 |
+
def filter_md_table_count(
|
| 208 |
+
md_table: str, column_name: str, filter_values: List[str]
|
| 209 |
+
) -> int:
|
| 210 |
+
"""
|
| 211 |
+
Filters a Markdown table based on a column name and a list of values, and returns the count of matching rows.
|
| 212 |
+
|
| 213 |
+
Args:
|
| 214 |
+
md_table (str): The Markdown table as a string.
|
| 215 |
+
column_name (str): The name of the column to filter on.
|
| 216 |
+
filter_values (List[str]): A list of values to filter against.
|
| 217 |
+
|
| 218 |
+
Returns:
|
| 219 |
+
int: The count of rows that match the filter criteria.
|
| 220 |
+
"""
|
| 221 |
+
|
| 222 |
+
try:
|
| 223 |
+
# Convert Markdown table to a Pandas DataFrame
|
| 224 |
+
df = pd.read_csv(
|
| 225 |
+
StringIO(md_table),
|
| 226 |
+
sep="|",
|
| 227 |
+
skipinitialspace=True,
|
| 228 |
+
engine="python",
|
| 229 |
+
skiprows=1,
|
| 230 |
+
)
|
| 231 |
+
df = df.rename(columns=lambda x: x.strip()).iloc[
|
| 232 |
+
:, 1:-1
|
| 233 |
+
] # Remove empty columns from Markdown parsing
|
| 234 |
+
|
| 235 |
+
# Check if the column exists
|
| 236 |
+
if column_name not in df.columns:
|
| 237 |
+
print(f"Column '{column_name}' not found in the table.")
|
| 238 |
+
return 0
|
| 239 |
+
|
| 240 |
+
# Filter rows based on the column and values
|
| 241 |
+
filtered_df = df[df[column_name].isin(filter_values)]
|
| 242 |
+
|
| 243 |
+
# Return the count of matching rows
|
| 244 |
+
return len(filtered_df)
|
| 245 |
+
except Exception as e:
|
| 246 |
+
print(f"Error processing Markdown table: {e}")
|
| 247 |
+
return 0
|