Skip to content

Commit 0eed9ed

Browse files
committed
update endpoint detection
1 parent f0dca22 commit 0eed9ed

3 files changed

Lines changed: 26 additions & 25 deletions

File tree

XUnity.AutoTranslator.LlmTranslators/Config/Configuration.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace XUnity.AutoTranslator.LlmTranslators.Config;
99
public class LlmConfig
1010
{
1111
public string? ApiKey { get; set; }
12-
public bool ApiKeyRequired { get; set; } = false;
1312
public string Url { get; set; } = string.Empty;
1413
public string Model { get; set; } = string.Empty;
1514
public string SystemPrompt { get; set; } = string.Empty;

XUnity.AutoTranslator.LlmTranslators/Config/configFunctions.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,16 @@ public static void FindCompatibleUrl(LlmConfig config)
5454
var schemesToTry = new List<string>();
5555
if (baseUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
5656
{
57+
schemesToTry.Add("https://" + baseUrl.Substring(7));
5758
schemesToTry.Add(baseUrl);
58-
schemesToTry.Add("https://" + baseUrl.Substring(7)); // then HTTPS
5959
}
6060
else if (baseUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
6161
{
62-
schemesToTry.Add(baseUrl); // HTTPS first
63-
schemesToTry.Add("http://" + baseUrl.Substring(8)); // then HTTP
62+
schemesToTry.Add(baseUrl);
63+
schemesToTry.Add("http://" + baseUrl.Substring(8));
6464
}
6565
else
6666
{
67-
// no scheme, try both
6867
schemesToTry.Add("https://" + baseUrl);
6968
schemesToTry.Add("http://" + baseUrl);
7069
}
@@ -81,10 +80,9 @@ public static void FindCompatibleUrl(LlmConfig config)
8180

8281
var variants = new List<string>
8382
{
84-
Combine("responses"),
85-
Combine("v1/responses"),
8683
Combine("v1/chat/completions"),
87-
Combine("chat/completions"),
84+
Combine("api/v1/chat/completions"),
85+
Combine("api/paas/v4/chat/completions")
8886
};
8987

9088

@@ -100,24 +98,24 @@ public static void FindCompatibleUrl(LlmConfig config)
10098
var req = (HttpWebRequest)WebRequest.Create(url);
10199
req.Method = "POST";
102100
req.ContentType = "application/json";
103-
req.Timeout = 2500;
104-
105-
if (!string.IsNullOrWhiteSpace(config.ApiKey))
101+
req.Timeout = 2000;
102+
// Only send api key on https
103+
if (!string.IsNullOrWhiteSpace(config.ApiKey) && url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
106104
req.Headers["Authorization"] = $"Bearer {config.ApiKey}";
107105

108106
using (var writer = new StreamWriter(req.GetRequestStream()))
109107
{
110108
writer.Write($@"
111-
{{
112-
""model"": ""{config.Model}"",
113-
""max_tokens"": 1,
114-
""messages"": [
115109
{{
116-
""role"": ""user"",
117-
""content"": ""Hello there.""
118-
}}
119-
]
120-
}}");
110+
""model"": ""{config.Model}"",
111+
""max_tokens"": 1,
112+
""messages"": [
113+
{{
114+
""role"": ""user"",
115+
""content"": ""Hello there.""
116+
}}
117+
]
118+
}}");
121119
}
122120

123121
using var resp = (HttpWebResponse)req.GetResponse();
@@ -136,14 +134,18 @@ public static void FindCompatibleUrl(LlmConfig config)
136134
}
137135
}
138136
}
139-
catch (WebException)
137+
catch (WebException) // ex)
140138
{
141-
// if (ex.Response is HttpWebResponse response)
142-
// Console.WriteLine($"Failed {url} - Status Code: {(int)response.StatusCode} ({response.StatusCode})");
139+
/*
140+
if (ex.Response is HttpWebResponse r)
141+
Console.WriteLine($"Failed {url}: {(int)r.StatusCode} {r.StatusCode}");
142+
*/
143143
}
144144
});
145145
}
146146
if (!found)
147-
throw new Exception("Endpoint is not working or API key or specified model is not provided");
147+
throw new InvalidOperationException(
148+
$"Failed to connect {baseUrl}. Endpoint may not exist, or require a valid API key."
149+
);
148150
}
149151
}

XUnity.AutoTranslator.LlmTranslators/LLMTranslateEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override void OnCreateRequest(IHttpRequestCreationContext context)
3131
var request = new XUnityWebRequest("POST", _config.Url, requestData);
3232
request.Headers[HttpRequestHeader.ContentType] = "application/json";
3333

34-
if (_config.ApiKeyRequired)
34+
if (!string.IsNullOrEmpty(_config.ApiKey) && _config.Url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
3535
request.Headers[HttpRequestHeader.Authorization] = $"Bearer {_config.ApiKey}";
3636

3737
context.Complete(request);

0 commit comments

Comments
 (0)