summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/OpenAssistant.py
diff options
context:
space:
mode:
authorTekky <98614666+xtekky@users.noreply.github.com>2023-08-28 22:08:23 +0200
committerGitHub <noreply@github.com>2023-08-28 22:08:23 +0200
commit7e687b3d178c00a27d7e5ae2613fe88ee7844639 (patch)
tree4034e8ae9fc7ca9af295f04358bb00516b464e0b /g4f/Provider/OpenAssistant.py
parentMerge pull request #851 from Luneye/patch-1 (diff)
parentMerge branch 'main' into hugging (diff)
downloadgpt4free-7e687b3d178c00a27d7e5ae2613fe88ee7844639.tar
gpt4free-7e687b3d178c00a27d7e5ae2613fe88ee7844639.tar.gz
gpt4free-7e687b3d178c00a27d7e5ae2613fe88ee7844639.tar.bz2
gpt4free-7e687b3d178c00a27d7e5ae2613fe88ee7844639.tar.lz
gpt4free-7e687b3d178c00a27d7e5ae2613fe88ee7844639.tar.xz
gpt4free-7e687b3d178c00a27d7e5ae2613fe88ee7844639.tar.zst
gpt4free-7e687b3d178c00a27d7e5ae2613fe88ee7844639.zip
Diffstat (limited to 'g4f/Provider/OpenAssistant.py')
-rw-r--r--g4f/Provider/OpenAssistant.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/g4f/Provider/OpenAssistant.py b/g4f/Provider/OpenAssistant.py
new file mode 100644
index 00000000..542af78f
--- /dev/null
+++ b/g4f/Provider/OpenAssistant.py
@@ -0,0 +1,98 @@
+import json
+from aiohttp import ClientSession
+
+from ..typing import Any, AsyncGenerator
+from .base_provider import AsyncGeneratorProvider, get_cookies, format_prompt
+
+class OpenAssistant(AsyncGeneratorProvider):
+ url = "https://open-assistant.io/chat"
+ needs_auth = True
+ working = True
+ model = "OA_SFT_Llama_30B_6"
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: list[dict[str, str]],
+ proxy: str = None,
+ cookies: dict = None,
+ **kwargs: Any
+ ) -> AsyncGenerator:
+ if proxy and "://" not in proxy:
+ proxy = f"http://{proxy}"
+ if not cookies:
+ cookies = get_cookies("open-assistant.io")
+
+ headers = {
+ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
+ }
+ async with ClientSession(
+ cookies=cookies,
+ headers=headers
+ ) as session:
+ async with session.post("https://open-assistant.io/api/chat", proxy=proxy) as response:
+ chat_id = (await response.json())["id"]
+
+ data = {
+ "chat_id": chat_id,
+ "content": f"<s>[INST]\n{format_prompt(messages)}\n[/INST]",
+ "parent_id": None
+ }
+ async with session.post("https://open-assistant.io/api/chat/prompter_message", proxy=proxy, json=data) as response:
+ parent_id = (await response.json())["id"]
+
+ data = {
+ "chat_id": chat_id,
+ "parent_id": parent_id,
+ "model_config_name": model if model else cls.model,
+ "sampling_parameters":{
+ "top_k": 50,
+ "top_p": None,
+ "typical_p": None,
+ "temperature": 0.35,
+ "repetition_penalty": 1.1111111111111112,
+ "max_new_tokens": 1024,
+ **kwargs
+ },
+ "plugins":[]
+ }
+ async with session.post("https://open-assistant.io/api/chat/assistant_message", proxy=proxy, json=data) as response:
+ data = await response.json()
+ if "id" in data:
+ message_id = data["id"]
+ elif "message" in data:
+ raise RuntimeError(data["message"])
+ else:
+ response.raise_for_status()
+
+ params = {
+ 'chat_id': chat_id,
+ 'message_id': message_id,
+ }
+ async with session.post("https://open-assistant.io/api/chat/events", proxy=proxy, params=params) as response:
+ start = "data: "
+ async for line in response.content:
+ line = line.decode("utf-8")
+ if line and line.startswith(start):
+ line = json.loads(line[len(start):])
+ if line["event_type"] == "token":
+ yield line["text"]
+
+ params = {
+ 'chat_id': chat_id,
+ }
+ async with session.delete("https://open-assistant.io/api/chat", proxy=proxy, params=params) as response:
+ response.raise_for_status()
+
+ @classmethod
+ @property
+ def params(cls):
+ params = [
+ ("model", "str"),
+ ("messages", "list[dict[str, str]]"),
+ ("stream", "bool"),
+ ("proxy", "str"),
+ ]
+ param = ", ".join([": ".join(p) for p in params])
+ return f"g4f.provider.{cls.__name__} supports: ({param})"