XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

fix: Correct conversation loading and improve file handling

- Modify `ConversationManager.load` to fix conversation loading when `self.provider` is not set. - Update `ConversationManager.load` to correctly handle `self.provider` and `self.data`. - Change description in `get_parser` from "GPT CLI client with conversation history" to "G4F CLI client with conversation history". - Improve error handling in `run_client_args` for file reading. - Add `UnicodeDecodeError` handling when reading input files as text in `run_client_args`. - Simplify and correct the construction of `input_text` in `run_client_args`.

8b2d1afd
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +18 -12
Modified g4f/cli/client.py +18 -12
@@ -55,8 +55,10 @@ class ConversationManager:
55 55 if not self.provider:
56 56 self.provider = None
57 57 self.data = data.get("data", {})
58 if self.provider and data.get(self.provider):
59 self.conversation = JsonConversation(**data.get(self.provider))
58 if self.provider and self.data.get(self.provider):
59 self.conversation = JsonConversation(**self.data.get(self.provider))
60 elif not self.provider and self.data:
61 self.conversation = JsonConversation(**self.data)
60 62 self.history = data.get("history", [])
61 63 except (json.JSONDecodeError, KeyError) as e:
62 64 print(f"Error loading conversation: {e}", file=sys.stderr)
@@ -183,7 +185,7 @@ def save_content(content, filepath: str, allowed_types = None):
183 185 def get_parser():
184 186 """Parse command line arguments."""
185 187 parser = argparse.ArgumentParser(
186 description="GPT CLI client with conversation history",
188 description="G4F CLI client with conversation history",
187 189 formatter_class=argparse.ArgumentDefaultsHelpFormatter
188 190 )
189 191 parser.add_argument("--debug", "-d", action="store_true", help="Enable verbose logging.")
@@ -263,25 +265,29 @@ async def run_args(input_text: str, args):
263 265
264 266 # Save conversation state
265 267 conversation.save()
266
267 except Exception as e:
268 except:
268 269 print(traceback.format_exc(), file=sys.stderr)
269 270 sys.exit(1)
270 271
271 272 def run_client_args(args):
272 273 input_text = ""
273 274 if args.input and os.path.isfile(args.input[0]):
274 with open(args.input[0], 'rb') as f:
275 try:
275 try:
276 with open(args.input[0], 'rb') as f:
276 277 if is_accepted_format(f.read(12)):
277 278 input_text = (Path(args.input[0]), " ".join(args.input[1:]))
278 except ValueError:
279 # If not a valid image, read as text
279 except ValueError:
280 # If not a valid image, read as text
281 try:
280 282 with open(args.input[0], 'r', encoding='utf-8') as f:
281 283 file_content = f.read().strip()
282 if len(args.input) > 1:
283 input_text = " ".join(args.input[1:]) + "\n"
284 input_text += f"```{os.path.basename(args.input[0])}\n" + file_content + "\n```"
284 except UnicodeDecodeError:
285 print(f"Error reading file {args.input[0]} as text. Ensure it is a valid text file.", file=sys.stderr)
286 sys.exit(1)
287 if len(args.input) > 1:
288 input_text = f"{' '.join(args.input[1:])}\n```{os.path.basename(args.input[0])}\n" + file_content + "\n```"
289 else:
290 input_text = file_content
285 291 elif args.input:
286 292 input_text = (" ".join(args.input)).strip()
287 293 if not input_text: