XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Add PerplexityLabs Provider

ec2de56b
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +100 -2
Added g4f/Provider/PerplexityLabs.py +97 -0
@@ -0,0 +1,97 @@
1 from __future__ import annotations
2
3 import random
4 import json
5 from aiohttp import ClientSession, WSMsgType
6
7 from ..typing import AsyncResult, Messages
8 from .base_provider import AsyncGeneratorProvider
9
10 API_URL = "https://labs-api.perplexity.ai/socket.io/"
11 WS_URL = "wss://labs-api.perplexity.ai/socket.io/"
12 MODELS = ['pplx-7b-online', 'pplx-70b-online', 'pplx-7b-chat', 'pplx-70b-chat', 'mistral-7b-instruct',
13 'codellama-34b-instruct', 'llama-2-70b-chat', 'llava-7b-chat', 'mixtral-8x7b-instruct',
14 'mistral-medium', 'related']
15 DEFAULT_MODEL = MODELS[1]
16 MODEL_MAP = {
17 "mistralai/Mistral-7B-Instruct-v0.1": "mistral-7b-instruct",
18 "meta-llama/Llama-2-70b-chat-hf": "llama-2-70b-chat",
19 "mistralai/Mixtral-8x7B-Instruct-v0.1": "mixtral-8x7b-instruct",
20 }
21
22 class PerplexityLabs(AsyncGeneratorProvider):
23 url = "https://labs.perplexity.ai"
24 working = True
25 supports_gpt_35_turbo = True
26
27 @classmethod
28 async def create_async_generator(
29 cls,
30 model: str,
31 messages: Messages,
32 proxy: str = None,
33 **kwargs
34 ) -> AsyncResult:
35 if not model:
36 model = DEFAULT_MODEL
37 elif model in MODEL_MAP:
38 model = MODEL_MAP[model]
39 elif model not in MODELS:
40 raise ValueError(f"Model is not supported: {model}")
41 headers = {
42 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0",
43 "Accept": "*/*",
44 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
45 "Accept-Encoding": "gzip, deflate, br",
46 "Origin": cls.url,
47 "Connection": "keep-alive",
48 "Referer": f"{cls.url}/",
49 "Sec-Fetch-Dest": "empty",
50 "Sec-Fetch-Mode": "cors",
51 "Sec-Fetch-Site": "same-site",
52 "TE": "trailers",
53 }
54 async with ClientSession(headers=headers) as session:
55 t = format(random.getrandbits(32), '08x')
56 async with session.get(
57 f"{API_URL}?EIO=4&transport=polling&t={t}",
58 proxy=proxy
59 ) as response:
60 text = await response.text()
61
62 sid = json.loads(text[1:])['sid']
63 post_data = '40{"jwt":"anonymous-ask-user"}'
64 async with session.post(
65 f'{API_URL}?EIO=4&transport=polling&t={t}&sid={sid}',
66 data=post_data,
67 proxy=proxy
68 ) as response:
69 assert await response.text() == 'OK'
70
71 async with session.ws_connect(f'{WS_URL}?EIO=4&transport=websocket&sid={sid}', autoping=False) as ws:
72 await ws.send_str('2probe')
73 assert(await ws.receive_str() == '3probe')
74 await ws.send_str('5')
75 assert(await ws.receive_str())
76 assert(await ws.receive_str() == '6')
77 message_data = {
78 'version': '2.2',
79 'source': 'default',
80 'model': model,
81 'messages': messages
82 }
83 await ws.send_str('42' + json.dumps(['perplexity_playground', message_data]))
84 last_message = 0
85 while True:
86 message = await ws.receive_str()
87 if message == '2':
88 await ws.send_str('3')
89 continue
90 try:
91 data = json.loads(message[2:])[1]
92 yield data["output"][last_message:]
93 last_message = len(data["output"])
94 if data["final"]:
95 break
96 except:
97 raise RuntimeError(f"Message: {message}")
Modified g4f/Provider/__init__.py +3 -2
@@ -9,10 +9,11 @@ from .needs_auth import *
9 9 from .unfinished import *
10 10 from .selenium import *
11 11
12 from .Aura import Aura
13 12 from .AiAsk import AiAsk
14 13 from .AiChatOnline import AiChatOnline
15 14 from .AItianhu import AItianhu
15 from .Aura import Aura
16 from .Bestim import Bestim
16 17 from .Bing import Bing
17 18 from .ChatAnywhere import ChatAnywhere
18 19 from .ChatBase import ChatBase
@@ -45,12 +46,12 @@ from .Koala import Koala
45 46 from .Liaobots import Liaobots
46 47 from .Llama2 import Llama2
47 48 from .OnlineGpt import OnlineGpt
49 from .PerplexityLabs import PerplexityLabs
48 50 from .Phind import Phind
49 51 from .Pi import Pi
50 52 from .Vercel import Vercel
51 53 from .Ylokh import Ylokh
52 54 from .You import You
53 from .Bestim import Bestim
54 55
55 56 import sys
56 57