summaryrefslogtreecommitdiffstats
path: root/g4f/Provider
diff options
context:
space:
mode:
authormadonchik123 <68397448+madonchik123@users.noreply.github.com>2023-12-01 23:11:52 +0100
committerGitHub <noreply@github.com>2023-12-01 23:11:52 +0100
commitb0276f6c9e2925c65a00b7189bad92feb25cefbd (patch)
tree1909ac2fd081e2e8ad5b5dfa8ae46bd6fd58c579 /g4f/Provider
parent~ (diff)
downloadgpt4free-b0276f6c9e2925c65a00b7189bad92feb25cefbd.tar
gpt4free-b0276f6c9e2925c65a00b7189bad92feb25cefbd.tar.gz
gpt4free-b0276f6c9e2925c65a00b7189bad92feb25cefbd.tar.bz2
gpt4free-b0276f6c9e2925c65a00b7189bad92feb25cefbd.tar.lz
gpt4free-b0276f6c9e2925c65a00b7189bad92feb25cefbd.tar.xz
gpt4free-b0276f6c9e2925c65a00b7189bad92feb25cefbd.tar.zst
gpt4free-b0276f6c9e2925c65a00b7189bad92feb25cefbd.zip
Diffstat (limited to 'g4f/Provider')
-rw-r--r--g4f/Provider/PI.py111
-rw-r--r--g4f/Provider/__init__.py3
2 files changed, 113 insertions, 1 deletions
diff --git a/g4f/Provider/PI.py b/g4f/Provider/PI.py
new file mode 100644
index 00000000..1e2edde8
--- /dev/null
+++ b/g4f/Provider/PI.py
@@ -0,0 +1,111 @@
+from __future__ import annotations
+
+from ..typing import AsyncResult, Messages
+from .base_provider import AsyncGeneratorProvider
+
+import json
+import cloudscraper
+
+class PI(AsyncGeneratorProvider):
+ url = "https://chat-gpt.com"
+ working = True
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: Messages,
+ proxy: str = None,
+ **kwargs
+ ) -> AsyncResult:
+ Conversation = kwargs['conversation']
+ UserPrompt = messages[-1]
+ if UserPrompt['role'] == 'user':
+ UserPrompt = UserPrompt['content']
+ else:
+ UserPrompt = messages[-2]['content']
+ if Conversation == None:
+ Conversation = PI.Start_Conversation()
+ Answer = Ask_PI(UserPrompt,Conversation['sid'],Conversation['cookies'])
+
+ yield Answer[0]['text']
+
+ def Start_Conversation():
+ scraper.headers = {
+ 'accept-type': 'application/json'
+ }
+ response = scraper.post('https://pi.ai/api/chat/start', data="{}",headers={'x-api-version': '3'})
+ cookies = response.cookies
+
+ if 'Just a moment' in response.text:
+ return {
+ 'error': 'cloudflare detected',
+ 'sid': None,
+ 'cookies': None,
+ }
+ return {
+ 'sid': response.json()['conversations'][0]['sid'],
+ 'cookies': cookies
+ }
+
+ def GetConversationTitle(Conversation):
+ response = scraper.post('https://pi.ai/api/chat/start', data="{}",headers={'x-api-version': '3'}, cookies=Conversation['cookies'])
+ if 'Just a moment' in response.text:
+ return {
+ 'error': 'cloudflare detected',
+ 'title': 'Couldnt get the title',
+ }
+ return {
+ 'title': response.json()['conversations'][0]['title']
+ }
+
+ def GetChatHistory(Conversation):
+ params = {
+ 'conversation': Conversation['sid'],
+ }
+ response = scraper.get('https://pi.ai/api/chat/history', params=params, cookies=Conversation['cookies'])
+ if 'Just a moment' in response.text:
+ return {
+ 'error': 'cloudflare detected',
+ 'traceback': 'Couldnt get the chat history'
+ }
+ return response.json()
+
+session = cloudscraper.session()
+
+scraper = cloudscraper.create_scraper(
+ browser={
+ 'browser': 'chrome',
+ 'platform': 'windows',
+ 'desktop': True
+ },
+ sess=session
+)
+
+scraper.headers = {
+ 'Accept': '*/*',
+ 'Accept-Encoding': 'deflate,gzip,br',
+}
+
+def Ask_PI(message,sid,cookies):
+ json_data = {
+ 'text': message,
+ 'conversation': sid,
+ 'mode': 'BASE',
+ }
+ response = scraper.post('https://pi.ai/api/chat', json=json_data, cookies=cookies)
+
+ if 'Just a moment' in response.text:
+ return [{
+ 'error': 'cloudflare detected',
+ 'text': 'Couldnt generate the answer because we got detected by cloudflare please try again later'
+ }
+ ]
+ result = []
+ for line in response.iter_lines(chunk_size=1024, decode_unicode=True):
+ if line.startswith('data: {"text":'):
+ result.append(json.loads(line.split('data: ')[1].encode('utf-8')))
+ if line.startswith('data: {"title":'):
+ result.append(json.loads(line.split('data: ')[1].encode('utf-8')))
+
+ return result
diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py
index 2b47b071..199b4f27 100644
--- a/g4f/Provider/__init__.py
+++ b/g4f/Provider/__init__.py
@@ -70,4 +70,5 @@ __map__: dict[str, BaseProvider] = dict([
])
class ProviderUtils:
- convert: dict[str, BaseProvider] = __map__ \ No newline at end of file
+ convert: dict[str, BaseProvider] = __map__
+from .PI import PI \ No newline at end of file