Skip to content

Commit a41ccfc

Browse files
committed
fix style error
1 parent cccf633 commit a41ccfc

3 files changed

Lines changed: 53 additions & 53 deletions

File tree

devchat/engine/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
from .namespace import Namespace
33
from .recursive_prompter import RecursivePrompter
44
from .router import run_command
5-
from .command_runner import CommandRunner
65

76
__all__ = [
87
'parse_command',
98
'Command',
109
'CommandParser',
1110
'Namespace',
1211
'RecursivePrompter',
13-
'run_command',
14-
'CommandRunner'
12+
'run_command'
1513
]

devchat/engine/command_runner.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,25 @@ def run_command_with_parameters(self,
158158
del env['PYTHONPATH']
159159
# result = subprocess.run(command_run, shell=True, env=env)
160160
# return result
161-
process = subprocess.Popen(
162-
shlex.split(command_run),
163-
stdout=subprocess.PIPE,
164-
stderr=subprocess.STDOUT,
165-
text=True
166-
)
167-
168-
# 实时读取输出并打印
169-
stdout = ''
170-
while True:
171-
output = process.stdout.readline()
172-
if output == '' and process.poll() is not None:
173-
break
174-
if output:
175-
stdout += output
176-
print(output, end='\n')
177-
rc = process.poll()
178-
return (rc, stdout)
161+
with subprocess.Popen(
162+
shlex.split(command_run),
163+
stdin=subprocess.PIPE,
164+
stdout=subprocess.PIPE,
165+
stderr=subprocess.STDOUT,
166+
env=env,
167+
text=True
168+
) as process:
169+
stdout = ''
170+
while True:
171+
output = process.stdout.readline()
172+
if output == '' and process.poll() is not None:
173+
break
174+
if output:
175+
stdout += output
176+
print(output, end='\n')
177+
exit_code = process.poll()
178+
return (exit_code, stdout)
179+
return (-1, "")
179180
except Exception as err:
180181
print("Exception:", type(err), err, file=sys.stderr, flush=True)
181182
return (-1, "")

devchat/engine/router.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import json
3-
from typing import List
3+
from typing import List, Iterable
44
import openai
55
from devchat._cli.utils import init_dir
66
from . import Namespace, CommandParser, Command
@@ -95,29 +95,30 @@ def _call_gpt(messages: List[dict], # messages passed to GPT
9595

9696
for try_times in range(3):
9797
try:
98-
response = client.chat.completions.create(
98+
response: Iterable = client.chat.completions.create(
9999
messages=messages,
100100
model=model_name,
101101
stream=True,
102102
tools=tools
103103
)
104104

105105
response_result = {'content': None, 'function_name': None, 'parameters': ""}
106-
for chunk in response:
107-
chunk = chunk.dict()
108-
delta = chunk["choices"][0]["delta"]
109-
if 'tool_calls' in delta and delta['tool_calls']:
110-
tool_call = delta['tool_calls'][0]['function']
111-
if tool_call.get('name', None):
112-
response_result["function_name"] = tool_call["name"]
113-
if tool_call.get("arguments", None):
114-
response_result["parameters"] += tool_call["arguments"]
115-
if delta.get('content', None):
116-
if response_result["content"]:
117-
response_result["content"] += delta["content"]
118-
else:
119-
response_result["content"] = delta["content"]
120-
print(delta["content"], end='', flush=True)
106+
if isinstance(response, Iterable):
107+
for chunk in response:
108+
chunk = chunk.dict()
109+
delta = chunk["choices"][0]["delta"]
110+
if 'tool_calls' in delta and delta['tool_calls']:
111+
tool_call = delta['tool_calls'][0]['function']
112+
if tool_call.get('name', None):
113+
response_result["function_name"] = tool_call["name"]
114+
if tool_call.get("arguments", None):
115+
response_result["parameters"] += tool_call["arguments"]
116+
if delta.get('content', None):
117+
if response_result["content"]:
118+
response_result["content"] += delta["content"]
119+
else:
120+
response_result["content"] = delta["content"]
121+
print(delta["content"], end='', flush=True)
121122
if response_result["function_name"]:
122123
print("``` command_run")
123124
function_call = {
@@ -135,6 +136,7 @@ def _call_gpt(messages: List[dict], # messages passed to GPT
135136
except Exception as err:
136137
print("Exception Error:", err)
137138
return {'content': None, 'function_name': None, 'parameters': ""}
139+
return {'content': None, 'function_name': None, 'parameters': ""}
138140

139141

140142
def _create_messages():
@@ -193,7 +195,7 @@ def _auto_route(history_messages, model_name:str):
193195
response['function_name'],
194196
response['parameters'],
195197
model_name)
196-
elif not response['content']:
198+
if not response['content']:
197199
return (-1, "")
198200
return (-1, "")
199201

@@ -218,19 +220,18 @@ def run_command(
218220
# response = _auto_function_calling(history_messages, model_name)
219221
# return response['content']
220222
return _auto_route(history_messages, model_name)
221-
else:
222-
commands = input_text.split()
223-
command = commands[0][1:]
223+
commands = input_text.split()
224+
command = commands[0][1:]
224225

225-
command_obj = _load_command(command)
226-
if not command_obj or not command_obj.steps:
227-
return None
226+
command_obj = _load_command(command)
227+
if not command_obj or not command_obj.steps:
228+
return None
228229

229-
runner = CommandRunner(model_name)
230-
return runner.run_command(
231-
command,
232-
command_obj,
233-
history_messages,
234-
input_text,
235-
parent_hash,
236-
context_contents)
230+
runner = CommandRunner(model_name)
231+
return runner.run_command(
232+
command,
233+
command_obj,
234+
history_messages,
235+
input_text,
236+
parent_hash,
237+
context_contents)

0 commit comments

Comments
 (0)