(self, params)
| 64 | }).open() |
| 65 | |
| 66 | def call_tool(self, params): |
| 67 | name = params["name"] |
| 68 | args = params.get("arguments", {}) |
| 69 | # print(params) |
| 70 | |
| 71 | payload = { |
| 72 | 'message': args.get('message'), |
| 73 | 'stream': True, |
| 74 | 're_chat': False |
| 75 | } |
| 76 | resp = ChatSerializers(data={ |
| 77 | 'chat_id': self._get_chat_id(), |
| 78 | 'chat_user_id': str(uuid.uuid7()), |
| 79 | 'chat_user_type': ChatUserType.ANONYMOUS_USER, |
| 80 | 'application_id': self.application.id, |
| 81 | 'ip_address': '-', |
| 82 | 'source': {"type": ChatSourceChoices.ONLINE.value}, |
| 83 | 'debug': False, |
| 84 | }).chat(payload) |
| 85 | chunks = [] |
| 86 | for raw_line in resp: |
| 87 | line = raw_line.decode("utf-8", errors="replace").rstrip("\r\n") |
| 88 | if not line.startswith("data:"): |
| 89 | continue |
| 90 | payload = line[5:].strip() |
| 91 | if not payload or payload == "[DONE]": |
| 92 | continue |
| 93 | try: |
| 94 | event = json.loads(payload) |
| 95 | except json.JSONDecodeError: |
| 96 | continue |
| 97 | if event.get("operate") is True: |
| 98 | chunks.append(event.get("content", "")) |
| 99 | if event.get("is_end"): |
| 100 | break |
| 101 | |
| 102 | data = ''.join(chunks) |
| 103 | # 排除<tool_calls_render></tool_calls_render>标签 |
| 104 | data = re.sub(r'<tool_calls_render>.*?</tool_calls_render>', '', data, flags=re.DOTALL) |
| 105 | return {"content": [{"type": "text", "text": data}]} |
no test coverage detected