Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support workflow command #148

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix style error
  • Loading branch information
yangbobo2021 committed Nov 28, 2023
commit 983a5eb0abe4d7dda26601a9752efe29607f1cfb
4 changes: 1 addition & 3 deletions devchat/engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
from .namespace import Namespace
from .recursive_prompter import RecursivePrompter
from .router import run_command
from .command_runner import CommandRunner

__all__ = [
'parse_command',
'Command',
'CommandParser',
'Namespace',
'RecursivePrompter',
'run_command',
'CommandRunner'
'run_command'
]
39 changes: 20 additions & 19 deletions devchat/engine/command_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import openai

from devchat.utils import get_logger
from . import Command
from .command_parser import Command


logger = get_logger(__name__)
Expand Down Expand Up @@ -158,24 +158,25 @@ def run_command_with_parameters(self,
del env['PYTHONPATH']
# result = subprocess.run(command_run, shell=True, env=env)
# return result
process = subprocess.Popen(
shlex.split(command_run),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True
)

# 实时读取输出并打印
stdout = ''
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
stdout += output
print(output, end='\n')
rc = process.poll()
return (rc, stdout)
with subprocess.Popen(
shlex.split(command_run),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=env,
text=True
) as process:
stdout = ''
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
stdout += output
print(output, end='\n')
exit_code = process.poll()
return (exit_code, stdout)
return (-1, "")
except Exception as err:
print("Exception:", type(err), err, file=sys.stderr, flush=True)
return (-1, "")
39 changes: 20 additions & 19 deletions devchat/engine/router.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import json
from typing import List
from typing import List, Iterable
import openai
from devchat._cli.utils import init_dir
from . import Namespace, CommandParser, Command
from .namespace import Namespace
from .command_parser import CommandParser, Command
from .command_runner import CommandRunner


Expand Down Expand Up @@ -95,15 +96,15 @@ def _call_gpt(messages: List[dict], # messages passed to GPT

for try_times in range(3):
try:
response = client.chat.completions.create(
response: Iterable = client.chat.completions.create(
messages=messages,
model=model_name,
stream=True,
tools=tools
)

response_result = {'content': None, 'function_name': None, 'parameters': ""}
for chunk in response:
for chunk in response: # pylint: disable=E1133
chunk = chunk.dict()
delta = chunk["choices"][0]["delta"]
if 'tool_calls' in delta and delta['tool_calls']:
Expand Down Expand Up @@ -135,6 +136,7 @@ def _call_gpt(messages: List[dict], # messages passed to GPT
except Exception as err:
print("Exception Error:", err)
return {'content': None, 'function_name': None, 'parameters': ""}
return {'content': None, 'function_name': None, 'parameters': ""}


def _create_messages():
Expand Down Expand Up @@ -193,7 +195,7 @@ def _auto_route(history_messages, model_name:str):
response['function_name'],
response['parameters'],
model_name)
elif not response['content']:
if not response['content']:
return (-1, "")
return (-1, "")

Expand All @@ -218,19 +220,18 @@ def run_command(
# response = _auto_function_calling(history_messages, model_name)
# return response['content']
return _auto_route(history_messages, model_name)
else:
commands = input_text.split()
command = commands[0][1:]
commands = input_text.split()
command = commands[0][1:]

command_obj = _load_command(command)
if not command_obj or not command_obj.steps:
return None
command_obj = _load_command(command)
if not command_obj or not command_obj.steps:
return None

runner = CommandRunner(model_name)
return runner.run_command(
command,
command_obj,
history_messages,
input_text,
parent_hash,
context_contents)
runner = CommandRunner(model_name)
return runner.run_command(
command,
command_obj,
history_messages,
input_text,
parent_hash,
context_contents)