Merge pull request #1301 from xtekky/go

Add proxy support / fix streaming
This commit is contained in:
H Lohaus 2023-12-02 00:35:41 +01:00 committed by GitHub
commit 89a56d0c34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,31 +23,29 @@ class Pi(BaseProvider):
**kwargs **kwargs
) -> CreateResult: ) -> CreateResult:
if not scraper: if not scraper:
scraper = cls.get_scraper() scraper = cls.get_scraper(proxy)
if not conversation: if not conversation:
conversation = cls.start_conversation(scraper) conversation = cls.start_conversation(scraper)
answer = cls.ask(scraper, messages, conversation) answer = cls.ask(scraper, messages, conversation)
last_answer = 0
for line in answer: for line in answer:
if "text" in line: if "text" in line:
yield line["text"][last_answer:] yield line["text"]
last_answer = len(line["text"])
def get_scraper(): def get_scraper(proxy: str):
scraper = create_scraper( return create_scraper(
browser={ browser={
'browser': 'chrome', 'browser': 'chrome',
'platform': 'windows', 'platform': 'windows',
'desktop': True 'desktop': True
}, },
sess=session() headers={
)
scraper.headers = {
'Accept': '*/*', 'Accept': '*/*',
'Accept-Encoding': 'deflate,gzip,br', 'Accept-Encoding': 'deflate,gzip,br',
},
proxies={
"https": proxy
} }
return scraper )
def start_conversation(scraper: CloudScraper): def start_conversation(scraper: CloudScraper):
response = scraper.post('https://pi.ai/api/chat/start', data="{}", headers={ response = scraper.post('https://pi.ai/api/chat/start', data="{}", headers={