-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathllmsetup.py
More file actions
1045 lines (896 loc) · 38.8 KB
/
llmsetup.py
File metadata and controls
1045 lines (896 loc) · 38.8 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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
########################################################
# APISCAN - AI Security Scanner Module #
# Licensed under the AGPL-v3.0 #
# Author: Perry Mertens pamsniffer@gmail.com (C) 2026 #
# For use with --api11 flag or AI features #
########################################################
import sys
import os
import subprocess
import json
import time
import platform
from pathlib import Path
TEST_SCRIPT = '''import os
VARS = [
"LLM_PROVIDER",
"LLM_MODEL",
"LLM_TEMPERATURE",
"LLM_TOP_P",
"LLM_MAX_TOKENS",
"LLM_CONNECT_TIMEOUT",
"LLM_READ_TIMEOUT",
"LLM_API_BASE",
"LLM_API_PORT",
"LLM_VERIFY_SSL",
"LLM_ANTHROPIC_THINKING",
"LLM_MAX_RETRIES",
"OLLAMA_HOST",
"OLLAMA_API_KEY",
"OPENAI_API_KEY",
"LLM_API_KEY",
"ANTHROPIC_API_KEY",
"AZURE_OPENAI_ENDPOINT",
"AZURE_OPENAI_AUTH_TYPE",
"AZURE_OPENAI_API_KEY",
"AZURE_OPENAI_AD_TOKEN",
"AZURE_OPENAI_TOKEN_SCOPE",
"AZURE_OPENAI_API_VERSION",
"DEEPSEEK_API_KEY",
"MISTRAL_API_KEY",
"GEMINI_API_KEY",
"OPENROUTER_API_KEY",
"OPENROUTER_HTTP_REFERER",
"OPENROUTER_X_TITLE",
]
def mask_value(name, value):
if not value:
return "NOT SET"
if any(token in name for token in ["KEY", "TOKEN", "SECRET"]):
if len(value) <= 8:
return "***"
return value[:4] + "***" + value[-4:]
return value
print("APISCAN environment check")
print("=" * 40)
for var_name in VARS:
print(f"{var_name}: {mask_value(var_name, os.getenv(var_name, ''))}")
'''
class Colors:
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BLUE = '\033[94m'
CYAN = '\033[96m'
MAGENTA = '\033[95m'
BOLD = '\033[1m'
END = '\033[0m'
#================ print_header: ========================
def print_header(text):
print(f"\n{Colors.MAGENTA}{'='*60}{Colors.END}")
print(f"{Colors.BOLD}{Colors.BLUE}{text.center(60)}{Colors.END}")
print(f"{Colors.MAGENTA}{'='*60}{Colors.END}")
#================ print_success: ========================
def print_success(text):
print(f"{Colors.GREEN}[OK]{Colors.END} {text}")
#================ print_warning: ========================
def print_warning(text):
print(f"{Colors.YELLOW}[warning]{Colors.END} {text}")
#================ print_error: ========================
def print_error(text):
print(f"{Colors.RED}[error]{Colors.END} {text}")
#================ print_info: ========================
def print_info(text):
print(f"{Colors.CYAN}[info]{Colors.END} {text}")
#================ current_shell_setup: ========================
def get_current_shell_setup_command(shell_info, scripts_created=None):
os_type = shell_info["os"]
shell = shell_info["shell"]
def script_exists(script_name):
return scripts_created is None or script_name in scripts_created
if shell == "powershell" and script_exists("apiscan_env.ps1"):
return "./apiscan_env.ps1" if os_type != "windows" else ".\\apiscan_env.ps1"
if os_type == "windows" and shell == "cmd" and script_exists("apiscan_env.bat"):
return "apiscan_env.bat"
if shell == "fish" and script_exists("apiscan_env.fish"):
return "source apiscan_env.fish"
if script_exists("apiscan_env.sh"):
return "source apiscan_env.sh"
return None
#================ all_shell_commands: ========================
def get_all_shell_setup_commands(scripts_created=None):
commands = {}
if scripts_created is None or "apiscan_env.ps1" in scripts_created:
commands["powershell"] = "./apiscan_env.ps1"
if scripts_created is None or "apiscan_env.bat" in scripts_created:
commands["cmd"] = "apiscan_env.bat"
if scripts_created is None or "apiscan_env.sh" in scripts_created:
commands["bash"] = "source apiscan_env.sh"
if scripts_created is None or "apiscan_env.fish" in scripts_created:
commands["fish"] = "source apiscan_env.fish"
return commands
#================ build_env_content: ========================
def build_env_content(all_config, include_header=False, shell_info=None):
env_content = []
if include_header:
env_content.extend([
"# APISCAN LLM Configuration",
f"# Generated by llmsetup.py - {time.strftime('%Y-%m-%d %H:%M:%S')}",
f"# OS: {shell_info['os']}, Shell: {shell_info['shell']}",
""
])
for key, value in all_config.items():
if value and not key.startswith("_"):
env_content.append(f"{key}={value}")
return "\n".join(env_content)
#================ detect_shell: ========================
def detect_shell():
shell_info = {
"os": platform.system().lower(),
"shell": None,
"terminal": None
}
if shell_info["os"] == "windows":
shell_env = os.environ.get("SHELL", "")
shell_env_lower = shell_env.lower()
if "MSYSTEM" in os.environ:
shell_info["terminal"] = "git_bash"
shell_info["shell"] = "bash"
elif any(name in shell_env_lower for name in ["bash", "zsh", "fish", "sh"]):
if "fish" in shell_env_lower:
shell_info["shell"] = "fish"
elif "zsh" in shell_env_lower:
shell_info["shell"] = "zsh"
else:
shell_info["shell"] = "bash"
else:
prompt_env = os.environ.get("PROMPT")
if prompt_env:
shell_info["shell"] = "cmd"
elif any(var in os.environ for var in ["POWERSHELL_DISTRIBUTION_CHANNEL", "PSExecutionPolicyPreference"]):
shell_info["shell"] = "powershell"
elif os.environ.get("PSModulePath") and not prompt_env:
shell_info["shell"] = "powershell"
else:
shell_info["shell"] = "cmd"
if "WT_SESSION" in os.environ:
shell_info["terminal"] = "windows_terminal"
elif "ConEmuANSI" in os.environ:
shell_info["terminal"] = "conemu"
elif "MSYSTEM" in os.environ:
shell_info["terminal"] = "git_bash"
else:
shell_info["terminal"] = "default"
elif shell_info["os"] in ["linux", "darwin"]:
shell = os.environ.get("SHELL", "")
if "bash" in shell:
shell_info["shell"] = "bash"
elif "zsh" in shell:
shell_info["shell"] = "zsh"
elif "fish" in shell:
shell_info["shell"] = "fish"
else:
shell_info["shell"] = "sh"
term = os.environ.get("TERM", "")
if "xterm" in term:
shell_info["terminal"] = "xterm"
elif "gnome" in term.lower():
shell_info["terminal"] = "gnome-terminal"
elif "konsole" in term.lower():
shell_info["terminal"] = "konsole"
elif "alacritty" in term.lower():
shell_info["terminal"] = "alacritty"
else:
shell_info["terminal"] = "unknown"
return shell_info
try:
from ai_client import MODEL_ALIASES as AI_MODEL_ALIASES
except Exception:
AI_MODEL_ALIASES = {}
def _with_aliases(models, prefixes=None):
out = list(models)
for model in AI_MODEL_ALIASES.values():
if prefixes and not str(model).startswith(tuple(prefixes)):
continue
if model not in out:
out.append(model)
return out
LLM_PROVIDERS = {
"ollama": {
"name": "Ollama (Local)",
"package": "",
"client": "ollama",
"env_vars": ["OLLAMA_HOST", "OLLAMA_API_KEY"],
"auth_type": "none",
"base_url": "http://localhost:11434",
"models": [
"llama3.2", "llama3.1", "llama3",
"mistral", "mixtral", "codellama",
"phi3", "gemma2", "qwen2.5", "qwen3"
],
"required": False,
"description": "Local LLMs via Ollama (free, offline)"
},
"openai": {
"name": "OpenAI",
"package": "openai>=1.0.0",
"client": "openai",
"env_vars": ["OPENAI_API_KEY"],
"auth_type": "api_key",
"base_url": "https://api.openai.com/v1",
"models": _with_aliases([
"gpt-5.5",
"gpt-5.5-mini",
"gpt-5",
"gpt-5-mini",
"gpt-4.1",
"gpt-4.1-mini",
"gpt-4o",
"gpt-4o-mini",
"o4-mini",
"o3",
"o3-mini",
"o1",
"o1-mini",
], prefixes=("gpt-", "o")),
"required": False,
"description": "Official OpenAI API (GPT / o-series reasoning models)"
},
"openai_compat": {
"name": "OpenAI-compatible",
"package": "openai>=1.0.0",
"client": "openai_compat",
"env_vars": ["LLM_API_BASE", "LLM_API_KEY"],
"auth_type": "api_key",
"base_url": "https://api.openai.com/v1",
"models": _with_aliases([
"gpt-4.1",
"gpt-4o-mini",
"openai/gpt-4o",
"mistral-large-latest",
"deepseek-chat",
"gemini-2.5-pro",
]),
"required": False,
"description": "Any OpenAI-compatible / self-hosted endpoint"
},
"azure_openai": {
"name": "Azure OpenAI",
"package": "openai>=1.0.0",
"client": "azure_openai",
"env_vars": ["AZURE_OPENAI_ENDPOINT", "AZURE_OPENAI_AUTH_TYPE", "AZURE_OPENAI_API_KEY", "AZURE_OPENAI_API_VERSION"],
"auth_type": "api_key",
"base_url": "",
"models": ["gpt-4.1", "gpt-4o", "gpt-4o-mini", "o4-mini", "o3-mini"],
"required": False,
"description": "Azure OpenAI deployments; LLM_MODEL must be your deployment name"
},
"anthropic": {
"name": "Anthropic Claude",
"package": "anthropic>=0.67.0",
"client": "anthropic",
"env_vars": ["ANTHROPIC_API_KEY"],
"auth_type": "api_key",
"base_url": "https://api.anthropic.com",
"models": [
"claude-sonnet-4-7",
"claude-opus-4-5",
"claude-sonnet-4-5",
"claude-haiku-4-5",
"claude-3-7-sonnet-latest",
"claude-3-5-sonnet-latest",
"claude-3-5-haiku-latest",
],
"required": False,
"description": "Claude AI from Anthropic"
},
"deepseek": {
"name": "DeepSeek",
"package": "openai>=1.0.0",
"client": "openai",
"env_vars": ["DEEPSEEK_API_KEY"],
"auth_type": "api_key",
"base_url": "https://api.deepseek.com",
"models": ["deepseek-chat", "deepseek-coder", "deepseek-reasoner"],
"required": False,
"description": "DeepSeek AI (cost-effective alternative)"
},
"mistral": {
"name": "Mistral",
"package": "openai>=1.0.0",
"client": "mistral",
"env_vars": ["MISTRAL_API_KEY"],
"auth_type": "api_key",
"base_url": "https://api.mistral.ai/v1",
"models": ["mistral-large-latest", "mistral-small-latest", "codestral-latest"],
"required": False,
"description": "Mistral models through OpenAI-compatible chat completions"
},
"gemini": {
"name": "Google Gemini",
"package": "openai>=1.0.0",
"client": "gemini",
"env_vars": ["GEMINI_API_KEY"],
"auth_type": "api_key",
"base_url": "https://generativelanguage.googleapis.com/v1beta/openai",
"models": ["gemini-2.5-pro", "gemini-2.5-flash"],
"required": False,
"description": "Gemini through Google's OpenAI-compatible endpoint"
},
"openrouter": {
"name": "OpenRouter",
"package": "openai>=1.0.0",
"client": "openrouter",
"env_vars": ["OPENROUTER_API_KEY", "OPENROUTER_HTTP_REFERER", "OPENROUTER_X_TITLE"],
"auth_type": "api_key",
"base_url": "https://openrouter.ai/api/v1",
"models": ["openai/gpt-4o", "openai/gpt-4o-mini", "anthropic/claude-sonnet-4.5", "google/gemini-2.5-pro", "deepseek/deepseek-chat"],
"required": False,
"description": "OpenRouter multi-model gateway"
}
}
#================ create_shell_specific_files: ========================
def create_shell_specific_files(all_config):
scripts_created = []
ps_content = [
"# APISCAN Environment Setup for PowerShell",
"# Generated by llmsetup.py",
"# NO ADMIN RIGHTS NEEDED",
"",
'Write-Host "Setting APISCAN environment variables..." -ForegroundColor Yellow',
"",
]
for key, value in all_config.items():
if value and not key.startswith("_"):
escaped_ps = value.replace('"', '`"').replace('$', '`$')
ps_content.append(f'$env:{key} = "{escaped_ps}"')
ps_content.extend([
"",
'Write-Host ""',
'Write-Host "APISCAN environment configured!" -ForegroundColor Green',
'Write-Host ""',
'Write-Host "LLM Provider: $env:LLM_PROVIDER" -ForegroundColor Cyan',
'Write-Host "Model: $env:LLM_MODEL" -ForegroundColor Cyan',
'if ($env:OLLAMA_HOST) {',
' Write-Host "Ollama Host: $env:OLLAMA_HOST" -ForegroundColor Cyan',
'}',
'Write-Host ""',
])
ps_file = Path("apiscan_env.ps1")
ps_file.write_text("\n".join(ps_content), encoding="utf-8")
scripts_created.append("apiscan_env.ps1")
bat_content = [
"@echo off",
"REM APISCAN Environment Setup for CMD",
"REM NO ADMIN RIGHTS NEEDED",
"",
"echo Setting APISCAN environment variables...",
"",
]
for key, value in all_config.items():
if value and not key.startswith("_"):
escaped_cmd = value.replace("%", "%%")
bat_content.append(f"set {key}={escaped_cmd}")
bat_content.extend([
"",
"echo.",
"echo APISCAN environment configured!",
"echo.",
"echo LLM Provider: %LLM_PROVIDER%",
"echo Model: %LLM_MODEL%",
"if not \"%OLLAMA_HOST%\"==\"\" echo Ollama Host: %OLLAMA_HOST%",
"echo."
])
bat_file = Path("apiscan_env.bat")
bat_file.write_text("\r\n".join(bat_content), encoding="utf-8")
scripts_created.append("apiscan_env.bat")
bash_content = [
"#!/usr/bin/env bash",
"# APISCAN Environment Setup for Bash/Zsh",
"",
'echo "Setting APISCAN environment variables..."',
"",
]
for key, value in all_config.items():
if value and not key.startswith("_"):
escaped = value.replace('"', '\\"').replace('$', '\\$').replace('`', '\\`')
bash_content.append(f'export {key}="{escaped}"')
bash_content.extend([
"",
'echo ""',
'echo " APISCAN environment configured!"',
'echo ""',
'echo "LLM Provider: $LLM_PROVIDER"',
'echo "Model: $LLM_MODEL"',
'if [ -n "$OLLAMA_HOST" ]; then',
' echo "Ollama Host: $OLLAMA_HOST"',
'fi',
'echo ""',
])
bash_file = Path("apiscan_env.sh")
bash_file.write_text("\n".join(bash_content), encoding="utf-8")
bash_file.chmod(0o755)
scripts_created.append("apiscan_env.sh")
fish_content = [
"# APISCAN Environment Setup for Fish shell",
"",
'echo "Setting APISCAN environment variables..."',
"",
]
for key, value in all_config.items():
if value and not key.startswith("_"):
escaped = value.replace('"', '\\"').replace('$', '\\$').replace('`', '\\`')
fish_content.append(f'set -gx {key} "{escaped}"')
fish_content.extend([
"",
'echo ""',
'echo " APISCAN environment configured!"',
'echo ""',
'echo "LLM Provider: $LLM_PROVIDER"',
'echo "Model: $LLM_MODEL"',
'if test -n "$OLLAMA_HOST"',
' echo "Ollama Host: $OLLAMA_HOST"',
'end',
'echo ""'
])
fish_file = Path("apiscan_env.fish")
fish_file.write_text("\n".join(fish_content), encoding="utf-8")
scripts_created.append("apiscan_env.fish")
return scripts_created
#================ show_shell_instructions: ========================
def show_shell_instructions(shell_info, scripts_created):
os_type = shell_info["os"]
shell = shell_info["shell"]
print_header(f"SHELL CONFIGURATION - {shell.upper()} ({os_type.upper()})")
instructions = []
current_command = get_current_shell_setup_command(shell_info, scripts_created)
all_commands = get_all_shell_setup_commands(scripts_created)
instructions.extend([f"{Colors.CYAN}Current shell instructions:{Colors.END}", "", f"{Colors.BOLD}NO ADMIN RIGHTS NEEDED!{Colors.END}", ""])
if current_command:
instructions.extend([
f"{Colors.BOLD}1. Load environment variables:{Colors.END}",
f" {Colors.YELLOW}{current_command}{Colors.END}",
"",
f"{Colors.BOLD}2. Test environment:{Colors.END}",
f" {Colors.YELLOW}python test_env.py{Colors.END}",
"",
])
else:
instructions.extend([
f"{Colors.BOLD}1. No shell script was generated for this choice.{Colors.END}",
f" {Colors.YELLOW}Use the .env file directly or rerun setup and choose shell scripts.{Colors.END}",
"",
])
instructions.append(f"{Colors.CYAN}Generated setup files for this run:{Colors.END}")
if "apiscan_env.ps1" in scripts_created and "powershell" in all_commands:
instructions.append(f" PowerShell: {Colors.YELLOW}{all_commands['powershell']}{Colors.END}")
if "apiscan_env.bat" in scripts_created and "cmd" in all_commands:
instructions.append(f" cmd.exe: {Colors.YELLOW}{all_commands['cmd']}{Colors.END}")
if "apiscan_env.sh" in scripts_created and "bash" in all_commands:
instructions.append(f" Bash/Zsh/Linux shell: {Colors.YELLOW}{all_commands['bash']}{Colors.END}")
if "apiscan_env.fish" in scripts_created and "fish" in all_commands:
instructions.append(f" Fish: {Colors.YELLOW}{all_commands['fish']}{Colors.END}")
if os_type == "windows" and shell == "powershell":
instructions.extend([
"",
f"{Colors.BOLD}PowerShell fallback if ExecutionPolicy blocks scripts:{Colors.END}",
f" {Colors.YELLOW}powershell -ExecutionPolicy Bypass -File apiscan_env.ps1{Colors.END}",
])
if ".env" in scripts_created:
instructions.extend([
"",
f"{Colors.CYAN}Universal .env file:{Colors.END}",
f"{Colors.GREEN} File created: .env{Colors.END}",
])
print("\n".join(instructions))
#================ create_test_env_script: ========================
def create_test_env_script():
#APISCAN Environment Variables Test Script
#Run: python test_env.py
test_file = Path("test_env.py")
test_file.write_text(TEST_SCRIPT, encoding="utf-8")
print_success("test_env.py created - use this to test your setup")
return test_file
#================ test_environment_setup: ========================
def test_environment_setup(scripts_created):
print_header("TESTING ENVIRONMENT SETUP")
print_info("Created test_env.py for manual verification.")
print_info("Note: Environment variables are NOT set in this Python process yet.")
shell_info = detect_shell()
current_command = get_current_shell_setup_command(shell_info, scripts_created)
print(f"\n{Colors.BOLD}Follow these steps:{Colors.END}")
if current_command:
if shell_info["os"] == "windows":
window_name = "PowerShell window" if shell_info["shell"] == "powershell" else "CMD window"
print(f"1. Open a NEW {window_name}")
else:
print("1. Open a NEW terminal")
print(f"2. Run: {Colors.YELLOW}{current_command}{Colors.END}")
print(f"3. Run: {Colors.YELLOW}python test_env.py{Colors.END}")
else:
print(f"1. Open the generated {Colors.YELLOW}.env{Colors.END} file")
print("2. Load those values in your shell or application")
print(f"3. Run: {Colors.YELLOW}python test_env.py{Colors.END}")
print(f"\n{Colors.YELLOW} Note:{Colors.END}")
print("You need to open a NEW terminal/shell after running the setup scripts.")
return True
#================ select_providers: ========================
def select_providers():
print_header("SELECT LLM PROVIDERS")
print("Choose which AI providers you want to configure:\n")
for i, (provider_id, provider) in enumerate(LLM_PROVIDERS.items(), 1):
print(f"[{i}] {provider['name']:25}")
print(f" {provider['description']}")
if provider_id == "ollama":
print(f" {Colors.YELLOW}Ollama Host: http://localhost:11434 (default){Colors.END}")
configured = any(os.getenv(var) for var in provider["env_vars"])
if configured:
print(f" {Colors.GREEN} Already configured{Colors.END}")
print()
exit_idx = len(LLM_PROVIDERS) + 1
print(f"[{exit_idx}] Exit Configuration")
print(f" Return to main setup")
print()
print(f"{Colors.YELLOW}Select providers (e.g. '1' for Ollama or '1,2,3' for multiple, '{exit_idx}' to exit):{Colors.END}")
selection = input("> ").strip().lower()
if selection == str(exit_idx) or selection == "exit":
print_info("Exiting provider configuration")
return []
selected_providers = []
if selection == "all":
selected_providers = list(LLM_PROVIDERS.keys())
else:
try:
indices = [int(x.strip()) for x in selection.split(',')]
provider_ids = list(LLM_PROVIDERS.keys())
for idx in indices:
if 1 <= idx <= len(provider_ids):
selected_providers.append(provider_ids[idx-1])
elif idx == exit_idx:
print_info("Exiting provider configuration")
return []
except ValueError:
print_error("Invalid selection. Please try again.")
return select_providers()
return selected_providers
#================ configure_provider: ========================
def configure_provider(provider_id):
provider = LLM_PROVIDERS[provider_id]
print(f"\n{Colors.BOLD}Configuring {provider['name']}{Colors.END}")
print(f"{provider['description']}")
config = {}
if provider_id == "ollama":
ollama_host_default = "http://localhost:11434"
print_info(f"OLLAMA_HOST will be set to: {ollama_host_default}")
config["OLLAMA_HOST"] = ollama_host_default
response = input(f"Use a different OLLAMA_HOST? (y/N): ").strip().lower()
if response == 'y':
new_host = input(f"OLLAMA_HOST [{ollama_host_default}]: ").strip()
if new_host:
config["OLLAMA_HOST"] = new_host
current_key = os.getenv("OLLAMA_API_KEY", "")
if current_key:
masked = current_key[:4] + "***" + current_key[-4:] if len(current_key) > 8 else "***"
print_info(f"OLLAMA_API_KEY is already set: {masked}")
response = input("Change? (y/N): ").strip().lower()
if response == 'y':
new_key = input("OLLAMA_API_KEY (optional): ").strip()
if new_key:
config["OLLAMA_API_KEY"] = new_key
else:
config["OLLAMA_API_KEY"] = current_key
else:
new_key = input("OLLAMA_API_KEY (optional, press Enter to skip): ").strip()
if new_key:
config["OLLAMA_API_KEY"] = new_key
else:
env_vars_to_prompt = list(provider["env_vars"])
if provider_id == "openai_compat":
env_vars_to_prompt = ["LLM_API_KEY"]
if provider_id == "azure_openai":
env_vars_to_prompt = ["AZURE_OPENAI_ENDPOINT"]
if provider_id == "openrouter":
env_vars_to_prompt = ["OPENROUTER_API_KEY"]
for env_var in env_vars_to_prompt:
current = os.getenv(env_var, "")
if current:
if any(keyword in env_var.lower() for keyword in ["key", "secret", "token"]):
masked = current[:4] + "***" + current[-4:] if len(current) > 8 else "***"
print_info(f"{env_var} is already set: {masked}")
else:
print_info(f"{env_var} is already set: {current}")
response = input(f"Change? (y/N): ").strip().lower()
if response == 'y':
new_value = input(f"{env_var}: ").strip()
if new_value:
config[env_var] = new_value
else:
config[env_var] = current
else:
new_value = input(f"{env_var}: ").strip()
if new_value:
config[env_var] = new_value
if provider_id == "openai_compat":
default_base = provider.get("base_url", "")
current_base = os.getenv("LLM_API_BASE", default_base)
base_value = input(f"LLM_API_BASE [{current_base}]: ").strip() or current_base
if base_value:
config["LLM_API_BASE"] = base_value.rstrip("/")
if provider_id == "azure_openai":
auth_choice = input("Azure auth type [api_key/entra, default api_key]: ").strip().lower() or "api_key"
if auth_choice in ("aad", "entra_id", "managed_identity", "msi", "bearer"):
auth_choice = "entra"
if auth_choice not in ("api_key", "entra"):
print_warning(f"Unknown Azure auth type '{auth_choice}', using api_key")
auth_choice = "api_key"
config["AZURE_OPENAI_AUTH_TYPE"] = auth_choice
if auth_choice == "entra":
token = input("AZURE_OPENAI_AD_TOKEN (optional; leave empty to use azure-identity / az login): ").strip()
scope = input("AZURE_OPENAI_TOKEN_SCOPE [https://cognitiveservices.azure.com/.default]: ").strip()
if token:
config["AZURE_OPENAI_AD_TOKEN"] = token
config["AZURE_OPENAI_TOKEN_SCOPE"] = scope or "https://cognitiveservices.azure.com/.default"
else:
current_key = os.getenv("AZURE_OPENAI_API_KEY", "")
if current_key:
masked = current_key[:4] + "***" + current_key[-4:] if len(current_key) > 8 else "***"
print_info(f"AZURE_OPENAI_API_KEY is already set: {masked}")
if input("Change? (y/N): ").strip().lower() == "y":
new_key = input("AZURE_OPENAI_API_KEY: ").strip()
if new_key:
config["AZURE_OPENAI_API_KEY"] = new_key
else:
config["AZURE_OPENAI_API_KEY"] = current_key
else:
new_key = input("AZURE_OPENAI_API_KEY: ").strip()
if new_key:
config["AZURE_OPENAI_API_KEY"] = new_key
if "AZURE_OPENAI_API_VERSION" not in config:
config["AZURE_OPENAI_API_VERSION"] = os.getenv("AZURE_OPENAI_API_VERSION", "2024-08-01-preview")
print_warning("For Azure OpenAI, choose or type your deployment name as LLM_MODEL.")
if provider_id == "openrouter":
referer = input("OPENROUTER_HTTP_REFERER (optional): ").strip()
title = input("OPENROUTER_X_TITLE (optional, default APISCAN): ").strip()
if referer:
config["OPENROUTER_HTTP_REFERER"] = referer
if title:
config["OPENROUTER_X_TITLE"] = title
else:
config["OPENROUTER_X_TITLE"] = "APISCAN"
if provider["models"]:
print(f"\nAvailable models for {provider['name']}:")
for i, model in enumerate(provider["models"][:10], 1):
print(f" [{i}] {model}")
if len(provider["models"]) > 10:
print(f" ... and {len(provider['models']) - 10} more")
model_choice = input(f"\nChoose model [default: {provider['models'][0]}]: ").strip()
if model_choice.isdigit() and 1 <= int(model_choice) <= len(provider["models"]):
selected_model = provider["models"][int(model_choice) - 1]
elif model_choice:
matching_models = [m for m in provider["models"] if model_choice.lower() in m.lower()]
if matching_models:
selected_model = matching_models[0]
print_info(f"Selected: {selected_model}")
else:
selected_model = model_choice
print_warning(f"Model '{model_choice}' not in list, using custom name")
else:
selected_model = provider["models"][0]
config["LLM_MODEL"] = selected_model
config["LLM_PROVIDER"] = provider_id
config["LLM_TEMPERATURE"] = "0.0"
config["LLM_TOP_P"] = "0.95"
config["LLM_MAX_TOKENS"] = "4096"
if provider_id == "ollama":
print_success(f"Ollama configured!")
print_info(f" Host: {config.get('OLLAMA_HOST', 'http://localhost:11434')}")
print_info(f" Model: {config.get('LLM_MODEL', 'llama3:latest')}")
print_info(f" Ensure Ollama is running: ollama serve")
return config
#================ create_llm_config_file_shell_aware: ========================
def create_llm_config_file_shell_aware(providers_config):
print_header("CREATING LLM CONFIGURATION")
shell_info = detect_shell()
print_info(f"Detected: {shell_info['os'].upper()} - {shell_info['shell'].upper()} - {shell_info['terminal']}")
all_config = {}
for provider_id, config in providers_config.items():
all_config.update(config)
if "ollama" in providers_config and "OLLAMA_HOST" not in all_config:
all_config["OLLAMA_HOST"] = "http://localhost:11434"
print_success(f"OLLAMA_HOST automatically set to: {all_config['OLLAMA_HOST']}")
default_settings = {
"LLM_TEMPERATURE": "0.0",
"LLM_TOP_P": "0.95",
"LLM_MAX_TOKENS": "4096",
"LLM_CONNECT_TIMEOUT": "10",
"LLM_READ_TIMEOUT": "120",
"LLM_VERIFY_SSL": "true",
"LLM_MAX_RETRIES": "2"
}
for key, value in default_settings.items():
if key not in all_config:
all_config[key] = value
print("\nChoose configuration method:")
print(" [1] .env file + Shell scripts (recommended)")
print(" [2] .env file only")
print(" [3] Shell scripts only")
print(" [4] Everything (.env + scripts + json)")
choice = input("Choose (1-4): ").strip() or "1"
if choice == "4":
print_warning("Option 4 stores the full configuration, including API keys, in plain-text files.")
created_files = []
if choice in ["1", "2", "4"]:
env_file = Path(".env")
env_file.write_text(build_env_content(all_config, include_header=True, shell_info=shell_info), encoding="utf-8")
created_files.append(".env")
print_success(f".env file created")
if choice in ["1", "3", "4"]:
scripts = create_shell_specific_files(all_config)
created_files.extend(scripts)
for script in scripts:
print_success(f"{script} created")
if choice == "4":
config_data = {
"version": "2.1",
"shell_info": shell_info,
"providers": providers_config,
"config": all_config,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
}
json_path = Path("llm_config.json")
json_path.write_text(json.dumps(config_data, indent=2, ensure_ascii=False), encoding="utf-8")
created_files.append("llm_config.json")
print_success("llm_config.json created")
if created_files:
show_shell_instructions(shell_info, created_files)
return created_files
#================ show_quick_setup_guide: ========================
def show_quick_setup_guide(shell_info, scripts_created):
print_header("QUICK SETUP GUIDE")
guide = []
current_command = get_current_shell_setup_command(shell_info, scripts_created)
all_commands = get_all_shell_setup_commands(scripts_created)
if shell_info["shell"] == "powershell":
guide_title = "PowerShell Quick Setup"
elif shell_info["os"] == "windows":
guide_title = "CMD Quick Setup"
elif shell_info["shell"] == "fish":
guide_title = "Fish Shell Quick Setup"
elif shell_info["shell"] == "zsh":
guide_title = "Zsh Quick Setup"
else:
guide_title = "Bash Quick Setup"
guide.extend([f"{Colors.BOLD}{guide_title}:{Colors.END}", ""])
if current_command:
guide.extend([
"1. First: Set environment variables",
f" {Colors.YELLOW}{current_command}{Colors.END}",
"",
"2. Then: Test your setup",
f" {Colors.YELLOW}python test_env.py{Colors.END}",
"",
"3. Finally: Run APISCAN with AI",
f" {Colors.YELLOW}python apiscan.py --api11 --target https://api.example.com{Colors.END}"
])
else:
guide.extend([
"1. A .env file was generated without shell scripts",
f" {Colors.YELLOW}Load .env with your preferred tool or rerun setup and choose shell scripts{Colors.END}",
"",
"2. Then: Test your setup",
f" {Colors.YELLOW}python test_env.py{Colors.END}",
"",
"3. Finally: Run APISCAN with AI",
f" {Colors.YELLOW}python apiscan.py --api11 --target https://api.example.com{Colors.END}"
])
if shell_info["os"] == "windows" and shell_info["shell"] == "powershell":
guide.extend([
"",
"If ExecutionPolicy error:",
f" {Colors.YELLOW}Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser{Colors.END}",
f" {Colors.YELLOW}Or: powershell -ExecutionPolicy Bypass -File apiscan_env.ps1{Colors.END}",
])
if all_commands:
guide.append("")
guide.append(f"{Colors.CYAN}Alternative shell setup files:{Colors.END}")
if "powershell" in all_commands:
guide.append(f" PowerShell: {Colors.YELLOW}{all_commands['powershell']}{Colors.END}")
if "cmd" in all_commands:
guide.append(f" cmd.exe: {Colors.YELLOW}{all_commands['cmd']}{Colors.END}")
if "bash" in all_commands:
guide.append(f" Bash/Zsh/Linux shell: {Colors.YELLOW}{all_commands['bash']}{Colors.END}")
if "fish" in all_commands:
guide.append(f" Fish: {Colors.YELLOW}{all_commands['fish']}{Colors.END}")
guide.extend([
"",
f"{Colors.CYAN}For Ollama users:{Colors.END}",
"1. Download and install Ollama: https://ollama.com",
"2. Start Ollama service:",
" Windows: Open Ollama app",
" Linux/macOS: ollama serve",
"3. Pull a model: ollama pull llama3",
"4. Test Ollama: curl http://localhost:11434/api/tags"
])
print("\n".join(guide))
#================ main: ========================
def main():
print(f"{Colors.BOLD}{Colors.MAGENTA}")
print("")
print(" APISCAN LLM/AI SETUP v2.1 ")
print(" Multi-Shell & Ollama Support ")
print("")
print(f"{Colors.END}")
print(f"{Colors.CYAN}Features:{Colors.END}")
print(" Multi-shell support (CMD, PowerShell, Bash, Zsh, Fish)")
print(" Automatic shell detection")
print(f" {Colors.GREEN}NO ADMIN RIGHTS NEEDED{Colors.END}")
print(f" {Colors.YELLOW}Automatic OLLAMA_HOST configuration{Colors.END}")
print(" .env + shell scripts generation")
print(f" {Colors.CYAN}Permanent test_env.py script{Colors.END}")
shell_info = detect_shell()
print_info(f"Detected: {shell_info['os'].upper()} - {shell_info['shell'].upper()}")
selected_providers = select_providers()
if not selected_providers:
print_warning("No providers selected. Setup cancelled.")
return
providers_config = {}
for provider_id in selected_providers:
config = configure_provider(provider_id)
if config:
providers_config[provider_id] = config
if providers_config:
created_files = create_llm_config_file_shell_aware(providers_config)
else:
print_warning("No providers configured")
return
create_test_env_script()
print("\n" + "="*60)
response = input("Show test instructions? (Y/n): ").strip().lower()
if response != 'n':
test_environment_setup(created_files)
show_quick_setup_guide(shell_info, created_files)
print(f"\n{Colors.GREEN}{Colors.BOLD} LLM/AI Setup completed!{Colors.END}")
print(f"{Colors.CYAN}Next steps:{Colors.END}")
next_command = get_current_shell_setup_command(shell_info, created_files)
if next_command: