Skip to content

Commit 43a958e

Browse files
committed
fix bug
1 parent 27db677 commit 43a958e

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

api/GrokAI.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ class GrokAI:
4242
# 检查 NewChat 和 ChatID 是否都有值
4343
def __init__(self,GrokAccount: GrokAccount):
4444
self.GrokAccount = GrokAccount
45+
self.Console = Console()
4546
obj = self.GrokAccount
4647

4748
has_new_chat = obj.NewChat # 检查 NewChat 是否为 True
4849
has_chat_id = obj.ChatID is not None # 检查 ChatID 是否非 None
4950
if has_new_chat and has_chat_id:
5051
raise ValueError("NewChat 和 ChatID 不能同时为 True 和非 None")
5152

52-
def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True, imageGenerationCount: int = 2, isReasoning: bool = False):
53+
def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True, imageGenerationCount: int = 2, isReasoning: bool = False,test: bool = False):
5354
self.cookies = self.GrokAccount.cookies
5455
self.message = self.GrokAccount.message
5556
self.modelName = self.GrokAccount.modelName
@@ -85,26 +86,49 @@ def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True,
8586
'disableTextFollowUps': True
8687
}
8788

88-
console = Console()
89+
console = self.Console
90+
# 创建 cloudscraper 实例
8991
scraper = cloudscraper.create_scraper()
9092
response = scraper.post(self.url, headers=self.headers, cookies=self.cookies, json=self.data, stream=True)
93+
if test is True:
94+
# 這是測試代碼!!!!
95+
console.print(f"Request URL: {self.url}", style="bold blue")
96+
console.print(f"Headers: {self.headers}", style="bold blue")
97+
console.print(f"Cookies: {self.cookies}", style="bold blue")
98+
console.print(f"Data: {self.data}", style="bold blue")
99+
91100

92101
# 檢查狀態碼
93102
if response.status_code == 200:
94103
for line in response.iter_lines():
95104
if line:
96105
decoded_line = line.decode('utf-8')
97106
try:
107+
# 解析 JSON
98108
json_data = json.loads(decoded_line)
99-
token = json_data.get('result', {}).get('token', '')
100-
if token: # 只打印非空 token
109+
110+
# 提取 token
111+
token = json_data.get("result", {}).get("response", {}).get("token", "")
112+
113+
# 如果 token 存在,逐字輸出
114+
if token:
101115
print_by_char_rich(console, token, delay=0.05)
116+
102117
except json.JSONDecodeError:
103118
console.print("解析 JSON 失败", style="bold red")
104-
continue
105119
else:
106120
console.print("请求失败", style="bold red")
107121
console.print(response.text)
122+
'''
123+
for line in response.iter_lines():
124+
if line:
125+
decoded_line = line.decode('utf-8')
126+
try:
127+
json_data = json.loads(decoded_line)
128+
print(json_data)
129+
except json.JSONDecodeError:
130+
print("解析 JSON 失败", style="bold red")
131+
continue'''
108132

109133
'''
110134
@@ -203,5 +227,4 @@ def Chat(self, disableSearch: bool = False, enableImageGeneration: bool = True,
203227
else:
204228
console.print("请求失败", style="bold red")
205229
console.print(response.text)
206-
207230
'''

0 commit comments

Comments
 (0)