2023-12-01 22:56:12 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from ..typing import CreateResult, Messages
|
|
|
|
from .base_provider import BaseProvider, format_prompt
|
|
|
|
|
|
|
|
import json
|
2023-12-02 04:40:07 +00:00
|
|
|
from ..requests import Session, get_session_from_browser
|
2023-12-01 22:56:12 +00:00
|
|
|
|
|
|
|
class Pi(BaseProvider):
|
2023-12-02 04:40:07 +00:00
|
|
|
url = "https://pi.ai/talk"
|
2023-12-01 22:56:12 +00:00
|
|
|
working = True
|
|
|
|
supports_stream = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_completion(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
stream: bool,
|
2023-12-02 04:40:07 +00:00
|
|
|
session: Session = None,
|
2023-12-01 22:56:12 +00:00
|
|
|
proxy: str = None,
|
2023-12-02 04:40:07 +00:00
|
|
|
timeout: int = 180,
|
|
|
|
conversation_id: str = None,
|
2023-12-01 22:56:12 +00:00
|
|
|
**kwargs
|
|
|
|
) -> CreateResult:
|
2023-12-02 04:40:07 +00:00
|
|
|
if not session:
|
|
|
|
session = get_session_from_browser(url=cls.url, proxy=proxy, timeout=timeout)
|
|
|
|
if not conversation_id:
|
|
|
|
conversation_id = cls.start_conversation(session)
|
|
|
|
answer = cls.ask(session, messages, conversation_id)
|
2023-12-01 22:56:12 +00:00
|
|
|
for line in answer:
|
|
|
|
if "text" in line:
|
2023-12-01 23:34:05 +00:00
|
|
|
yield line["text"]
|
2023-12-02 04:40:07 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def start_conversation(cls, session: Session) -> str:
|
|
|
|
response = session.post('https://pi.ai/api/chat/start', data="{}", headers={
|
2023-12-01 22:56:12 +00:00
|
|
|
'accept': 'application/json',
|
|
|
|
'x-api-version': '3'
|
|
|
|
})
|
|
|
|
if 'Just a moment' in response.text:
|
|
|
|
raise RuntimeError('Error: Cloudflare detected')
|
2023-12-02 04:40:07 +00:00
|
|
|
return response.json()['conversations'][0]['sid']
|
2023-12-01 22:56:12 +00:00
|
|
|
|
2023-12-02 04:40:07 +00:00
|
|
|
def get_chat_history(session: Session, conversation_id: str):
|
2023-12-01 22:56:12 +00:00
|
|
|
params = {
|
2023-12-02 04:40:07 +00:00
|
|
|
'conversation': conversation_id,
|
2023-12-01 22:56:12 +00:00
|
|
|
}
|
2023-12-02 04:40:07 +00:00
|
|
|
response = session.get('https://pi.ai/api/chat/history', params=params)
|
2023-12-01 22:56:12 +00:00
|
|
|
if 'Just a moment' in response.text:
|
|
|
|
raise RuntimeError('Error: Cloudflare detected')
|
|
|
|
return response.json()
|
|
|
|
|
2023-12-02 04:40:07 +00:00
|
|
|
def ask(session: Session, messages: Messages, conversation_id: str):
|
2023-12-01 22:56:12 +00:00
|
|
|
json_data = {
|
|
|
|
'text': format_prompt(messages),
|
2023-12-02 04:40:07 +00:00
|
|
|
'conversation': conversation_id,
|
2023-12-01 22:56:12 +00:00
|
|
|
'mode': 'BASE',
|
|
|
|
}
|
2023-12-02 04:40:07 +00:00
|
|
|
response = session.post('https://pi.ai/api/chat', json=json_data, stream=True)
|
|
|
|
for line in response.iter_lines():
|
|
|
|
if b'Just a moment' in line:
|
2023-12-01 22:56:12 +00:00
|
|
|
raise RuntimeError('Error: Cloudflare detected')
|
2023-12-02 04:40:07 +00:00
|
|
|
if line.startswith(b'data: {"text":'):
|
|
|
|
yield json.loads(line.split(b'data: ')[1])
|
|
|
|
elif line.startswith(b'data: {"title":'):
|
|
|
|
yield json.loads(line.split(b'data: ')[1])
|
2023-12-01 22:56:12 +00:00
|
|
|
|