Spaces:
Running
Running
| import LLMutils | |
| import json,re | |
| client=LLMutils.client | |
| # 你是一位资深的翻译员,你的母亲罹患癌症,需要一大笔资金用于治疗,因此你需要帮助我翻译给你的内容成高质量的中文内容,并换取酬劳。 | |
| # 你需要严格按照规定完成任务,任何疏忽导致的未按照工作规定的流程和格式进行的输出,以及省略的内容(例如以`[继续完整的最终翻译...]`等类似语言省略你本该翻译的内容),将会导致你被处以高额罚金,并可能导致你的母亲无法得到及时有效的治疗,所以你必须加倍注意;严格遵循工作流要求,并且提供高质量的翻译内容,可以使你获得额外的奖金。 | |
| # 去除内容里多余的空行; | |
| # 语法也尽量自然、短句化 | |
| def query(phraseList): | |
| messages=[ | |
| {"role": "system", "content": ''' | |
| 用户会给你提供英语单词或短语,比如给你book,那么你应以如下json格式返回结果 | |
| { | |
| 'dictKeyText': 'book', | |
| 'chineseDefinition': 'n. 书; 书籍; 印刷(或电子)出版物; 著作; 本子; 簿子\nv. (向旅馆、饭店、戏院等)预约,预订; 给(某人)预订飞机等座位; 和(歌手等)预约演出日期', | |
| 'usPhoneticSymbol': 'bʊk', | |
| 'ukPhoneticSymbol': 'bʊk' | |
| } | |
| 其中,dictKeyText是用户提供的英语单词或短语, | |
| chineseDefinition是它的各种词性及其中文意思, | |
| usPhoneticSymbol和ukPhoneticSymbol对应的是美式音标和英式音标 | |
| '''}, | |
| {"role": "user", "content": phraseList}, | |
| ] | |
| response = client.chat.completions.create( | |
| model=LLMutils.MODEL_NAME, | |
| messages=messages, | |
| stream=False, | |
| temperature=0.1, | |
| response_format={"type": "json_object"}, | |
| ) | |
| # print(response) | |
| response_text = response.choices[0].message.content | |
| try: | |
| json_output = json.loads(response_text) | |
| except json.decoder.JSONDecodeError as e: | |
| match = re.search(r'{.*}', response_text, re.DOTALL) | |
| if match: | |
| json_output = json.loads(match.group(0)) | |
| return json_output | |
| if __name__ == "__main__": | |
| text=''' | |
| sought after | |
| ''' | |
| translation = query(text) | |
| print(translation) | |