From b0276f6c9e2925c65a00b7189bad92feb25cefbd Mon Sep 17 00:00:00 2001 From: madonchik123 <68397448+madonchik123@users.noreply.github.com> Date: Sat, 2 Dec 2023 01:11:52 +0300 Subject: Added new provider PI (Hacky way to use) (#1291) * Added new provider PI (Hacky way to use) * Updated models endpoint made it show real info about the model.py * Added cloudscraper to the requirements * Fixed some bugs aka made streaming also return role --- g4f/Provider/PI.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++ g4f/Provider/__init__.py | 3 +- 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 g4f/Provider/PI.py (limited to 'g4f/Provider') 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 -- cgit v1.2.3