summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/PI.py
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2023-12-01 23:56:12 +0100
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2023-12-01 23:56:12 +0100
commite74af803e7add7790b366545f1fe6c80934de798 (patch)
tree4cdd527acfd2e5514a3ca638c136a0c5d59faf04 /g4f/Provider/PI.py
parentAdded new provider PI (Hacky way to use) (#1291) (diff)
downloadgpt4free-e74af803e7add7790b366545f1fe6c80934de798.tar
gpt4free-e74af803e7add7790b366545f1fe6c80934de798.tar.gz
gpt4free-e74af803e7add7790b366545f1fe6c80934de798.tar.bz2
gpt4free-e74af803e7add7790b366545f1fe6c80934de798.tar.lz
gpt4free-e74af803e7add7790b366545f1fe6c80934de798.tar.xz
gpt4free-e74af803e7add7790b366545f1fe6c80934de798.tar.zst
gpt4free-e74af803e7add7790b366545f1fe6c80934de798.zip
Diffstat (limited to '')
-rw-r--r--g4f/Provider/PI.py111
1 files changed, 0 insertions, 111 deletions
diff --git a/g4f/Provider/PI.py b/g4f/Provider/PI.py
deleted file mode 100644
index 1e2edde8..00000000
--- a/g4f/Provider/PI.py
+++ /dev/null
@@ -1,111 +0,0 @@
-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