diff options
38 files changed, 329 insertions, 123 deletions
@@ -118,25 +118,99 @@ for message in response: print(message) ``` -providers: +##### Providers: ```py from g4f.Provider import ( Acytoo, Aichat, Ails, - AiService, - AItianhu, Bard, Bing, ChatgptAi, ChatgptLogin, DeepAi, - GetGpt + EasyChat, + Equing, + GetGpt, + H2o, + HuggingChat, + Opchatgpts, + OpenAssistant, + OpenaiChat, + Raycast, + Theb, + Vercel, + Wewordle, + Wuguokai, + You, + Yqcloud ) +# Usage: +response = g4f.ChatCompletion.create(..., provider=ProviderName) +``` +##### Needs cookies: -# usage: -response = g4f.ChatCompletion.create(..., provider=ProviderName) +Many providers need cookies to work. +In Bing you need a session, where you have passed the captcha. +And in others providers you have to log-in into your account. +If you run the g4l package locally, +cookies from your browsers are readed with `get_cookies`. +Else you have pass them in the parameter `cookies`: +```py +import g4f +from g4f.Provider import ( + Bard, + Bing, + H2o, + HuggingChat, + OpenAssistant, + OpenaiChat, + You, +) +# Usage: +response = g4f.ChatCompletion.create( + model=g4f.models.default, + messages=[{"role": "user", "content": "Hello"}], + provider=Bard, + #cookies=g4f.get_cookies(".google.com"), + cookies={"cookie_name": "value", "cookie_name2": "value2"}, + auth=True +) +``` + +##### Async support: + +Run providers `async` to improve speed / performance. +The full execution time corresponds to the execution time of the slowest provider. + +```py +import g4f, asyncio + +async def run_async(): + _providers = [ + g4f.Provider.Bard, + g4f.Provider.Bing, + g4f.Provider.H2o, + g4f.Provider.HuggingChat, + g4f.Provider.Liaobots, + g4f.Provider.OpenAssistant, + g4f.Provider.OpenaiChat, + g4f.Provider.You, + g4f.Provider.Yqcloud, + ] + responses = [ + provider.create_async( + model=None, + messages=[{"role": "user", "content": "Hello"}], + ) + for provider in _providers + ] + responses = await asyncio.gather(*responses) + for idx, provider in enumerate(_providers): + print(f"{provider.__name__}:", responses[idx]) + +asyncio.run(run_async()) ``` ### interference openai-proxy api (use with openai python package) @@ -186,30 +260,34 @@ if __name__ == "__main__": | Website| Provider| gpt-3.5 | gpt-4 | Streaming | Status | Auth | | ------ | ------- | ------- | ----- | --------- | ------ | ---- | | [chat.acytoo.com](https://chat.acytoo.com/) | g4f.provider.Acytoo | ✔️ | ❌ | ❌ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | -| [chat-gpt.org](https://chat-gpt.org/chat) | g4f.provider.Aichat | ✔️ | ❌ | ❌ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | -| [ai.ls](https://ai.ls) | g4f.provider.Ails | ✔️ | ❌ | ✔️ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | +| [chat-gpt.org](https://chat-gpt.org/chat) | g4f.provider.Aichat | ✔️ | ❌ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | +| [ai.ls](https://ai.ls) | g4f.provider.Ails | ✔️ | ❌ | ✔️ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | | [bard.google.com](https://bard.google.com) | g4f.provider.Bard | ❌ | ❌ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ✔️ | -| [chatgpt.ai](https://chatgpt.ai/gpt-4/) | g4f.provider.ChatgptAi | ❌ | ✔️ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | -| [opchatgpts.net](https://opchatgpts.net) | g4f.provider.ChatgptLogin | ✔️ | ❌ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | +| [bing.com](https://bing.com/chat) | g4f.provider.Bing | ❌ | ✔️ | ✔️ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ✔️ | +| [chatgpt.ai](https://chatgpt.ai/gpt-4/) | g4f.provider.ChatgptAi | ❌ | ✔️ | ❌ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | +| [opchatgpts.net](https://opchatgpts.net) | g4f.provider.ChatgptLogin | ✔️ | ❌ | ❌ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | | [deepai.org](https://deepai.org) | g4f.provider.DeepAi | ✔️ | ❌ | ✔️ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | | [free.easychat.work](https://free.easychat.work) | g4f.provider.EasyChat | ✔️ | ❌ | ✔️ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | | [next.eqing.tech](https://next.eqing.tech/) | g4f.provider.Equing | ✔️ | ❌ | ✔️ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | | [chat.getgpt.world](https://chat.getgpt.world/) | g4f.provider.GetGpt | ✔️ | ❌ | ✔️ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | | [gpt-gm.h2o.ai](https://gpt-gm.h2o.ai) | g4f.provider.H2o | ❌ | ❌ | ✔️ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | -| [opchatgpts.net](https://opchatgpts.net) | g4f.provider.Opchatgpts | ✔️ | ❌ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | +| [huggingface.co](https://huggingface.co/chat/) | g4f.provider.HuggingChat | ❌ | ❌ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ✔️ | +| [liaobots.com](https://liaobots.com) | g4f.provider.Liaobots | ✔️ | ✔️ | ✔️ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | +| [opchatgpts.net](https://opchatgpts.net) | g4f.provider.Opchatgpts | ✔️ | ❌ | ❌ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | +| [open-assistant.io](https://open-assistant.io/chat) | g4f.provider.OpenAssistant | ❌ | ❌ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ✔️ | +| [chat.openai.com](https://chat.openai.com) | g4f.provider.OpenaiChat | ✔️ | ✔️ | ✔️ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ✔️ | | [raycast.com](https://raycast.com) | g4f.provider.Raycast | ✔️ | ✔️ | ✔️ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ✔️ | | [theb.ai](https://theb.ai) | g4f.provider.Theb | ✔️ | ❌ | ✔️ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ✔️ | | [play.vercel.ai](https://play.vercel.ai) | g4f.provider.Vercel | ✔️ | ❌ | ❌ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | | [wewordle.org](https://wewordle.org/) | g4f.provider.Wewordle | ✔️ | ❌ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | -| [you.com](https://you.com) | g4f.provider.You | ✔️ | ❌ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | +| [chat.wuguokai.xyz](https://chat.wuguokai.xyz) | g4f.provider.Wuguokai | ✔️ | ❌ | ❌ | ![Unknown](https://img.shields.io/badge/Unknown-grey) | ❌ | +| [you.com](https://you.com) | g4f.provider.You | ✔️ | ❌ | ✔️ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | | [chat9.yqcloud.top](https://chat9.yqcloud.top/) | g4f.provider.Yqcloud | ✔️ | ❌ | ❌ | ![Active](https://img.shields.io/badge/Active-brightgreen) | ❌ | | [www.aitianhu.com](https://www.aitianhu.com/) | g4f.provider.AItianhu | ✔️ | ❌ | ❌ | ![Inactive](https://img.shields.io/badge/Inactive-red) | ❌ | | [aiservice.vercel.app](https://aiservice.vercel.app/) | g4f.provider.AiService | ✔️ | ❌ | ❌ | ![Inactive](https://img.shields.io/badge/Inactive-red) | ❌ | -| [bing.com](https://bing.com/chat) | g4f.provider.Bing | ❌ | ✔️ | ❌ | ![Inactive](https://img.shields.io/badge/Inactive-red) | ❌ | | [chat.dfehub.com](https://chat.dfehub.com/) | g4f.provider.DfeHub | ✔️ | ❌ | ✔️ | ![Inactive](https://img.shields.io/badge/Inactive-red) | ❌ | | [chat9.fastgpt.me](https://chat9.fastgpt.me/) | g4f.provider.FastGpt | ✔️ | ❌ | ✔️ | ![Inactive](https://img.shields.io/badge/Inactive-red) | ❌ | | [forefront.com](https://forefront.com) | g4f.provider.Forefront | ✔️ | ❌ | ✔️ | ![Inactive](https://img.shields.io/badge/Inactive-red) | ❌ | -| [liaobots.com](https://liaobots.com) | g4f.provider.Liaobots | ✔️ | ✔️ | ✔️ | ![Inactive](https://img.shields.io/badge/Inactive-red) | ✔️ | | [supertest.lockchat.app](http://supertest.lockchat.app) | g4f.provider.Lockchat | ✔️ | ✔️ | ✔️ | ![Inactive](https://img.shields.io/badge/Inactive-red) | ❌ | | [p5.v50.ltd](https://p5.v50.ltd) | g4f.provider.V50 | ✔️ | ❌ | ❌ | ![Inactive](https://img.shields.io/badge/Inactive-red) | ❌ | diff --git a/g4f/Provider/AItianhu.py b/g4f/Provider/AItianhu.py index abf66cc1..0982d3c6 100644 --- a/g4f/Provider/AItianhu.py +++ b/g4f/Provider/AItianhu.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json import requests @@ -17,10 +19,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/Acytoo.py b/g4f/Provider/Acytoo.py index 5baa2b8d..48a3a344 100644 --- a/g4f/Provider/Acytoo.py +++ b/g4f/Provider/Acytoo.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import time import requests diff --git a/g4f/Provider/AiService.py b/g4f/Provider/AiService.py index 3453bfd9..2b5a6e7d 100644 --- a/g4f/Provider/AiService.py +++ b/g4f/Provider/AiService.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import requests from ..typing import Any, CreateResult @@ -16,10 +18,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..59640533 100644 --- a/g4f/Provider/Aichat.py +++ b/g4f/Provider/Aichat.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import requests from ..typing import Any, CreateResult @@ -15,10 +17,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/Ails.py b/g4f/Provider/Ails.py index 9a58d505..4eb21729 100644 --- a/g4f/Provider/Ails.py +++ b/g4f/Provider/Ails.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import hashlib import json import time @@ -9,6 +11,7 @@ import requests from ..typing import SHA256, Any, CreateResult from .base_provider import BaseProvider + class Ails(BaseProvider): url: str = "https://ai.ls" working = True diff --git a/g4f/Provider/Bard.py b/g4f/Provider/Bard.py index 978fa98e..2137d820 100644 --- a/g4f/Provider/Bard.py +++ b/g4f/Provider/Bard.py @@ -1,9 +1,13 @@ +from __future__ import annotations + import json import random import re + from aiohttp import ClientSession -from .base_provider import AsyncProvider, get_cookies, format_prompt +from .base_provider import AsyncProvider, format_prompt, get_cookies + class Bard(AsyncProvider): url = "https://bard.google.com" diff --git a/g4f/Provider/Bing.py b/g4f/Provider/Bing.py index c31ba5a7..cec82108 100644 --- a/g4f/Provider/Bing.py +++ b/g4f/Provider/Bing.py @@ -1,9 +1,17 @@ -import asyncio, aiohttp, json, os, random +from __future__ import annotations -from aiohttp import ClientSession -from ..typing import Any, AsyncGenerator, CreateResult, Union +import asyncio +import json +import os +import random + +import aiohttp +from aiohttp import ClientSession + +from ..typing import Any, AsyncGenerator, CreateResult, Union from .base_provider import AsyncGeneratorProvider, get_cookies + class Bing(AsyncGeneratorProvider): url = "https://bing.com/chat" needs_auth = True @@ -26,41 +34,21 @@ class Bing(AsyncGeneratorProvider): prompt = messages[-1]["content"] context = create_context(messages[:-1]) - if cookies: + if cookies and "SRCHD" in cookies: #TODO: Will implement proper cookie retrieval later and use a try-except mechanism in 'stream_generate' instead of defaulting the cookie value like this cookies_dict = { - 'MUID' : '', - 'BCP' : '', - 'MUIDB' : '', - 'USRLOC' : '', - 'SRCHD' : 'AF=hpcodx', - 'MMCASM' : '', - '_UR' : '', - 'ANON' : '', - 'NAP' : '', - 'ABDEF' : '', + 'SRCHD' : cookies["SRCHD"], 'PPLState' : '1', 'KievRPSSecAuth': '', - '_U' : '', 'SUID' : '', - '_EDGE_S' : '', - 'WLS' : '', - '_HPVN' : '', - '_SS' : '', - '_clck' : '', 'SRCHUSR' : '', - '_RwBf' : '', 'SRCHHPGUSR' : '', - 'ipv6' : '', } 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..7613ccf1 100644 --- a/g4f/Provider/ChatgptAi.py +++ b/g4f/Provider/ChatgptAi.py @@ -1,6 +1,10 @@ -import re, requests +from __future__ import annotations -from ..typing import Any, CreateResult +import re + +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider @@ -15,10 +19,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/ChatgptLogin.py b/g4f/Provider/ChatgptLogin.py index d445e023..e4584d32 100644 --- a/g4f/Provider/ChatgptLogin.py +++ b/g4f/Provider/ChatgptLogin.py @@ -1,6 +1,12 @@ -import base64, os, re, requests +from __future__ import annotations -from ..typing import Any, CreateResult +import base64 +import os +import re + +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider diff --git a/g4f/Provider/DeepAi.py b/g4f/Provider/DeepAi.py index 073b04a5..feba6b41 100644 --- a/g4f/Provider/DeepAi.py +++ b/g4f/Provider/DeepAi.py @@ -1,6 +1,11 @@ -import json, js2py, requests +from __future__ import annotations -from ..typing import Any, CreateResult +import json + +import js2py +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider diff --git a/g4f/Provider/DfeHub.py b/g4f/Provider/DfeHub.py index 477bb546..d40e0380 100644 --- a/g4f/Provider/DfeHub.py +++ b/g4f/Provider/DfeHub.py @@ -1,6 +1,12 @@ -import json, re, time , requests +from __future__ import annotations -from ..typing import Any, CreateResult +import json +import re +import time + +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider diff --git a/g4f/Provider/EasyChat.py b/g4f/Provider/EasyChat.py index 959b9d10..946d4a4d 100644 --- a/g4f/Provider/EasyChat.py +++ b/g4f/Provider/EasyChat.py @@ -1,6 +1,11 @@ -import json, requests, random +from __future__ import annotations -from ..typing import Any, CreateResult +import json +import random + +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider diff --git a/g4f/Provider/Equing.py b/g4f/Provider/Equing.py index 8595c73e..0ebb93a5 100644 --- a/g4f/Provider/Equing.py +++ b/g4f/Provider/Equing.py @@ -1,6 +1,10 @@ -import requests, json +from __future__ import annotations + +import json +from abc import ABC, abstractmethod + +import requests -from abc import ABC, abstractmethod from ..typing import Any, CreateResult diff --git a/g4f/Provider/FastGpt.py b/g4f/Provider/FastGpt.py index 8bf58423..ef47f752 100644 --- a/g4f/Provider/FastGpt.py +++ b/g4f/Provider/FastGpt.py @@ -1,6 +1,11 @@ -import requests, json, random +from __future__ import annotations + +import json +import random from abc import ABC, abstractmethod +import requests + from ..typing import Any, CreateResult diff --git a/g4f/Provider/Forefront.py b/g4f/Provider/Forefront.py index 658355f4..8f51fb57 100644 --- a/g4f/Provider/Forefront.py +++ b/g4f/Provider/Forefront.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json import requests diff --git a/g4f/Provider/GetGpt.py b/g4f/Provider/GetGpt.py index d79cb9db..74e772b0 100644 --- a/g4f/Provider/GetGpt.py +++ b/g4f/Provider/GetGpt.py @@ -1,7 +1,13 @@ -import os, json, uuid, requests +from __future__ import annotations -from Crypto.Cipher import AES -from ..typing import Any, CreateResult +import json +import os +import uuid + +import requests +from Crypto.Cipher import AES + +from ..typing import Any, CreateResult from .base_provider import BaseProvider diff --git a/g4f/Provider/H2o.py b/g4f/Provider/H2o.py index ac5fcfb7..b62da977 100644 --- a/g4f/Provider/H2o.py +++ b/g4f/Provider/H2o.py @@ -1,5 +1,8 @@ +from __future__ import annotations + import json import uuid + from aiohttp import ClientSession from ..typing import AsyncGenerator diff --git a/g4f/Provider/HuggingChat.py b/g4f/Provider/HuggingChat.py index 4564d7b3..7b62b342 100644 --- a/g4f/Provider/HuggingChat.py +++ b/g4f/Provider/HuggingChat.py @@ -1,8 +1,11 @@ +from __future__ import annotations + import json + from aiohttp import ClientSession from ..typing import AsyncGenerator -from .base_provider import AsyncGeneratorProvider, get_cookies, format_prompt +from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies class HuggingChat(AsyncGeneratorProvider): diff --git a/g4f/Provider/Liaobots.py b/g4f/Provider/Liaobots.py index e69a565e..2360c8a5 100644 --- a/g4f/Provider/Liaobots.py +++ b/g4f/Provider/Liaobots.py @@ -1,5 +1,8 @@ -import uuid +from __future__ import annotations + import json +import uuid + from aiohttp import ClientSession from ..typing import AsyncGenerator @@ -28,6 +31,7 @@ models = { class Liaobots(AsyncGeneratorProvider): url = "https://liaobots.com" + working = True supports_stream = True supports_gpt_35_turbo = True supports_gpt_4 = True diff --git a/g4f/Provider/Lockchat.py b/g4f/Provider/Lockchat.py index 3c1ce0d1..c15eec8d 100644 --- a/g4f/Provider/Lockchat.py +++ b/g4f/Provider/Lockchat.py @@ -1,6 +1,10 @@ -import json, requests +from __future__ import annotations -from ..typing import Any, CreateResult +import json + +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider diff --git a/g4f/Provider/Opchatgpts.py b/g4f/Provider/Opchatgpts.py index a47b0099..241646f0 100644 --- a/g4f/Provider/Opchatgpts.py +++ b/g4f/Provider/Opchatgpts.py @@ -1,6 +1,8 @@ +from __future__ import annotations + import requests -from ..typing import Any, CreateResult +from ..typing import Any, CreateResult from .base_provider import BaseProvider diff --git a/g4f/Provider/OpenAssistant.py b/g4f/Provider/OpenAssistant.py index 542af78f..3a931597 100644 --- a/g4f/Provider/OpenAssistant.py +++ b/g4f/Provider/OpenAssistant.py @@ -1,8 +1,12 @@ +from __future__ import annotations + import json + from aiohttp import ClientSession from ..typing import Any, AsyncGenerator -from .base_provider import AsyncGeneratorProvider, get_cookies, format_prompt +from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies + class OpenAssistant(AsyncGeneratorProvider): url = "https://open-assistant.io/chat" diff --git a/g4f/Provider/OpenaiChat.py b/g4f/Provider/OpenaiChat.py index 9ca0cd58..f2d1ed6f 100644 --- a/g4f/Provider/OpenaiChat.py +++ b/g4f/Provider/OpenaiChat.py @@ -1,14 +1,18 @@ +from __future__ import annotations + has_module = True try: from revChatGPT.V1 import AsyncChatbot except ImportError: has_module = False -from .base_provider import AsyncGeneratorProvider, get_cookies, format_prompt -from ..typing import AsyncGenerator -from httpx import AsyncClient import json +from httpx import AsyncClient + +from ..typing import AsyncGenerator +from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies + class OpenaiChat(AsyncGeneratorProvider): url = "https://chat.openai.com" diff --git a/g4f/Provider/Raycast.py b/g4f/Provider/Raycast.py index 057f5b31..7ddc8acd 100644 --- a/g4f/Provider/Raycast.py +++ b/g4f/Provider/Raycast.py @@ -1,6 +1,10 @@ -import json, requests +from __future__ import annotations -from ..typing import Any, CreateResult +import json + +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider diff --git a/g4f/Provider/Theb.py b/g4f/Provider/Theb.py index 9a47bb89..72fce3ac 100644 --- a/g4f/Provider/Theb.py +++ b/g4f/Provider/Theb.py @@ -1,6 +1,11 @@ -import json, random, requests +from __future__ import annotations -from ..typing import Any, CreateResult +import json +import random + +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider @@ -17,10 +22,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..81a95ba8 100644 --- a/g4f/Provider/V50.py +++ b/g4f/Provider/V50.py @@ -1,8 +1,13 @@ -import uuid, requests +from __future__ import annotations -from ..typing import Any, CreateResult +import uuid + +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider + class V50(BaseProvider): url = 'https://p5.v50.ltd' supports_gpt_35_turbo = True @@ -16,11 +21,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" : {}, diff --git a/g4f/Provider/Vercel.py b/g4f/Provider/Vercel.py index f2377db4..8aaf5656 100644 --- a/g4f/Provider/Vercel.py +++ b/g4f/Provider/Vercel.py @@ -1,7 +1,13 @@ -import base64, json, uuid, quickjs +from __future__ import annotations -from curl_cffi import requests -from ..typing import Any, CreateResult, TypedDict +import base64 +import json +import uuid + +import quickjs +from curl_cffi import requests + +from ..typing import Any, CreateResult, TypedDict from .base_provider import BaseProvider diff --git a/g4f/Provider/Wewordle.py b/g4f/Provider/Wewordle.py index 6a8a6a7b..99c81a84 100644 --- a/g4f/Provider/Wewordle.py +++ b/g4f/Provider/Wewordle.py @@ -1,6 +1,13 @@ -import json, random, string, time, requests +from __future__ import annotations -from ..typing import Any, CreateResult +import json +import random +import string +import time + +import requests + +from ..typing import Any, CreateResult from .base_provider import BaseProvider diff --git a/g4f/Provider/Wuguokai.py b/g4f/Provider/Wuguokai.py index 906283ad..a9614626 100644 --- a/g4f/Provider/Wuguokai.py +++ b/g4f/Provider/Wuguokai.py @@ -1,4 +1,9 @@ -import random, requests, json +from __future__ import annotations + +import random + +import requests + from ..typing import Any, CreateResult from .base_provider import BaseProvider @@ -6,8 +11,6 @@ from .base_provider import BaseProvider class Wuguokai(BaseProvider): url = 'https://chat.wuguokai.xyz' supports_gpt_35_turbo = True - supports_stream = False - needs_auth = False working = True @staticmethod @@ -43,7 +46,7 @@ class Wuguokai(BaseProvider): "userId": f"#/chat/{random.randint(1,99999999)}", "usingContext": True } - response = requests.post("https://ai-api20.wuguokai.xyz/api/chat-process", headers=headers, data=json.dumps(data),proxies=kwargs['proxy'] if 'proxy' in kwargs else {}) + response = requests.post("https://ai-api20.wuguokai.xyz/api/chat-process", headers=headers, timeout=3, json=data, proxies=kwargs['proxy'] if 'proxy' in kwargs else {}) _split = response.text.split("> 若回答失败请重试或多刷新几次界面后重试") if response.status_code == 200: if len(_split) > 1: diff --git a/g4f/Provider/You.py b/g4f/Provider/You.py index 1fbe1eb6..121d1dbd 100644 --- a/g4f/Provider/You.py +++ b/g4f/Provider/You.py @@ -1,6 +1,9 @@ -from aiohttp import ClientSession +from __future__ import annotations + import json +from aiohttp import ClientSession + from ..typing import AsyncGenerator from .base_provider import AsyncGeneratorProvider, format_prompt, get_cookies diff --git a/g4f/Provider/Yqcloud.py b/g4f/Provider/Yqcloud.py index 7c1d06ba..731e4ecb 100644 --- a/g4f/Provider/Yqcloud.py +++ b/g4f/Provider/Yqcloud.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from aiohttp import ClientSession from .base_provider import AsyncProvider, format_prompt diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py index 06643665..fa1bdb87 100644 --- a/g4f/Provider/__init__.py +++ b/g4f/Provider/__init__.py @@ -1,3 +1,4 @@ +from __future__ import annotations from .Acytoo import Acytoo from .Aichat import Aichat from .Ails import Ails @@ -30,7 +31,7 @@ from .FastGpt import FastGpt from .V50 import V50 from .Wuguokai import Wuguokai -from .base_provider import BaseProvider +from .base_provider import BaseProvider, AsyncProvider, AsyncGeneratorProvider __all__ = [ 'BaseProvider', diff --git a/g4f/Provider/base_provider.py b/g4f/Provider/base_provider.py index def2cd6d..e667819a 100644 --- a/g4f/Provider/base_provider.py +++ b/g4f/Provider/base_provider.py @@ -1,9 +1,11 @@ -from abc import ABC, abstractmethod +from __future__ import annotations -from ..typing import Any, CreateResult, AsyncGenerator, Union +import asyncio +from abc import ABC, abstractmethod import browser_cookie3 -import asyncio + +from ..typing import Any, AsyncGenerator, CreateResult, Union class BaseProvider(ABC): diff --git a/g4f/__init__.py b/g4f/__init__.py index 3ee91493..065acee6 100644 --- a/g4f/__init__.py +++ b/g4f/__init__.py @@ -1,3 +1,4 @@ +from __future__ import annotations from . import models from .Provider import BaseProvider from .typing import Any, CreateResult, Union diff --git a/g4f/models.py b/g4f/models.py index 7d94151b..0c5eb961 100644 --- a/g4f/models.py +++ b/g4f/models.py @@ -1,3 +1,4 @@ +from __future__ import annotations from dataclasses import dataclass from .Provider import Bard, BaseProvider, GetGpt, H2o, Liaobots, Vercel, Equing diff --git a/testing/test_needs_auth.py b/testing/test_needs_auth.py index eddb040a..3cef1c61 100644 --- a/testing/test_needs_auth.py +++ b/testing/test_needs_auth.py @@ -36,14 +36,14 @@ Bing: Hello! How can I help you today? 3.28 secs No Stream Total: 10.14 secs """ -print("Yqcloud:", end="") +print("Bing: ", end="") for response in log_time_yield( g4f.ChatCompletion.create, model=g4f.models.gpt_35_turbo, messages=[{"role": "user", "content": _instruct}], - provider=g4f.Provider.Yqcloud, + provider=g4f.Provider.Bing, #cookies=g4f.get_cookies(".huggingface.co"), - stream=True, + #stream=True, auth=True ): print(response, end="") diff --git a/tool/readme_table.py b/tool/readme_table.py index b578b9ca..522c66a7 100644 --- a/tool/readme_table.py +++ b/tool/readme_table.py @@ -6,14 +6,35 @@ from urllib.parse import urlparse sys.path.append(str(Path(__file__).parent.parent)) from g4f import models, Provider -from g4f.Provider.base_provider import BaseProvider +from g4f.Provider.base_provider import BaseProvider, AsyncProvider from testing.test_providers import test -def main(): - print_providers() - print("\n", "-" * 50, "\n") - print_models() +def print_imports(): + print("##### Providers:") + print("```py") + print("from g4f.Provider import (") + for _provider in get_providers(): + if _provider.working: + print(f" {_provider.__name__},") + print(")") + print("# Usage:") + print("response = g4f.ChatCompletion.create(..., provider=ProviderName)") + print("```") + print() + print() + +def print_async(): + print("##### Async support:") + print("```py") + print("from g4f.Provider import (") + for _provider in get_providers(): + if issubclass(_provider, AsyncProvider): + print(f" {_provider.__name__},") + print(")") + print("```") + print() + print() def print_providers(): lines = [ @@ -48,18 +69,23 @@ def print_providers(): print("\n".join(lines)) -def get_providers() -> list[type[BaseProvider]]: +def get_provider_names() -> list[str]: provider_names = dir(Provider) ignore_names = [ "base_provider", "BaseProvider", + "AsyncProvider", + "AsyncGeneratorProvider" ] - provider_names = [ + return [ provider_name for provider_name in provider_names if not provider_name.startswith("__") and provider_name not in ignore_names ] - return [getattr(Provider, provider_name) for provider_name in provider_names] + + +def get_providers() -> list[type[BaseProvider]]: + return [getattr(Provider, provider_name) for provider_name in get_provider_names()] def print_models(): @@ -107,4 +133,8 @@ def get_models(): if __name__ == "__main__": - main() + print_imports() + print_async() + print_providers() + print("\n", "-" * 50, "\n") + print_models()
\ No newline at end of file |