2024-04-07 11:36:13 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-04-06 21:25:28 +03:00
|
|
|
import re
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
import logging
|
|
|
|
|
2024-11-20 21:58:16 +03:00
|
|
|
from typing import AsyncIterator, Iterator, AsyncGenerator, Optional
|
2024-04-06 21:25:28 +03:00
|
|
|
|
2024-04-07 11:36:13 +03:00
|
|
|
def filter_json(text: str) -> str:
|
2024-04-06 21:25:28 +03:00
|
|
|
"""
|
|
|
|
Parses JSON code block from a string.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text (str): A string containing a JSON code block.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: A dictionary parsed from the JSON code block.
|
|
|
|
"""
|
|
|
|
match = re.search(r"```(json|)\n(?P<code>[\S\s]+?)\n```", text)
|
|
|
|
if match:
|
|
|
|
return match.group("code")
|
2024-04-06 22:05:04 +03:00
|
|
|
return text
|
|
|
|
|
2024-11-20 21:58:16 +03:00
|
|
|
def find_stop(stop: Optional[list[str]], content: str, chunk: str = None):
|
2024-04-06 22:05:04 +03:00
|
|
|
first = -1
|
|
|
|
word = None
|
|
|
|
if stop is not None:
|
|
|
|
for word in list(stop):
|
|
|
|
first = content.find(word)
|
|
|
|
if first != -1:
|
|
|
|
content = content[:first]
|
|
|
|
break
|
2024-04-07 11:36:13 +03:00
|
|
|
if chunk is not None and first != -1:
|
2024-04-06 22:05:04 +03:00
|
|
|
first = chunk.find(word)
|
|
|
|
if first != -1:
|
|
|
|
chunk = chunk[:first]
|
|
|
|
else:
|
|
|
|
first = 0
|
|
|
|
return first, content, chunk
|
2024-04-07 11:36:13 +03:00
|
|
|
|
|
|
|
def filter_none(**kwargs) -> dict:
|
|
|
|
return {
|
|
|
|
key: value
|
|
|
|
for key, value in kwargs.items()
|
|
|
|
if value is not None
|
|
|
|
}
|
|
|
|
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
async def safe_aclose(generator: AsyncGenerator) -> None:
|
|
|
|
try:
|
2024-11-25 11:15:27 +03:00
|
|
|
if generator and hasattr(generator, 'aclose'):
|
|
|
|
await generator.aclose()
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
except Exception as e:
|
|
|
|
logging.warning(f"Error while closing generator: {e}")
|
|
|
|
|
|
|
|
# Helper function to convert a synchronous iterator to an async iterator
|
|
|
|
async def to_async_iterator(iterator: Iterator) -> AsyncIterator:
|
|
|
|
for item in iterator:
|
|
|
|
yield item
|