last ...
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
|
||||
namespace xAiApi.Helpers
|
||||
{
|
||||
public class XOllamaHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Get Version ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static async Task<string> GetVersion()
|
||||
{
|
||||
//
|
||||
var cmd = "ollama";
|
||||
var args = "--version";
|
||||
var cmdResult = await XOsHelper.Execute(cmd, args);
|
||||
|
||||
//
|
||||
var result =
|
||||
cmdResult.Error.IsNullOrEmpty()
|
||||
? cmdResult.Result
|
||||
: cmdResult.Error;
|
||||
|
||||
//
|
||||
result = result.Replace("\n", "");
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Exists Models ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static async Task<IEnumerable<string>> GetModels()
|
||||
{
|
||||
//
|
||||
var cmd = "ollama";
|
||||
var args = "list";
|
||||
var cmdResult = await XOsHelper.Execute(cmd, args);
|
||||
|
||||
//
|
||||
var cmdResultText =
|
||||
cmdResult.Error.IsNullOrEmpty()
|
||||
? cmdResult.Result
|
||||
: cmdResult.Error;
|
||||
|
||||
//
|
||||
var cmdResultList = cmdResultText
|
||||
.Split("\n")
|
||||
.ToList();
|
||||
if (cmdResultList.Count > 0)
|
||||
{
|
||||
cmdResultList.RemoveAt(0);
|
||||
}
|
||||
|
||||
//
|
||||
var result = new List<string>();
|
||||
if (cmdResultList.Count > 0)
|
||||
{
|
||||
//
|
||||
cmdResultList
|
||||
.ForEach(i =>
|
||||
{
|
||||
//
|
||||
if (!i.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
var d = i.Split(" ");
|
||||
if (d.Length > 0)
|
||||
{
|
||||
result.Add(d[0]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Specified Model Exists ...
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<bool> HasModel(string model)
|
||||
{
|
||||
//
|
||||
var result = !model.IsNullOrEmpty();
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
var models = await GetModels();
|
||||
result = models.HasChild();
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = models
|
||||
.Any(n => n == model || n.ToNormalString().Contains(model.ToNormalString()));
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Search Available Models ...
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<IEnumerable<string>> FindModel(string model)
|
||||
{
|
||||
//
|
||||
var result = new List<string>();
|
||||
|
||||
//
|
||||
if (model.IsNullOrEmpty())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
var models = await GetModels();
|
||||
if (!models.HasChild())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = models
|
||||
.Where(n => n == model || n.ToNormalString().Contains(model.ToNormalString()))
|
||||
.ToList();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Attributes;
|
||||
using xExceptions.Constants;
|
||||
|
||||
namespace xAiApi.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Several OS Type ...
|
||||
/// </summary>
|
||||
public enum XOSType
|
||||
{
|
||||
[StringValue("Unknown")]
|
||||
XUnknown,
|
||||
|
||||
[StringValue("Mac")]
|
||||
XMacOs,
|
||||
|
||||
[StringValue("Linux")]
|
||||
XLinux,
|
||||
|
||||
[StringValue("Windows")]
|
||||
XWindows,
|
||||
|
||||
[StringValue("FreeBSD")]
|
||||
XFreeBSD,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OS Command Execution Result ...
|
||||
/// </summary>
|
||||
public class XOSCMDResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Execution Result ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string Result { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Execution Error ...
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string Error { get; set; }
|
||||
}
|
||||
|
||||
public static class XOsHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieve OS Type ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static XOSType GetOSType()
|
||||
{
|
||||
//
|
||||
XOSType result = XOSType.XUnknown;
|
||||
|
||||
//
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
result = XOSType.XMacOs;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
result = XOSType.XLinux;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
result = XOSType.XWindows;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
|
||||
{
|
||||
result = XOSType.XFreeBSD;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve OS Description ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetOSDescription()
|
||||
{
|
||||
//
|
||||
var result = RuntimeInformation.OSDescription;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute an Specified Command ...
|
||||
/// </summary>
|
||||
/// <param name="cmd">Specified Command</param>
|
||||
/// <param name="args">Command Arguments</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<XOSCMDResult> Execute(
|
||||
string cmd,
|
||||
string args
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = new XOSCMDResult
|
||||
{
|
||||
Error = "Unknown",
|
||||
};
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate CMD ...
|
||||
var isValid = !cmd.IsNullOrEmpty();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Validate OS Type ...
|
||||
XOSType osType = GetOSType();
|
||||
isValid = osType != XOSType.XUnknown;
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
ProcessStartInfo psi = new ProcessStartInfo();
|
||||
|
||||
//
|
||||
psi.FileName = cmd;
|
||||
psi.Arguments = args;
|
||||
psi.UseShellExecute = false;
|
||||
psi.RedirectStandardError = true;
|
||||
psi.RedirectStandardOutput = true;
|
||||
|
||||
//
|
||||
Process process = Process.Start(psi);
|
||||
|
||||
//
|
||||
// Read the output
|
||||
string output = process.StandardOutput.ReadToEnd();
|
||||
string error = process.StandardError.ReadToEnd();
|
||||
|
||||
//
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
//
|
||||
result = new XOSCMDResult
|
||||
{
|
||||
Error = error,
|
||||
Result = output,
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Error = ex.Message;
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user