summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/Pi.py
blob: 385f477b37cdd4102b5d15caefadc694e2811985 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from __future__ import annotations

from ..typing import CreateResult, Messages
from .base_provider import BaseProvider, format_prompt

import json
from ..requests import Session, get_session_from_browser

class Pi(BaseProvider):
    url             = "https://pi.ai/talk"
    working         = True
    supports_stream = True

    @classmethod
    def create_completion(
        cls,
        model: str,
        messages: Messages,
        stream: bool,
        session: Session = None,
        proxy: str = None,
        timeout: int = 180,
        conversation_id: str = None,
        **kwargs
    ) -> CreateResult:
        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)
        for line in answer:
            if "text" in line:
                yield line["text"]
    
    @classmethod
    def start_conversation(cls, session: Session) -> str:
        response = session.post('https://pi.ai/api/chat/start', data="{}", headers={
            'accept': 'application/json',
            'x-api-version': '3'
        })
        if 'Just a moment' in response.text:
            raise RuntimeError('Error: Cloudflare detected')
        return response.json()['conversations'][0]['sid']
        
    def get_chat_history(session: Session, conversation_id: str):
        params = {
            'conversation': conversation_id,
        }
        response = session.get('https://pi.ai/api/chat/history', params=params)
        if 'Just a moment' in response.text:
            raise RuntimeError('Error: Cloudflare detected')
        return response.json()

    def ask(session: Session, messages: Messages, conversation_id: str):
        json_data = {
            'text': format_prompt(messages),
            'conversation': conversation_id,
            'mode': 'BASE',
        }
        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:
                raise RuntimeError('Error: Cloudflare detected')
            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])