2023-12-02 01:56:12 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import json
|
2024-01-01 19:48:57 +03:00
|
|
|
|
|
|
|
from ..typing import CreateResult, Messages
|
|
|
|
from .base_provider import AbstractProvider, format_prompt
|
2024-03-12 04:06:06 +03:00
|
|
|
from ..requests import Session, get_session_from_browser, raise_for_status
|
2023-12-02 01:56:12 +03:00
|
|
|
|
2024-01-01 19:48:57 +03:00
|
|
|
class Pi(AbstractProvider):
|
2023-12-02 07:40:07 +03:00
|
|
|
url = "https://pi.ai/talk"
|
2023-12-02 01:56:12 +03:00
|
|
|
working = True
|
|
|
|
supports_stream = True
|
2024-03-12 04:06:06 +03:00
|
|
|
_session = None
|
2024-07-25 09:21:55 +03:00
|
|
|
default_model = "pi"
|
2023-12-02 01:56:12 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_completion(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
stream: bool,
|
|
|
|
proxy: str = None,
|
2023-12-02 07:40:07 +03:00
|
|
|
timeout: int = 180,
|
|
|
|
conversation_id: str = None,
|
2024-09-24 13:23:53 +03:00
|
|
|
webdriver: WebDriver = None,
|
2023-12-02 01:56:12 +03:00
|
|
|
**kwargs
|
|
|
|
) -> CreateResult:
|
2024-03-12 04:06:06 +03:00
|
|
|
if cls._session is None:
|
|
|
|
cls._session = get_session_from_browser(url=cls.url, proxy=proxy, timeout=timeout)
|
2023-12-02 07:40:07 +03:00
|
|
|
if not conversation_id:
|
2024-03-12 04:06:06 +03:00
|
|
|
conversation_id = cls.start_conversation(cls._session)
|
2023-12-06 15:47:22 +03:00
|
|
|
prompt = format_prompt(messages)
|
|
|
|
else:
|
|
|
|
prompt = messages[-1]["content"]
|
2024-03-12 04:06:06 +03:00
|
|
|
answer = cls.ask(cls._session, prompt, conversation_id)
|
2023-12-02 01:56:12 +03:00
|
|
|
for line in answer:
|
|
|
|
if "text" in line:
|
2023-12-02 02:34:05 +03:00
|
|
|
yield line["text"]
|
2023-12-02 07:40:07 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def start_conversation(cls, session: Session) -> str:
|
|
|
|
response = session.post('https://pi.ai/api/chat/start', data="{}", headers={
|
2023-12-02 01:56:12 +03:00
|
|
|
'accept': 'application/json',
|
|
|
|
'x-api-version': '3'
|
|
|
|
})
|
2024-03-12 04:06:06 +03:00
|
|
|
raise_for_status(response)
|
2023-12-02 07:40:07 +03:00
|
|
|
return response.json()['conversations'][0]['sid']
|
2023-12-02 01:56:12 +03:00
|
|
|
|
2023-12-02 07:40:07 +03:00
|
|
|
def get_chat_history(session: Session, conversation_id: str):
|
2023-12-02 01:56:12 +03:00
|
|
|
params = {
|
2023-12-02 07:40:07 +03:00
|
|
|
'conversation': conversation_id,
|
2023-12-02 01:56:12 +03:00
|
|
|
}
|
2023-12-02 07:40:07 +03:00
|
|
|
response = session.get('https://pi.ai/api/chat/history', params=params)
|
2024-03-12 04:06:06 +03:00
|
|
|
raise_for_status(response)
|
2023-12-02 01:56:12 +03:00
|
|
|
return response.json()
|
|
|
|
|
2023-12-06 15:47:22 +03:00
|
|
|
def ask(session: Session, prompt: str, conversation_id: str):
|
2023-12-02 01:56:12 +03:00
|
|
|
json_data = {
|
2023-12-06 15:47:22 +03:00
|
|
|
'text': prompt,
|
2023-12-02 07:40:07 +03:00
|
|
|
'conversation': conversation_id,
|
2023-12-02 01:56:12 +03:00
|
|
|
'mode': 'BASE',
|
|
|
|
}
|
2023-12-02 07:40:07 +03:00
|
|
|
response = session.post('https://pi.ai/api/chat', json=json_data, stream=True)
|
2024-03-12 04:06:06 +03:00
|
|
|
raise_for_status(response)
|
2023-12-02 07:40:07 +03:00
|
|
|
for line in response.iter_lines():
|
|
|
|
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])
|
2024-07-25 09:21:55 +03:00
|
|
|
|