mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-11-10 19:11:01 +00:00
Add needs auth to provierds, Add PerplexityApi provider
Add proxy support to nodriver
This commit is contained in:
parent
d1c16ceb98
commit
24c5502d76
@ -8,8 +8,7 @@ class DeepInfra(Openai):
|
||||
label = "DeepInfra"
|
||||
url = "https://deepinfra.com"
|
||||
working = True
|
||||
needs_auth = False
|
||||
has_auth = True
|
||||
needs_auth = True
|
||||
supports_stream = True
|
||||
supports_message_history = True
|
||||
default_model = "meta-llama/Meta-Llama-3-70b-instruct"
|
||||
|
@ -9,6 +9,7 @@ from ..image import to_bytes
|
||||
class Reka(AbstractProvider):
|
||||
url = "https://chat.reka.ai/"
|
||||
working = True
|
||||
needs_auth = True
|
||||
supports_stream = True
|
||||
default_vision_model = "reka"
|
||||
cookies = {}
|
||||
@ -20,7 +21,6 @@ class Reka(AbstractProvider):
|
||||
messages: Messages,
|
||||
stream: bool,
|
||||
proxy: str = None,
|
||||
timeout: int = 180,
|
||||
api_key: str = None,
|
||||
image: ImageType = None,
|
||||
**kwargs
|
||||
|
@ -10,6 +10,7 @@ from ..errors import ResponseError, MissingAuthError
|
||||
class Replicate(AsyncGeneratorProvider, ProviderModelMixin):
|
||||
url = "https://replicate.com"
|
||||
working = True
|
||||
needs_auth = True
|
||||
default_model = "meta/meta-llama-3-70b-instruct"
|
||||
model_aliases = {
|
||||
"meta-llama/Meta-Llama-3-70B-Instruct": default_model
|
||||
|
@ -1,3 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import json
|
||||
import base64
|
||||
@ -42,7 +44,6 @@ class You(AsyncGeneratorProvider, ProviderModelMixin):
|
||||
]
|
||||
model_aliases = {
|
||||
"claude-v2": "claude-2",
|
||||
"gpt-4o": "gpt-4o",
|
||||
}
|
||||
_cookies = None
|
||||
_cookies_used = 0
|
||||
@ -185,15 +186,7 @@ class You(AsyncGeneratorProvider, ProviderModelMixin):
|
||||
@classmethod
|
||||
async def create_cookies(cls, client: StreamSession) -> Cookies:
|
||||
if not cls._telemetry_ids:
|
||||
try:
|
||||
cls._telemetry_ids = await get_telemetry_ids()
|
||||
except RuntimeError as e:
|
||||
if str(e) == "Event loop is closed":
|
||||
if debug.logging:
|
||||
print("Event loop is closed error occurred in create_cookies.")
|
||||
else:
|
||||
raise
|
||||
|
||||
user_uuid = str(uuid.uuid4())
|
||||
telemetry_id = cls._telemetry_ids.pop()
|
||||
if debug.logging:
|
||||
|
@ -59,7 +59,7 @@ class Gemini(AsyncGeneratorProvider):
|
||||
_cookies: Cookies = None
|
||||
|
||||
@classmethod
|
||||
async def nodriver_login(cls) -> AsyncIterator[str]:
|
||||
async def nodriver_login(cls, proxy: str = None) -> AsyncIterator[str]:
|
||||
try:
|
||||
import nodriver as uc
|
||||
except ImportError:
|
||||
@ -71,7 +71,10 @@ class Gemini(AsyncGeneratorProvider):
|
||||
user_data_dir = None
|
||||
if debug.logging:
|
||||
print(f"Open nodriver with user_dir: {user_data_dir}")
|
||||
browser = await uc.start(user_data_dir=user_data_dir)
|
||||
browser = await uc.start(
|
||||
user_data_dir=user_data_dir,
|
||||
browser_args=None if proxy is None else [f"--proxy-server={proxy}"],
|
||||
)
|
||||
login_url = os.environ.get("G4F_LOGIN_URL")
|
||||
if login_url:
|
||||
yield f"Please login: [Google Gemini]({login_url})\n\n"
|
||||
@ -134,7 +137,7 @@ class Gemini(AsyncGeneratorProvider):
|
||||
) as session:
|
||||
snlm0e = await cls.fetch_snlm0e(session, cls._cookies) if cls._cookies else None
|
||||
if not snlm0e:
|
||||
async for chunk in cls.nodriver_login():
|
||||
async for chunk in cls.nodriver_login(proxy):
|
||||
yield chunk
|
||||
if cls._cookies is None:
|
||||
async for chunk in cls.webdriver_login(proxy):
|
||||
|
@ -403,7 +403,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
|
||||
except NoValidHarFileError as e:
|
||||
error = e
|
||||
if cls._api_key is None:
|
||||
await cls.nodriver_access_token()
|
||||
await cls.nodriver_access_token(proxy)
|
||||
if cls._api_key is None and cls.needs_auth:
|
||||
raise error
|
||||
cls.default_model = cls.get_model(await cls.get_default_model(session, cls._headers))
|
||||
@ -625,7 +625,7 @@ this.fetch = async (url, options) => {
|
||||
cls._update_cookie_header()
|
||||
|
||||
@classmethod
|
||||
async def nodriver_access_token(cls):
|
||||
async def nodriver_access_token(cls, proxy: str = None):
|
||||
try:
|
||||
import nodriver as uc
|
||||
except ImportError:
|
||||
@ -637,7 +637,10 @@ this.fetch = async (url, options) => {
|
||||
user_data_dir = None
|
||||
if debug.logging:
|
||||
print(f"Open nodriver with user_dir: {user_data_dir}")
|
||||
browser = await uc.start(user_data_dir=user_data_dir)
|
||||
browser = await uc.start(
|
||||
user_data_dir=user_data_dir,
|
||||
browser_args=None if proxy is None else [f"--proxy-server={proxy}"],
|
||||
)
|
||||
page = await browser.get("https://chatgpt.com/")
|
||||
await page.select("[id^=headlessui-menu-button-]", 240)
|
||||
api_key = await page.evaluate(
|
||||
|
31
g4f/Provider/needs_auth/PerplexityApi.py
Normal file
31
g4f/Provider/needs_auth/PerplexityApi.py
Normal file
@ -0,0 +1,31 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from .Openai import Openai
|
||||
from ...typing import AsyncResult, Messages
|
||||
|
||||
class PerplexityApi(Openai):
|
||||
label = "Perplexity API"
|
||||
url = "https://www.perplexity.ai"
|
||||
working = True
|
||||
default_model = "llama-3-sonar-large-32k-online"
|
||||
models = [
|
||||
"llama-3-sonar-small-32k-chat",
|
||||
"llama-3-sonar-small-32k-online",
|
||||
"llama-3-sonar-large-32k-chat",
|
||||
"llama-3-sonar-large-32k-online",
|
||||
"llama-3-8b-instruct",
|
||||
"llama-3-70b-instruct",
|
||||
"mixtral-8x7b-instruct"
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def create_async_generator(
|
||||
cls,
|
||||
model: str,
|
||||
messages: Messages,
|
||||
api_base: str = "https://api.perplexity.ai",
|
||||
**kwargs
|
||||
) -> AsyncResult:
|
||||
return super().create_async_generator(
|
||||
model, messages, api_base=api_base, **kwargs
|
||||
)
|
@ -8,3 +8,4 @@ from .Openai import Openai
|
||||
from .Groq import Groq
|
||||
from .OpenRouter import OpenRouter
|
||||
from .OpenaiAccount import OpenaiAccount
|
||||
from .PerplexityApi import PerplexityApi
|
@ -88,36 +88,34 @@ async def get_telemetry_ids(proxy: str = None) -> list:
|
||||
except NoValidHarFileError as e:
|
||||
if debug.logging:
|
||||
logging.error(e)
|
||||
if debug.logging:
|
||||
logging.error('Getting telemetry_id for you.com with nodriver')
|
||||
|
||||
try:
|
||||
from nodriver import start
|
||||
except ImportError:
|
||||
raise MissingRequirementsError('Add .har file from you.com or install "nodriver" package | pip install -U nodriver')
|
||||
page = None
|
||||
try:
|
||||
browser = await start()
|
||||
page = await browser.get("https://you.com")
|
||||
if debug.logging:
|
||||
logging.error('Getting telemetry_id for you.com with nodriver')
|
||||
|
||||
browser = page = None
|
||||
try:
|
||||
browser = await start(
|
||||
browser_args=None if proxy is None else [f"--proxy-server={proxy}"],
|
||||
)
|
||||
page = await browser.get("https://you.com")
|
||||
while not await page.evaluate('"GetTelemetryID" in this'):
|
||||
await page.sleep(1)
|
||||
|
||||
async def get_telemetry_id():
|
||||
return await page.evaluate(
|
||||
f'this.GetTelemetryID("{public_token}", "{telemetry_url}");',
|
||||
await_promise=True
|
||||
)
|
||||
|
||||
return [await get_telemetry_id()]
|
||||
|
||||
finally:
|
||||
try:
|
||||
if page is not None:
|
||||
await page.close()
|
||||
|
||||
if browser is not None:
|
||||
await browser.close()
|
||||
|
||||
except Exception as e:
|
||||
if debug.logging:
|
||||
logging.error(e)
|
||||
|
@ -133,7 +133,7 @@ class NewBaseRetryProvider(BaseRetryProvider):
|
||||
if not stream:
|
||||
yield await provider.create_async(model, messages, **kwargs)
|
||||
elif hasattr(provider, "create_async_generator"):
|
||||
async for token in provider.create_async_generator(model, messages, stream, **kwargs):
|
||||
async for token in provider.create_async_generator(model, messages, stream=stream, **kwargs):
|
||||
yield token
|
||||
else:
|
||||
for token in provider.create_completion(model, messages, stream, **kwargs):
|
||||
|
Loading…
Reference in New Issue
Block a user