def on_submit(self): prompt = self.prompt_edit.toPlainText() question = self.question_edit.toPlainText() api_url = "https://api.devbit.cn/ai/api/ai/chat" api_key = "ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSVV6STFOaUo5LmV5SjBkQ0k2TkN3aVlYVmtJam9pTlRNeE5XTmpZekV3TlRnME5HSTNNVGcyWmpoa09XRTVOVFppTnpGbU5HWWlMQ0pzZFNJNkltZGhiMmhoYjNkbGJpSXNJbVY0Y0NJNk1UWTROVFUwT0RjNU9Td2lkWElpT2pJc0ltcDBhU0k2SWtGUVNWOVVUMHRGVGw4MU16RTFZMk5qTVRBMU9EUTBZamN4T0RabU9HUTVZVGsxTm1JM01XWTBaaTAwSW4wLjM0WWhudzVpblNRVTZROWpkZ0RxRHlCQk1GM2xmN01HYTZBcXZJMV9ydjA=" headers = { "Authorization": "Bearer "+api_key, "Accept": "application/json" } payload = { "prompt": prompt, "question": question, "cid": self.cid, "stream": False } response = requests.post(api_url, json=payload, headers=headers) if response.status_code == 200: print(response.json()) json_response = response.json() self.cid = str(json_response['data']['cid']) self.json_response_edit.setText(str(json_response['data']['answer'])) else: print("Request failed with error code {reaponse.status_code}") self.json_response_edit.setText(f"请求失败,错误码 {response.status_code}")怎么在点击提交按钮时,根据复选框状态判定cid是否回传?
时间: 2024-04-27 14:22:19 浏览: 186
你可以在`payload`中添加一个`cid`字段,根据复选框的状态来判断是否传入`cid`。假设你的复选框是`self.cid_checkbox`,那么你可以这样修改`on_submit`方法:
```
def on_submit(self):
prompt = self.prompt_edit.toPlainText()
question = self.question_edit.toPlainText()
api_url = "https://api.devbit.cn/ai/api/ai/chat"
api_key = "ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSVV6STFOaUo5LmV5SjBkQ0k2TkN3aVlYVmtJam9pTlRNeE5XTmpZekV3TlRnME5HSTNNVGcyWmpoa09XRTVOVFppTnpGbU5HWWlMQ0pzZFNJNkltZGhiMmhoYjNkbGJpSXNJbVY0Y0NJNk1UWTROVFUwT0RjNU9Td2lkWElpT2pJc0ltcDBhU0k2SWtGUVNWOVVUMHRGVGw4MU16RTFZMk5qTVRBMU9EUTBZamN4T0RabU9HUTVZVGsxTm1JM01XWTBaaTAwSW4wLjM0WWhudzVpblNRVTZROWpkZ0RxRHlCQk1GM2xmN01HYTZBcXZJMV9ydjA="
headers = {
"Authorization": "Bearer " + api_key,
"Accept": "application/json"
}
payload = {
"prompt": prompt,
"question": question,
"stream": False
}
# 根据复选框状态判断是否传入cid
if self.cid_checkbox.isChecked():
payload["cid"] = self.cid
response = requests.post(api_url, json=payload, headers=headers)
if response.status_code == 200:
print(response.json())
json_response = response.json()
self.cid = str(json_response['data']['cid'])
self.json_response_edit.setText(str(json_response['data']['answer']))
else:
print(f"Request failed with error code {response.status_code}")
self.json_response_edit.setText(f"请求失败,错误码 {response.status_code}")
```
这样,如果`self.cid_checkbox`被选中,就会在`payload`中传入`cid`;否则不传入`cid`。
阅读全文