-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (113 loc) · 4.36 KB
/
main.py
File metadata and controls
140 lines (113 loc) · 4.36 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
#!/usr/bin/env -S uv run --script
import argparse
from argparse import RawTextHelpFormatter
import time
from langchain_core.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate)
from rich.console import Console
from rich.live import Live
from rich.markdown import Markdown
from rich.status import Status
from rich.text import Text
from model import llm
# Argparse
parser = argparse.ArgumentParser(
description="Manual AI Agent Helper for Unix",
epilog="https://github.com/uberkael/AgentManu\n© Kael <uberkael@gmail.com>",
formatter_class=RawTextHelpFormatter,
prog="AgentManu")
parser.add_argument(
'-l', "-lang", "-i", "--idiom", "--language",
dest='lang',
help='Output Language',
default='spanish')
parser.add_argument('-v', '--version',
action='version',
version="""%(prog)s 1.0\n
https://github.com/uberkael/AgentManu
© Kael <uberkael@gmail.com>""",
help='Show program version and exit')
parser.add_argument(
'cmd_list',
nargs=argparse.REMAINDER,
help='Command to explain')
args = parser.parse_args()
# Early exit if no command is provided
if len(args.cmd_list) < 1:
parser.print_help()
exit(0)
lang = args.lang.capitalize()
cmd_args = len(args.cmd_list) > 1
cmd = ' '.join(args.cmd_list) if cmd_args else args.cmd_list[0]
console = Console()
system_prompt_single = SystemMessagePromptTemplate.from_template(
"""You are an AI assistant that explains Unix commands in {lang}.
Your responses must be accurate, minimal, and clear with very rich Markdown.
Follow the style of TLDR pages, tealdeer or similar tools:
- Describe the general purpose of the command (2-3 sentences max).
- Show minimal usage or syntax of different uses (2-6 sentences max).
Do not include '$', '#', or any other symbols before the command or output.
Avoid extra commentary, greetings, or formatting outside the Markdown.""",
input_variables=["lang"]
)
system_prompt_args = SystemMessagePromptTemplate.from_template(
"""You are an AI assistant that explains Unix commands in {lang}.
Your responses must be accurate, minimal, and clear with very rich Markdown.
If the command has no arguments, follow the style of TLDR pages:
- Describe the general purpose of the command (2-3 sentences max).
- Show minimal usage or syntax of different uses.
In case of commands with arguments, follow this style:
Respond with a **very short** paragraph (2-3 sentences max), formatted in
Markdown (very rich), that explains what the command does and when to use it.
If the command includes multiple parts or arguments, briefly explain each part.
If helpful, include **a single compact code block example**, also in Markdown.
The code block should include both the command and its expected output,
all within **1-2 lines** and **no more than 80 characters wide**.
Ensure the command and output appear **together in the same code block**,
and that the Markdown syntax is complete and properly closed.
Do not include '$', '#', or any other symbols before the command or output.
Avoid extra commentary, greetings, or formatting outside the Markdown.""",
input_variables=["lang"]
)
system_prompt = system_prompt_args if cmd_args else system_prompt_single
user_prompt = HumanMessagePromptTemplate.from_template(
"""Follow the previous instructions strictly.
Explain the following Unix command: `{cmd}`.
Do not add anything else beyond the explanation and example.""",
input_variables=["cmd"])
chat_prompt = ChatPromptTemplate.from_messages(
[
system_prompt,
user_prompt
]
)
chain = (
{ # type: ignore
"cmd": lambda x: x["cmd"],
"lang": lambda x: x["lang"]
}
| chat_prompt
| llm
)
tokens = []
content = ""
with Status("", spinner="bouncingBar", spinner_style="green") as status:
# Wait for the first token (do not update Live yet)
first_token = next(chain.stream({"cmd": cmd, "lang": lang}))
tokens.append(first_token)
content += f"{first_token.content}█" # type: ignore
# Close the spinner and continue with Live for the rest of the stream
with Live(Text(content), console=console, transient=True) as live:
for token in chain.stream({"cmd": cmd, "lang": lang}):
tokens.append(token)
content = content.rstrip('█')
content += f"{token.content}█" # type: ignore
live.update(Text(content))
content = content.rstrip('█')
live.update(Text(content))
time.sleep(0.4) # Pause at the end
# Show final result in Markdown
content_markdown = Markdown(content)
console.print(content_markdown)