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 } 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("Request failed with error code {reaponse.status_code}") self.json_response_edit.setText(f"请求失败,错误码 {response.status_code}")这段代码运行后提示{'data': None, 'code': 403, 'msg': '会话失效!'} Traceback (most recent call last): File "e:\python\工程文件\Untitled-6.py", line 141, in on_submit self.cid = str(json_response['data']['cid']) TypeError: 'NoneType' object is not subscriptable怎么解决?
时间: 2024-02-14 18:08:40 浏览: 81
根据提示信息,代码运行后返回的是 {'data': None, 'code': 403, 'msg': '会话失效!'},即会话失效。因此,无法通过 ['data']['cid'] 获取到值,导致出现 TypeError: 'NoneType' object is not subscriptable 错误。
解决方法是重新生成一个新的会话,或者检查之前生成的会话是否过期。可以尝试通过修改 payload 中的参数来重新生成会话,例如增加一个随机数参数,绕过之前的会话。另外,也可以尝试联系接口提供方,寻求帮助。
相关问题
def generatorAnswer(self, prompt: str, history: List[List[str]] = [], streaming: bool = False): if streaming: history += [[]] for inum, (stream_resp, _) in enumerate(self.checkPoint.model.stream_chat( self.checkPoint.tokenizer, prompt, history=history[-self.history_len:-1] if self.history_len > 0 else [], max_length=self.max_token, temperature=self.temperature )): # self.checkPoint.clear_torch_cache() history[-1] = [prompt, stream_resp] answer_result = AnswerResult() answer_result.history = history answer_result.llm_output = {"answer": stream_resp} yield answer_result else: response, _ = self.checkPoint.model.chat( self.checkPoint.tokenizer, prompt, history=history[-self.history_len:] if self.history_len > 0 else [], max_length=self.max_token, temperature=self.temperature ) self.checkPoint.clear_torch_cache() history += [[prompt, response]] answer_result = AnswerResult() answer_result.history = history answer_result.llm_output = {"answer": response} yield answer_result
这段代码是用来生成AI对话回答的,其中包括以下参数:
- prompt: 代表AI的输入,即对话的开端
- history: 代表之前的对话历史,可以为空
- streaming: 是否使用流式对话,即实时回答对话
如果使用流式对话,该函数会在对话过程中不断生成回答;如果不使用流式对话,该函数会在输入完整的对话后一次性生成回答。该函数返回的是一个生成器,每次调用生成一个回答,其中包含了对话历史和AI的回答结果。
def initUI(self): self.setWindowTitle(self.title) screen = QApplication.primaryScreen() size = screen.size() self.setGeometry((size.width() - self.width) // 2, (size.height() - self.height) // 2, self.width, self.height) # Prompt Label and Edit box prompt_label = QLabel('对象:', self) prompt_label.setStyleSheet('color: #222; font-size: 30px; margin-bottom: 10px; font-weight: bold;') self.prompt_edit = QTextEdit(self) self.prompt_edit.setStyleSheet('color: #333; font-size: 24px; border: 1px solid #ccc; padding: 5px;') prompt_layout = QHBoxLayout() prompt_layout.addWidget(prompt_label) prompt_layout.addWidget(self.prompt_edit) # Question Label and Edit box question_label = QLabel('问题:', self) question_label.setStyleSheet('color: #222; font-size: 30px; margin-bottom: 10px; font-weight: bold;') self.question_edit = QTextEdit(self) self.question_edit.setStyleSheet('color: #333; font-size: 24px; border: 1px solid #ccc; padding: 5px;') question_layout = QHBoxLayout() question_layout.addWidget(question_label) question_layout.addWidget(self.question_edit) liangge_layout_layout = QVBoxLayout() liangge_layout_layout.addLayout(prompt_layout) liangge_layout_layout.addLayout(question_layout) # Adding submit button to question layout self.submit_btn = QPushButton('发送', self) self.submit_btn.setFixedWidth(150) self.submit_btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding) self.submit_btn.setStyleSheet('color: #fff; background-color: #20639b; border: none; font-size: 24px; padding: 10px; border-radius: 5px;') self.submit_btn.clicked.connect(self.on_submit) # Change layout of question and submit button using QHBoxLayout question_submit_layout = QHBoxLayout() question_submit_layout.addLayout(liangge_layout_layout) question_submit_layout.addWidget(self.submit_btn) # JSON response Label and Edit box json_response_label = QLabel('回复:', self) json_response_label.setStyleSheet('color: #222; font-size: 30px; margin-bottom: 10px; font-weight: bold;') self.json_response_edit = QTextEdit(self) self.json_response_edit.setStyleSheet('color: #333; font-size: 24px; border: 1px solid #ccc; padding: 5px;') json_response_layout = QHBoxLayout() json_response_layout.addWidget(json_response_label) json_response_layout.addWidget(self.json_response_edit) # Main layout main_layout = QVBoxLayout() main_layout.setSpacing(20) main_layout.addLayout(question_submit_layout) main_layout.addLayout(json_response_layout) main_layout.setContentsMargins(30, 30, 30, 30) self.setLayout(main_layout) self.show()如何让按钮文本内容竖着显示?
你可以使用QPushButton的setStyleSheet方法设置按钮的样式表,将文本旋转90度,例如:
```
self.submit_btn.setStyleSheet('color: #fff; background-color: #20639b; border: none; font-size: 24px; padding: 10px; border-radius: 5px; transform: rotate(90deg);')
```
这里使用了transform属性将文本旋转了90度。
阅读全文