summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/helper.py
diff options
context:
space:
mode:
authorTekky <98614666+xtekky@users.noreply.github.com>2023-09-26 15:06:06 +0200
committerGitHub <noreply@github.com>2023-09-26 15:06:06 +0200
commit41af8aff6ce09be4131d50e0e24b8622628441fc (patch)
tree1519231f3513b57acf2548b77f1fc89e092fc4cb /g4f/Provider/helper.py
parent~ (diff)
parentAdd ChatgptDuo and Aibn Provider (diff)
downloadgpt4free-41af8aff6ce09be4131d50e0e24b8622628441fc.tar
gpt4free-41af8aff6ce09be4131d50e0e24b8622628441fc.tar.gz
gpt4free-41af8aff6ce09be4131d50e0e24b8622628441fc.tar.bz2
gpt4free-41af8aff6ce09be4131d50e0e24b8622628441fc.tar.lz
gpt4free-41af8aff6ce09be4131d50e0e24b8622628441fc.tar.xz
gpt4free-41af8aff6ce09be4131d50e0e24b8622628441fc.tar.zst
gpt4free-41af8aff6ce09be4131d50e0e24b8622628441fc.zip
Diffstat (limited to 'g4f/Provider/helper.py')
-rw-r--r--g4f/Provider/helper.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py
new file mode 100644
index 00000000..e14ae65e
--- /dev/null
+++ b/g4f/Provider/helper.py
@@ -0,0 +1,54 @@
+from __future__ import annotations
+
+import asyncio
+import sys
+from asyncio import AbstractEventLoop
+
+import browser_cookie3
+
+_cookies: dict[str, dict[str, str]] = {}
+
+# Use own event_loop_policy with a selector event loop on windows.
+if sys.platform == 'win32':
+ _event_loop_policy = asyncio.WindowsSelectorEventLoopPolicy()
+else:
+ _event_loop_policy = asyncio.get_event_loop_policy()
+
+# If event loop is already running, handle nested event loops
+# If "nest_asyncio" is installed, patch the event loop.
+def get_event_loop() -> AbstractEventLoop:
+ try:
+ asyncio.get_running_loop()
+ except RuntimeError:
+ return _event_loop_policy.get_event_loop()
+ try:
+ event_loop = _event_loop_policy.get_event_loop()
+ if not hasattr(event_loop.__class__, "_nest_patched"):
+ import nest_asyncio
+ nest_asyncio.apply(event_loop)
+ return event_loop
+ except ImportError:
+ raise RuntimeError(
+ 'Use "create_async" instead of "create" function in a running event loop. Or install the "nest_asyncio" package.')
+
+# Load cookies for a domain from all supported browser.
+# Cache the results in the "_cookies" variable
+def get_cookies(cookie_domain: str) -> dict:
+ if cookie_domain not in _cookies:
+ _cookies[cookie_domain] = {}
+ try:
+ for cookie in browser_cookie3.load(cookie_domain):
+ _cookies[cookie_domain][cookie.name] = cookie.value
+ except:
+ pass
+ return _cookies[cookie_domain]
+
+
+def format_prompt(messages: list[dict[str, str]], add_special_tokens=False):
+ if add_special_tokens or len(messages) > 1:
+ formatted = "\n".join(
+ ["%s: %s" % ((message["role"]).capitalize(), message["content"]) for message in messages]
+ )
+ return f"{formatted}\nAssistant:"
+ else:
+ return messages[0]["content"] \ No newline at end of file