Improve tools support in OpenaiTemplate and GeminiPro

Update models in DDG, PerplexityLabs, Gemini
Fix issues with curl_cffi in new versions
This commit is contained in:
hlohaus 2025-02-21 04:36:54 +01:00
parent c3ed6d0f8f
commit e53483d85b
33 changed files with 300 additions and 172 deletions

View file

@ -409,6 +409,14 @@ class AsyncAuthedProvider(AsyncGeneratorProvider):
def get_cache_file(cls) -> Path:
return Path(get_cookies_dir()) / f"auth_{cls.parent if hasattr(cls, 'parent') else cls.__name__}.json"
@classmethod
def write_cache_file(cls, cache_file: Path, auth_result: AuthResult = None):
if auth_result is not None:
cache_file.parent.mkdir(parents=True, exist_ok=True)
cache_file.write_text(json.dumps(auth_result.get_dict()))
elif cache_file.exists():
cache_file.unlink()
@classmethod
def create_completion(
cls,
@ -416,35 +424,25 @@ class AsyncAuthedProvider(AsyncGeneratorProvider):
messages: Messages,
**kwargs
) -> CreateResult:
auth_result = AuthResult()
auth_result: AuthResult = None
cache_file = cls.get_cache_file()
try:
if cache_file.exists():
with cache_file.open("r") as f:
auth_result = AuthResult(**json.load(f))
else:
auth_result = cls.on_auth(**kwargs)
for chunk in auth_result:
if hasattr(chunk, "get_dict"):
auth_result = chunk
else:
yield chunk
raise MissingAuthError
yield from to_sync_generator(cls.create_authed(model, messages, auth_result, **kwargs))
except (MissingAuthError, NoValidHarFileError):
auth_result = cls.on_auth(**kwargs)
for chunk in auth_result:
if hasattr(chunk, "get_dict"):
if isinstance(chunk, AuthResult):
auth_result = chunk
else:
yield chunk
yield from to_sync_generator(cls.create_authed(model, messages, auth_result, **kwargs))
finally:
if hasattr(auth_result, "get_dict"):
data = auth_result.get_dict()
cache_file.parent.mkdir(parents=True, exist_ok=True)
cache_file.write_text(json.dumps(data))
elif cache_file.exists():
cache_file.unlink()
cls.write_cache_file(cache_file, auth_result)
@classmethod
async def create_async_generator(
@ -453,19 +451,14 @@ class AsyncAuthedProvider(AsyncGeneratorProvider):
messages: Messages,
**kwargs
) -> AsyncResult:
auth_result: AuthResult = None
cache_file = cls.get_cache_file()
try:
auth_result = AuthResult()
cache_file = Path(get_cookies_dir()) / f"auth_{cls.parent if hasattr(cls, 'parent') else cls.__name__}.json"
if cache_file.exists():
with cache_file.open("r") as f:
auth_result = AuthResult(**json.load(f))
else:
auth_result = cls.on_auth_async(**kwargs)
async for chunk in auth_result:
if hasattr(chunk, "get_dict"):
auth_result = chunk
else:
yield chunk
raise MissingAuthError
response = to_async_iterator(cls.create_authed(model, messages, **kwargs, auth_result=auth_result))
async for chunk in response:
yield chunk
@ -474,16 +467,16 @@ class AsyncAuthedProvider(AsyncGeneratorProvider):
cache_file.unlink()
auth_result = cls.on_auth_async(**kwargs)
async for chunk in auth_result:
if hasattr(chunk, "get_dict"):
if isinstance(chunk, AuthResult):
auth_result = chunk
else:
yield chunk
response = to_async_iterator(cls.create_authed(model, messages, **kwargs, auth_result=auth_result))
async for chunk in response:
if cache_file is not None:
cls.write_cache_file(cache_file, auth_result)
cache_file = None
yield chunk
finally:
if hasattr(auth_result, "get_dict"):
cache_file.parent.mkdir(parents=True, exist_ok=True)
cache_file.write_text(json.dumps(auth_result.get_dict()))
elif cache_file.exists():
cache_file.unlink()
if cache_file is not None:
cls.write_cache_file(cache_file, auth_result)