2023-10-05 06:13:37 +03:00
|
|
|
|
|
|
|
import sys, re
|
|
|
|
from pathlib import Path
|
|
|
|
from os import path
|
|
|
|
|
2023-10-07 05:03:36 +03:00
|
|
|
sys.path.append(str(Path(__file__).parent.parent.parent))
|
2023-10-05 06:13:37 +03:00
|
|
|
|
|
|
|
import g4f
|
|
|
|
|
2023-11-12 20:03:04 +03:00
|
|
|
g4f.debug.logging = True
|
|
|
|
|
2023-10-05 06:13:37 +03:00
|
|
|
def read_code(text):
|
2023-10-23 10:46:25 +03:00
|
|
|
if match := re.search(r"```(python|py|)\n(?P<code>[\S\s]+?)\n```", text):
|
2023-10-05 06:13:37 +03:00
|
|
|
return match.group("code")
|
|
|
|
|
|
|
|
def input_command():
|
|
|
|
print("Enter/Paste the cURL command. Ctrl-D or Ctrl-Z ( windows ) to save it.")
|
|
|
|
contents = []
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
line = input()
|
2023-10-09 14:33:20 +03:00
|
|
|
except EOFError:
|
2023-10-05 06:13:37 +03:00
|
|
|
break
|
|
|
|
contents.append(line)
|
|
|
|
return "\n".join(contents)
|
|
|
|
|
|
|
|
name = input("Name: ")
|
|
|
|
provider_path = f"g4f/Provider/{name}.py"
|
|
|
|
|
|
|
|
example = """
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from aiohttp import ClientSession
|
|
|
|
|
2023-10-09 14:33:20 +03:00
|
|
|
from ..typing import AsyncResult, Messages
|
2023-10-05 06:13:37 +03:00
|
|
|
from .base_provider import AsyncGeneratorProvider
|
2023-10-09 14:33:20 +03:00
|
|
|
from .helper import format_prompt
|
2023-10-05 06:13:37 +03:00
|
|
|
|
|
|
|
|
2023-10-09 14:33:20 +03:00
|
|
|
class ChatGpt(AsyncGeneratorProvider):
|
2023-11-18 06:38:31 +03:00
|
|
|
url = "https://chat-gpt.com"
|
|
|
|
working = True
|
2023-10-05 06:13:37 +03:00
|
|
|
supports_gpt_35_turbo = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
2023-10-09 14:33:20 +03:00
|
|
|
messages: Messages,
|
|
|
|
proxy: str = None,
|
2023-10-05 06:13:37 +03:00
|
|
|
**kwargs
|
2023-10-09 14:33:20 +03:00
|
|
|
) -> AsyncResult:
|
2023-10-05 06:13:37 +03:00
|
|
|
headers = {
|
|
|
|
"authority": "chat-gpt.com",
|
|
|
|
"accept": "application/json",
|
|
|
|
"origin": cls.url,
|
|
|
|
"referer": f"{cls.url}/chat",
|
|
|
|
}
|
|
|
|
async with ClientSession(headers=headers) as session:
|
2023-10-09 14:33:20 +03:00
|
|
|
prompt = format_prompt(messages)
|
2023-10-05 06:13:37 +03:00
|
|
|
data = {
|
|
|
|
"prompt": prompt,
|
2023-10-09 14:33:20 +03:00
|
|
|
"purpose": "",
|
2023-10-05 06:13:37 +03:00
|
|
|
}
|
2023-10-09 14:33:20 +03:00
|
|
|
async with session.post(f"{cls.url}/api/chat", json=data, proxy=proxy) as response:
|
2023-10-05 06:13:37 +03:00
|
|
|
response.raise_for_status()
|
2023-10-09 14:33:20 +03:00
|
|
|
async for chunk in response.content:
|
|
|
|
if chunk:
|
|
|
|
yield chunk.decode()
|
2023-10-05 06:13:37 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
if not path.isfile(provider_path):
|
|
|
|
command = input_command()
|
|
|
|
|
|
|
|
prompt = f"""
|
|
|
|
Create a provider from a cURL command. The command is:
|
|
|
|
```bash
|
|
|
|
{command}
|
|
|
|
```
|
|
|
|
A example for a provider:
|
|
|
|
```py
|
|
|
|
{example}
|
|
|
|
```
|
|
|
|
The name for the provider class:
|
|
|
|
{name}
|
|
|
|
Replace "hello" with `format_prompt(messages)`.
|
|
|
|
And replace "gpt-3.5-turbo" with `model`.
|
|
|
|
"""
|
|
|
|
|
2023-10-10 10:49:29 +03:00
|
|
|
print("Create code...")
|
2023-10-09 14:33:20 +03:00
|
|
|
response = []
|
|
|
|
for chunk in g4f.ChatCompletion.create(
|
2023-10-05 06:13:37 +03:00
|
|
|
model=g4f.models.gpt_35_long,
|
|
|
|
messages=[{"role": "user", "content": prompt}],
|
2023-10-09 14:33:20 +03:00
|
|
|
timeout=300,
|
2023-10-10 10:49:29 +03:00
|
|
|
stream=True,
|
2023-10-09 14:33:20 +03:00
|
|
|
):
|
|
|
|
print(chunk, end="", flush=True)
|
2023-10-10 10:49:29 +03:00
|
|
|
response.append(chunk)
|
2023-10-09 14:33:20 +03:00
|
|
|
print()
|
|
|
|
response = "".join(response)
|
|
|
|
|
2023-10-23 10:46:25 +03:00
|
|
|
if code := read_code(response):
|
2023-10-05 06:13:37 +03:00
|
|
|
with open(provider_path, "w") as file:
|
|
|
|
file.write(code)
|
2023-10-09 14:33:20 +03:00
|
|
|
print("Saved at:", provider_path)
|
2023-10-23 10:46:25 +03:00
|
|
|
with open("g4f/Provider/__init__.py", "a") as file:
|
2023-10-05 06:13:37 +03:00
|
|
|
file.write(f"\nfrom .{name} import {name}")
|
|
|
|
else:
|
|
|
|
with open(provider_path, "r") as file:
|
2023-10-12 16:49:07 +03:00
|
|
|
code = file.read()
|