-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrs-cli.py
94 lines (70 loc) · 2.68 KB
/
trs-cli.py
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
import os
import sys
import openai
import argparse
import tiktoken
from loguru import logger
from typing import Tuple
from rich.console import Console
from rich.markdown import Markdown
from colored import Fore, Back, Style
from trs.main import TRS
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='trs-cli',
description='Chat with and summarize CTI reports'
)
parser.add_argument(
'-c', '--chat',
required=True,
action='store_true',
help='Enter chat mode'
)
args = parser.parse_args()
OPENAI_KEY = os.environ.get('OPENAI_API_KEY')
if OPENAI_KEY is None:
logger.error('OPENAI_API_KEY environment variable not set')
sys.exit(1)
trs = TRS(openai_key=OPENAI_KEY)
COMMAND_HANDLERS = {
'!summ': trs.summarize,
'!detect': trs.detections,
'!custom': trs.custom
}
if args.chat:
console = Console()
print(f'{Style.BOLD}{Fore.cyan_3}commands:{Style.reset}')
print(f'* {Fore.cyan_3}!summ <url>{Style.reset} - summarize a threat report')
print(f'* {Fore.cyan_3}!detect <url>{Style.reset} - identify detections in report')
print(f'* {Fore.cyan_3}!custom <prompt_name> <url>{Style.reset} - process URL with a custom prompt')
print(f'* {Fore.cyan_3}!exit|!quit{Style.reset} - exit application')
print(f'{Style.BOLD}{Fore.dark_orange_3b}ready to chat!{Style.reset}\n')
try:
while True:
prompt = input('💀 >> ').strip()
if prompt.lower() in ['!exit', '!quit', '!q', '!x']:
logger.info('exiting')
break
command, *args = prompt.split()
handler = COMMAND_HANDLERS.get(command.lower())
if handler:
result = handler(*args)
if command.lower() == '!summ':
summary, mindmap, iocs = result
print('🤖 >>')
console.print(Markdown(summary))
console.print(Markdown(mindmap))
print(iocs)
else:
print('🤖 >>')
console.print(Markdown(result))
else:
result = trs.qna(prompt=prompt)
print('🤖 >>')
console.print(Markdown(result))
except KeyboardInterrupt:
logger.info('caught keyboard interrupt, exiting')
sys.exit(0)
except Exception as err:
logger.error(f'error: {err}')
sys.exit(1)