Skip to content

Commit

Permalink
v1.5.0 - обновление g4f, бета версия chatgpt-агента
Browse files Browse the repository at this point in the history
  • Loading branch information
neurogen-dev committed Sep 25, 2023
1 parent fdbadad commit b8a90eb
Show file tree
Hide file tree
Showing 39 changed files with 1,141 additions and 769 deletions.
1 change: 1 addition & 0 deletions backend/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

app = Flask(__name__)
CORS(app)

LOG = logging.getLogger(__name__)
embedding_proc = embedding_processing()

Expand Down
2 changes: 1 addition & 1 deletion backend/embedding_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def embedding(self, text_list):
response_embedding = self.transform_embedding_to_dict(embeddings_list,text_list)
return response_embedding

def transform_embedding_to_dict(self, embedding_list, text_list, model_name="text-embedding-elmo-002"):
def transform_embedding_to_dict(self, embedding_list, text_list, model_name="text-embedding-ada-002"):
prompt_tokens = sum(len(text) for text in text_list)
total_tokens = sum(len(embedding) for embedding in embedding_list)

Expand Down
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openai_api_key": "",
"purgpt_api_key": "",
"chatty_api_key": "",
"daku_api_key": "",
"daku_api_key": "sk-xxxxxxxxxxxxxxxxxxxxxxxx1",
"usage_limit": 999,
"language": "ru_RU",
"users": [],
Expand Down
59 changes: 18 additions & 41 deletions g4f/Provider/AItianhu.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,38 @@
from __future__ import annotations

import json
from aiohttp import ClientSession, http
from curl_cffi.requests import AsyncSession

from ..typing import AsyncGenerator
from .base_provider import AsyncGeneratorProvider, format_prompt
from .base_provider import AsyncProvider, format_prompt


class AItianhu(AsyncGeneratorProvider):
class AItianhu(AsyncProvider):
url = "https://www.aitianhu.com"
working = True
supports_gpt_35_turbo = True

@classmethod
async def create_async_generator(
async def create_async(
cls,
model: str,
messages: list[dict[str, str]],
proxy: str = None,
**kwargs
) -> AsyncGenerator:
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "de,en-US;q=0.7,en;q=0.3",
"Content-Type": "application/json",
"Origin": cls.url,
"Connection": "keep-alive",
"Referer": cls.url + "/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
) -> str:
data = {
"prompt": format_prompt(messages),
"options": {},
"systemMessage": "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully.",
"temperature": 0.8,
"top_p": 1,
**kwargs
}
async with ClientSession(
headers=headers,
version=http.HttpVersion10
) as session:
data = {
"prompt": format_prompt(messages),
"options": {},
"systemMessage": "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully.",
"temperature": 0.8,
"top_p": 1,
**kwargs
}
async with session.post(
cls.url + "/api/chat-process",
proxy=proxy,
json=data,
ssl=False,
) as response:
response.raise_for_status()
async for line in response.content:
line = json.loads(line.decode('utf-8'))
token = line["detail"]["choices"][0]["delta"].get("content")
if token:
yield token
async with AsyncSession(proxies={"https": proxy}, impersonate="chrome107", verify=False) as session:
response = await session.post(cls.url + "/api/chat-process", json=data)
response.raise_for_status()
line = response.text.splitlines()[-1]
line = json.loads(line)
return line["text"]


@classmethod
Expand Down
4 changes: 2 additions & 2 deletions g4f/Provider/Acytoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def _create_header():
}


