| 776 | self.api_key = self.envs["DIFY_API_KEY"] |
| 777 | |
| 778 | def do_translate(self, text): |
| 779 | headers = { |
| 780 | "Authorization": f"Bearer {self.api_key}", |
| 781 | "Content-Type": "application/json", |
| 782 | } |
| 783 | |
| 784 | payload = { |
| 785 | "inputs": { |
| 786 | "lang_out": self.lang_out, |
| 787 | "lang_in": self.lang_in, |
| 788 | "text": text, |
| 789 | }, |
| 790 | "response_mode": "blocking", |
| 791 | "user": "translator-service", |
| 792 | } |
| 793 | |
| 794 | # 向 Dify 服务器发送请求 |
| 795 | response = requests.post( |
| 796 | self.api_url, headers=headers, data=json.dumps(payload) |
| 797 | ) |
| 798 | response.raise_for_status() |
| 799 | response_data = response.json() |
| 800 | |
| 801 | # 解析响应 |
| 802 | return response_data.get("answer", "") |
| 803 | |
| 804 | |
| 805 | class ArgosTranslator(BaseTranslator): |