Skip to content

Commit

Permalink
fix style error
Browse files Browse the repository at this point in the history
  • Loading branch information
yangbobo2021 committed Nov 28, 2023
1 parent cccf633 commit a41ccfc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 53 deletions.
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'
]
37 changes: 19 additions & 18 deletions devchat/engine/command_runner.py
Original file line number Diff line number Diff line change
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, "")
65 changes: 33 additions & 32 deletions devchat/engine/router.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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
Expand Down Expand Up @@ -95,29 +95,30 @@ 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:
chunk = chunk.dict()
delta = chunk["choices"][0]["delta"]
if 'tool_calls' in delta and delta['tool_calls']:
tool_call = delta['tool_calls'][0]['function']
if tool_call.get('name', None):
response_result["function_name"] = tool_call["name"]
if tool_call.get("arguments", None):
response_result["parameters"] += tool_call["arguments"]
if delta.get('content', None):
if response_result["content"]:
response_result["content"] += delta["content"]
else:
response_result["content"] = delta["content"]
print(delta["content"], end='', flush=True)
if isinstance(response, Iterable):
for chunk in response:
chunk = chunk.dict()
delta = chunk["choices"][0]["delta"]
if 'tool_calls' in delta and delta['tool_calls']:
tool_call = delta['tool_calls'][0]['function']
if tool_call.get('name', None):
response_result["function_name"] = tool_call["name"]
if tool_call.get("arguments", None):
response_result["parameters"] += tool_call["arguments"]
if delta.get('content', None):
if response_result["content"]:
response_result["content"] += delta["content"]
else:
response_result["content"] = delta["content"]
print(delta["content"], end='', flush=True)
if response_result["function_name"]:
print("``` command_run")
function_call = {
Expand All @@ -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)

0 comments on commit a41ccfc

Please sign in to comment.