Skip to content

Commit

Permalink
RavenDB-23557: refactor: improve AI connection string comparison and …
Browse files Browse the repository at this point in the history
…type handling
  • Loading branch information
ArieSLV committed Jan 29, 2025
1 parent 4051b92 commit 1974b0c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
45 changes: 34 additions & 11 deletions src/Raven.Client/Documents/Operations/ETL/AI/AiConnectionString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,48 @@ private static string GenerateIdentifier(string input)
return string.IsNullOrEmpty(finalResult) ? $"{nameof(AiConnectionString)}Identifier" : finalResult;
}

public bool HasCriticalChanges(AiConnectionString other)
public bool HasCriticalChanges(AiConnectionString newConnectionString)
{
if (other == null)
if (newConnectionString == null)
return true;

if (Identifier != other.Identifier)
if (Identifier != newConnectionString.Identifier)
return true;

var settingsPairs = new (AbstractAiSettings ExistingSettings, AbstractAiSettings NewSettings)[]
var oldProvider = GetActiveProvider();
var newProvider = newConnectionString.GetActiveProvider();

if (oldProvider != newProvider)
return true;

return oldProvider switch
{
(OpenAiSettings, other.OpenAiSettings),
(AzureOpenAiSettings, other.AzureOpenAiSettings),
(OllamaSettings, other.OllamaSettings),
(OnnxSettings, other.OnnxSettings),
(GoogleSettings, other.GoogleSettings),
(HuggingFaceSettings, other.HuggingFaceSettings)
AiConnectorType.OpenAi => OpenAiSettings.HasCriticalChanges(newConnectionString.OpenAiSettings),
AiConnectorType.AzureOpenAI => AzureOpenAiSettings.HasCriticalChanges(newConnectionString.AzureOpenAiSettings),
AiConnectorType.Ollama => OllamaSettings.HasCriticalChanges(newConnectionString.OllamaSettings),
AiConnectorType.Onnx => OnnxSettings.HasCriticalChanges(newConnectionString.OnnxSettings),
AiConnectorType.Google => GoogleSettings.HasCriticalChanges(newConnectionString.GoogleSettings),
AiConnectorType.HuggingFace => HuggingFaceSettings.HasCriticalChanges(newConnectionString.HuggingFaceSettings),
_ => true
};
}

return settingsPairs.Any(pair => pair.ExistingSettings != null && pair.ExistingSettings.HasCriticalChanges(pair.NewSettings));
private AiConnectorType GetActiveProvider()
{
if (OpenAiSettings != null)
return AiConnectorType.OpenAi;
if (AzureOpenAiSettings != null)
return AiConnectorType.AzureOpenAI;
if (OllamaSettings != null)
return AiConnectorType.Ollama;
if (OnnxSettings != null)
return AiConnectorType.Onnx;
if (GoogleSettings != null)
return AiConnectorType.Google;
if (HuggingFaceSettings != null)
return AiConnectorType.HuggingFace;

return AiConnectorType.None;
}

public override DynamicJsonValue ToJson()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public override async ValueTask ExecuteAsync()
return;

var connectionStringName = RequestHandler.GetStringQueryString("connectionStringName", false);
var type = RequestHandler.GetStringQueryString("type", false);
var typeString = RequestHandler.GetStringQueryString("type", false);
if (Enum.TryParse(typeString, ignoreCase: true, out ConnectionStringType connectionStringType) == false && typeString != null)
throw new NotSupportedException($"Unknown connection string type: {connectionStringType}");

await RequestHandler.ServerStore.EnsureNotPassiveAsync();
RequestHandler.HttpContext.Response.StatusCode = (int)HttpStatusCode.OK;
Expand All @@ -40,17 +42,9 @@ public override async ValueTask ExecuteAsync()
using (context.OpenReadTransaction())
using (var rawRecord = RequestHandler.ServerStore.Cluster.ReadRawDatabaseRecord(context, RequestHandler.DatabaseName))
{
if (string.IsNullOrWhiteSpace(connectionStringName))
{
if (Enum.TryParse<ConnectionStringType>(type, ignoreCase: true, out var connectionStringType) == false)
throw new NotSupportedException($"Unknown connection string type: {connectionStringType}");

connectionStrings = rawRecord.GetConnectionString(connectionStringName, connectionStringType);
}
else
{
connectionStrings = rawRecord.GetConnectionStrings();
}
connectionStrings = string.IsNullOrWhiteSpace(connectionStringName)
? rawRecord.GetConnectionString(connectionStringName, connectionStringType)
: rawRecord.GetConnectionStrings();
}

await using (var writer = new AsyncBlittableJsonTextWriter(context, RequestHandler.ResponseBodyStream()))
Expand Down

0 comments on commit 1974b0c

Please sign in to comment.