From 4259e235c68a27ebe48411ef4872a53b43329f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Fri, 30 Aug 2024 01:18:27 +0800 Subject: [PATCH] azure --- .../LunaTranslator/ocrengines/chatgptlike.py | 4 --- .../LunaTranslator/translator/azureopenai.py | 14 ++++++++++ .../LunaTranslator/translator/chatgpt.py | 25 ----------------- .../LunaTranslator/translator/gptcommon.py | 4 --- .../files/defaultconfig/config.json | 18 ++++++------ .../defaultconfig/translatorsetting.json | 28 ++++++++----------- docs/zh/guochandamoxing.md | 4 +++ 7 files changed, 38 insertions(+), 59 deletions(-) create mode 100644 LunaTranslator/LunaTranslator/translator/azureopenai.py delete mode 100644 LunaTranslator/LunaTranslator/translator/chatgpt.py diff --git a/LunaTranslator/LunaTranslator/ocrengines/chatgptlike.py b/LunaTranslator/LunaTranslator/ocrengines/chatgptlike.py index 51176f83..bb15637b 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/chatgptlike.py +++ b/LunaTranslator/LunaTranslator/ocrengines/chatgptlike.py @@ -39,9 +39,6 @@ class OCR(baseocr): ) return data - def createparam(self): - return None - def createheaders(self): return {"Authorization": "Bearer " + self.config["SECRET_KEY"]} @@ -93,7 +90,6 @@ class OCR(baseocr): response = self.proxysession.post( self.createurl(), headers=self.createheaders(), - params=self.createparam(), json=self.createdata(message), ) try: diff --git a/LunaTranslator/LunaTranslator/translator/azureopenai.py b/LunaTranslator/LunaTranslator/translator/azureopenai.py new file mode 100644 index 00000000..064eb36f --- /dev/null +++ b/LunaTranslator/LunaTranslator/translator/azureopenai.py @@ -0,0 +1,14 @@ +from translator.gptcommon import gptcommon + + +class TS(gptcommon): + def createurl(self): + return f'https://{self.config["endpoint"]}/openai/deployments/{self.config["deployment-id"]}/completions?api-version={self.config["api-version"]}' + + def createheaders(self): + _ = super().createheaders() + _.update({"api-key": self.multiapikeycurrent["api-key"]}) + + def translate(self, query): + self.checkempty(["api-key", "api-version", "endpoint", "deployment-id"]) + return super().translate(query) diff --git a/LunaTranslator/LunaTranslator/translator/chatgpt.py b/LunaTranslator/LunaTranslator/translator/chatgpt.py deleted file mode 100644 index 79a9abeb..00000000 --- a/LunaTranslator/LunaTranslator/translator/chatgpt.py +++ /dev/null @@ -1,25 +0,0 @@ -from translator.gptcommon import gptcommon - - -class TS(gptcommon): - def createurl(self): - return self.config["OPENAI_API_BASE"] + self.config["Appedix"] - - def createparam(self): - api_type = self.config["api_type"] - if api_type in [1, 2]: - api_version = "2023-05-15" - return {"api-version": api_version} - else: - return super().createparam() - - def createheaders(self): - api_type = self.config["api_type"] - _ = super().createheaders() - if api_type == 1: # azure - _.update({"api-key": self.multiapikeycurrent["SECRET_KEY"]}) - return _ - - def translate(self, query): - self.checkempty(["SECRET_KEY", "model"]) - return super().translate(query) diff --git a/LunaTranslator/LunaTranslator/translator/gptcommon.py b/LunaTranslator/LunaTranslator/translator/gptcommon.py index dcc23bf2..627b3c96 100644 --- a/LunaTranslator/LunaTranslator/translator/gptcommon.py +++ b/LunaTranslator/LunaTranslator/translator/gptcommon.py @@ -50,9 +50,6 @@ class gptcommon(basetrans): pass return data - def createparam(self): - return None - def createheaders(self): return {"Authorization": "Bearer " + self.multiapikeycurrent["SECRET_KEY"]} @@ -158,7 +155,6 @@ class gptcommon(basetrans): response = self.proxysession.post( self.createurl(), headers=self.createheaders(), - params=self.createparam(), json=self.createdata(message), stream=usingstream, ) diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index fa563142..4744e044 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -1561,6 +1561,13 @@ } }, "fanyi": { + "chatgpt-3rd-party": { + "type": "api", + "use": false, + "color": "blue", + "name": "ChatGPT_兼容接口", + "is_gpt_like": true + }, "microsoft": { "use": false, "color": "gray", @@ -1932,11 +1939,11 @@ "color": "blue", "name": "有道api" }, - "chatgpt": { + "azureopenai": { "type": "api", "use": false, "color": "blue", - "name": "ChatGPT", + "name": "Azure Openai", "is_gpt_like": true }, "cohere": { @@ -1953,13 +1960,6 @@ "name": "claude", "is_gpt_like": true }, - "chatgpt-3rd-party": { - "type": "api", - "use": false, - "color": "blue", - "name": "ChatGPT_兼容接口", - "is_gpt_like": true - }, "hanshant": { "type": "offline", "use": false, diff --git a/LunaTranslator/files/defaultconfig/translatorsetting.json b/LunaTranslator/files/defaultconfig/translatorsetting.json index 5d4d4513..72e2e2e7 100644 --- a/LunaTranslator/files/defaultconfig/translatorsetting.json +++ b/LunaTranslator/files/defaultconfig/translatorsetting.json @@ -209,20 +209,20 @@ } } }, - "chatgpt": { + "azureopenai": { "args": { - "SECRET_KEY": "", + "api-key": "", "Temperature": 0.3, "top_p": 0.3, "max_tokens": 128, "frequency_penalty": 0, "model": "gpt-3.5-turbo", "附带上下文个数": 0, - "OPENAI_API_BASE": "https://api.openai.com/v1", - "Appedix": "/chat/completions", + "endpoint": "https://your-resource-name.openai.azure.com", + "deployment-id":"", + "api-version":"2024-06-01", "使用自定义promt": false, "自定义promt": "", - "api_type": 0, "流式输出": true, "user_user_prompt": "{sentence}", "use_user_user_prompt": false, @@ -245,12 +245,15 @@ "type": "split", "rank": 2.5 }, - "OPENAI_API_BASE": { + "endpoint": { "rank": 0 }, - "Appedix": { + "deployment-id": { "rank": 1 }, + "api-version": { + "rank": 1.5 + }, "model": { "rank": 2 }, @@ -282,16 +285,7 @@ "type": "switch", "rank": 3 }, - "api_type": { - "type": "combo", - "rank": 1.5, - "list": [ - "open_ai", - "azure", - "azure_ad" - ] - }, - "SECRET_KEY": { + "api-key": { "rank": 1.6 }, "附带上下文个数": { diff --git a/docs/zh/guochandamoxing.md b/docs/zh/guochandamoxing.md index 50975910..dd25a7df 100644 --- a/docs/zh/guochandamoxing.md +++ b/docs/zh/guochandamoxing.md @@ -132,6 +132,10 @@ **model** https://docs.cohere.com/docs/models +### **Azure** + +https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#completions +