From a7ee7e9eda7c97f6d68b810362d6a6b0b6c38cf2 Mon Sep 17 00:00:00 2001 From: zggsong Date: Thu, 18 Jan 2024 12:56:14 +0800 Subject: [PATCH] perf(openai): Compatible with both old and new APIs --- STranslate/ViewModels/ServiceHandler.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/STranslate/ViewModels/ServiceHandler.cs b/STranslate/ViewModels/ServiceHandler.cs index 0e5540aa..50b52076 100644 --- a/STranslate/ViewModels/ServiceHandler.cs +++ b/STranslate/ViewModels/ServiceHandler.cs @@ -116,9 +116,10 @@ public static async Task OpenAIHandlerAsync(ITranslator service, string content, UriBuilder uriBuilder = new(service.Url); - if (!uriBuilder.Path.EndsWith("/v1/completions")) + // 兼容旧版API: https://platform.openai.com/docs/guides/text-generation + if (!uriBuilder.Path.EndsWith("/v1/chat/completions") && !uriBuilder.Path.EndsWith("/v1/completions")) { - uriBuilder.Path = uriBuilder.Path.TrimEnd('/') + "/v1/completions"; + uriBuilder.Path = uriBuilder.Path.TrimEnd('/') + "/v1/chat/completions"; } var a_model = (service as TranslatorOpenAI)?.Model;