Spaces:
Sleeping
Sleeping
Fix PatentsView API integration to match current v1 query format
Browse filesUse wipo_kind instead of patent_type for kind codes, _text_all instead
of _text_any for relevant results, and correct nested field names
(cpc_at_issue, total_hits).
Co-authored-by: Cursor <cursoragent@cursor.com>
61 unmodified lines626364656667686970717273747576777879808182838437 unmodified lines1221231241251261271281291303 unmodified lines1341351361371381396 unmodified lines1461471481491501511523 unmodified lines15615715815916016116216316416516616716861 unmodified linesdef build_patentsview_query(keywords: list[str], kind_codes: list[str]) -> dict:"""Build a PatentsView API query from keywords and kind codes."""keyword_clauses = []for kw in keywords:keyword_clauses.append({"_or": [{"_text_any": {"patent_abstract": kw}},{"_text_any": {"patent_title": kw}},]})query = {"_and": keyword_clauses} if len(keyword_clauses) > 1 else keyword_clauses[0]if kind_codes:code_clause = {"_or": [{"patent_type": code} for code in kind_codes]}query = {"_and": [query, code_clause]}return query# ββ Endpoints ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ37 unmodified lines"q": query,"f": ["patent_id", "patent_title", "patent_abstract", "patent_date","patent_type", "patent_num_claims","assignees.assignee_organization","cpcs.cpc_group_id", "cpcs.cpc_group_title",],"s": [{"patent_date": "desc"}],"o": {"size": 10},3 unmodified linesresp = await client.post(PATENTSVIEW_BASE, headers=headers, json=params)if resp.status_code != 200:return await _fallback_search_with_gemini(req)data = resp.json()6 unmodified linesif assignees and assignees[0].get("assignee_organization"):assignee = assignees[0]["assignee_organization"]cpcs = p.get("cpcs", [])cpc_str = cpcs[0]["cpc_group_id"] if cpcs else ""year = p.get("patent_date", "")[:4]3 unmodified lines"title": p.get("patent_title", ""),"assignee": assignee,"year": int(year) if year.isdigit() else 0,"kindCode": p.get("patent_type", ""),"abstract": p.get("patent_abstract", "")[:500],"cpc": cpc_str,"numClaims": p.get("patent_num_claims", 0),})return {"results": results, "source": "patentsview", "total": data.get("total_patent_count", len(results))}async def _fallback_search_with_gemini(req: SearchRequest) -> dict:61 unmodified lines62636465666768697071727374757677787980818283848586878837 unmodified lines1261271281291301311321331343 unmodified lines1381391401411421431441456 unmodified lines1521531541551561571583 unmodified lines16216316416516616716816917017117217361 unmodified linesdef build_patentsview_query(keywords: list[str], kind_codes: list[str]) -> dict:"""Build a PatentsView API query from keywords and kind codes.Each keyword phrase is matched with _text_all (all words must appear)against title OR abstract. A patent matches if ANY keyword phrase hits."""keyword_clauses = []for kw in keywords:keyword_clauses.append({"_or": [{"_text_all": {"patent_abstract": kw}},{"_text_all": {"patent_title": kw}},]})if len(keyword_clauses) > 1:text_clause = {"_or": keyword_clauses}else:text_clause = keyword_clauses[0]if kind_codes:return {"_and": [text_clause, {"wipo_kind": kind_codes}]}return text_clause# ββ Endpoints ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ37 unmodified lines"q": query,"f": ["patent_id", "patent_title", "patent_abstract", "patent_date","patent_type", "wipo_kind","assignees.assignee_organization","cpc_at_issue.cpc_group_id", "cpc_at_issue.cpc_group",],"s": [{"patent_date": "desc"}],"o": {"size": 10},3 unmodified linesresp = await client.post(PATENTSVIEW_BASE, headers=headers, json=params)if resp.status_code != 200:reason = resp.headers.get("X-Status-Reason", resp.text[:500])print(f"[PatentsView] HTTP {resp.status_code}: {reason}")return await _fallback_search_with_gemini(req)data = resp.json()6 unmodified linesif assignees and assignees[0].get("assignee_organization"):assignee = assignees[0]["assignee_organization"]cpcs = p.get("cpc_at_issue", [])cpc_str = cpcs[0]["cpc_group_id"] if cpcs else ""year = p.get("patent_date", "")[:4]3 unmodified lines"title": p.get("patent_title", ""),"assignee": assignee,"year": int(year) if year.isdigit() else 0,"kindCode": p.get("wipo_kind", ""),"abstract": p.get("patent_abstract", "")[:500],"cpc": cpc_str,})return {"results": results, "source": "patentsview", "total": data.get("total_hits", len(results))}async def _fallback_search_with_gemini(req: SearchRequest) -> dict:
backend/server.py+22-17