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/AItianhu.py | 6 ++---- g4f/Provider/AiService.py | 6 ++---- g4f/Provider/Aichat.py | 6 ++---- g4f/Provider/Bing.py | 5 +---- g4f/Provider/ChatgptAi.py | 6 ++---- g4f/Provider/Theb.py | 6 ++---- g4f/Provider/V50.py | 8 +++----- 7 files changed, 14 insertions(+), 29 deletions(-) diff --git a/g4f/Provider/AItianhu.py b/g4f/Provider/AItianhu.py index abf66cc1..28e66dcf 100644 --- a/g4f/Provider/AItianhu.py +++ b/g4f/Provider/AItianhu.py @@ -17,10 +17,8 @@ class AItianhu(BaseProvider): messages: list[dict[str, str]], stream: bool, **kwargs: Any) -> CreateResult: - base = "" - for message in messages: - base += "%s: %s\n" % (message["role"], message["content"]) - base += "assistant:" + base = "\n".join(f"{message['role']}: {message['content']}" for message in messages) + base += "\nassistant: " headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" diff --git a/g4f/Provider/AiService.py b/g4f/Provider/AiService.py index 3453bfd9..3ce2f3df 100644 --- a/g4f/Provider/AiService.py +++ b/g4f/Provider/AiService.py @@ -16,10 +16,8 @@ class AiService(BaseProvider): stream: bool, **kwargs: Any, ) -> CreateResult: - base = "" - for message in messages: - base += "%s: %s\n" % (message["role"], message["content"]) - base += "assistant:" + base = "\n".join(f"{message['role']}: {message['content']}" for message in messages) + base += "\nassistant: " headers = { "accept": "*/*", diff --git a/g4f/Provider/Aichat.py b/g4f/Provider/Aichat.py index 62954e07..4c3f35b6 100644 --- a/g4f/Provider/Aichat.py +++ b/g4f/Provider/Aichat.py @@ -15,10 +15,8 @@ class Aichat(BaseProvider): messages: list[dict[str, str]], stream: bool, **kwargs: Any) -> CreateResult: - base = "" - for message in messages: - base += "%s: %s\n" % (message["role"], message["content"]) - base += "assistant:" + chat = "\n".join(f"{message['role']}: {message['content']}" for message in messages) + chat += "\nassistant: " headers = { "authority": "chat-gpt.org", diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index 48968e39..1642877d 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -40,10 +40,7 @@ class Bing(AsyncGeneratorProvider): return stream_generate(prompt, context, cookies_dict) def create_context(messages: list[dict[str, str]]): - context = "" - - for message in messages: - context += "[%s](#message)\n%s\n\n" % (message["role"], message["content"]) + context = "".join(f"[{message['role']}](#message)\n{message['content']}\n\n" for message in messages) return context 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( diff --git a/g4f/Provider/Theb.py b/g4f/Provider/Theb.py index 9a47bb89..118de53b 100644 --- a/g4f/Provider/Theb.py +++ b/g4f/Provider/Theb.py @@ -17,10 +17,8 @@ class Theb(BaseProvider): messages: list[dict[str, str]], stream: bool, **kwargs: Any) -> CreateResult: - conversation = '' - for message in messages: - conversation += '%s: %s\n' % (message['role'], message['content']) - conversation += 'assistant: ' + conversation = "\n".join(f"{message['role']}: {message['content']}" for message in messages) + conversation += "\nassistant: " auth = kwargs.get("auth", { "bearer_token":"free", diff --git a/g4f/Provider/V50.py b/g4f/Provider/V50.py index 1dc7651d..ad3fd9e4 100644 --- a/g4f/Provider/V50.py +++ b/g4f/Provider/V50.py @@ -16,11 +16,9 @@ class V50(BaseProvider): messages: list[dict[str, str]], stream: bool, **kwargs: Any) -> CreateResult: - conversation = '' - for message in messages: - conversation += '%s: %s\n' % (message['role'], message['content']) - - conversation += 'assistant: ' + conversation = "\n".join(f"{message['role']}: {message['content']}" for message in messages) + conversation += "\nassistant: " + payload = { "prompt" : conversation, "options" : {}, -- cgit v1.2.3