Skip to content

Commit

Permalink
fix: support all yiyan api
Browse files Browse the repository at this point in the history
  • Loading branch information
lss233 committed Apr 20, 2023
1 parent 17a8234 commit 21d2ce1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
38 changes: 30 additions & 8 deletions adapter/baidu/yiyan.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ def __init__(self, session_id: str = ""):
super().__init__(session_id)
self.session_id = session_id
self.account = botManager.pick('yiyan-cookie')
self.acs_client = httpx.AsyncClient(proxies=self.account.proxy)
self.client = httpx.AsyncClient(proxies=self.account.proxy)
self.__setup_headers()
self.__setup_headers(self.acs_client)
self.__setup_headers(self.client)
self.conversation_id = None
self.parent_chat_id = ''

Expand All @@ -60,10 +62,16 @@ async def on_reset(self):
self.conversation_id = None
self.parent_chat_id = 0

def __setup_headers(self):
self.client.headers['Cookie'] = self.account.cookie_content
self.client.headers['Content-Type'] = 'application/json;charset=UTF-8'
self.client.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36'
def __setup_headers(self, client):
client.headers['Cookie'] = f"BDUSS={self.account.BDUSS};"
client.headers['Content-Type'] = 'application/json;charset=UTF-8'
client.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
client.headers['Sec-Fetch-User'] = '?1'
client.headers['Sec-Fetch-Mode'] = 'navigate'
client.headers['Sec-Fetch-Site'] = 'none'
client.headers['Sec-Ch-Ua-Platform'] = '"Windows"'
client.headers['Sec-Ch-Ua-Mobile'] = ' ?0'
client.headers['Sec-Ch-Ua'] = '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"'

async def new_conversation(self, prompt: str):
self.client.headers['Acs-Token'] = await self.get_sign()
Expand All @@ -87,6 +95,18 @@ async def ask(self, prompt) -> Generator[str, None, None]:
if not self.conversation_id:
await self.new_conversation(prompt)

req = await self.client.post(
url="https://yiyan.baidu.com/eb/chat/query",
json={
"text": prompt,
"timestamp": get_ts(),
"deviceType": "pc",
}
)

req.raise_for_status()
self.__check_response(req.json())

req = await self.client.post(
url="https://yiyan.baidu.com/eb/chat/new",
json={
Expand All @@ -98,7 +118,7 @@ async def ask(self, prompt) -> Generator[str, None, None]:
"deviceType": "pc",
"code": 0,
"msg": "",
"sign": await self.get_sign()
"sign": self.client.headers['Acs-Token']
}
)

Expand All @@ -123,7 +143,7 @@ async def ask(self, prompt) -> Generator[str, None, None]:
"stop": 0,
"timestamp": get_ts(),
"deviceType": "pc",
"sign": await self.get_sign()
"sign": self.client.headers['Acs-Token']
}
)
req.raise_for_status()
Expand Down Expand Up @@ -162,7 +182,9 @@ def __check_response(self, resp):
raise Exception(resp['msg'])

async def get_sign(self):
req = await self.client.get("https://chatgpt-proxy.lss233.com/yiyan-api/acs")
# 目前只需要这一个参数来计算 Acs-Token
self.acs_client.headers['Cookie'] = f"BAIDUID={self.account.BAIDUID};"
req = await self.acs_client.get("https://chatgpt-proxy.lss233.com/yiyan-api/acs")
return req.json()['acs']

async def __download_image(self, url: str) -> bytes:
Expand Down
8 changes: 6 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,12 @@ class BardAuths(BaseModel):


class YiyanCookiePath(BaseModel):
cookie_content: str
""""文心一言网站的 Cookie 内容"""
BDUSS: Optional[str] = None
"""百度 Cookie 中的 BDUSS 字段"""
BAIDUID: Optional[str] = None
"""百度 Cookie 中的 BAIDUID 字段"""
cookie_content: Optional[str] = None
"""百度 Cookie (已弃用)"""
proxy: Optional[str] = None
"""可选的代理地址,留空则检测系统代理"""

Expand Down
11 changes: 11 additions & 0 deletions manager/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import OpenAIAuth
import openai
import regex
import requests
import urllib3.exceptions
from aiohttp import ClientConnectorError
Expand Down Expand Up @@ -203,6 +204,16 @@ def login_yiyan(self):
if proxy := self.__check_proxy(account.proxy):
account.proxy = proxy
try:
if account.cookie_content:
logger.error("cookie_content 字段已弃用,请填写 BDUSS 和 BAIDUID!")
account.BDUSS = regex.findall(r"BDUSS=(.*?);", account.cookie_content)
account.BAIDUID = regex.findall(r"BAIDUID=(.*?);", account.cookie_content)
if not account.BAIDUID:
logger.error("未填写 BAIDUID,可能会有较高封号风险!")
if not account.BDUSS:
logger.error("未填写 BDUSS,无法使用!")
assert account.BDUSS

self.bots["yiyan-cookie"].append(account)
logger.success("解析成功!", i=i + 1)
except Exception as e:
Expand Down

0 comments on commit 21d2ce1

Please sign in to comment.