diff options
Diffstat (limited to 'etc/tool')
-rw-r--r-- | etc/tool/create_provider.py | 54 |
1 files changed, 25 insertions, 29 deletions
diff --git a/etc/tool/create_provider.py b/etc/tool/create_provider.py index 4e3d7b02..26212f6a 100644 --- a/etc/tool/create_provider.py +++ b/etc/tool/create_provider.py @@ -12,23 +12,13 @@ def read_code(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: + except EOFError: break contents.append(line) return "\n".join(contents) @@ -41,12 +31,12 @@ from __future__ import annotations from aiohttp import ClientSession -from ..typing import AsyncGenerator +from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider -from .helper import format_prompt +from .helper import format_prompt -class ChatgptDuo(AsyncGeneratorProvider): +class ChatGpt(AsyncGeneratorProvider): url = "https://chat-gpt.com" supports_gpt_35_turbo = True working = True @@ -55,9 +45,10 @@ class ChatgptDuo(AsyncGeneratorProvider): async def create_async_generator( cls, model: str, - messages: list[dict[str, str]], + messages: Messages, + proxy: str = None, **kwargs - ) -> AsyncGenerator: + ) -> AsyncResult: headers = { "authority": "chat-gpt.com", "accept": "application/json", @@ -65,16 +56,16 @@ class ChatgptDuo(AsyncGeneratorProvider): "referer": f"{cls.url}/chat", } async with ClientSession(headers=headers) as session: - prompt = format_prompt(messages), + prompt = format_prompt(messages) data = { "prompt": prompt, - "purpose": "ask", + "purpose": "", } - async with session.post(cls.url + "/api/chat", json=data) as response: + async with session.post(f"{cls.url}/api/chat", json=data, proxy=proxy) as response: response.raise_for_status() - async for stream in response.content: - if stream: - yield stream.decode() + async for chunk in response.content: + if chunk: + yield chunk.decode() """ if not path.isfile(provider_path): @@ -95,18 +86,23 @@ Replace "hello" with `format_prompt(messages)`. And replace "gpt-3.5-turbo" with `model`. """ - print("Create code...") - response = g4f.ChatCompletion.create( + response = [] + for chunk in 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) + timeout=300, + stream=True + ): + response.append(chunk) + print(chunk, end="", flush=True) + print() + response = "".join(response) + + code = read_code(response) if code: with open(provider_path, "w") as file: file.write(code) + print("Saved at:", provider_path) with open(f"g4f/Provider/__init__.py", "a") as file: file.write(f"\nfrom .{name} import {name}") else: |