From 7483a7c310d581c6012ed51607b6b57b3cab8018 Mon Sep 17 00:00:00 2001 From: kqlio67 Date: Wed, 4 Sep 2024 02:09:29 +0300 Subject: Adding new providers AiChats.py Nexra.py Snova.py --- g4f/Provider/AiChats.py | 66 +++++++++++++++++++++++ g4f/Provider/Nexra.py | 111 +++++++++++++++++++++++++++++++++++++++ g4f/Provider/Snova.py | 133 +++++++++++++++++++++++++++++++++++++++++++++++ g4f/Provider/__init__.py | 3 ++ 4 files changed, 313 insertions(+) create mode 100644 g4f/Provider/AiChats.py create mode 100644 g4f/Provider/Nexra.py create mode 100644 g4f/Provider/Snova.py (limited to 'g4f/Provider') diff --git a/g4f/Provider/AiChats.py b/g4f/Provider/AiChats.py new file mode 100644 index 00000000..f3eb3fc0 --- /dev/null +++ b/g4f/Provider/AiChats.py @@ -0,0 +1,66 @@ +from __future__ import annotations + +from aiohttp import ClientSession +import json + +from ..typing import AsyncResult, Messages +from .base_provider import AsyncGeneratorProvider +from .helper import format_prompt + + +class AiChats(AsyncGeneratorProvider): + url = "https://ai-chats.org" + api_endpoint = "https://ai-chats.org/chat/send2/" + working = True + supports_gpt_4 = True + supports_message_history = True + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: Messages, + proxy: str = None, + **kwargs + ) -> AsyncResult: + + headers = { + "accept": "application/json, text/event-stream", + "accept-language": "en-US,en;q=0.9", + "cache-control": "no-cache", + "content-type": "application/json", + "origin": cls.url, + "pragma": "no-cache", + "referer": f"{cls.url}/chat/", + "sec-ch-ua": '"Chromium";v="127", "Not)A;Brand";v="99"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": '"Linux"', + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "same-origin", + "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36", + } + + async with ClientSession(headers=headers) as session: + prompt = format_prompt(messages) + data = { + "type": "chat", + "messagesHistory": [ + { + "from": "you", + "content": prompt + } + ] + } + async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response: + response.raise_for_status() + full_response = await response.text() + + message = "" + for line in full_response.split('\n'): + if line.startswith('data: ') and line != 'data: ': + message += line[6:] + + message = message.strip() + + yield message diff --git a/g4f/Provider/Nexra.py b/g4f/Provider/Nexra.py new file mode 100644 index 00000000..4914b930 --- /dev/null +++ b/g4f/Provider/Nexra.py @@ -0,0 +1,111 @@ +from __future__ import annotations + +import json +from aiohttp import ClientSession + +from ..typing import AsyncResult, Messages +from .base_provider import AsyncGeneratorProvider, ProviderModelMixin +from .helper import format_prompt + + +class Nexra(AsyncGeneratorProvider, ProviderModelMixin): + url = "https://nexra.aryahcr.cc" + api_endpoint = "https://nexra.aryahcr.cc/api/chat/gpt" + working = True + supports_gpt_35_turbo = True + supports_gpt_4 = True + supports_stream = True + supports_system_message = True + supports_message_history = True + + default_model = 'gpt-3.5-turbo' + models = [ + # Working with text + 'gpt-4', + 'gpt-4-0613', + 'gpt-4-32k', + 'gpt-4-0314', + 'gpt-4-32k-0314', + + 'gpt-3.5-turbo', + 'gpt-3.5-turbo-16k', + 'gpt-3.5-turbo-0613', + 'gpt-3.5-turbo-16k-0613', + 'gpt-3.5-turbo-0301', + + 'gpt-3', + 'text-davinci-003', + 'text-davinci-002', + 'code-davinci-002', + 'text-curie-001', + 'text-babbage-001', + 'text-ada-001', + 'davinci', + 'curie', + 'babbage', + 'ada', + 'babbage-002', + 'davinci-002', + ] + + model_aliases = { + "gpt-4": "gpt-4-0613", + "gpt-4": "gpt-4-32k", + "gpt-4": "gpt-4-0314", + "gpt-4": "gpt-4-32k-0314", + + "gpt-3.5-turbo": "gpt-3.5-turbo-16k", + "gpt-3.5-turbo": "gpt-3.5-turbo-0613", + "gpt-3.5-turbo": "gpt-3.5-turbo-16k-0613", + "gpt-3.5-turbo": "gpt-3.5-turbo-0301", + + "gpt-3": "text-davinci-003", + "gpt-3": "text-davinci-002", + "gpt-3": "code-davinci-002", + "gpt-3": "text-curie-001", + "gpt-3": "text-babbage-001", + "gpt-3": "text-ada-001", + "gpt-3": "text-ada-001", + "gpt-3": "davinci", + "gpt-3": "curie", + "gpt-3": "babbage", + "gpt-3": "ada", + "gpt-3": "babbage-002", + "gpt-3": "davinci-002", + } + + @classmethod + def get_model(cls, model: str) -> str: + if model in cls.models: + return model + elif model in cls.model_aliases: + return cls.model_aliases[model] + else: + return cls.default_model + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: Messages, + proxy: str = None, + **kwargs + ) -> AsyncResult: + model = cls.get_model(model) + + headers = { + "Content-Type": "application/json", + } + async with ClientSession(headers=headers) as session: + data = { + "messages": messages, + "prompt": format_prompt(messages), + "model": model, + "markdown": False, + "stream": False, + } + async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response: + response.raise_for_status() + result = await response.text() + json_result = json.loads(result) + yield json_result["gpt"] diff --git a/g4f/Provider/Snova.py b/g4f/Provider/Snova.py new file mode 100644 index 00000000..76dfac40 --- /dev/null +++ b/g4f/Provider/Snova.py @@ -0,0 +1,133 @@ +from __future__ import annotations + +import json +from typing import AsyncGenerator + +from aiohttp import ClientSession + +from ..typing import AsyncResult, Messages +from .base_provider import AsyncGeneratorProvider, ProviderModelMixin +from .helper import format_prompt + + +class Snova(AsyncGeneratorProvider, ProviderModelMixin): + url = "https://fast.snova.ai" + api_endpoint = "https://fast.snova.ai/api/completion" + working = True + supports_stream = True + supports_system_message = True + supports_message_history = True + + default_model = 'Meta-Llama-3.1-8B-Instruct' + models = [ + 'Meta-Llama-3.1-8B-Instruct', + 'Meta-Llama-3.1-70B-Instruct', + 'Meta-Llama-3.1-405B-Instruct', + 'Samba-CoE', + 'ignos/Mistral-T5-7B-v1', + 'v1olet/v1olet_merged_dpo_7B', + 'macadeliccc/WestLake-7B-v2-laser-truthy-dpo', + 'cookinai/DonutLM-v1', + ] + + model_aliases = { + "llama-3.1-8b": "Meta-Llama-3.1-8B-Instruct", + "llama-3.1-70b": "Meta-Llama-3.1-70B-Instruct", + "llama-3.1-405b": "Meta-Llama-3.1-405B-Instruct", + + "mistral-7b": "ignos/Mistral-T5-7B-v1", + + "samba-coe-v0.1": "Samba-CoE", + "v1olet-merged-7b": "v1olet/v1olet_merged_dpo_7B", + "westlake-7b-v2": "macadeliccc/WestLake-7B-v2-laser-truthy-dpo", + "donutlm-v1": "cookinai/DonutLM-v1", + } + + @classmethod + def get_model(cls, model: str) -> str: + if model in cls.models: + return model + elif model in cls.model_aliases: + return cls.model_aliases[model] + else: + return cls.default_model + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: Messages, + proxy: str = None, + **kwargs + ) -> AsyncGenerator[str, None]: + model = cls.get_model(model) + + headers = { + "accept": "text/event-stream", + "accept-language": "en-US,en;q=0.9", + "cache-control": "no-cache", + "content-type": "application/json", + "origin": cls.url, + "pragma": "no-cache", + "priority": "u=1, i", + "referer": f"{cls.url}/", + "sec-ch-ua": '"Chromium";v="127", "Not)A;Brand";v="99"', + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": '"Linux"', + "sec-fetch-dest": "empty", + "sec-fetch-mode": "cors", + "sec-fetch-site": "same-origin", + "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" + } + async with ClientSession(headers=headers) as session: + data = { + "body": { + "messages": [ + { + "role": "system", + "content": "You are a helpful assistant." + }, + { + "role": "user", + "content": format_prompt(messages), + "id": "1-id", + "ref": "1-ref", + "revision": 1, + "draft": False, + "status": "done", + "enableRealTimeChat": False, + "meta": None + } + ], + "max_tokens": 1000, + "stop": ["<|eot_id|>"], + "stream": True, + "stream_options": {"include_usage": True}, + "model": model + }, + "env_type": "tp16" + } + async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response: + response.raise_for_status() + full_response = "" + async for line in response.content: + line = line.decode().strip() + if line.startswith("data: "): + data = line[6:] + if data == "[DONE]": + break + try: + json_data = json.loads(data) + choices = json_data.get("choices", []) + if choices: + delta = choices[0].get("delta", {}) + content = delta.get("content", "") + full_response += content + except json.JSONDecodeError: + continue + except Exception as e: + print(f"Error processing chunk: {e}") + print(f"Problematic data: {data}") + continue + + yield full_response.strip() diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py index 83d3e6e1..10499fdf 100644 --- a/g4f/Provider/__init__.py +++ b/g4f/Provider/__init__.py @@ -12,6 +12,7 @@ from .needs_auth import * from .AI365VIP import AI365VIP from .Allyfy import Allyfy from .AiChatOnline import AiChatOnline +from .AiChats import AiChats from .Aura import Aura from .Bing import Bing from .BingCreateImages import BingCreateImages @@ -42,11 +43,13 @@ from .MagickPenAsk import MagickPenAsk from .MagickPenChat import MagickPenChat from .MetaAI import MetaAI from .MetaAIAccount import MetaAIAccount +from .Nexra import Nexra from .Ollama import Ollama from .PerplexityLabs import PerplexityLabs from .Pi import Pi from .Pizzagpt import Pizzagpt from .Reka import Reka +from .Snova import Snova from .Replicate import Replicate from .ReplicateHome import ReplicateHome from .Rocks import Rocks -- cgit v1.2.3