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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! Patent database API integrations for USPTO PatentsView and EPO
//!
//! This module provides async clients for fetching patent data from:
//! - USPTO PatentsView API (Free, no authentication required)
//! - EPO Open Patent Services (Free tier available)
//!
//! Converts patent data to SemanticVector format for RuVector discovery.
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use chrono::{NaiveDate, Utc};
use reqwest::{Client, StatusCode};
use serde::Deserialize;
use tokio::time::sleep;
use crate::api_clients::SimpleEmbedder;
use crate::ruvector_native::{Domain, SemanticVector};
use crate::{FrameworkError, Result};
/// Rate limiting configuration
const USPTO_RATE_LIMIT_MS: u64 = 200; // ~5 requests/second
const EPO_RATE_LIMIT_MS: u64 = 1000; // Conservative 1 request/second
const MAX_RETRIES: u32 = 3;
const RETRY_DELAY_MS: u64 = 1000;
// ============================================================================
// USPTO PatentsView API Client
// ============================================================================
/// USPTO PatentsView API response
#[derive(Debug, Deserialize)]
struct UsptoPatentsResponse {
#[serde(default)]
patents: Vec<UsptoPatent>,
#[serde(default)]
count: i32,
#[serde(default)]
total_patent_count: Option<i32>,
}
/// USPTO Patent record
#[derive(Debug, Deserialize)]
struct UsptoPatent {
/// Patent number
patent_number: String,
/// Patent title
#[serde(default)]
patent_title: Option<String>,
/// Patent abstract
#[serde(default)]
patent_abstract: Option<String>,
/// Grant date (YYYY-MM-DD)
#[serde(default)]
patent_date: Option<String>,
/// Application filing date
#[serde(default)]
app_date: Option<String>,
/// Assignees (organizations/companies)
#[serde(default)]
assignees: Vec<UsptoAssignee>,
/// Inventors
#[serde(default)]
inventors: Vec<UsptoInventor>,
/// CPC classifications
#[serde(default)]
cpcs: Vec<UsptoCpc>,
/// Citation counts
#[serde(default)]
cited_patent_count: Option<i32>,
#[serde(default)]
citedby_patent_count: Option<i32>,
}
#[derive(Debug, Deserialize)]
struct UsptoAssignee {
#[serde(default)]
assignee_organization: Option<String>,
#[serde(default)]
assignee_individual_name_first: Option<String>,
#[serde(default)]
assignee_individual_name_last: Option<String>,
}
#[derive(Debug, Deserialize)]
struct UsptoInventor {
#[serde(default)]
inventor_name_first: Option<String>,
#[serde(default)]
inventor_name_last: Option<String>,
}
#[derive(Debug, Deserialize)]
struct UsptoCpc {
/// CPC section (e.g., "Y02")
#[serde(default)]
cpc_section_id: Option<String>,
/// CPC subclass (e.g., "Y02E")
#[serde(default)]
cpc_subclass_id: Option<String>,
/// CPC group (e.g., "Y02E10/50")
#[serde(default)]
cpc_group_id: Option<String>,
}
/// USPTO citation response
#[derive(Debug, Deserialize)]
struct UsptoCitationsResponse {
#[serde(default)]
patents: Vec<UsptoCitation>,
}
#[derive(Debug, Deserialize)]
struct UsptoCitation {
patent_number: String,
#[serde(default)]
patent_title: Option<String>,
}
/// Client for USPTO PatentsView API (PatentSearch API v2)
///
/// PatentsView provides free access to USPTO patent data with no authentication required.
/// Uses the new PatentSearch API (ElasticSearch-based) as of May 2025.
/// API documentation: https://search.patentsview.org/docs/
pub struct UsptoPatentClient {
client: Client,
base_url: String,
rate_limit_delay: Duration,
embedder: Arc<SimpleEmbedder>,
}
impl UsptoPatentClient {
/// Create a new USPTO PatentsView client
///
/// No authentication required for the PatentsView API.
/// Uses the new PatentSearch API at search.patentsview.org
pub fn new() -> Result<Self> {
let client = Client::builder()
.timeout(Duration::from_secs(30))
.user_agent("RuVector-Discovery/1.0")
.build()
.map_err(FrameworkError::Network)?;
Ok(Self {
client,
base_url: "https://search.patentsview.org/api/v1".to_string(),
rate_limit_delay: Duration::from_millis(USPTO_RATE_LIMIT_MS),
embedder: Arc::new(SimpleEmbedder::new(512)), // Higher dimension for technical text
})
}
/// Search patents by keyword query
///
/// # Arguments
/// * `query` - Search keywords (e.g., "artificial intelligence", "solar cell")
/// * `max_results` - Maximum number of results to return (max 1000 per page)
///
/// # Example
/// ```ignore
/// let client = UsptoPatentClient::new()?;
/// let patents = client.search_patents("quantum computing", 50).await?;
/// ```
pub async fn search_patents(
&self,
query: &str,
max_results: usize,
) -> Result<Vec<SemanticVector>> {
let per_page = max_results.min(100);
let encoded_query = urlencoding::encode(query);
// New PatentSearch API uses GET with query parameters
// Query format: q=patent_title:*query* OR patent_abstract:*query*
let url = format!(
"{}/patent/?q=patent_title:*{}*%20OR%20patent_abstract:*{}*&f=patent_id,patent_title,patent_abstract,patent_date,assignees,inventors,cpcs&o={{\"size\":{},\"matched_subentities_only\":true}}",
self.base_url, encoded_query, encoded_query, per_page
);
sleep(self.rate_limit_delay).await;
let response = self.fetch_with_retry(&url).await?;
let uspto_response: UsptoPatentsResponse = response.json().await?;
self.convert_patents_to_vectors(uspto_response.patents)
}
/// Search patents by assignee (company/organization name)
///
/// # Arguments
/// * `company_name` - Company or organization name (e.g., "IBM", "Google")
/// * `max_results` - Maximum number of results to return
///
/// # Example
/// ```ignore
/// let patents = client.search_by_assignee("Tesla Inc", 100).await?;
/// ```
pub async fn search_by_assignee(
&self,
company_name: &str,
max_results: usize,
) -> Result<Vec<SemanticVector>> {
let per_page = max_results.min(100);
let encoded_name = urlencoding::encode(company_name);
// New PatentSearch API format
let url = format!(
"{}/patent/?q=assignees.assignee_organization:*{}*&f=patent_id,patent_title,patent_abstract,patent_date,assignees,inventors,cpcs&o={{\"size\":{},\"matched_subentities_only\":true}}",
self.base_url, encoded_name, per_page
);
sleep(self.rate_limit_delay).await;
let response = self.fetch_with_retry(&url).await?;
let uspto_response: UsptoPatentsResponse = response.json().await?;
self.convert_patents_to_vectors(uspto_response.patents)
}
/// Search patents by CPC classification code
///
/// # Arguments
/// * `cpc_class` - CPC classification (e.g., "Y02E" for climate tech energy, "G06N" for AI)
/// * `max_results` - Maximum number of results to return
///
/// # Example - Climate Change Mitigation Technologies
/// ```ignore
/// let climate_patents = client.search_by_cpc("Y02", 200).await?;
/// ```
///
/// # Common CPC Classes
/// * `Y02` - Climate change mitigation technologies
/// * `Y02E` - Climate tech - Energy generation/transmission/distribution
/// * `G06N` - Computing arrangements based on AI/ML/neural networks
/// * `A61` - Medical or veterinary science
/// * `H01` - Electric elements (batteries, solar cells, etc.)
pub async fn search_by_cpc(
&self,
cpc_class: &str,
max_results: usize,
) -> Result<Vec<SemanticVector>> {
let per_page = max_results.min(100);
let encoded_cpc = urlencoding::encode(cpc_class);
// New PatentSearch API - query cpcs.cpc_group field
let url = format!(
"{}/patent/?q=cpcs.cpc_group:{}*&f=patent_id,patent_title,patent_abstract,patent_date,assignees,inventors,cpcs&o={{\"size\":{},\"matched_subentities_only\":true}}",
self.base_url, encoded_cpc, per_page
);
sleep(self.rate_limit_delay).await;
let response = self.fetch_with_retry(&url).await?;
let uspto_response: UsptoPatentsResponse = response.json().await?;
self.convert_patents_to_vectors(uspto_response.patents)
}
/// Get detailed information for a specific patent
///
/// # Arguments
/// * `patent_number` - USPTO patent number (e.g., "10000000")
///
/// # Example
/// ```ignore
/// let patent = client.get_patent("10123456").await?;
/// ```
pub async fn get_patent(&self, patent_number: &str) -> Result<Option<SemanticVector>> {
// New PatentSearch API - direct patent lookup
let url = format!(
"{}/patent/?q=patent_id:{}&f=patent_id,patent_title,patent_abstract,patent_date,assignees,inventors,cpcs&o={{\"size\":1}}",
self.base_url, patent_number
);
sleep(self.rate_limit_delay).await;
let response = self.fetch_with_retry(&url).await?;
let uspto_response: UsptoPatentsResponse = response.json().await?;
let mut vectors = self.convert_patents_to_vectors(uspto_response.patents)?;
Ok(vectors.pop())
}
/// Get citations for a patent (both citing and cited patents)
///
/// # Arguments
/// * `patent_number` - USPTO patent number
///
/// # Returns
/// Tuple of (patents that cite this patent, patents cited by this patent)
pub async fn get_citations(
&self,
patent_number: &str,
) -> Result<(Vec<SemanticVector>, Vec<SemanticVector>)> {
// Get patents that cite this one (forward citations)
let citing = self.get_citing_patents(patent_number).await?;
// Get patents cited by this one (backward citations)
let cited = self.get_cited_patents(patent_number).await?;
Ok((citing, cited))
}
/// Get patents that cite the given patent (forward citations)
/// Note: Citation data requires separate API endpoints in PatentSearch API v2
async fn get_citing_patents(&self, _patent_number: &str) -> Result<Vec<SemanticVector>> {
// The new PatentSearch API handles citations differently
// Forward citations are available via /api/v1/us_patent_citation/ endpoint
// For now, return empty - full citation support requires additional implementation
Ok(Vec::new())
}
/// Get patents cited by the given patent (backward citations)
/// Note: Citation data requires separate API endpoints in PatentSearch API v2
async fn get_cited_patents(&self, _patent_number: &str) -> Result<Vec<SemanticVector>> {
// The new PatentSearch API handles citations differently
// Backward citations are available via /api/v1/us_patent_citation/ endpoint
// For now, return empty - full citation support requires additional implementation
Ok(Vec::new())
}
/// Convert USPTO patent records to SemanticVectors
fn convert_patents_to_vectors(&self, patents: Vec<UsptoPatent>) -> Result<Vec<SemanticVector>> {
let mut vectors = Vec::new();
for patent in patents {
let title = patent.patent_title.unwrap_or_else(|| "Untitled Patent".to_string());
let abstract_text = patent.patent_abstract.unwrap_or_default();
// Create combined text for embedding
let text = format!("{} {}", title, abstract_text);
let embedding = self.embedder.embed_text(&text);
// Parse grant date (prefer patent_date, fallback to app_date)
let timestamp = patent
.patent_date
.or(patent.app_date)
.as_ref()
.and_then(|d| NaiveDate::parse_from_str(d, "%Y-%m-%d").ok())
.and_then(|d| d.and_hms_opt(0, 0, 0))
.map(|dt| dt.and_utc())
.unwrap_or_else(Utc::now);
// Extract assignee names
let assignees = patent
.assignees
.iter()
.map(|a| {
a.assignee_organization
.clone()
.or_else(|| {
let first = a.assignee_individual_name_first.as_deref().unwrap_or("");
let last = a.assignee_individual_name_last.as_deref().unwrap_or("");
if !first.is_empty() || !last.is_empty() {
Some(format!("{} {}", first, last).trim().to_string())
} else {
None
}
})
.unwrap_or_default()
})
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join(", ");
// Extract inventor names
let inventors = patent
.inventors
.iter()
.filter_map(|i| {
let first = i.inventor_name_first.as_deref().unwrap_or("");
let last = i.inventor_name_last.as_deref().unwrap_or("");
if !first.is_empty() || !last.is_empty() {
Some(format!("{} {}", first, last).trim().to_string())
} else {
None
}
})
.collect::<Vec<_>>()
.join(", ");
// Extract CPC codes
let cpc_codes = patent
.cpcs
.iter()
.filter_map(|cpc| {
cpc.cpc_group_id
.clone()
.or_else(|| cpc.cpc_subclass_id.clone())
.or_else(|| cpc.cpc_section_id.clone())
})
.collect::<Vec<_>>()
.join(", ");
// Build metadata
let mut metadata = HashMap::new();
metadata.insert("patent_number".to_string(), patent.patent_number.clone());
metadata.insert("title".to_string(), title);
metadata.insert("abstract".to_string(), abstract_text);
metadata.insert("assignee".to_string(), assignees);
metadata.insert("inventors".to_string(), inventors);
metadata.insert("cpc_codes".to_string(), cpc_codes);
metadata.insert(
"citations_count".to_string(),
patent.citedby_patent_count.unwrap_or(0).to_string(),
);
metadata.insert(
"cited_count".to_string(),
patent.cited_patent_count.unwrap_or(0).to_string(),
);
metadata.insert("source".to_string(), "uspto".to_string());
vectors.push(SemanticVector {
id: format!("US{}", patent.patent_number),
embedding,
domain: Domain::Research, // Could be Domain::Innovation if that variant exists
timestamp,
metadata,
});
}
Ok(vectors)
}
/// GET request with retry logic
async fn fetch_with_retry(&self, url: &str) -> Result<reqwest::Response> {
let mut retries = 0;
loop {
match self.client.get(url).send().await {
Ok(response) => {
if response.status() == StatusCode::TOO_MANY_REQUESTS && retries < MAX_RETRIES
{
retries += 1;
sleep(Duration::from_millis(RETRY_DELAY_MS * retries as u64)).await;
continue;
}
if !response.status().is_success() {
return Err(FrameworkError::Network(
reqwest::Error::from(response.error_for_status().unwrap_err()),
));
}
return Ok(response);
}
Err(_) if retries < MAX_RETRIES => {
retries += 1;
sleep(Duration::from_millis(RETRY_DELAY_MS * retries as u64)).await;
}
Err(e) => return Err(FrameworkError::Network(e)),
}
}
}
/// POST request with retry logic (kept for backwards compatibility)
#[allow(dead_code)]
async fn post_with_retry(
&self,
url: &str,
json: &serde_json::Value,
) -> Result<reqwest::Response> {
let mut retries = 0;
loop {
match self.client.post(url).json(json).send().await {
Ok(response) => {
if response.status() == StatusCode::TOO_MANY_REQUESTS && retries < MAX_RETRIES
{
retries += 1;
sleep(Duration::from_millis(RETRY_DELAY_MS * retries as u64)).await;
continue;
}
if !response.status().is_success() {
return Err(FrameworkError::Network(
reqwest::Error::from(response.error_for_status().unwrap_err()),
));
}
return Ok(response);
}
Err(_) if retries < MAX_RETRIES => {
retries += 1;
sleep(Duration::from_millis(RETRY_DELAY_MS * retries as u64)).await;
}
Err(e) => return Err(FrameworkError::Network(e)),
}
}
}
}
impl Default for UsptoPatentClient {
fn default() -> Self {
Self::new().expect("Failed to create USPTO client")
}
}
// ============================================================================
// EPO Open Patent Services Client (Placeholder)
// ============================================================================
/// Client for European Patent Office (EPO) Open Patent Services
///
/// Note: This is a placeholder for future EPO integration.
/// The EPO OPS API requires registration and OAuth authentication.
/// See: https://developers.epo.org/
pub struct EpoClient {
client: Client,
base_url: String,
consumer_key: Option<String>,
consumer_secret: Option<String>,
rate_limit_delay: Duration,
embedder: Arc<SimpleEmbedder>,
}
impl EpoClient {
/// Create a new EPO client
///
/// # Arguments
/// * `consumer_key` - EPO API consumer key (from developer registration)
/// * `consumer_secret` - EPO API consumer secret
///
/// Registration required at: https://developers.epo.org/
pub fn new(consumer_key: Option<String>, consumer_secret: Option<String>) -> Result<Self> {
let client = Client::builder()
.timeout(Duration::from_secs(30))
.build()
.map_err(FrameworkError::Network)?;
Ok(Self {
client,
base_url: "https://ops.epo.org/3.2/rest-services".to_string(),
consumer_key,
consumer_secret,
rate_limit_delay: Duration::from_millis(EPO_RATE_LIMIT_MS),
embedder: Arc::new(SimpleEmbedder::new(512)),
})
}
/// Search European patents
///
/// Note: Implementation requires OAuth authentication flow.
/// This is a placeholder for future development.
pub async fn search_patents(
&self,
_query: &str,
_max_results: usize,
) -> Result<Vec<SemanticVector>> {
Err(FrameworkError::Config(
"EPO client not yet implemented. Requires OAuth authentication.".to_string(),
))
}
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_uspto_client_creation() {
let client = UsptoPatentClient::new();
assert!(client.is_ok());
}
#[tokio::test]
async fn test_epo_client_creation() {
let client = EpoClient::new(None, None);
assert!(client.is_ok());
}
#[test]
fn test_default_client() {
let client = UsptoPatentClient::default();
assert_eq!(
client.rate_limit_delay,
Duration::from_millis(USPTO_RATE_LIMIT_MS)
);
}
#[test]
fn test_rate_limiting() {
let client = UsptoPatentClient::new().unwrap();
assert_eq!(
client.rate_limit_delay,
Duration::from_millis(USPTO_RATE_LIMIT_MS)
);
}
#[test]
fn test_cpc_classification_mapping() {
// Verify we handle different CPC code lengths correctly
let test_cases = vec![
("Y02", "cpc_section_id"),
("G06N", "cpc_subclass_id"),
("Y02E10/50", "cpc_group_id"),
];
for (code, expected_field) in test_cases {
let field = if code.len() <= 3 {
"cpc_section_id"
} else if code.len() <= 4 {
"cpc_subclass_id"
} else {
"cpc_group_id"
};
assert_eq!(field, expected_field, "Failed for CPC code: {}", code);
}
}
#[tokio::test]
#[ignore] // Requires network access
async fn test_search_patents_integration() {
let client = UsptoPatentClient::new().unwrap();
// Test basic search
let result = client.search_patents("quantum computing", 5).await;
// Should either succeed or fail with network error, not panic
match result {
Ok(patents) => {
assert!(patents.len() <= 5);
for patent in patents {
assert!(patent.id.starts_with("US"));
assert_eq!(patent.domain, Domain::Research);
assert!(!patent.metadata.is_empty());
}
}
Err(e) => {
// Network errors are acceptable in tests
println!("Network test skipped: {}", e);
}
}
}
#[tokio::test]
#[ignore] // Requires network access
async fn test_search_by_cpc_integration() {
let client = UsptoPatentClient::new().unwrap();
// Test CPC search for AI/ML patents
let result = client.search_by_cpc("G06N", 5).await;
match result {
Ok(patents) => {
assert!(patents.len() <= 5);
for patent in patents {
let cpc_codes = patent.metadata.get("cpc_codes").map(|s| s.as_str()).unwrap_or("");
// Should contain G06N classification
assert!(
cpc_codes.contains("G06N") || cpc_codes.is_empty(),
"Expected G06N in CPC codes, got: {}",
cpc_codes
);
}
}
Err(e) => {
println!("Network test skipped: {}", e);
}
}
}
#[test]
fn test_embedding_dimension() {
let client = UsptoPatentClient::new().unwrap();
// Verify embedding dimension is set correctly for technical text
let embedding = client.embedder.embed_text("test patent description");
assert_eq!(embedding.len(), 512);
}
}