diff options
Diffstat (limited to '')
-rw-r--r-- | etc/tool/create_provider.py | 114 | ||||
-rw-r--r-- | etc/tool/improve_code.py | 47 | ||||
-rw-r--r-- | etc/tool/provider_init.py (renamed from tool/provider_init.py) | 0 | ||||
-rw-r--r-- | etc/tool/readme_table.py (renamed from tool/readme_table.py) | 0 | ||||
-rw-r--r-- | etc/tool/vercel.py (renamed from tool/vercel.py) | 2 |
5 files changed, 162 insertions, 1 deletions
diff --git a/etc/tool/create_provider.py b/etc/tool/create_provider.py new file mode 100644 index 00000000..4e3d7b02 --- /dev/null +++ b/etc/tool/create_provider.py @@ -0,0 +1,114 @@ + +import sys, re +from pathlib import Path +from os import path + +sys.path.append(str(Path(__file__).parent.parent.parent)) + +import g4f + +def read_code(text): + match = re.search(r"```(python|py|)\n(?P<code>[\S\s]+?)\n```", text) + if match: + return match.group("code") + +def read_result(result): + lines = [] + for line in result.split("\n"): + if (line.startswith("```")): + break + if (line): + lines.append(line) + explanation = "\n".join(lines) if lines else "" + return explanation, read_code(result) + +def input_command(): + print("Enter/Paste the cURL command. Ctrl-D or Ctrl-Z ( windows ) to save it.") + contents = [] + while True: + try: + line = input() + except: + 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 + +from ..typing import AsyncGenerator +from .base_provider import AsyncGeneratorProvider +from .helper import format_prompt + + +class ChatgptDuo(AsyncGeneratorProvider): + url = "https://chat-gpt.com" + supports_gpt_35_turbo = True + working = True + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: list[dict[str, str]], + **kwargs + ) -> AsyncGenerator: + headers = { + "authority": "chat-gpt.com", + "accept": "application/json", + "origin": cls.url, + "referer": f"{cls.url}/chat", + } + async with ClientSession(headers=headers) as session: + prompt = format_prompt(messages), + data = { + "prompt": prompt, + "purpose": "ask", + } + async with session.post(cls.url + "/api/chat", json=data) as response: + response.raise_for_status() + async for stream in response.content: + if stream: + yield stream.decode() +""" + +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`. +""" + + print("Create code...") + response = g4f.ChatCompletion.create( + model=g4f.models.gpt_35_long, + messages=[{"role": "user", "content": prompt}], + auth=True, + timeout=120, + ) + print(response) + explanation, code = read_result(response) + if code: + with open(provider_path, "w") as file: + file.write(code) + with open(f"g4f/Provider/__init__.py", "a") as file: + file.write(f"\nfrom .{name} import {name}") +else: + with open(provider_path, "r") as file: + code = file.read() diff --git a/etc/tool/improve_code.py b/etc/tool/improve_code.py new file mode 100644 index 00000000..9a940b51 --- /dev/null +++ b/etc/tool/improve_code.py @@ -0,0 +1,47 @@ + +import sys, re +from pathlib import Path +from os import path + +sys.path.append(str(Path(__file__).parent.parent.parent)) + +import g4f + +def read_code(text): + match = re.search(r"```(python|py|)\n(?P<code>[\S\s]+?)\n```", text) + if match: + return match.group("code") + +path = input("Path: ") + +with open(path, "r") as file: + code = file.read() + +prompt = f""" +Improve the code in this file: +```py +{code} +``` +Don't remove anything. +Add typehints if possible. +Don't add any typehints to kwargs. +Don't remove license comments. +""" + +print("Create code...") +response = [] +for chunk in g4f.ChatCompletion.create( + model=g4f.models.gpt_35_long, + messages=[{"role": "user", "content": prompt}], + timeout=300, + stream=True +): + response.append(chunk) + print(chunk, end="", flush=True) +print() +response = "".join(response) + +code = read_code(response) +if code: + with open(path, "w") as file: + file.write(code)
\ No newline at end of file diff --git a/tool/provider_init.py b/etc/tool/provider_init.py index 22f21d4d..22f21d4d 100644 --- a/tool/provider_init.py +++ b/etc/tool/provider_init.py diff --git a/tool/readme_table.py b/etc/tool/readme_table.py index b5b64cb1..b5b64cb1 100644 --- a/tool/readme_table.py +++ b/etc/tool/readme_table.py diff --git a/tool/vercel.py b/etc/tool/vercel.py index 7b87e298..75df6c70 100644 --- a/tool/vercel.py +++ b/etc/tool/vercel.py @@ -100,4 +100,4 @@ def main(): if __name__ == "__main__": - main() + main()
\ No newline at end of file |