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-02-15 08:27:26 浏览: 145
message-prompt-window.zip_message. prompt
你可以在 `payload` 中加入一个键值对 `stream`,如果复选框被勾选,则值为 True,否则为 False。然后在发送请求时,只有当 `stream` 的值为 True 时才把 `cid` 回传到服务器。
修改后的代码可以如下所示:
```python
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"
}
stream = self.checkbox.isChecked()
payload = {
"prompt": prompt,
"question": question,
"stream": stream
}
if stream:
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()
if stream:
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` 才会被回传。
阅读全文