summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/Aichat.py
diff options
context:
space:
mode:
authorHeiner Lohaus <heiner.lohaus@netformic.com>2023-09-05 17:27:24 +0200
committerHeiner Lohaus <heiner.lohaus@netformic.com>2023-09-05 17:27:24 +0200
commit5ca47b44b2b42abb4f48163c17500b5ee67ab28f (patch)
treeb8fba4bde73d59c05857459eac41b25347d65c8e /g4f/Provider/Aichat.py
parent~ | Merge pull request #869 from ahobsonsayers/add-console-script (diff)
downloadgpt4free-5ca47b44b2b42abb4f48163c17500b5ee67ab28f.tar
gpt4free-5ca47b44b2b42abb4f48163c17500b5ee67ab28f.tar.gz
gpt4free-5ca47b44b2b42abb4f48163c17500b5ee67ab28f.tar.bz2
gpt4free-5ca47b44b2b42abb4f48163c17500b5ee67ab28f.tar.lz
gpt4free-5ca47b44b2b42abb4f48163c17500b5ee67ab28f.tar.xz
gpt4free-5ca47b44b2b42abb4f48163c17500b5ee67ab28f.tar.zst
gpt4free-5ca47b44b2b42abb4f48163c17500b5ee67ab28f.zip
Diffstat (limited to 'g4f/Provider/Aichat.py')
-rw-r--r--g4f/Provider/Aichat.py55
1 files changed, 27 insertions, 28 deletions
diff --git a/g4f/Provider/Aichat.py b/g4f/Provider/Aichat.py
index 59640533..8edd17e2 100644
--- a/g4f/Provider/Aichat.py
+++ b/g4f/Provider/Aichat.py
@@ -1,25 +1,22 @@
from __future__ import annotations
-import requests
+from aiohttp import ClientSession
-from ..typing import Any, CreateResult
-from .base_provider import BaseProvider
+from .base_provider import AsyncProvider, format_prompt
-class Aichat(BaseProvider):
+class Aichat(AsyncProvider):
url = "https://chat-gpt.org/chat"
working = True
supports_gpt_35_turbo = True
@staticmethod
- def create_completion(
+ async def create_async(
model: str,
messages: list[dict[str, str]],
- stream: bool, **kwargs: Any) -> CreateResult:
-
- chat = "\n".join(f"{message['role']}: {message['content']}" for message in messages)
- chat += "\nassistant: "
-
+ proxy: str = None,
+ **kwargs
+ ) -> str:
headers = {
"authority": "chat-gpt.org",
"accept": "*/*",
@@ -35,21 +32,23 @@ class Aichat(BaseProvider):
"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/113.0.0.0 Safari/537.36",
}
-
- json_data = {
- "message": base,
- "temperature": kwargs.get('temperature', 0.5),
- "presence_penalty": 0,
- "top_p": kwargs.get('top_p', 1),
- "frequency_penalty": 0,
- }
-
- response = requests.post(
- "https://chat-gpt.org/api/text",
- headers=headers,
- json=json_data,
- )
- response.raise_for_status()
- if not response.json()['response']:
- raise Exception("Error Response: " + response.json())
- yield response.json()["message"]
+ async with ClientSession(
+ headers=headers
+ ) as session:
+ json_data = {
+ "message": format_prompt(messages),
+ "temperature": kwargs.get('temperature', 0.5),
+ "presence_penalty": 0,
+ "top_p": kwargs.get('top_p', 1),
+ "frequency_penalty": 0,
+ }
+ async with session.post(
+ "https://chat-gpt.org/api/text",
+ proxy=proxy,
+ json=json_data
+ ) as response:
+ response.raise_for_status()
+ result = await response.json()
+ if not result['response']:
+ raise Exception(f"Error Response: {result}")
+ return result["message"]