def _create_payload(messages: list[dict[str, str]], temperature: float = 0.5, **kwargs):
def _create_payload(messages: list[dict[str, str]], model: str, temperature: float = 0.5, **kwargs):
return {
'key' : '',
'model' : 'gpt-3.5-turbo',
'model' : model,
'messages' : messages,
'temperature' : temperature,
'password' : ''
Expand Down
107 changes: 53 additions & 54 deletions g4f/Provider/Aivvm.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,77 @@
from __future__ import annotations
import requests

from aiohttp import ClientSession

from .base_provider import AsyncGeneratorProvider
from ..typing import AsyncGenerator
from .base_provider import BaseProvider
from ..typing import CreateResult

models = {
"gpt-4": {
"id": "gpt-4",
"name": "GPT-4",
},
"gpt-3.5-turbo": {
"id": "gpt-3.5-turbo",
"name": "GPT-3.5",
},
"gpt-3.5-turbo-16k": {
"id": "gpt-3.5-turbo-16k",
"name": "GPT-3.5-16k",
},
'gpt-3.5-turbo': {'id': 'gpt-3.5-turbo', 'name': 'GPT-3.5'},
'gpt-3.5-turbo-0613': {'id': 'gpt-3.5-turbo-0613', 'name': 'GPT-3.5-0613'},
'gpt-3.5-turbo-16k': {'id': 'gpt-3.5-turbo-16k', 'name': 'GPT-3.5-16K'},
'gpt-3.5-turbo-16k-0613': {'id': 'gpt-3.5-turbo-16k-0613', 'name': 'GPT-3.5-16K-0613'},
'gpt-4': {'id': 'gpt-4', 'name': 'GPT-4'},
'gpt-4-0613': {'id': 'gpt-4-0613', 'name': 'GPT-4-0613'},
'gpt-4-32k': {'id': 'gpt-4-32k', 'name': 'GPT-4-32K'},
'gpt-4-32k-0613': {'id': 'gpt-4-32k-0613', 'name': 'GPT-4-32K-0613'},
}

class Aivvm(AsyncGeneratorProvider):
url = "https://chat.aivvm.com"
class Aivvm(BaseProvider):
url = 'https://chat.aivvm.com'
supports_stream = True
working = True
supports_gpt_35_turbo = True
supports_gpt_4 = True


@classmethod
async def create_async_generator(
cls,
def create_completion(cls,
model: str,
messages: list[dict[str, str]],
proxy: str = None,
stream: bool,
**kwargs
) -> AsyncGenerator:
model = model if model else "gpt-3.5-turbo"
if model not in models:
) -> CreateResult:
if not model:
model = "gpt-3.5-turbo"
elif model not in models:
raise ValueError(f"Model are not supported: {model}")

headers = {
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
"Accept" : "*/*",
"Accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
"Origin" : cls.url,
"Referer" : cls.url + "/",
"Sec-Fetch-Dest" : "empty",
"Sec-Fetch-Mode" : "cors",
"Sec-Fetch-Site" : "same-origin",
"authority" : "chat.aivvm.com",
"accept" : "*/*",
"accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
"content-type" : "application/json",
"origin" : "https://chat.aivvm.com",
"referer" : "https://chat.aivvm.com/",
"sec-ch-ua" : '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
"sec-ch-ua-mobile" : "?0",
"sec-ch-ua-platform" : '"macOS"',
"sec-fetch-dest" : "empty",
"sec-fetch-mode" : "cors",
"sec-fetch-site" : "same-origin",
"user-agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
}
async with ClientSession(
headers=headers
) as session:
data = {
"temperature": 1,
"key": "",
"messages": messages,
"model": models[model],
"prompt": "",
**kwargs
}
async with session.post(cls.url + "/api/chat", json=data, proxy=proxy) as response:
response.raise_for_status()
async for stream in response.content.iter_any():
yield stream.decode()

json_data = {
"model" : models[model],
"messages" : messages,
"key" : "",
"prompt" : "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.",
"temperature" : kwargs.get("temperature", 0.7)
}

response = requests.post(
"https://chat.aivvm.com/api/chat", headers=headers, json=json_data, stream=True)

for line in response.iter_content(chunk_size=1048):
yield line.decode('utf-8')

@classmethod
@property
def params(cls):
params = [
("model", "str"),
("messages", "list[dict[str, str]]"),
("stream", "bool"),
("temperature", "float"),
('model', 'str'),
('messages', 'list[dict[str, str]]'),
('stream', 'bool'),
('temperature', 'float'),
]
param = ", ".join([": ".join(p) for p in params])
return f"g4f.provider.{cls.__name__} supports: ({param})"
param = ', '.join([': '.join(p) for p in params])
return f'g4f.provider.{cls.__name__} supports: ({param})'
17 changes: 9 additions & 8 deletions g4f/Provider/Bard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Bard(AsyncProvider):
url = "https://bard.google.com"
needs_auth = True
working = True
_snlm0e = None

@classmethod
async def create_async(
Expand All @@ -31,7 +32,6 @@ async def create_async(

headers = {
'authority': 'bard.google.com',
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
'origin': 'https://bard.google.com',
'referer': 'https://bard.google.com/',
'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',
Expand All @@ -42,13 +42,14 @@ async def create_async(
cookies=cookies,
headers=headers
) as session:
async with session.get(cls.url, proxy=proxy) as response:
text = await response.text()
if not cls._snlm0e:
async with session.get(cls.url, proxy=proxy) as response:
text = await response.text()

match = re.search(r'SNlM0e\":\"(.*?)\"', text)
if not match:
raise RuntimeError("No snlm0e value.")
snlm0e = match.group(1)
match = re.search(r'SNlM0e\":\"(.*?)\"', text)
if not match:
raise RuntimeError("No snlm0e value.")
cls._snlm0e = match.group(1)

params = {
'bl': 'boq_assistant-bard-web-server_20230326.21_p0',
Expand All @@ -57,7 +58,7 @@ async def create_async(
}

data = {
'at': snlm0e,
'at': cls._snlm0e,
'f.req': json.dumps([None, json.dumps([[prompt]])])
}

Expand Down
9 changes: 8 additions & 1 deletion g4f/Provider/ChatgptLogin.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ async def create_async(
}
async with session.post("https://opchatgpts.net/wp-admin/admin-ajax.php", data=data) as response:
response.raise_for_status()
return (await response.json())["data"]
data = await response.json()
if "data" in data:
return data["data"]
elif "msg" in data:
raise RuntimeError(data["msg"])
else:
raise RuntimeError(f"Response: {data}")


@classmethod
@property
Expand Down
7 changes: 4 additions & 3 deletions g4f/Provider/CodeLinkAva.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ async def create_async_generator(
}
async with session.post("https://ava-alpha-api.codelink.io/api/chat", json=data) as response:
response.raise_for_status()
start = "data: "
async for line in response.content:
line = line.decode()
if line.startswith("data: ") and not line.startswith("data: [DONE]"):
line = json.loads(line[len(start):-1])
if line.startswith("data: "):
if line.startswith("data: [DONE]"):
break
line = json.loads(line[6:-1])
content = line["choices"][0]["delta"].get("content")
if content:
yield content
Expand Down
2 changes: 1 addition & 1 deletion g4f/Provider/DeepAi.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def create_async_generator(
async with ClientSession(
headers=headers
) as session:
async with session.post("https://api.deepai.org/make_me_a_pizza", proxy=proxy, data=payload) as response:
async with session.post("https://api.deepai.org/make_me_a_sandwich", proxy=proxy, data=payload) as response:
response.raise_for_status()
async for stream in response.content.iter_any():
if stream:
Expand Down
2 changes: 1 addition & 1 deletion g4f/Provider/EasyChat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class EasyChat(BaseProvider):
url: str = "https://free.easychat.work"
supports_stream = True
supports_gpt_35_turbo = True
working = True
working = False

@staticmethod
def create_completion(
Expand Down
6 changes: 3 additions & 3 deletions g4f/Provider/Equing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import requests

from ..typing import Any, CreateResult
from .base_provider import BaseProvider


class Equing(ABC):
class Equing(BaseProvider):
url: str = 'https://next.eqing.tech/'
working = True
needs_auth = False
working = False
supports_stream = True
supports_gpt_35_turbo = True
supports_gpt_4 = False
Expand Down
2 changes: 1 addition & 1 deletion g4f/Provider/GetGpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class GetGpt(BaseProvider):
url = 'https://chat.getgpt.world/'
supports_stream = True
working = True
working = False
supports_gpt_35_turbo = True

@staticmethod
Expand Down
Loading

0 comments on commit b8a90eb

Please sign in to comment.