mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-09-12 08:29:30 -04:00
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.
This commit is contained in:
parent
9de43a0e95
commit
17c9adf485
@ -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"
|
||||
|
@ -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": "*/*",
|
||||
|
@ -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",
|
||||
|
@ -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
|
||||
|
||||
|
@ -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(
|
||||
|
@ -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",
|
||||
|
@ -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" : {},
|
||||
|
Loading…
Reference in New Issue
Block a user