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

XFEstudio/gpt4free

Update g4f/models.py. Fix g4f/Provider/GPROChat.py

1d676e3f
kqlio67 <kqlio67@users.noreply.github.com>
提交于

代码差异

2 个文件 +48 -56
Modified g4f/Provider/GPROChat.py +46 -55
@@ -1,76 +1,67 @@
1 1 from __future__ import annotations
2
2 import hashlib
3 3 import time
4 from hashlib import sha256
5
6 from aiohttp import BaseConnector, ClientSession
7
8 from ..errors import RateLimitError
9 from ..requests import raise_for_status
10 from ..requests.aiohttp import get_connector
4 from aiohttp import ClientSession
11 5 from ..typing import AsyncResult, Messages
12 6 from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
13
7 from .helper import format_prompt
14 8
15 9 class GPROChat(AsyncGeneratorProvider, ProviderModelMixin):
16 url = "https://gprochat.com/"
10 label = "GPROChat"
11 url = "https://gprochat.com"
12 api_endpoint = "https://gprochat.com/api/generate"
17 13 working = True
18 14 supports_stream = True
19 15 supports_message_history = True
20 16 default_model = 'gemini-pro'
21 17
18 @staticmethod
19 def generate_signature(timestamp: int, message: str) -> str:
20 secret_key = "2BC120D4-BB36-1B60-26DE-DB630472A3D8"
21 hash_input = f"{timestamp}:{message}:{secret_key}"
22 signature = hashlib.sha256(hash_input.encode('utf-8')).hexdigest()
23 return signature
24
25 @classmethod
26 def get_model(cls, model: str) -> str:
27 if model in cls.models:
28 return model
29 elif model in cls.model_aliases:
30 return cls.model_aliases[model]
31 else:
32 return cls.default_model
33
22 34 @classmethod
23 35 async def create_async_generator(
24 36 cls,
25 37 model: str,
26 38 messages: Messages,
27 39 proxy: str = None,
28 connector: BaseConnector = None,
29 **kwargs,
40 **kwargs
30 41 ) -> AsyncResult:
42 model = cls.get_model(model)
43 timestamp = int(time.time() * 1000)
44 prompt = format_prompt(messages)
45 sign = cls.generate_signature(timestamp, prompt)
46
31 47 headers = {
32 "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0",
33 "Accept": "*/*",
34 "Accept-Language": "en-US,en;q=0.5",
35 "Accept-Encoding": "gzip, deflate, br",
36 "Content-Type": "text/plain;charset=UTF-8",
37 "Referer": f"{cls.url}/",
38 "Origin": cls.url,
39 "Sec-Fetch-Dest": "empty",
40 "Sec-Fetch-Mode": "cors",
41 "Sec-Fetch-Site": "same-origin",
42 "Connection": "keep-alive",
43 "TE": "trailers",
48 "accept": "*/*",
49 "origin": cls.url,
50 "referer": f"{cls.url}/",
51 "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
52 "content-type": "text/plain;charset=UTF-8"
53 }
54
55 data = {
56 "messages": [{"role": "user", "parts": [{"text": prompt}]}],
57 "time": timestamp,
58 "pass": None,
59 "sign": sign
44 60 }
45 async with ClientSession(
46 connector=get_connector(connector, proxy), headers=headers
47 ) as session:
48 timestamp = int(time.time() * 1e3)
49 data = {
50 "messages": [
51 {
52 "role": "model" if message["role"] == "assistant" else "user",
53 "parts": [{"text": message["content"]}],
54 }
55 for message in messages
56 ],
57 "time": timestamp,
58 "pass": None,
59 "sign": generate_signature(timestamp, messages[-1]["content"]),
60 }
61 async with session.post(
62 f"{cls.url}/api/generate", json=data, proxy=proxy
63 ) as response:
64 if response.status == 500:
65 if "Quota exceeded" in await response.text():
66 raise RateLimitError(
67 f"Response {response.status}: Rate limit reached"
68 )
69 await raise_for_status(response)
70 async for chunk in response.content.iter_any():
71 yield chunk.decode(errors="ignore")
72
73 61
74 def generate_signature(time: int, text: str, secret: str = ""):
75 message = f"{time}:{text}:{secret}"
76 return sha256(message.encode()).hexdigest()
62 async with ClientSession(headers=headers) as session:
63 async with session.post(cls.api_endpoint, json=data, proxy=proxy) as response:
64 response.raise_for_status()
65 async for chunk in response.content.iter_any():
66 if chunk:
67 yield chunk.decode()
Modified g4f/models.py +2 -1
@@ -28,6 +28,7 @@ from .Provider import (
28 28 Gemini,
29 29 GeminiPro,
30 30 GigaChat,
31 GPROChat,
31 32 HuggingChat,
32 33 HuggingFace,
33 34 Koala,
@@ -272,7 +273,7 @@ phi_3_5_mini = Model(
272 273 gemini_pro = Model(
273 274 name = 'gemini-pro',
274 275 base_provider = 'Google DeepMind',
275 best_provider = IterListProvider([GeminiPro, LiteIcoding, Blackbox, AIChatFree, Liaobots, Airforce])
276 best_provider = IterListProvider([GeminiPro, LiteIcoding, Blackbox, AIChatFree, GPROChat, Liaobots, Airforce])
276 277 )
277 278
278 279 gemini_flash = Model(