Skip to content

Commit

Permalink
Add support for ignoring SSL errors for local AI Server
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jun 29, 2024
1 parent fabfeb0 commit c82de23
Show file tree
Hide file tree
Showing 4 changed files with 1,075 additions and 1 deletion.
27 changes: 26 additions & 1 deletion MyApp.ServiceInterface/Data/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using System.Data;
using System.Net.Http.Headers;
using MyApp.ServiceModel;
using ServiceStack;
using ServiceStack.OrmLite;
Expand All @@ -24,8 +25,32 @@ public class AppConfig
public string? RedditClient { get; set; }
public string? RedditSecret { get; set; }
public string? RedditAccessToken { get; set; }
public JsonApiClient CreateAiServerClient() => new(AiServerBaseUrl) { BearerToken = AiServerApiKey };

public JsonApiClient CreateAiServerClientIgnoringSsl(string baseUrl)
{
var client = new JsonApiClient(AiServerBaseUrl);

// Ignore local SSL Errors
var handler = HttpUtils.HttpClientHandlerFactory();
handler.ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => true;
var httpClient = new HttpClient(handler, disposeHandler:client.HttpMessageHandler == null) {
BaseAddress = new Uri(AiServerBaseUrl),
};
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AiServerApiKey);
client = new JsonApiClient(httpClient) {
BearerToken = AiServerApiKey
};
return client;
}

public JsonApiClient CreateAiServerClient()
{
// if (Env.IsLinux && AiServerBaseUrl.StartsWith("https://localhost"))
// return CreateAiServerClientIgnoringSsl(AiServerBaseUrl);

return new JsonApiClient(AiServerBaseUrl) { BearerToken = AiServerApiKey };
}

public string CacheDir { get; set; }
public string ProfilesDir { get; set; }
public string NotificationsEmail { get; set; } = "notifications@pvq.app";
Expand Down
20 changes: 20 additions & 0 deletions MyApp.Tests/AiServerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AiServer.ServiceModel;
using NUnit.Framework;
using ServiceStack;
using ServiceStack.DataAnnotations;
using ServiceStack.Text;

namespace MyApp.Tests;

[Explicit("Requires Local AI Server")]
public class AiServerTests
{
[Test]
public void Can_QueryApiModels()
{
var client = TestUtils.CreateAiDevClient();
var api = client.Api(new QueryApiModels());
api.ThrowIfError();
api.Response.PrintDump();
}
}
10 changes: 10 additions & 0 deletions MyApp.Tests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,15 @@ await client.ApiAsync(new Authenticate
});
return client;
}

public static JsonApiClient CreateAiDevClient() => new("https://localhost:5005")
{
BearerToken = Environment.GetEnvironmentVariable("AK_PVQ")
};

public static JsonApiClient CreateAiProdClient() => new("https://openai.servicestack.net")
{
BearerToken = Environment.GetEnvironmentVariable("AK_PVQ")
};

}
Loading

0 comments on commit c82de23

Please sign in to comment.