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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
@tool
|
| 97 |
def check_citation_status(citation_id: str) -> Dict:
|
| 98 |
"""Check the current status and treatment of a legal citation
|
|
@@ -158,6 +426,37 @@ def check_citation_status(citation_id: str) -> Dict:
|
|
| 158 |
error_msg = f"Unexpected error: {str(e)}"
|
| 159 |
print(error_msg) # debugging
|
| 160 |
return {"error": error_msg}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
|
| 163 |
final_answer = FinalAnswerTool()
|
|
|
|
| 93 |
return error_msg
|
| 94 |
|
| 95 |
|
| 96 |
+
@tool
|
| 97 |
+
def check_citation_status(citation_id: str) -> Dict:
|
| 98 |
+
"""Check the current status and treatment of a legal citation
|
| 99 |
+
Args:
|
| 100 |
+
citation_id: The citation ID (e.g., "403 U.S. 713")
|
| 101 |
+
"""
|
| 102 |
+
try:
|
| 103 |
+
# Use the correct API base URL with v4
|
| 104 |
+
api_base = "https://www.courtlistener.com/api/rest/v4/"
|
| 105 |
+
|
| 106 |
+
# API vrne z oklepaji mi rabimo brez!
|
| 107 |
+
if "(" in citation_id:
|
| 108 |
+
citation_id = citation_id.split("(")[0].strip()
|
| 109 |
+
|
| 110 |
+
print(f"Looking up citation: {citation_id}")
|
| 111 |
+
|
| 112 |
+
# API
|
| 113 |
+
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 114 |
+
|
| 115 |
+
params = {
|
| 116 |
+
"cite": citation_id,
|
| 117 |
+
"court": "scotus"
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
print(f"Request URL: {api_base}opinions/")
|
| 121 |
+
print(f"Params: {params}")
|
| 122 |
+
|
| 123 |
+
# API request, ki ga pošiljamo COurtListnereju
|
| 124 |
+
response = requests.get(f"{api_base}opinions/", params=params, headers=headers)
|
| 125 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 126 |
+
import datetime
|
| 127 |
+
import requests
|
| 128 |
+
import pytz
|
| 129 |
+
import yaml
|
| 130 |
+
from tools.final_answer import FinalAnswerTool
|
| 131 |
+
from Gradio_UI import GradioUI
|
| 132 |
+
from typing import List, Dict
|
| 133 |
+
from bs4 import BeautifulSoup
|
| 134 |
+
|
| 135 |
+
COURTLISTENER_API_KEY = "INSERT_API_KEY"
|
| 136 |
+
API_BASE = "https://www.courtlistener.com/api/rest/v4/"
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
@tool
|
| 140 |
+
def search_terms_of_service(company_name: str) -> Dict:
|
| 141 |
+
"""Search for a company's Terms of Service link
|
| 142 |
+
|
| 143 |
+
Args:
|
| 144 |
+
company_name: Name of the organization/company to search for
|
| 145 |
+
|
| 146 |
+
Returns:
|
| 147 |
+
Dictionary containing company name and link to their Terms of Service
|
| 148 |
+
"""
|
| 149 |
+
|
| 150 |
+
try:
|
| 151 |
+
# API URL
|
| 152 |
+
api_base = "https://05bf-176-76-227-247.ngrok-free.app" # lokalno ga runnamo
|
| 153 |
+
|
| 154 |
+
# API request naredimo
|
| 155 |
+
url = f"{api_base}/company/{company_name}"
|
| 156 |
+
|
| 157 |
+
print(f"Request URL: {url}")
|
| 158 |
+
|
| 159 |
+
response = requests.get(url)
|
| 160 |
+
|
| 161 |
+
print(f"Response status: {response.status_code}")
|
| 162 |
+
print(f"Response body preview: {response.text[:200]}...")
|
| 163 |
+
|
| 164 |
+
if response.status_code == 404: #HTTP error!
|
| 165 |
+
return {"error": f"Company '{company_name}' not found in the database"}
|
| 166 |
+
|
| 167 |
+
response.raise_for_status()
|
| 168 |
+
|
| 169 |
+
data = response.json()
|
| 170 |
+
|
| 171 |
+
# ali obstaja več linkov?
|
| 172 |
+
if not data.get('secondary_links') or len(data['secondary_links']) == 0:
|
| 173 |
+
return {"error": f"No Terms of Service links found for {company_name}"}
|
| 174 |
+
|
| 175 |
+
# returnamo ime in link, lahko jih več
|
| 176 |
+
result = {
|
| 177 |
+
"company_name": data["name"],
|
| 178 |
+
"tos_link": data["secondary_links"][0], # prvi link
|
| 179 |
+
"all_links": data["secondary_links"] # več linkov
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
return result
|
| 183 |
+
|
| 184 |
+
except requests.exceptions.RequestException as e:
|
| 185 |
+
error_msg = f"API Error: {str(e)}"
|
| 186 |
+
if hasattr(e, 'response') and e.response is not None:
|
| 187 |
+
error_msg += f" (Status code: {e.response.status_code})"
|
| 188 |
+
if hasattr(e.response, 'text'):
|
| 189 |
+
error_msg += f" - {e.response.text[:200]}"
|
| 190 |
+
print(error_msg) # debug, da prizna, če se zmoti
|
| 191 |
+
return {"error": error_msg}
|
| 192 |
+
|
| 193 |
+
except Exception as e:
|
| 194 |
+
error_msg = f"Unexpected error: {str(e)}"
|
| 195 |
+
print(error_msg) # debug, da AI prizna, če se zmoti
|
| 196 |
+
return {"error": error_msg}
|
| 197 |
+
|
| 198 |
+
@tool
|
| 199 |
+
def extract_tos_content(company_name: str) -> Dict:
|
| 200 |
+
"""
|
| 201 |
+
Search for a company's Terms of Service, access the link, and extract raw content from GitLab repository.
|
| 202 |
+
|
| 203 |
+
Args:
|
| 204 |
+
company_name: Name of the organization/company to search for
|
| 205 |
+
|
| 206 |
+
Returns:
|
| 207 |
+
Dictionary containing company name, link, and extracted content
|
| 208 |
+
"""
|
| 209 |
+
|
| 210 |
+
tos_info = search_terms_of_service(company_name)
|
| 211 |
+
|
| 212 |
+
# error
|
| 213 |
+
if "error" in tos_info:
|
| 214 |
+
return tos_info
|
| 215 |
+
|
| 216 |
+
try:
|
| 217 |
+
# link od zgoraj
|
| 218 |
+
tos_link = tos_info["tos_link"]
|
| 219 |
+
|
| 220 |
+
print(f"Accessing TOS link: {tos_link}")
|
| 221 |
+
|
| 222 |
+
# Gitlab repo
|
| 223 |
+
if "code.europa.eu" in tos_link and "/-/blob/" in tos_link:
|
| 224 |
+
# spremeni '/-/blob/' z '/-/raw/'
|
| 225 |
+
raw_url = tos_link.replace("/-/blob/", "/-/raw/")
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
if "?" in raw_url:
|
| 229 |
+
raw_url = raw_url.split("?")[0]
|
| 230 |
+
|
| 231 |
+
print(f"Accessing raw content URL: {raw_url}")
|
| 232 |
+
|
| 233 |
+
# request za raw data iz GItlabaa
|
| 234 |
+
response = requests.get(raw_url)
|
| 235 |
+
response.raise_for_status()
|
| 236 |
+
|
| 237 |
+
return {
|
| 238 |
+
"company_name": tos_info["company_name"],
|
| 239 |
+
"tos_link": tos_link,
|
| 240 |
+
"raw_url": raw_url,
|
| 241 |
+
"content": response.text, # cel raw data fajl
|
| 242 |
+
"content_type": "markdown" if raw_url.endswith(".md") else "text"
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
response = requests.get(tos_link)
|
| 246 |
+
response.raise_for_status()
|
| 247 |
+
|
| 248 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 249 |
+
|
| 250 |
+
content = ""
|
| 251 |
+
|
| 252 |
+
pre_element = soup.find('pre')
|
| 253 |
+
if pre_element:
|
| 254 |
+
content = pre_element.get_text()
|
| 255 |
+
else:
|
| 256 |
+
main_content = soup.find('main') or soup.find('article') or soup.find('body')
|
| 257 |
+
if main_content:
|
| 258 |
+
content = main_content.get_text(strip=True)
|
| 259 |
+
|
| 260 |
+
return {
|
| 261 |
+
"company_name": tos_info["company_name"],
|
| 262 |
+
"tos_link": tos_link,
|
| 263 |
+
"content": content,
|
| 264 |
+
"note": "Content extracted from HTML page"
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
except requests.exceptions.RequestException as e:
|
| 268 |
+
error_msg = f"Error accessing TOS link: {str(e)}"
|
| 269 |
+
if hasattr(e, 'response') and e.response is not None:
|
| 270 |
+
error_msg += f" (Status code: {e.response.status_code})"
|
| 271 |
+
if hasattr(e.response, 'text'):
|
| 272 |
+
error_msg += f" - {e.response.text[:200]}"
|
| 273 |
+
print(error_msg)
|
| 274 |
+
return {"error": error_msg}
|
| 275 |
+
|
| 276 |
+
except Exception as e:
|
| 277 |
+
error_msg = f"Unexpected error while processing content: {str(e)}"
|
| 278 |
+
print(error_msg)
|
| 279 |
+
return {"error": error_msg}
|
| 280 |
+
|
| 281 |
+
@tool
|
| 282 |
+
def search_court_cases(keyword: str, jurisdiction: str = None,
|
| 283 |
+
date_range: tuple = None) -> List[Dict]:
|
| 284 |
+
|
| 285 |
+
"""Search court cases by keyword and jurisdiction
|
| 286 |
+
Args:
|
| 287 |
+
keyword: Legal concepts or case terms to search
|
| 288 |
+
jurisdiction: Court jurisdiction (e.g., 'scotus' for Supreme Court)
|
| 289 |
+
date_range: Tuple of (start_date, end_date) in UTC
|
| 290 |
+
"""
|
| 291 |
+
|
| 292 |
+
try:
|
| 293 |
+
# API
|
| 294 |
+
api_base = "https://www.courtlistener.com/api/rest/v4/"
|
| 295 |
+
|
| 296 |
+
# zgradimo query
|
| 297 |
+
params = {
|
| 298 |
+
"q": keyword,
|
| 299 |
+
"type": "o", # 'o' opinion oz. mnenje
|
| 300 |
+
"page_size": 5
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
if jurisdiction:
|
| 305 |
+
if jurisdiction.lower() == "scotus":
|
| 306 |
+
params["court"] = "scotus"
|
| 307 |
+
else:
|
| 308 |
+
params["court"] = jurisdiction
|
| 309 |
+
|
| 310 |
+
# Datum in format
|
| 311 |
+
if date_range:
|
| 312 |
+
params["date_filed__gte"] = date_range[0].strftime("%Y-%m-%d")
|
| 313 |
+
params["date_filed__lte"] = date_range[1].strftime("%Y-%m-%d")
|
| 314 |
+
|
| 315 |
+
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 316 |
+
|
| 317 |
+
print(f"Request URL: {api_base}search/")
|
| 318 |
+
print(f"Params: {params}")
|
| 319 |
+
|
| 320 |
+
response = requests.get(f"{api_base}search/", params=params, headers=headers)
|
| 321 |
+
|
| 322 |
+
print(f"Response status: {response.status_code}")
|
| 323 |
+
print(f"Response body preview: {response.text[:200]}...")
|
| 324 |
+
|
| 325 |
+
# Handle HTTP errors
|
| 326 |
+
if response.status_code == 403:
|
| 327 |
+
return "Authentication failed: Please check your API key"
|
| 328 |
+
|
| 329 |
+
response.raise_for_status()
|
| 330 |
+
|
| 331 |
+
data = response.json()
|
| 332 |
+
|
| 333 |
+
if 'results' not in data or len(data['results']) == 0:
|
| 334 |
+
return "No matching cases found in the database"
|
| 335 |
+
|
| 336 |
+
cases = []
|
| 337 |
+
for result in data['results']:
|
| 338 |
+
case_info = {
|
| 339 |
+
"case_name": result.get('caseName', result.get('case_name', 'Unknown')),
|
| 340 |
+
"citation": result.get('citation', 'No citation available'),
|
| 341 |
+
"court": result.get('court_full_name', result.get('court', 'Unknown')),
|
| 342 |
+
"date_filed": result.get('date_filed', 'Unknown date'),
|
| 343 |
+
"summary": result.get('text', result.get('snippet', ''))[:200] + '...' if result.get('text') or result.get('snippet') else "No summary available"
|
| 344 |
+
}
|
| 345 |
+
cases.append(case_info)
|
| 346 |
+
|
| 347 |
+
return cases
|
| 348 |
+
|
| 349 |
+
except requests.exceptions.RequestException as e:
|
| 350 |
+
error_msg = f"API Error: {str(e)}"
|
| 351 |
+
if hasattr(e, 'response') and e.response is not None:
|
| 352 |
+
error_msg += f" (Status code: {e.response.status_code})"
|
| 353 |
+
if hasattr(e.response, 'text'):
|
| 354 |
+
error_msg += f" - {e.response.text[:200]}"
|
| 355 |
+
print(error_msg) # debug
|
| 356 |
+
return error_msg
|
| 357 |
+
|
| 358 |
+
except Exception as e:
|
| 359 |
+
error_msg = f"Unexpected error: {str(e)}"
|
| 360 |
+
print(error_msg) # debug
|
| 361 |
+
return error_msg
|
| 362 |
+
|
| 363 |
+
|
| 364 |
@tool
|
| 365 |
def check_citation_status(citation_id: str) -> Dict:
|
| 366 |
"""Check the current status and treatment of a legal citation
|
|
|
|
| 426 |
error_msg = f"Unexpected error: {str(e)}"
|
| 427 |
print(error_msg) # debugging
|
| 428 |
return {"error": error_msg}
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
final_answer = FinalAnswerTool()
|
| 432 |
+
|
| 433 |
+
#kateri model in kje se povežemo!
|
| 434 |
+
model = HfApiModel(
|
| 435 |
+
max_tokens=2096,
|
| 436 |
+
temperature=0.3,
|
| 437 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/',
|
| 438 |
+
custom_role_conversions=None,
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
#prompti za agenta
|
| 442 |
+
with open("prompts.yaml", 'r') as stream:
|
| 443 |
+
prompt_templates = yaml.safe_load(stream)
|
| 444 |
+
|
| 445 |
+
#param za agenta
|
| 446 |
+
agent = CodeAgent(
|
| 447 |
+
model=model,
|
| 448 |
+
tools=[final_answer,search_terms_of_service,extract_tos_content], #search_court_cases, check_citation_status], #orodja
|
| 449 |
+
max_steps=6,
|
| 450 |
+
verbosity_level=1,
|
| 451 |
+
grammar=None,
|
| 452 |
+
planning_interval=None,
|
| 453 |
+
name="Legal Research Assistant",
|
| 454 |
+
description="Specializes in US federal and state court decisions analysis",
|
| 455 |
+
prompt_templates=prompt_templates
|
| 456 |
+
)
|
| 457 |
+
|
| 458 |
+
GradioUI(agent).launch() #launch za UI
|
| 459 |
+
|
| 460 |
|
| 461 |
|
| 462 |
final_answer = FinalAnswerTool()
|