Skip to content

Commit

Permalink
Merge pull request #1833 from hlohaus/curl
Browse files Browse the repository at this point in the history
Update event loop on windows only for old curl_cffi
  • Loading branch information
hlohaus authored Apr 17, 2024
2 parents 8ec9420 + 0a3fe0b commit 0f04dac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 5 additions & 2 deletions g4f/locals/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ def create_completion(model: str, messages: Messages, stream: bool = False, **kw
if message["role"] != "system"
) + "\nASSISTANT: "

def should_not_stop(token_id: int, token: str):
return "USER" not in token

with model.chat_session(system_message, prompt_template):
if stream:
for token in model.generate(conversation, streaming=True):
for token in model.generate(conversation, streaming=True, callback=should_not_stop):
yield token
else:
yield model.generate(conversation)
yield model.generate(conversation, callback=should_not_stop)
9 changes: 7 additions & 2 deletions g4f/providers/base_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@

# Set Windows event loop policy for better compatibility with asyncio and curl_cffi
if sys.platform == 'win32':
if isinstance(asyncio.get_event_loop_policy(), asyncio.WindowsProactorEventLoopPolicy):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
try:
from curl_cffi import aio
if not hasattr(aio, "_get_selector"):
if isinstance(asyncio.get_event_loop_policy(), asyncio.WindowsProactorEventLoopPolicy):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
except ImportError:
pass

def get_running_loop(check_nested: bool) -> Union[AbstractEventLoop, None]:
try:
Expand Down

0 comments on commit 0f04dac

Please sign in to comment.