From 17c9adf4852a0ca0d1a5cfa448478fa195a9c368 Mon Sep 17 00:00:00 2001 From: hs_junxiang Date: Mon, 4 Sep 2023 13:34:31 +0800 Subject: Join the messages A better approach is to use the `.join()` method of strings, which reduces string concatenation operations and improves performance. Additionally, using formatted strings (f-strings) makes the code cleaner and more readable. --- g4f/Provider/ChatgptAi.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'g4f/Provider/ChatgptAi.py') diff --git a/g4f/Provider/ChatgptAi.py b/g4f/Provider/ChatgptAi.py index 13c591cb..97b62840 100644 --- a/g4f/Provider/ChatgptAi.py +++ b/g4f/Provider/ChatgptAi.py @@ -15,10 +15,8 @@ class ChatgptAi(BaseProvider): messages: list[dict[str, str]], stream: bool, **kwargs: Any) -> CreateResult: - chat = "" - for message in messages: - chat += "%s: %s\n" % (message["role"], message["content"]) - chat += "assistant: " + chat = "\n".join(f"{message['role']}: {message['content']}" for message in messages) + chat += "\nassistant: " response = requests.get("https://chatgpt.ai/") nonce, post_id, _, bot_id = re.findall( -- cgit v1.2.3