mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-13 12:33:48 +00:00
Added an information view to all data sources (#273)
This commit is contained in:
1 parent
9d71978feb
commit
c836bd7f33
61 files changed
+1596
-1372
No files matched your search
@@ -1,4 +1,4 @@
|
||||
using ERI_Client.V1;
|
||||
using AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
namespace AIStudio.Tools;
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
namespace AIStudio.Tools;
|
||||
|
||||
public static class DirectoryInfoExtensions
|
||||
{
|
||||
private static readonly EnumerationOptions ENUMERATION_OPTIONS = new()
|
||||
{
|
||||
IgnoreInaccessible = true,
|
||||
RecurseSubdirectories = true,
|
||||
ReturnSpecialDirectories = false,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Determines the size of the directory and all its subdirectories, as well as the number of files. When desired,
|
||||
/// it can report the found files up to a certain limit.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// You might set reportMaxFiles to a negative value to report all files. Any positive value will limit the number
|
||||
/// of reported files. The cancellation token can be used to stop the operation. The cancellation operation is also able
|
||||
/// to cancel slow operations, e.g., when the directory is on a slow network drive.
|
||||
///
|
||||
/// After stopping the operation, the total size and number of files are reported as they were at the time of cancellation.
|
||||
///
|
||||
/// Please note that the entire operation is done on a background thread. Thus, when reporting the found files or the
|
||||
/// current total size, you need to use the appropriate dispatcher to update the UI. Usually, you can use the InvokeAsync
|
||||
/// method to update the UI from a background thread.
|
||||
/// </remarks>
|
||||
/// <param name="directoryInfo">The root directory to determine the size of.</param>
|
||||
/// <param name="reportCurrentTotalSize">The callback to report the current total size of the directory.</param>
|
||||
/// <param name="reportCurrentNumFiles">The callback to report the current number of files found.</param>
|
||||
/// <param name="reportNextFile">The callback to report the next file found. The file name is relative to the root directory.</param>
|
||||
/// <param name="reportMaxFiles">The maximum number of files to report. A negative value reports all files.</param>
|
||||
/// <param name="done">The callback to report that the operation is done.</param>
|
||||
/// <param name="cancellationToken">The cancellation token to stop the operation.</param>
|
||||
public static async Task DetermineContentSize(this DirectoryInfo directoryInfo, Action<long> reportCurrentTotalSize, Action<long> reportCurrentNumFiles, Action<string> reportNextFile, int reportMaxFiles = -1, Action? done = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var rootDirectoryLen = directoryInfo.FullName.Length;
|
||||
long totalSize = 0;
|
||||
long numFiles = 0;
|
||||
|
||||
await Task.Factory.StartNew(() => {
|
||||
foreach (var file in directoryInfo.EnumerateFiles("*", ENUMERATION_OPTIONS))
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
totalSize += file.Length;
|
||||
numFiles++;
|
||||
|
||||
if (numFiles % 100 == 0)
|
||||
{
|
||||
reportCurrentTotalSize(totalSize);
|
||||
reportCurrentNumFiles(numFiles);
|
||||
}
|
||||
|
||||
if (reportMaxFiles < 0 || numFiles <= reportMaxFiles)
|
||||
reportNextFile(file.FullName[rootDirectoryLen..]);
|
||||
}
|
||||
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
||||
|
||||
reportCurrentTotalSize(totalSize);
|
||||
reportCurrentNumFiles(numFiles);
|
||||
|
||||
if(done is not null)
|
||||
done();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace AIStudio.Tools.ERIClient;
|
||||
|
||||
public sealed class APIResponse<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Was the API call successful?
|
||||
/// </summary>
|
||||
public bool Successful { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// When the API call was not successful, this will contain the error message.
|
||||
/// </summary>
|
||||
public string Message { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// The data returned by the API call.
|
||||
/// </summary>
|
||||
public T? Data { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// An authentication field.
|
||||
/// </summary>
|
||||
public enum AuthField
|
||||
{
|
||||
NONE,
|
||||
USERNAME,
|
||||
PASSWORD,
|
||||
TOKEN,
|
||||
KERBEROS_TICKET,
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// The mapping between an AuthField and the field name in the authentication request.
|
||||
/// </summary>
|
||||
/// <param name="AuthField">The AuthField that is mapped to the field name.</param>
|
||||
/// <param name="FieldName">The field name in the authentication request.</param>
|
||||
public record AuthFieldMapping(AuthField AuthField, string FieldName);
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
public enum AuthMethod
|
||||
{
|
||||
NONE,
|
||||
KERBEROS,
|
||||
USERNAME_PASSWORD,
|
||||
TOKEN,
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// The response to an authentication request.
|
||||
/// </summary>
|
||||
/// <param name="Success">True, when the authentication was successful.</param>
|
||||
/// <param name="Token">The token to use for further requests.</param>
|
||||
/// <param name="Message">When the authentication was not successful, this contains the reason.</param>
|
||||
public readonly record struct AuthResponse(bool Success, string? Token, string? Message);
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// Describes one authentication scheme for this data source.
|
||||
/// </summary>
|
||||
/// <param name="AuthMethod">The method used for authentication, e.g., "API Key," "Username/Password," etc.</param>
|
||||
/// <param name="AuthFieldMappings">A list of field mappings for the authentication method. The client must know,
|
||||
/// e.g., how the password field is named in the request.</param>
|
||||
public readonly record struct AuthScheme(AuthMethod AuthMethod, List<AuthFieldMapping> AuthFieldMappings);
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// A chat thread, which is a list of content blocks.
|
||||
/// </summary>
|
||||
/// <param name="ContentBlocks">The content blocks in this chat thread.</param>
|
||||
public readonly record struct ChatThread(List<ContentBlock> ContentBlocks);
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// A block of content of a chat thread.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Images and other media are base64 encoded.
|
||||
/// </remarks>
|
||||
/// <param name="Content">The content of the block. Remember that images and other media are base64 encoded.</param>
|
||||
/// <param name="Role">The role of the content in the chat thread.</param>
|
||||
/// <param name="Type">The type of the content, e.g., text, image, video, etc.</param>
|
||||
public readonly record struct ContentBlock(string Content, Role Role, ContentType Type);
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// The type of content.
|
||||
/// </summary>
|
||||
public enum ContentType
|
||||
{
|
||||
NONE,
|
||||
UNKNOWN,
|
||||
|
||||
TEXT,
|
||||
IMAGE,
|
||||
VIDEO,
|
||||
AUDIO,
|
||||
SPEECH,
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// Matching context returned by the data source as a result of a retrieval request.
|
||||
/// </summary>
|
||||
/// <param name="Name">The name of the source, e.g., a document name, database name,
|
||||
/// collection name, etc.</param>
|
||||
/// <param name="Category">What are the contents of the source? For example, is it a
|
||||
/// dictionary, a book chapter, business concept, a paper, etc.</param>
|
||||
/// <param name="Path">The path to the content, e.g., a URL, a file path, a path in a
|
||||
/// graph database, etc.</param>
|
||||
/// <param name="Type">The type of the content, e.g., text, image, video, audio, speech, etc.</param>
|
||||
/// <param name="MatchedContent">The content that matched the user prompt. For text, you
|
||||
/// return the matched text and, e.g., three words before and after it.</param>
|
||||
/// <param name="SurroundingContent">The surrounding content of the matched content.
|
||||
/// For text, you may return, e.g., one sentence or paragraph before and after
|
||||
/// the matched content.</param>
|
||||
/// <param name="Links">Links to related content, e.g., links to Wikipedia articles,
|
||||
/// links to sources, etc.</param>
|
||||
public readonly record struct Context(
|
||||
string Name,
|
||||
string Category,
|
||||
string? Path,
|
||||
ContentType Type,
|
||||
string MatchedContent,
|
||||
string[] SurroundingContent,
|
||||
string[] Links);
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// Information about the data source.
|
||||
/// </summary>
|
||||
/// <param name="Name">The name of the data source, e.g., "Internal Organization Documents."</param>
|
||||
/// <param name="Description">A short description of the data source. What kind of data does it contain?
|
||||
/// What is the data source used for?</param>
|
||||
public readonly record struct DataSourceInfo(string Name, string Description);
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// Represents information about the used embedding for this data source. The purpose of this information is to give the
|
||||
/// interested user an idea of what kind of embedding is used and what it does.
|
||||
/// </summary>
|
||||
/// <param name="EmbeddingType">What kind of embedding is used. For example, "Transformer Embedding," "Contextual Word
|
||||
/// Embedding," "Graph Embedding," etc.</param>
|
||||
/// <param name="EmbeddingName">Name the embedding used. This can be a library, a framework, or the name of the used
|
||||
/// algorithm.</param>
|
||||
/// <param name="Description">A short description of the embedding. Describe what the embedding is doing.</param>
|
||||
/// <param name="UsedWhen">Describe when the embedding is used. For example, when the user prompt contains certain
|
||||
/// keywords, or anytime?</param>
|
||||
/// <param name="Link">A link to the embedding's documentation or the source code. Might be null.</param>
|
||||
public readonly record struct EmbeddingInfo(
|
||||
string EmbeddingType,
|
||||
string EmbeddingName,
|
||||
string Description,
|
||||
string UsedWhen,
|
||||
string? Link);
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// Known types of providers that can process data.
|
||||
/// </summary>
|
||||
public enum ProviderType
|
||||
{
|
||||
/// <summary>
|
||||
/// The related data is not allowed to be sent to any provider.
|
||||
/// </summary>
|
||||
NONE,
|
||||
|
||||
/// <summary>
|
||||
/// The related data can be sent to any provider.
|
||||
/// </summary>
|
||||
ANY,
|
||||
|
||||
/// <summary>
|
||||
/// The related data can be sent to a provider that is hosted by the same organization, either on-premises or locally.
|
||||
/// </summary>
|
||||
SELF_HOSTED,
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
public static class ProviderTypeExtensions
|
||||
{
|
||||
public static string Explain(this ProviderType providerType) => providerType switch
|
||||
{
|
||||
ProviderType.NONE => "The related data is not allowed to be sent to any LLM provider. This means that this data source cannot be used at the moment.",
|
||||
ProviderType.ANY => "The related data can be sent to any provider, regardless of where it is hosted (cloud or self-hosted).",
|
||||
ProviderType.SELF_HOSTED => "The related data can be sent to a provider that is hosted by the same organization, either on-premises or locally. Cloud-based providers are not allowed.",
|
||||
|
||||
_ => "Unknown configuration. This data source cannot be used at the moment.",
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// Information about a retrieval process, which this data source implements.
|
||||
/// </summary>
|
||||
/// <param name="Id">A unique identifier for the retrieval process. This can be a GUID, a unique name, or an increasing integer.</param>
|
||||
/// <param name="Name">The name of the retrieval process, e.g., "Keyword-Based Wikipedia Article Retrieval".</param>
|
||||
/// <param name="Description">A short description of the retrieval process. What kind of retrieval process is it?</param>
|
||||
/// <param name="Link">A link to the retrieval process's documentation, paper, Wikipedia article, or the source code. Might be null.</param>
|
||||
/// <param name="ParametersDescription">A dictionary that describes the parameters of the retrieval process. The key is the parameter name,
|
||||
/// and the value is a description of the parameter. Although each parameter will be sent as a string, the description should indicate the
|
||||
/// expected type and range, e.g., 0.0 to 1.0 for a float parameter.</param>
|
||||
/// <param name="Embeddings">A list of embeddings used in this retrieval process. It might be empty in case no embedding is used.</param>
|
||||
public readonly record struct RetrievalInfo(
|
||||
string Id,
|
||||
string Name,
|
||||
string Description,
|
||||
string? Link,
|
||||
Dictionary<string, string>? ParametersDescription,
|
||||
List<EmbeddingInfo> Embeddings);
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// The retrieval request sent by AI Studio.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Images and other media are base64 encoded.
|
||||
/// </remarks>
|
||||
/// <param name="LatestUserPrompt">The latest user prompt that AI Studio received.</param>
|
||||
/// <param name="LatestUserPromptType">The type of the latest user prompt, e.g., text, image, etc.</param>
|
||||
/// <param name="Thread">The chat thread that the user is currently in.</param>
|
||||
/// <param name="RetrievalProcessId">Optional. The ID of the retrieval process that the data source should use.
|
||||
/// When null, the data source chooses an appropriate retrieval process. Selecting a retrieval process is optional
|
||||
/// for AI Studio users. Most users do not specify a retrieval process.</param>
|
||||
/// <param name="Parameters">A dictionary of parameters that the data source should use for the retrieval process.
|
||||
/// Although each parameter will be sent as a string, the retrieval process specifies the expected type and range.</param>
|
||||
/// <param name="MaxMatches">The maximum number of matches that the data source should return. AI Studio uses
|
||||
/// any value below 1 to indicate that the data source should return as many matches as appropriate.</param>
|
||||
public readonly record struct RetrievalRequest(
|
||||
string LatestUserPrompt,
|
||||
ContentType LatestUserPromptType,
|
||||
ChatThread Thread,
|
||||
string? RetrievalProcessId,
|
||||
Dictionary<string, string>? Parameters,
|
||||
int MaxMatches);
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// Possible roles of any chat thread.
|
||||
/// </summary>
|
||||
public enum Role
|
||||
{
|
||||
NONE,
|
||||
UNKNOW,
|
||||
|
||||
SYSTEM,
|
||||
USER,
|
||||
AI,
|
||||
AGENT,
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the security requirements for this data source.
|
||||
/// </summary>
|
||||
/// <param name="AllowedProviderType">Which provider types are allowed to process the data?</param>
|
||||
public readonly record struct SecurityRequirements(ProviderType AllowedProviderType);
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AIStudio.Tools.ERIClient;
|
||||
|
||||
public abstract class ERIClientBase(string baseAddress) : IDisposable
|
||||
{
|
||||
protected static readonly JsonSerializerOptions JSON_OPTIONS = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
AllowTrailingCommas = true,
|
||||
PropertyNamingPolicy = null,
|
||||
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters =
|
||||
{
|
||||
new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseUpper),
|
||||
}
|
||||
};
|
||||
|
||||
protected readonly HttpClient httpClient = new()
|
||||
{
|
||||
BaseAddress = new Uri(baseAddress),
|
||||
};
|
||||
|
||||
protected string securityToken = string.Empty;
|
||||
|
||||
#region Implementation of IDisposable
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.httpClient.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using AIStudio.Assistants.ERI;
|
||||
using AIStudio.Settings;
|
||||
|
||||
namespace AIStudio.Tools.ERIClient;
|
||||
|
||||
public static class ERIClientFactory
|
||||
{
|
||||
public static IERIClient? Get(ERIVersion version, IERIDataSource dataSource) => version switch
|
||||
{
|
||||
ERIVersion.V1 => new ERIClientV1($"{dataSource.Hostname}:{dataSource.Port}"),
|
||||
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,344 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
namespace AIStudio.Tools.ERIClient;
|
||||
|
||||
public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIClient
|
||||
{
|
||||
#region Implementation of IERIClient
|
||||
|
||||
public async Task<APIResponse<List<AuthScheme>>> GetAuthMethodsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var response = await this.httpClient.GetAsync("/auth/methods", cancellationToken);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = $"Failed to retrieve the authentication methods: there was an issue communicating with the ERI server. Code: {response.StatusCode}, Reason: {response.ReasonPhrase}"
|
||||
};
|
||||
}
|
||||
|
||||
var authMethods = await response.Content.ReadFromJsonAsync<List<AuthScheme>>(JSON_OPTIONS, cancellationToken);
|
||||
if(authMethods is null)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to retrieve the authentication methods: the ERI server did not return a valid response."
|
||||
};
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Successful = true,
|
||||
Data = authMethods
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<APIResponse<AuthResponse>> AuthenticateAsync(IERIDataSource dataSource, RustService rustService, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var authMethod = dataSource.AuthMethod;
|
||||
var username = dataSource.Username;
|
||||
switch (dataSource.AuthMethod)
|
||||
{
|
||||
case AuthMethod.NONE:
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Post, $"auth?authMethod={authMethod}"))
|
||||
{
|
||||
using var noneAuthResponse = await this.httpClient.SendAsync(request, cancellationToken);
|
||||
if(!noneAuthResponse.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = $"Failed to authenticate with the ERI server. Code: {noneAuthResponse.StatusCode}, Reason: {noneAuthResponse.ReasonPhrase}"
|
||||
};
|
||||
}
|
||||
|
||||
var noneAuthResult = await noneAuthResponse.Content.ReadFromJsonAsync<AuthResponse>(JSON_OPTIONS, cancellationToken);
|
||||
if(noneAuthResult == default)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to authenticate with the ERI server: the response was invalid."
|
||||
};
|
||||
}
|
||||
|
||||
this.securityToken = noneAuthResult.Token ?? string.Empty;
|
||||
return new()
|
||||
{
|
||||
Successful = true,
|
||||
Data = noneAuthResult
|
||||
};
|
||||
}
|
||||
|
||||
case AuthMethod.USERNAME_PASSWORD:
|
||||
var passwordResponse = await rustService.GetSecret(dataSource);
|
||||
if (!passwordResponse.Success)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to retrieve the password."
|
||||
};
|
||||
}
|
||||
|
||||
var password = await passwordResponse.Secret.Decrypt(Program.ENCRYPTION);
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Post, $"auth?authMethod={authMethod}"))
|
||||
{
|
||||
// We must send both values inside the header. The username field is named 'user'.
|
||||
// The password field is named 'password'.
|
||||
request.Headers.Add("user", username);
|
||||
request.Headers.Add("password", password);
|
||||
|
||||
using var usernamePasswordAuthResponse = await this.httpClient.SendAsync(request, cancellationToken);
|
||||
if(!usernamePasswordAuthResponse.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = $"Failed to authenticate with the ERI server. Code: {usernamePasswordAuthResponse.StatusCode}, Reason: {usernamePasswordAuthResponse.ReasonPhrase}"
|
||||
};
|
||||
}
|
||||
|
||||
var usernamePasswordAuthResult = await usernamePasswordAuthResponse.Content.ReadFromJsonAsync<AuthResponse>(JSON_OPTIONS, cancellationToken);
|
||||
if(usernamePasswordAuthResult == default)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to authenticate with the server: the response was invalid."
|
||||
};
|
||||
}
|
||||
|
||||
this.securityToken = usernamePasswordAuthResult.Token ?? string.Empty;
|
||||
return new()
|
||||
{
|
||||
Successful = true,
|
||||
Data = usernamePasswordAuthResult
|
||||
};
|
||||
}
|
||||
|
||||
case AuthMethod.TOKEN:
|
||||
var tokenResponse = await rustService.GetSecret(dataSource);
|
||||
if (!tokenResponse.Success)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to retrieve the access token."
|
||||
};
|
||||
}
|
||||
|
||||
var token = await tokenResponse.Secret.Decrypt(Program.ENCRYPTION);
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Post, $"auth?authMethod={authMethod}"))
|
||||
{
|
||||
request.Headers.Add("Authorization", $"Bearer {token}");
|
||||
|
||||
using var tokenAuthResponse = await this.httpClient.SendAsync(request, cancellationToken);
|
||||
if(!tokenAuthResponse.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = $"Failed to authenticate with the ERI server. Code: {tokenAuthResponse.StatusCode}, Reason: {tokenAuthResponse.ReasonPhrase}"
|
||||
};
|
||||
}
|
||||
|
||||
var tokenAuthResult = await tokenAuthResponse.Content.ReadFromJsonAsync<AuthResponse>(JSON_OPTIONS, cancellationToken);
|
||||
if(tokenAuthResult == default)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to authenticate with the ERI server: the response was invalid."
|
||||
};
|
||||
}
|
||||
|
||||
this.securityToken = tokenAuthResult.Token ?? string.Empty;
|
||||
return new()
|
||||
{
|
||||
Successful = true,
|
||||
Data = tokenAuthResult
|
||||
};
|
||||
}
|
||||
|
||||
default:
|
||||
this.securityToken = string.Empty;
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "The authentication method is not supported yet."
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<APIResponse<DataSourceInfo>> GetDataSourceInfoAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, "/dataSource");
|
||||
request.Headers.Add("token", this.securityToken);
|
||||
|
||||
using var response = await this.httpClient.SendAsync(request, cancellationToken);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = $"Failed to retrieve the data source information: there was an issue communicating with the ERI server. Code: {response.StatusCode}, Reason: {response.ReasonPhrase}"
|
||||
};
|
||||
}
|
||||
|
||||
var dataSourceInfo = await response.Content.ReadFromJsonAsync<DataSourceInfo>(JSON_OPTIONS, cancellationToken);
|
||||
if(dataSourceInfo == default)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to retrieve the data source information: the ERI server did not return a valid response."
|
||||
};
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Successful = true,
|
||||
Data = dataSourceInfo
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<APIResponse<List<EmbeddingInfo>>> GetEmbeddingInfoAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, "/embedding/info");
|
||||
request.Headers.Add("token", this.securityToken);
|
||||
|
||||
using var response = await this.httpClient.SendAsync(request, cancellationToken);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = $"Failed to retrieve the embedding information: there was an issue communicating with the ERI server. Code: {response.StatusCode}, Reason: {response.ReasonPhrase}"
|
||||
};
|
||||
}
|
||||
|
||||
var embeddingInfo = await response.Content.ReadFromJsonAsync<List<EmbeddingInfo>>(JSON_OPTIONS, cancellationToken);
|
||||
if(embeddingInfo is null)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to retrieve the embedding information: the ERI server did not return a valid response."
|
||||
};
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Successful = true,
|
||||
Data = embeddingInfo
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<APIResponse<List<RetrievalInfo>>> GetRetrievalInfoAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, "/retrieval/info");
|
||||
request.Headers.Add("token", this.securityToken);
|
||||
|
||||
using var response = await this.httpClient.SendAsync(request, cancellationToken);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = $"Failed to retrieve the retrieval information: there was an issue communicating with the ERI server. Code: {response.StatusCode}, Reason: {response.ReasonPhrase}"
|
||||
};
|
||||
}
|
||||
|
||||
var retrievalInfo = await response.Content.ReadFromJsonAsync<List<RetrievalInfo>>(JSON_OPTIONS, cancellationToken);
|
||||
if(retrievalInfo is null)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to retrieve the retrieval information: the ERI server did not return a valid response."
|
||||
};
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Successful = true,
|
||||
Data = retrievalInfo
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<APIResponse<List<Context>>> ExecuteRetrievalAsync(RetrievalRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/retrieval");
|
||||
requestMessage.Headers.Add("token", this.securityToken);
|
||||
|
||||
using var content = new StringContent(JsonSerializer.Serialize(request, JSON_OPTIONS), Encoding.UTF8, "application/json");
|
||||
requestMessage.Content = content;
|
||||
|
||||
using var response = await this.httpClient.SendAsync(requestMessage, cancellationToken);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = $"Failed to execute the retrieval request: there was an issue communicating with the ERI server. Code: {response.StatusCode}, Reason: {response.ReasonPhrase}"
|
||||
};
|
||||
}
|
||||
|
||||
var contexts = await response.Content.ReadFromJsonAsync<List<Context>>(JSON_OPTIONS, cancellationToken);
|
||||
if(contexts is null)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to execute the retrieval request: the ERI server did not return a valid response."
|
||||
};
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Successful = true,
|
||||
Data = contexts
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<APIResponse<SecurityRequirements>> GetSecurityRequirementsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, "/security/requirements");
|
||||
request.Headers.Add("token", this.securityToken);
|
||||
|
||||
using var response = await this.httpClient.SendAsync(request, cancellationToken);
|
||||
if(!response.IsSuccessStatusCode)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = $"Failed to retrieve the security requirements: there was an issue communicating with the ERI server. Code: {response.StatusCode}, Reason: {response.ReasonPhrase}"
|
||||
};
|
||||
}
|
||||
|
||||
var securityRequirements = await response.Content.ReadFromJsonAsync<SecurityRequirements>(JSON_OPTIONS, cancellationToken);
|
||||
if(securityRequirements == default)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
Successful = false,
|
||||
Message = "Failed to retrieve the security requirements: the ERI server did not return a valid response."
|
||||
};
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Successful = true,
|
||||
Data = securityRequirements
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
namespace AIStudio.Tools.ERIClient;
|
||||
|
||||
public interface IERIClient : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieves the available authentication methods from the ERI server.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// No authentication is required to retrieve the available authentication methods.
|
||||
/// </remarks>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The available authentication methods.</returns>
|
||||
public Task<APIResponse<List<AuthScheme>>> GetAuthMethodsAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Authenticate the user to the ERI server.
|
||||
/// </summary>
|
||||
/// <param name="dataSource">The data source to use.</param>
|
||||
/// <param name="rustService">The Rust service.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The authentication response.</returns>
|
||||
public Task<APIResponse<AuthResponse>> AuthenticateAsync(IERIDataSource dataSource, RustService rustService, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the data source information from the ERI server.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The data source information.</returns>
|
||||
public Task<APIResponse<DataSourceInfo>> GetDataSourceInfoAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the embedding information from the ERI server.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>A list of embedding information.</returns>
|
||||
public Task<APIResponse<List<EmbeddingInfo>>> GetEmbeddingInfoAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the retrieval information from the ERI server.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>A list of retrieval information.</returns>
|
||||
public Task<APIResponse<List<RetrievalInfo>>> GetRetrievalInfoAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Executes a retrieval request on the ERI server.
|
||||
/// </summary>
|
||||
/// <param name="request">The retrieval request.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The retrieved contexts to use for augmentation and generation.</returns>
|
||||
public Task<APIResponse<List<Context>>> ExecuteRetrievalAsync(RetrievalRequest request, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the security requirements from the ERI server.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The security requirements.</returns>
|
||||
public Task<APIResponse<SecurityRequirements>> GetSecurityRequirementsAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace AIStudio.Tools;
|
||||
|
||||
public static class FileInfoExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the file size in human-readable format.
|
||||
/// </summary>
|
||||
/// <param name="fileInfo">The file info object.</param>
|
||||
/// <returns>The file size in human-readable format.</returns>
|
||||
public static string FileSize(this FileInfo fileInfo)
|
||||
{
|
||||
if (!fileInfo.Exists)
|
||||
return "N/A";
|
||||
|
||||
return fileInfo.Length.FileSize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace AIStudio.Tools;
|
||||
|
||||
public static class LongExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Formats the file size in a human-readable format.
|
||||
/// </summary>
|
||||
/// <param name="sizeBytes">The size in bytes.</param>
|
||||
/// <returns>The formatted file size.</returns>
|
||||
public static string FileSize(this long sizeBytes)
|
||||
{
|
||||
string[] sizes = { "B", "kB", "MB", "GB", "TB" };
|
||||
var order = 0;
|
||||
while (sizeBytes >= 1024 && order < sizes.Length - 1)
|
||||
{
|
||||
order++;
|
||||
sizeBytes /= 1024;
|
||||
}
|
||||
|
||||
return $"{sizeBytes:0.##} {sizes[order]}";
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using ERI_Client.V1;
|
||||
using AIStudio.Tools.ERIClient.DataModel;
|
||||
|
||||
namespace AIStudio.Tools.Validation;
|
||||
|
||||
|
||||
Reference in new issue
Block a user