-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
141 lines (125 loc) · 5.3 KB
/
__init__.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
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
import sys, os
if not os.environ.get("SHOW_VOID_TERMINAL_LOGS", None):
from loguru import logger
logger.disable("void_terminal")
from void_terminal.toolbox import get_conf
from void_terminal.toolbox import set_conf
from void_terminal.toolbox import set_multi_conf
from void_terminal.toolbox import get_plugin_handle
from void_terminal.toolbox import get_plugin_default_kwargs
from void_terminal.toolbox import get_chat_handle
from void_terminal.toolbox import get_chat_default_kwargs
from functools import wraps
def chat_to_markdown_str(chat):
result = ""
for i, cc in enumerate(chat):
result += f'\n\n{cc[0]}\n\n{cc[1]}'
if i != len(chat)-1:
result += '\n\n---'
return result
def silence_stdout(func):
@wraps(func)
def wrapper(*args, **kwargs):
_original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
for q in func(*args, **kwargs):
sys.stdout = _original_stdout
yield q
sys.stdout = open(os.devnull, 'w')
sys.stdout.close()
sys.stdout = _original_stdout
return wrapper
def silence_stdout_fn(func):
@wraps(func)
def wrapper(*args, **kwargs):
_original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
result = func(*args, **kwargs)
sys.stdout.close()
sys.stdout = _original_stdout
return result
return wrapper
class VoidTerminal():
def __init__(self) -> None:
pass
vt = VoidTerminal()
# get_conf = silence_stdout_fn(get_conf)
# set_conf = silence_stdout_fn(set_conf)
# set_multi_conf = silence_stdout_fn(set_multi_conf)
# get_plugin_handle = silence_stdout_fn(get_plugin_handle)
# get_plugin_default_kwargs = silence_stdout_fn(get_plugin_default_kwargs)
# get_chat_handle = silence_stdout_fn(get_chat_handle)
# get_chat_default_kwargs = silence_stdout_fn(get_chat_default_kwargs)
# chat_to_markdown_str = chat_to_markdown_str
vt.get_conf = get_conf
vt.set_conf = set_conf
vt.set_multi_conf = set_multi_conf
vt.get_plugin_handle = get_plugin_handle
vt.get_plugin_default_kwargs = get_plugin_default_kwargs
vt.get_chat_handle = get_chat_handle
vt.get_chat_default_kwargs = get_chat_default_kwargs
vt.chat_to_markdown_str = chat_to_markdown_str
def chat_to_markdown_str(chat):
result = ""
for i, cc in enumerate(chat):
result += f'\n\n{cc[0]}\n\n{cc[1]}'
if i != len(chat)-1:
result += '\n\n---'
return result
def add_env_variable(variable:str, value:str):
bashrc_path = os.path.expanduser("~/.bashrc")
with open(bashrc_path, "r") as bashrc_file:
env_lines = bashrc_file.readlines()
found_conflict = False
for i, line in enumerate(env_lines):
if line.startswith(f"export {variable}="):
old_value = line.split("=")[1].strip()
print(f"Conflict detected with {variable}={old_value}. Resolve conflict.")
env_lines[i] = f"export {variable}={value} # Void-Terminal\n"
found_conflict = True
if not env_lines[i].endswith('\n'): env_lines[i] += '\n'
# If the variable is not found, append it to .bashrc
if not found_conflict:
env_lines.append(f"export {variable}={value} # Void-Terminal\n")
with open(bashrc_path, "w") as bashrc_file:
bashrc_file.writelines(env_lines)
os.environ[variable] = value
def plugin_shortcut(main_input, plugin, advanced_arg=None):
from rich.live import Live
from rich.markdown import Markdown
plugin = vt.get_plugin_handle(plugin)
plugin_kwargs = vt.get_plugin_default_kwargs()
plugin_kwargs['main_input'] = main_input
if advanced_arg is not None:
plugin_kwargs['plugin_kwargs'] = advanced_arg
my_working_plugin = plugin(**plugin_kwargs)
with Live(Markdown(""), auto_refresh=False, vertical_overflow="visible") as live:
for cookies, chat, hist, msg in my_working_plugin:
md_str = vt.chat_to_markdown_str(chat)
md = Markdown(md_str)
live.update(md, refresh=True)
def cli():
import argparse
# Create ArgumentParser object
parser = argparse.ArgumentParser()
# Add an argument named 'c' with a short option '-c'.
# This argument takes one value.
parser.add_argument('input', nargs='+', help='The input string')
parser.add_argument('-c', '--cmd', action='store_true', help="Call the commandline helper plugin.")
parser.add_argument('-a', '--ask', action='store_true', help="A shortcut to ask currently selected llm.")
parser.add_argument('-s', '--set_conf', action='store_true', help="Set permanent configuration in .bashrc")
args = parser.parse_args()
if args.set_conf:
assert len(args.input) == 2, '参数数量错误,示例 ` vt --set_conf API_KEY "sk-abcdefghijklmn" `'
add_env_variable(args.input[0], args.input[1])
if args.ask:
inputs = " ".join(args.input)
LLM_MODEL, = vt.get_conf('LLM_MODEL')
plugin_shortcut(inputs, plugin='void_terminal.crazy_functions.询问多个大语言模型->同时问询_指定模型', advanced_arg={"advanced_arg": LLM_MODEL})
elif args.cmd:
# use the commandline helper shortcut
inputs = " ".join(args.input)
plugin_shortcut(inputs, plugin='void_terminal.crazy_functions.命令行助手->命令行助手')
else:
# echo, do nothing
print(args.input)