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 lines626364656566676869707168697071727372737475767577787980818278798384818586878837 unmodified lines1261271281251291301271311321331343 unmodified lines1381391401411421431441456 unmodified lines1521531541491551561571583 unmodified lines16216316415916516616716216816916517017117217361 unmodified lines
def build_patentsview_query(keywords: list[str], kind_codes: list[str]) -> dict: """Build a PatentsView API query from keywords and kind codes.""" """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_any": {"patent_abstract": kw}}, {"_text_any": {"patent_title": kw}}, ]} ) keyword_clauses.append({"_or": [ {"_text_all": {"patent_abstract": kw}}, {"_text_all": {"patent_title": kw}}, ]})
query = {"_and": keyword_clauses} if len(keyword_clauses) > 1 else keyword_clauses[0] if len(keyword_clauses) > 1: text_clause = {"_or": keyword_clauses} else: text_clause = keyword_clauses[0]
if kind_codes: code_clause = {"_or": [{"patent_type": code} for code in kind_codes]} query = {"_and": [query, code_clause]} return {"_and": [text_clause, {"wipo_kind": kind_codes}]}
return query return text_clause
# ββ Endpoints ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ37 unmodified lines "q": query, "f": [ "patent_id", "patent_title", "patent_abstract", "patent_date", "patent_type", "patent_num_claims", "patent_type", "wipo_kind", "assignees.assignee_organization", "cpcs.cpc_group_id", "cpcs.cpc_group_title", "cpc_at_issue.cpc_group_id", "cpc_at_issue.cpc_group", ], "s": [{"patent_date": "desc"}], "o": {"size": 10},3 unmodified lines resp = 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 lines if assignees and assignees[0].get("assignee_organization"): assignee = assignees[0]["assignee_organization"]
cpcs = p.get("cpcs", []) 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("patent_type", ""), "kindCode": p.get("wipo_kind", ""), "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))} 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