-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathmain.py
More file actions
789 lines (614 loc) · 22.2 KB
/
Copy pathmain.py
File metadata and controls
789 lines (614 loc) · 22.2 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
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import json
import os
from pathlib import Path
from typing import Optional
import click
from cli.client import EcragApiClient
from cli.config import get_config
def pretty_print(data):
"""Pretty print JSON data."""
click.echo(json.dumps(data, indent=2))
def run_rag_query(client: EcragApiClient, query: str, top_n: int, max_tokens: int):
"""Run the standard RAG query flow used by both query and chat rag."""
result = client.ragqna(query, top_n, max_tokens)
pretty_print(result)
def run_chatqna_query(client: EcragApiClient, query: str, top_n: int, max_tokens: int):
"""Run the default ChatQnA query flow used by the top-level query command."""
result = client.chatqna(query, top_n, max_tokens)
pretty_print(result)
@click.group()
@click.option("--host", default=None, help="Server host URL (env: ECRAG_HOST)")
@click.option("--port", default=None, type=int, help="Server port (env: ECRAG_PORT)")
@click.option("--mega-port", default=None, type=int, help="Mega service port (env: ECRAG_MEGA_PORT)")
@click.pass_context
def cli(ctx, host: Optional[str], port: Optional[int], mega_port: Optional[int]):
"""EdgeCraft RAG CLI Tool.
Configure server connection via command-line options or environment variables:
- ECRAG_HOST: Server host (default: http://localhost)
- ECRAG_PORT: Server port (default: 16010)
- ECRAG_MEGA_PORT: Mega service port (default: 16011)
"""
ctx.ensure_object(dict)
# Get defaults from config
config = get_config()
# Use provided options or environment/defaults
final_host = host or config.host
final_port = port or config.port
final_mega_port = mega_port or config.mega_port
# Normalize host URL
if not final_host.startswith(("http://", "https://")):
final_host = f"http://{final_host}"
ctx.obj["client"] = EcragApiClient(host=final_host, server_port=final_port, mega_port=final_mega_port)
# ============== Pipeline Commands ==============
@cli.group()
def pipeline():
"""Manage pipelines."""
pass
@pipeline.command()
@click.option("-n", "--name", required=True, help="Pipeline name")
@click.option("-f", "--file", type=click.Path(exists=True), help="Pipeline JSON file")
@click.option("-d", "--data", help="Pipeline data as JSON string")
@click.pass_context
def create(ctx, name: str, file: Optional[str], data: Optional[str]):
"""Create a new pipeline."""
client = ctx.obj["client"]
if file:
with open(file, "r") as f:
pipeline_data = json.load(f)
elif data:
pipeline_data = json.loads(data)
else:
click.echo("Error: either --file or --data must be provided")
return
pipeline_data["name"] = name
result = client.create_pipeline(pipeline_data)
pretty_print(result)
@pipeline.command()
@click.pass_context
def list(ctx):
"""List all pipelines."""
client = ctx.obj["client"]
result = client.get_pipelines()
pretty_print(result)
@pipeline.command()
@click.option("-n", "--name", required=True, help="Pipeline name")
@click.pass_context
def get(ctx, name: str):
"""Get a specific pipeline."""
client = ctx.obj["client"]
result = client.get_pipeline(name)
pretty_print(result)
@pipeline.command()
@click.option("-n", "--name", required=True, help="Pipeline name")
@click.pass_context
def get_json(ctx, name: str):
"""Get pipeline JSON data."""
client = ctx.obj["client"]
result = client.get_pipeline_json(name)
pretty_print(result)
@pipeline.command()
@click.option("-n", "--name", required=True, help="Pipeline name")
@click.pass_context
def activate(ctx, name: str):
"""Activate a pipeline."""
client = ctx.obj["client"]
result = client.activate_pipeline(name)
pretty_print(result)
@pipeline.command()
@click.option("-n", "--name", required=True, help="Pipeline name")
@click.pass_context
def deactivate(ctx, name: str):
"""Deactivate a pipeline."""
client = ctx.obj["client"]
result = client.deactivate_pipeline(name)
pretty_print(result)
@pipeline.command()
@click.option("-n", "--name", required=True, help="Pipeline name")
@click.pass_context
def delete(ctx, name: str):
"""Delete a pipeline."""
client = ctx.obj["client"]
if click.confirm(f"Are you sure you want to delete pipeline '{name}'?"):
result = client.delete_pipeline(name)
pretty_print(result)
@pipeline.command()
@click.option("-n", "--name", required=True, help="Pipeline name")
@click.pass_context
def benchmark(ctx, name: Optional[str]):
"""Get pipeline benchmark data."""
client = ctx.obj["client"]
if name:
result = client.get_pipeline_benchmarks(name)
else:
result = client.get_pipeline_benchmark()
pretty_print(result)
@pipeline.command()
@click.option("-f", "--file", type=click.Path(exists=True), required=True, help="Pipeline JSON file to import")
@click.pass_context
def import_pipeline(ctx, file: str):
"""Import a pipeline from JSON file."""
client = ctx.obj["client"]
result = client.import_pipeline(file)
pretty_print(result)
# ============== Model Commands ==============
@cli.group()
def model():
"""Manage models."""
pass
@model.command()
@click.option("--type", "model_type", default="LLM", help="Model type (LLM, vLLM, reranker, embedding, etc.)")
@click.option("--id", "model_id", required=True, help="Model ID")
@click.option("--path", "model_path", default="./", help="Model path")
@click.option("--device", default="cpu", help="Device (cpu, gpu)")
@click.option("--weight", default="INT4", help="Weight type (INT4, INT8, FP16)")
@click.pass_context
def load(ctx, model_type: str, model_id: str, model_path: str, device: str, weight: str):
"""Load a model."""
client = ctx.obj["client"]
model_data = {
"model_type": model_type,
"model_id": model_id,
"model_path": model_path,
"device": device,
"weight": weight,
}
result = client.load_model(model_data)
pretty_print(result)
@model.command()
@click.pass_context
def list(ctx): # noqa: F811
"""List all models."""
client = ctx.obj["client"]
result = client.get_models()
pretty_print(result)
@model.command()
@click.option("--id", "model_id", required=True, help="Model ID")
@click.pass_context
def get(ctx, model_id: str): # noqa: F811
"""Get a specific model."""
client = ctx.obj["client"]
result = client.get_model(model_id)
pretty_print(result)
@model.command()
@click.option("--id", "model_id", required=True, help="Model ID")
@click.option("--device", help="New device")
@click.option("--weight", help="New weight")
@click.pass_context
def update(ctx, model_id: str, device: Optional[str], weight: Optional[str]):
"""Update a model."""
client = ctx.obj["client"]
model_data = {}
if device:
model_data["device"] = device
if weight:
model_data["weight"] = weight
result = client.update_model(model_id, model_data)
pretty_print(result)
@model.command()
@click.option("--id", "model_id", required=True, help="Model ID")
@click.pass_context
def delete(ctx, model_id: str): # noqa: F811
"""Delete a model."""
client = ctx.obj["client"]
if click.confirm(f"Are you sure you want to delete model '{model_id}'?"):
result = client.delete_model(model_id)
pretty_print(result)
@model.command()
@click.option("--id", "model_id", required=True, help="Model ID")
@click.pass_context
def weights(ctx, model_id: str):
"""Get available weights for a model."""
client = ctx.obj["client"]
result = client.get_model_weights(model_id)
pretty_print(result)
@model.command()
@click.option("--type", "model_type", required=True, help="Model type (LLM, vLLM, reranker, embedding, etc.)")
@click.option("--server", help="vLLM server address (optional)")
@click.pass_context
def available(ctx, model_type: str, server: Optional[str]):
"""List available models by type."""
client = ctx.obj["client"]
result = client.get_available_models(model_type, server)
pretty_print(result)
# ============== Knowledge Base Commands ==============
@cli.group()
def kb():
"""Manage knowledge bases."""
pass
@kb.command()
@click.option("-n", "--name", required=True, help="Knowledge base name")
@click.option("--description", help="Knowledge base description")
@click.option("-f", "--file", type=click.Path(exists=True), help="KB config JSON file")
@click.pass_context
def create(ctx, name: str, description: Optional[str], file: Optional[str]): # noqa: F811
"""Create a knowledge base."""
client = ctx.obj["client"]
if file:
with open(file, "r") as f:
kb_data = json.load(f)
else:
kb_data = {"name": name}
if description:
kb_data["description"] = description
result = client.create_knowledge_base(kb_data)
pretty_print(result)
@kb.command()
@click.pass_context
def list(ctx): # noqa: F811
"""List all knowledge bases."""
client = ctx.obj["client"]
result = client.get_knowledge_bases()
pretty_print(result)
@kb.command()
@click.option("-n", "--name", required=True, help="Knowledge base name")
@click.pass_context
def get(ctx, name: str): # noqa: F811
"""Get a specific knowledge base."""
client = ctx.obj["client"]
result = client.get_knowledge_base(name)
pretty_print(result)
@kb.command()
@click.option("-n", "--name", required=True, help="Knowledge base name")
@click.pass_context
def get_json(ctx, name: str): # noqa: F811
"""Get knowledge base JSON data."""
client = ctx.obj["client"]
result = client.get_knowledge_base_json(name)
pretty_print(result)
@kb.command()
@click.option("-n", "--name", required=True, help="Knowledge base name")
@click.option("--page", "page_num", default=1, type=int, help="Page number")
@click.option("--size", "page_size", default=20, type=int, help="Page size")
@click.pass_context
def filemap(ctx, name: str, page_num: int, page_size: int):
"""Get knowledge base file map."""
client = ctx.obj["client"]
result = client.get_knowledge_base_filemap(name, page_num, page_size)
pretty_print(result)
@kb.command()
@click.option("-n", "--name", required=True, help="Knowledge base name")
@click.option("--active", type=bool, help="Set active status")
@click.option("--description", help="Update description")
@click.pass_context
def update(ctx, name: str, active: Optional[bool], description: Optional[str]): # noqa: F811
"""Update a knowledge base."""
client = ctx.obj["client"]
kb_data = {"name": name}
if active is not None:
kb_data["active"] = active
if description:
kb_data["description"] = description
result = client.update_knowledge_base(kb_data)
pretty_print(result)
@kb.command()
@click.option("-n", "--name", required=True, help="Knowledge base name")
@click.pass_context
def delete(ctx, name: str): # noqa: F811
"""Delete a knowledge base."""
client = ctx.obj["client"]
if click.confirm(f"Are you sure you want to delete knowledge base '{name}'?"):
result = client.delete_knowledge_base(name)
pretty_print(result)
@kb.command()
@click.option("-n", "--name", required=True, help="Knowledge base name")
@click.option("--paths", multiple=True, required=True, help="File paths to add")
@click.pass_context
def add_files(ctx, name: str, paths: tuple):
"""Add files to a knowledge base."""
client = ctx.obj["client"]
result = client.add_files_to_kb(name, list(paths))
pretty_print(result)
@kb.command()
@click.option("-n", "--name", required=True, help="Knowledge base name")
@click.option("--paths", multiple=True, required=True, help="File paths to delete")
@click.pass_context
def delete_files(ctx, name: str, paths: tuple):
"""Delete files from a knowledge base."""
client = ctx.obj["client"]
result = client.delete_files_from_kb(name, list(paths))
pretty_print(result)
# ============== Experience Commands ==============
@cli.group()
def experience():
"""Manage experiences (Q&A pairs)."""
pass
@experience.command()
@click.pass_context
def list(ctx): # noqa: F811
"""List all experiences."""
client = ctx.obj["client"]
result = client.get_experiences()
pretty_print(result)
@experience.command()
@click.option("--id", required=True, help="Experience ID")
@click.pass_context
def get(ctx, id: str): # noqa: F811
"""Get a specific experience."""
client = ctx.obj["client"]
result = client.get_experience(id)
pretty_print(result)
@experience.command()
@click.option("--id", required=True, help="Experience ID")
@click.option("--question", required=True, help="Question")
@click.option("--content", multiple=True, required=True, help="Answer content")
@click.pass_context
def create(ctx, id: str, question: str, content: tuple): # noqa: F811
"""Create or update an experience."""
client = ctx.obj["client"]
exp_data = {"idx": id, "question": question, "content": list(content)}
result = client.update_experience(exp_data)
pretty_print(result)
@experience.command()
@click.option("--id", required=True, help="Experience ID")
@click.pass_context
def delete(ctx, id: str): # noqa: F811
"""Delete an experience."""
client = ctx.obj["client"]
if click.confirm(f"Are you sure you want to delete experience '{id}'?"):
result = client.delete_experience(id)
pretty_print(result)
@experience.command()
@click.option("-f", "--file", type=click.Path(exists=True), required=True, help="Experiences JSON file")
@click.pass_context
def load_file(ctx, file: str):
"""Load experiences from a file."""
client = ctx.obj["client"]
result = client.add_experiences_from_file(file)
pretty_print(result)
# ============== Agent Commands ==============
@cli.group()
def agent():
"""Manage agents."""
pass
@agent.command()
@click.pass_context
def list(ctx): # noqa: F811
"""List all agents."""
client = ctx.obj["client"]
result = client.get_agents()
pretty_print(result)
@agent.command()
@click.option("-n", "--name", required=True, help="Agent name")
@click.pass_context
def get(ctx, name: str): # noqa: F811
"""Get a specific agent."""
client = ctx.obj["client"]
result = client.get_agent(name)
pretty_print(result)
@agent.command()
@click.option("--type", required=True, help="Agent type (react_llm, etc.)")
@click.pass_context
def configs(ctx, type: str):
"""Get default configs for an agent type."""
client = ctx.obj["client"]
result = client.get_agent_configs(type)
pretty_print(result)
@agent.command()
@click.option("-n", "--name", required=True, help="Agent name")
@click.option("--type", required=True, help="Agent type")
@click.option("--pipeline", required=True, help="Pipeline index or name")
@click.pass_context
def create(ctx, name: str, type: str, pipeline: str): # noqa: F811
"""Create an agent."""
client = ctx.obj["client"]
agent_data = {"name": name, "type": type, "pipeline_idx": pipeline}
result = client.create_agent(agent_data)
pretty_print(result)
@agent.command()
@click.option("-n", "--name", required=True, help="Agent name")
@click.option("--active", type=bool, help="Active status")
@click.pass_context
def update(ctx, name: str, active: Optional[bool]): # noqa: F811
"""Update an agent."""
client = ctx.obj["client"]
agent_data = {}
if active is not None:
agent_data["active"] = active
result = client.update_agent(name, agent_data)
pretty_print(result)
@agent.command()
@click.option("-n", "--name", required=True, help="Agent name")
@click.pass_context
def delete(ctx, name: str): # noqa: F811
"""Delete an agent."""
client = ctx.obj["client"]
if click.confirm(f"Are you sure you want to delete agent '{name}'?"):
result = client.delete_agent(name)
pretty_print(result)
# ============== Prompt Commands ==============
@cli.group()
def prompt():
"""Manage system prompts."""
pass
@prompt.command()
@click.pass_context
def get(ctx): # noqa: F811
"""Get the current system prompt."""
client = ctx.obj["client"]
result = client.get_prompt()
pretty_print(result)
@prompt.command()
@click.pass_context
def get_tagged(ctx):
"""Get the tagged system prompt."""
client = ctx.obj["client"]
result = client.get_tagged_prompt()
pretty_print(result)
@prompt.command()
@click.pass_context
def get_default(ctx):
"""Get the default system prompt."""
client = ctx.obj["client"]
result = client.get_default_prompt()
pretty_print(result)
@prompt.command()
@click.option("--text", help="Prompt text")
@click.option("-f", "--file", type=click.Path(exists=True), help="Prompt file")
@click.pass_context
def set(ctx, text: Optional[str], file: Optional[str]):
"""Update the system prompt."""
client = ctx.obj["client"]
if file:
with open(file, "r") as f:
prompt_text = f.read()
elif text:
prompt_text = text
else:
click.echo("Error: either --text or --file must be provided")
return
result = client.update_prompt(prompt_text)
pretty_print(result)
@prompt.command()
@click.pass_context
def reset(ctx):
"""Reset the system prompt to default."""
client = ctx.obj["client"]
if click.confirm("Are you sure you want to reset prompt to default?"):
result = client.reset_prompt()
pretty_print(result)
# ============== Data Commands ==============
@cli.group()
def data():
"""Manage data (nodes, documents, files)."""
pass
@data.command()
@click.pass_context
def nodes(ctx):
"""Get all nodes in active knowledge base."""
client = ctx.obj["client"]
result = client.get_nodes()
pretty_print(result)
@data.command()
@click.option("-n", "--name", required=True, help="Document name")
@click.pass_context
def nodes_by_doc(ctx, name: str):
"""Get nodes by document name."""
client = ctx.obj["client"]
result = client.get_nodes_by_document(name)
pretty_print(result)
@data.command()
@click.pass_context
def documents(ctx):
"""Get all document names in active knowledge base."""
client = ctx.obj["client"]
result = client.get_documents()
pretty_print(result)
@data.command()
@click.pass_context
def files(ctx):
"""Get all files."""
client = ctx.obj["client"]
result = client.get_files()
pretty_print(result)
@data.command()
@click.option("-n", "--name", required=True, help="File name")
@click.pass_context
def get_file(ctx, name: str):
"""Get a specific file."""
client = ctx.obj["client"]
result = client.get_file(name)
pretty_print(result)
@data.command()
@click.option("-n", "--name", required=True, help="File name")
@click.option("--path", required=True, type=click.Path(exists=True), help="File path")
@click.pass_context
def upload(ctx, name: str, path: str):
"""Upload a file."""
client = ctx.obj["client"]
result = client.upload_file(name, path)
pretty_print(result)
# ============== Session Commands ==============
@cli.group()
def session():
"""Manage sessions."""
pass
@session.command()
@click.pass_context
def list(ctx): # noqa: F811
"""List all sessions."""
client = ctx.obj["client"]
result = client.get_sessions()
pretty_print(result)
@session.command()
@click.option("--id", required=True, help="Session ID")
@click.pass_context
def get(ctx, id: str): # noqa: F811
"""Get a specific session."""
client = ctx.obj["client"]
result = client.get_session(id)
pretty_print(result)
# ============== System Commands ==============
@cli.group()
def system():
"""Get system information."""
pass
@system.command()
@click.pass_context
def info(ctx):
"""Get system information."""
client = ctx.obj["client"]
result = client.get_system_info()
pretty_print(result)
@system.command()
@click.pass_context
def devices(ctx):
"""Get available inference devices."""
client = ctx.obj["client"]
result = client.get_available_devices()
pretty_print(result)
# ============== Chat/Query Commands ==============
@cli.group()
def chat():
"""Chat and query operations."""
pass
@chat.command()
@click.option("--query", required=True, help="Query string")
@click.option("--top-n", default=5, type=int, help="Number of results to retrieve")
@click.option("--max-tokens", default=512, type=int, help="Max tokens in response")
@click.pass_context
def retrieve(ctx, query: str, top_n: int, max_tokens: int):
"""Retrieve relevant context chunks."""
client = ctx.obj["client"]
result = client.retrieval(query, top_n, max_tokens)
pretty_print(result)
@chat.command()
@click.option("--query", required=True, help="Query string")
@click.option("--top-n", default=5, type=int, help="Number of results to retrieve")
@click.option("--max-tokens", default=512, type=int, help="Max tokens in response")
@click.pass_context
def rag(ctx, query: str, top_n: int, max_tokens: int):
"""Run RAG pipeline (retrieval + generation)."""
client = ctx.obj["client"]
run_rag_query(client, query, top_n, max_tokens)
@cli.command()
@click.argument("query")
@click.option("--top-n", default=5, type=int, help="Number of results to retrieve")
@click.option("--max-tokens", default=512, type=int, help="Max tokens in response")
@click.pass_context
def query(ctx, query: str, top_n: int, max_tokens: int):
"""Shortcut for chat mega using a positional query argument."""
client = ctx.obj["client"]
run_chatqna_query(client, query, top_n, max_tokens)
@chat.command()
@click.option("--query", required=True, help="Query string")
@click.option("--top-n", default=5, type=int, help="Number of results to retrieve")
@click.option("--max-tokens", default=512, type=int, help="Max tokens in response")
@click.pass_context
def mega(ctx, query: str, top_n: int, max_tokens: int):
"""Run full ChatQnA (mega service)."""
client = ctx.obj["client"]
result = client.chatqna(query, top_n, max_tokens)
pretty_print(result)
@chat.command()
@click.option("--server", required=True, help="vLLM server address")
@click.option("--model", required=True, help="Model name")
@click.pass_context
def check_vllm(ctx, server: str, model: str):
"""Check vLLM server connection."""
client = ctx.obj["client"]
result = client.check_vllm_connection(server, model)
pretty_print(result)
if __name__ == "__main__":
cli(obj={})