Create assistant.py
Browse files- assistant.py +20 -0
assistant.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from command_classifier import classify_command
|
| 2 |
+
from executor import execute_command
|
| 3 |
+
|
| 4 |
+
def get_assistant_response(pipe, command: str, execute: bool) -> str:
|
| 5 |
+
command_type = classify_command(command)
|
| 6 |
+
if execute:
|
| 7 |
+
result = execute_command(command_type, command)
|
| 8 |
+
if result:
|
| 9 |
+
return result
|
| 10 |
+
|
| 11 |
+
prompt = f"<|user|>\n{command}\n<|assistant|>\n"
|
| 12 |
+
try:
|
| 13 |
+
output = pipe(prompt)[0]["generated_text"]
|
| 14 |
+
if "<|assistant|>" in output:
|
| 15 |
+
response = output.split("<|assistant|>")[-1].strip()
|
| 16 |
+
else:
|
| 17 |
+
response = output.strip()
|
| 18 |
+
return f"🤖 ({command_type}) {response}"
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"⚠️ Error: {e}"
|