File size: 12,808 Bytes
0342e21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c7d6c4
0342e21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d58300
2c7d6c4
 
0342e21
 
2c7d6c4
0342e21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc3020
0342e21
9d58300
0342e21
9d58300
 
0342e21
9d58300
 
 
 
0342e21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d58300
0342e21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
    from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
    import datetime
    import requests
    import pytz
    import yaml
    from tools.final_answer import FinalAnswerTool
    from Gradio_UI import GradioUI
    from typing import List, Dict
    from bs4 import BeautifulSoup

    COURTLISTENER_API_KEY = "INSERT_API_KEY"  
    API_BASE = "https://www.courtlistener.com/api/rest/v4/"


    @tool
    def search_terms_of_service(company_name: str) -> Dict:
        """Search for a company's Terms of Service link
        
        Args:
            company_name: Name of the organization/company to search for
        
        Returns:
            Dictionary containing company name and link to their Terms of Service
        """
        
        try:
            # API URL
            api_base = "https://05bf-176-76-227-247.ngrok-free.app"  # lokalno ga runnamo
            
            # API request naredimo
            url = f"{api_base}/company/{company_name}"
            
            print(f"Request URL: {url}")
            
            response = requests.get(url)
            
            print(f"Response status: {response.status_code}")
            print(f"Response body preview: {response.text[:200]}...")
            
            if response.status_code == 404: #HTTP error!  
                return {"error": f"Company '{company_name}' not found in the database"}
            
            response.raise_for_status()
            
            data = response.json()
            
            # ali obstaja več linkov?
            if not data.get('secondary_links') or len(data['secondary_links']) == 0:
                return {"error": f"No Terms of Service links found for {company_name}"}
            
        # returnamo ime in link, lahko jih več
            result = {
                "company_name": data["name"],
                "tos_link": data["secondary_links"][0],  # prvi link
                "all_links": data["secondary_links"]  # več linkov
            }
            
            return result
        
        except requests.exceptions.RequestException as e:
            error_msg = f"API Error: {str(e)}"
            if hasattr(e, 'response') and e.response is not None:
                error_msg += f" (Status code: {e.response.status_code})"
                if hasattr(e.response, 'text'):
                    error_msg += f" - {e.response.text[:200]}"
            print(error_msg)  # debug, da prizna, če se zmoti
            return {"error": error_msg}
        
        except Exception as e:
            error_msg = f"Unexpected error: {str(e)}"
            print(error_msg)  # debug, da AI prizna, če se zmoti
            return {"error": error_msg}

    @tool
    def extract_tos_content(company_name: str) -> Dict:
        """
        Search for a company's Terms of Service, access the link, and extract raw content from GitLab repository.
        
        Args:
            company_name: Name of the organization/company to search for
        
        Returns:
            Dictionary containing company name, link, and extracted content
        """
        
        tos_info = search_terms_of_service(company_name)
        
        # error
        if "error" in tos_info:
            return tos_info
        
        try:
            # link od zgoraj
            tos_link = tos_info["tos_link"]
            
            print(f"Accessing TOS link: {tos_link}")
            
            # Gitlab repo
            if "code.europa.eu" in tos_link and "/-/blob/" in tos_link:
                # spremeni '/-/blob/' z '/-/raw/'
                raw_url = tos_link.replace("/-/blob/", "/-/raw/")
                
                
                if "?" in raw_url:
                    raw_url = raw_url.split("?")[0]
                    
                print(f"Accessing raw content URL: {raw_url}")
                
                # request za raw data iz GItlabaa
                response = requests.get(raw_url)
                response.raise_for_status()
                
                return {
                    "company_name": tos_info["company_name"],
                    "tos_link": tos_link,
                    "raw_url": raw_url,
                    "content": response.text,  # cel raw data fajl
                    "content_type": "markdown" if raw_url.endswith(".md") else "text"
                }
            
            response = requests.get(tos_link)
            response.raise_for_status()
            
            soup = BeautifulSoup(response.text, 'html.parser')
            
            content = ""
            
            pre_element = soup.find('pre')
            if pre_element:
                content = pre_element.get_text()
            else:
                main_content = soup.find('main') or soup.find('article') or soup.find('body')
                if main_content:
                    content = main_content.get_text(strip=True)
            
            return {
                "company_name": tos_info["company_name"],
                "tos_link": tos_link,
                "content": content,
                "note": "Content extracted from HTML page"
            }
            
        except requests.exceptions.RequestException as e:
            error_msg = f"Error accessing TOS link: {str(e)}"
            if hasattr(e, 'response') and e.response is not None:
                error_msg += f" (Status code: {e.response.status_code})"
                if hasattr(e.response, 'text'):
                    error_msg += f" - {e.response.text[:200]}"
            print(error_msg)
            return {"error": error_msg}
            
        except Exception as e:
            error_msg = f"Unexpected error while processing content: {str(e)}"
            print(error_msg)
            return {"error": error_msg}

    @tool
    def search_court_cases(keyword: str, jurisdiction: str = None, 
                        date_range: tuple = None) -> List[Dict]:
        
        """Search court cases by keyword and jurisdiction
        Args:
            keyword: Legal concepts or case terms to search
            jurisdiction: Court jurisdiction (e.g., 'scotus' for Supreme Court)
            date_range: Tuple of (start_date, end_date) in UTC
        """
        
        try:
            # API
            api_base = "https://www.courtlistener.com/api/rest/v4/"
            
            # zgradimo query
            params = {
                "q": keyword,
                "type": "o",  # 'o' opinion oz. mnenje
                "page_size": 5
            }
            
            
            if jurisdiction:
                if jurisdiction.lower() == "scotus":
                    params["court"] = "scotus"
                else:
                    params["court"] = jurisdiction
            
            # Datum in format
            if date_range:
                params["date_filed__gte"] = date_range[0].strftime("%Y-%m-%d")
                params["date_filed__lte"] = date_range[1].strftime("%Y-%m-%d")
            
            headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
            
            print(f"Request URL: {api_base}search/")
            print(f"Params: {params}")
            
            response = requests.get(f"{api_base}search/", params=params, headers=headers)
            
            print(f"Response status: {response.status_code}")
            print(f"Response body preview: {response.text[:200]}...")
            
            # Handle HTTP errors
            if response.status_code == 403:
                return "Authentication failed: Please check your API key"
            
            response.raise_for_status()
            
            data = response.json()
            
            if 'results' not in data or len(data['results']) == 0:
                return "No matching cases found in the database"
            
            cases = []
            for result in data['results']:
                case_info = {
                    "case_name": result.get('caseName', result.get('case_name', 'Unknown')),
                    "citation": result.get('citation', 'No citation available'),
                    "court": result.get('court_full_name', result.get('court', 'Unknown')),
                    "date_filed": result.get('date_filed', 'Unknown date'),
                    "summary": result.get('text', result.get('snippet', ''))[:200] + '...' if result.get('text') or result.get('snippet') else "No summary available"
                }
                cases.append(case_info)
            
            return cases
        
        except requests.exceptions.RequestException as e:
            error_msg = f"API Error: {str(e)}"
            if hasattr(e, 'response') and e.response is not None:
                error_msg += f" (Status code: {e.response.status_code})"
                if hasattr(e.response, 'text'):
                    error_msg += f" - {e.response.text[:200]}"
            print(error_msg)  # debug
            return error_msg
        
        except Exception as e:
            error_msg = f"Unexpected error: {str(e)}"
            print(error_msg)  # debug
            return error_msg


    @tool
    def check_citation_status(citation_id: str) -> Dict:
        """Check the current status and treatment of a legal citation
        Args:
            citation_id: The citation ID (e.g., "403 U.S. 713")
        """
        try:
            # Use the correct API base URL with v4
            api_base = "https://www.courtlistener.com/api/rest/v4/"
            
            # API vrne z oklepaji mi rabimo brez!
            if "(" in citation_id:
                citation_id = citation_id.split("(")[0].strip()
            
            print(f"Looking up citation: {citation_id}")
            
            # API
            headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
            
            params = {
                "cite": citation_id,
                "court": "scotus"  
            }
            
            print(f"Request URL: {api_base}opinions/")
            print(f"Params: {params}")
            
            # API request, ki ga pošiljamo COurtListnereju
            response = requests.get(f"{api_base}opinions/", params=params, headers=headers)
            
            # debugiranje
            print(f"Response status: {response.status_code}")
            print(f"Response body preview: {response.text[:200]}...")
            
            response.raise_for_status()
            
            # JSON
            data = response.json()
            
            if 'results' in data and len(data['results']) > 0:
                result = data['results'][0]
                return {
                    "status": result.get('status', 'Unknown'),
                    "citation": result.get('citation', citation_id),
                    "court": result.get('court_full_name', 'Unknown'),
                    "precedential_status": result.get('precedential_status', 'Unknown'),
                    "date_filed": result.get('date_filed', 'Unknown'),
                    "case_name": result.get('case_name', 'Unknown')
                }
            else:
                return {"error": f"No citation found for {citation_id}"}
            
        except requests.exceptions.RequestException as e:
            error_msg = f"API Error: {str(e)}"
            if hasattr(e, 'response') and e.response is not None:
                error_msg += f" (Status code: {e.response.status_code})"
                if hasattr(e.response, 'text'):
                    error_msg += f" - {e.response.text[:200]}"
            print(error_msg)  # debugging
            return {"error": error_msg}
        
        except Exception as e:
            error_msg = f"Unexpected error: {str(e)}"
            print(error_msg)  # debugging
            return {"error": error_msg} 
        

    final_answer = FinalAnswerTool()

    #kateri model in kje se povežemo!
    model = HfApiModel(
        max_tokens=2096,
        temperature=0.3,  
        model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/',
        custom_role_conversions=None,
    )

    #prompti za agenta
    with open("prompts.yaml", 'r') as stream:
        prompt_templates = yaml.safe_load(stream)

    #param za agenta
    agent = CodeAgent(
        model=model,
        tools=[final_answer,search_terms_of_service,extract_tos_content], #search_court_cases, check_citation_status], #orodja
        max_steps=6,
        verbosity_level=1,
        grammar=None,
        planning_interval=None,
        name="Legal Research Assistant",
        description="Specializes in US federal and state court decisions analysis",
        prompt_templates=prompt_templates
    )

    GradioUI(agent).launch() #launch za UI