64 lines
1.7 KiB
Python
Raw Normal View History

2023-11-29 14:07:47 +08:00
import os
2024-02-08 13:19:24 +08:00
from typing import List
2023-11-29 14:07:47 +08:00
from .command_runner import CommandRunner
2024-02-08 13:19:24 +08:00
from .namespace import Namespace
from .recursive_prompter import RecursivePrompter
from .util import CommandUtil
2023-11-29 14:07:47 +08:00
2024-02-08 13:19:24 +08:00
def load_workflow_instruction(user_input: str):
user_input = user_input.strip()
if len(user_input) == 0:
2023-11-29 14:07:47 +08:00
return None
if user_input[:1] != "/":
2023-11-29 14:07:47 +08:00
return None
workflows_dir = os.path.join(os.path.expanduser("~/.chat"), "workflows")
2023-11-30 08:42:37 +08:00
if not os.path.exists(workflows_dir):
return None
if not os.path.isdir(workflows_dir):
return None
namespace = Namespace(workflows_dir)
prompter = RecursivePrompter(namespace)
2024-02-08 13:19:24 +08:00
command_name = user_input.split()[0][1:]
command_prompts = prompter.run(command_name)
2023-11-30 08:42:37 +08:00
2024-02-26 13:57:37 +08:00
return [command_prompts]
2023-11-30 08:42:37 +08:00
2023-11-29 14:07:47 +08:00
def run_command(
model_name: str, history_messages: List[dict], input_text: str, parent_hash: str, auto_fun: bool
):
2023-11-29 14:07:47 +08:00
"""
load command config, and then run Command
"""
# split input_text by ' ','\n','\t'
if len(input_text.strip()) == 0:
return None
if input_text.strip()[:1] != "/":
if not (auto_fun and model_name.startswith("gpt-")):
2023-11-29 14:07:47 +08:00
return None
2024-02-08 13:19:24 +08:00
# TODO
# use auto select workflow to run command
return None
2023-11-29 14:07:47 +08:00
commands = input_text.split()
command = commands[0][1:]
2024-02-08 13:19:24 +08:00
command_obj = CommandUtil.load_command(command)
if not command_obj or not command_obj.steps:
2023-11-29 14:07:47 +08:00
return None
runner = CommandRunner(model_name)
return runner.run_command(
2024-02-08 13:19:24 +08:00
command_name=command,
command=command_obj,
history_messages=history_messages,
input_text=input_text,
parent_hash=parent_hash,
2024-02-08 13:19:24 +08:00
)