-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
394 lines (355 loc) · 11 KB
/
Copy pathmain.go
File metadata and controls
394 lines (355 loc) · 11 KB
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
package main
import (
"flag"
"fmt"
"net/url"
"os"
"strconv"
"strings"
)
var version = "dev"
const usage = `pqai - PQAI API command-line client (https://api.projectpq.ai)
Usage:
pqai <command> [flags] [args]
Search (token required):
search <query> search prior-art documents by text query (102)
combos <query> search prior-art "combinations" by text query (103)
prior-art <pn> search prior art for a patent (documents predating its filing)
similar <pn> search documents similar to a patent
Documents/patents (token required):
patent <pn> look up patent data
document <id> look up a document in the PQAI database
vector <pn> <field> look up a patent's embedding vector (field: cpcs | abstract)
snippet <pn> -q <text> look up the snippet for a query-document pair
mapping <pn> -q <text> look up the per-element mapping for a query-document pair
dataset fetch a dataset sample (-name, -n)
Drawings:
drawings <pn> list a patent's drawings (token required)
drawing <pn> <n> download a patent drawing (PNG, no token required)
Classification (token required):
cpcs <text> suggest CPC classifications for text
gaus <text> suggest a Group Art Unit for text
cpc-def <cpc> look up a CPC class definition
Config:
config set-api-key <token> save the API token to the global config file (used from any folder)
config set-api-key --from-dotenv <path> read the token from a dotenv file and save it to the global config
config show show the global config file location and where the active token came from
Environment variables:
PQAI_API_KEY API access token (required, except for the drawing-download route)
PQAI_ENDPOINT override the API base URL (default: https://api.projectpq.ai)
The token is resolved in this order (highest priority first):
1. PQAI_API_KEY exported in your shell
2. A .env file in the current folder
3. The global config file saved via 'pqai config set-api-key' (works from any folder)
Run 'pqai <command> -h' to see flags for each command.
version print the CLI version
`
func main() {
_, hadShellEnv := os.LookupEnv("PQAI_API_KEY")
loadDotEnv()
_, hadEnvAfterDotenv := os.LookupEnv("PQAI_API_KEY")
loadGlobalConfig()
switch {
case hadShellEnv:
apiKeySource = "shell environment variable (export PQAI_API_KEY=...)"
case hadEnvAfterDotenv:
apiKeySource = "local .env file in the current folder"
default:
if v, ok := os.LookupEnv("PQAI_API_KEY"); ok && v != "" {
apiKeySource = "global config file (pqai config set-api-key)"
}
}
if len(os.Args) < 2 {
fmt.Fprint(os.Stderr, usage)
os.Exit(2)
}
c := NewClient()
cmd, args := os.Args[1], os.Args[2:]
var err error
switch cmd {
case "search":
err = cmdSearch(c, "/search/102/", args)
case "combos":
err = cmdSearch(c, "/search/103/", args)
case "prior-art":
err = cmdPatentSearch(c, "/prior-art/patent/", args)
case "similar":
err = cmdPatentSearch(c, "/similar/", args)
case "snippet":
err = cmdSnippet(c, "/snippets/", args)
case "mapping":
err = cmdSnippet(c, "/mappings/", args)
case "document":
err = cmdDocument(c, args)
case "patent":
err = cmdPatent(c, args)
case "vector":
err = cmdVector(c, args)
case "dataset":
err = cmdDataset(c, args)
case "drawings":
err = cmdDrawings(c, args)
case "drawing":
err = cmdDrawing(c, args)
case "cpcs":
err = cmdText(c, "/suggest/cpcs", "text", args)
case "gaus":
err = cmdText(c, "/predict/gaus", "text", args)
case "cpc-def":
err = cmdText(c, "/definitions/cpcs", "cpc", args)
case "config":
err = cmdConfig(args)
case "help", "-h", "--help":
fmt.Print(usage)
case "version", "-v", "--version":
fmt.Println("pqai " + version)
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n\n", cmd)
fmt.Fprint(os.Stderr, usage)
os.Exit(2)
}
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
}
func cmdSearch(c *Client, route string, args []string) error {
fs := flag.NewFlagSet("search", flag.ExitOnError)
n := fs.Int("n", 10, "number of results")
offset := fs.Int("offset", 0, "pagination offset (0-based)")
index := fs.String("index", "", "CPC subclass (e.g. H04W, auto)")
cc := fs.String("cc", "", "country code list (e.g. US,EP,WO)")
dtype := fs.String("dtype", "", "cutoff-date basis: priority|publication|filing")
after := fs.String("after", "", "cutoff start date (e.g. 2016-01-01)")
before := fs.String("before", "", "cutoff end date (e.g. 2019-12-31)")
typ := fs.String("type", "", "document type: patent|npl")
snip := fs.Bool("snip", false, "include a matching snippet")
maps := fs.Bool("maps", false, "include per-element mapping")
lq := fs.String("lq", "", `latent query JSON (e.g. {"relevant":[],"irrelevant":[]})`)
asJSON := fs.Bool("json", false, "print raw JSON output")
pos := parseArgs(fs, args)
if len(pos) < 1 {
return fmt.Errorf("please provide a search query")
}
params := url.Values{"q": {pos[0]}, "n": {strconv.Itoa(*n)}}
if *offset > 0 {
params.Set("offset", strconv.Itoa(*offset))
}
setIf(params, "index", *index)
setIf(params, "cc", *cc)
setIf(params, "dtype", *dtype)
setIf(params, "after", *after)
setIf(params, "before", *before)
setIf(params, "type", *typ)
setIf(params, "lq", *lq)
if *snip {
params.Set("snip", "1")
}
if *maps {
params.Set("maps", "1")
}
body, err := c.Get(route, params, true)
if err != nil {
return err
}
if *asJSON {
printJSON(body)
} else {
printSearchResults(body)
}
return nil
}
func cmdPatentSearch(c *Client, route string, args []string) error {
fs := flag.NewFlagSet("patent-search", flag.ExitOnError)
n := fs.Int("n", 10, "number of results")
offset := fs.Int("offset", 0, "pagination offset (0-based)")
index := fs.String("index", "", "CPC subclass (e.g. H04W, auto)")
typ := fs.String("type", "", "document type: patent|npl")
asJSON := fs.Bool("json", false, "print raw JSON output")
pos := parseArgs(fs, args)
if len(pos) < 1 {
return fmt.Errorf("please provide a patent number (e.g. US7654321B2)")
}
params := url.Values{"pn": {pos[0]}, "n": {strconv.Itoa(*n)}}
if *offset > 0 {
params.Set("offset", strconv.Itoa(*offset))
}
setIf(params, "index", *index)
setIf(params, "type", *typ)
body, err := c.Get(route, params, true)
if err != nil {
return err
}
if *asJSON {
printJSON(body)
} else {
printSearchResults(body)
}
return nil
}
func cmdSnippet(c *Client, route string, args []string) error {
fs := flag.NewFlagSet("snippet", flag.ExitOnError)
q := fs.String("q", "", "text query")
pos := parseArgs(fs, args)
if len(pos) < 1 || *q == "" {
return fmt.Errorf("usage: pqai snippet|mapping <pn> -q <query>")
}
params := url.Values{"q": {*q}, "pn": {pos[0]}}
body, err := c.Get(route, params, true)
if err != nil {
return err
}
printJSON(body)
return nil
}
func cmdDocument(c *Client, args []string) error {
if len(args) < 1 {
return fmt.Errorf("please provide a document ID (e.g. US7654321B2)")
}
body, err := c.Get("/documents/", url.Values{"id": {args[0]}}, true)
if err != nil {
return err
}
printJSON(body)
return nil
}
func cmdPatent(c *Client, args []string) error {
if len(args) < 1 {
return fmt.Errorf("please provide a patent number (e.g. US7654321B2)")
}
body, err := c.Get("/patents/"+url.PathEscape(args[0]), nil, true)
if err != nil {
return err
}
printJSON(body)
return nil
}
func cmdVector(c *Client, args []string) error {
if len(args) < 2 {
return fmt.Errorf("usage: pqai vector <pn> <field> (field: cpcs | abstract)")
}
route := "/patents/" + url.PathEscape(args[0]) + "/vectors/" + url.PathEscape(args[1])
body, err := c.Get(route, nil, true)
if err != nil {
return err
}
printJSON(body)
return nil
}
func cmdDataset(c *Client, args []string) error {
fs := flag.NewFlagSet("dataset", flag.ExitOnError)
name := fs.String("name", "PoC", "dataset name")
n := fs.Int("n", 0, "sample number")
parseArgs(fs, args)
params := url.Values{"dataset": {*name}, "n": {strconv.Itoa(*n)}}
body, err := c.Get("/datasets/", params, true)
if err != nil {
return err
}
printJSON(body)
return nil
}
func cmdDrawings(c *Client, args []string) error {
fs := flag.NewFlagSet("drawings", flag.ExitOnError)
thumb := fs.Bool("thumb", false, "list thumbnails instead")
pos := parseArgs(fs, args)
if len(pos) < 1 {
return fmt.Errorf("please provide a patent number (e.g. US7654321B2)")
}
kind := "drawings"
if *thumb {
kind = "thumbnails"
}
body, err := c.Get("/patents/"+url.PathEscape(pos[0])+"/"+kind, nil, true)
if err != nil {
return err
}
printJSON(body)
return nil
}
func cmdDrawing(c *Client, args []string) error {
fs := flag.NewFlagSet("drawing", flag.ExitOnError)
thumb := fs.Bool("thumb", false, "download as a thumbnail")
w := fs.Int("w", 0, "thumbnail width (px)")
h := fs.Int("h", 0, "thumbnail height (px)")
out := fs.String("o", "", "output file path (default: <pn>_<n>.png)")
pos := parseArgs(fs, args)
if len(pos) < 2 {
return fmt.Errorf("usage: pqai drawing <pn> <n> [-thumb] [-w 300] [-o out.png]")
}
pn, n := pos[0], pos[1]
kind := "drawings"
if *thumb || *w > 0 || *h > 0 {
kind = "thumbnails"
}
params := url.Values{}
if *w > 0 {
params.Set("w", strconv.Itoa(*w))
}
if *h > 0 {
params.Set("h", strconv.Itoa(*h))
}
route := "/patents/" + url.PathEscape(pn) + "/" + kind + "/" + url.PathEscape(n)
body, err := c.Get(route, params, false)
if err != nil {
return err
}
path := *out
if path == "" {
path = fmt.Sprintf("%s_%s.png", pn, n)
}
if err := os.WriteFile(path, body, 0o644); err != nil {
return err
}
fmt.Printf("Saved: %s (%d bytes)\n", path, len(body))
return nil
}
func cmdText(c *Client, route, key string, args []string) error {
if len(args) < 1 {
return fmt.Errorf("please provide text")
}
body, err := c.Get(route, url.Values{key: {args[0]}}, true)
if err != nil {
return err
}
printJSON(body)
return nil
}
// parseArgs allows flags and positional arguments to appear in any order,
// which Go's flag package does not support natively (it stops parsing flags
// at the first positional argument). It returns the positional arguments.
func parseArgs(fs *flag.FlagSet, args []string) []string {
var flagArgs, positional []string
for i := 0; i < len(args); i++ {
a := args[i]
if a == "--" {
positional = append(positional, args[i+1:]...)
break
}
if len(a) > 1 && a[0] == '-' {
flagArgs = append(flagArgs, a)
name := strings.TrimLeft(a, "-")
if strings.Contains(name, "=") {
continue // value embedded, no extra token to consume
}
if f := fs.Lookup(name); f != nil {
if bv, ok := f.Value.(interface{ IsBoolFlag() bool }); ok && bv.IsBoolFlag() {
continue // boolean flags don't consume the next token
}
}
if i+1 < len(args) {
i++
flagArgs = append(flagArgs, args[i])
}
continue
}
positional = append(positional, a)
}
fs.Parse(flagArgs)
return positional
}
func setIf(params url.Values, key, val string) {
if val != "" {
params.Set(key, val)
}
}