From a41ccfcf552937c71ca03e7a558d3f1c0a305ec1 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Tue, 28 Nov 2023 17:20:14 +0800 Subject: [PATCH] fix style error --- devchat/engine/__init__.py | 4 +- devchat/engine/command_runner.py | 37 +++++++++--------- devchat/engine/router.py | 65 ++++++++++++++++---------------- 3 files changed, 53 insertions(+), 53 deletions(-) diff --git a/devchat/engine/__init__.py b/devchat/engine/__init__.py index 3a553f26..5fb7a41f 100644 --- a/devchat/engine/__init__.py +++ b/devchat/engine/__init__.py @@ -2,7 +2,6 @@ from .namespace import Namespace from .recursive_prompter import RecursivePrompter from .router import run_command -from .command_runner import CommandRunner __all__ = [ 'parse_command', @@ -10,6 +9,5 @@ 'CommandParser', 'Namespace', 'RecursivePrompter', - 'run_command', - 'CommandRunner' + 'run_command' ] diff --git a/devchat/engine/command_runner.py b/devchat/engine/command_runner.py index 22f2c081..257cdf88 100644 --- a/devchat/engine/command_runner.py +++ b/devchat/engine/command_runner.py @@ -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, "") diff --git a/devchat/engine/router.py b/devchat/engine/router.py index eb59ba78..a0b04864 100644 --- a/devchat/engine/router.py +++ b/devchat/engine/router.py @@ -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 @@ -95,7 +95,7 @@ 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, @@ -103,21 +103,22 @@ def _call_gpt(messages: List[dict], # messages passed to GPT ) 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 = { @@ -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(): @@ -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, "") @@ -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)