mirror of
https://github.com/nomic-ai/gpt4all
synced 2024-11-08 07:10:32 +00:00
6ab38d8aea
* First workin version of the C# bindings * Update README.md Signed-off-by: mvenditto <venditto.matteo@gmail.com> * Added more docs + fixed prompt callback signature * build scripts revision * Added .editorconfig + fixed style issues --------- Signed-off-by: mvenditto <venditto.matteo@gmail.com>
83 lines
2.2 KiB
C#
83 lines
2.2 KiB
C#
using Gpt4All.Bindings;
|
|
using Gpt4All.Extensions;
|
|
|
|
namespace Gpt4All;
|
|
|
|
public class Gpt4All : IGpt4AllModel
|
|
{
|
|
private readonly ILLModel _model;
|
|
|
|
internal Gpt4All(ILLModel model)
|
|
{
|
|
_model = model;
|
|
}
|
|
|
|
public Task<ITextPredictionResult> GetPredictionAsync(string text, PredictRequestOptions opts, CancellationToken cancellationToken = default)
|
|
{
|
|
return Task.Run(() =>
|
|
{
|
|
var result = new TextPredictionResult();
|
|
var context = opts.ToPromptContext();
|
|
|
|
_model.Prompt(text, context, responseCallback: e =>
|
|
{
|
|
if (e.IsError)
|
|
{
|
|
result.Success = false;
|
|
result.ErrorMessage = e.Response;
|
|
return false;
|
|
}
|
|
result.Append(e.Response);
|
|
return true;
|
|
}, cancellationToken: cancellationToken);
|
|
|
|
return (ITextPredictionResult)result;
|
|
}, CancellationToken.None);
|
|
}
|
|
|
|
public Task<ITextPredictionStreamingResult> GetStreamingPredictionAsync(string text, PredictRequestOptions opts, CancellationToken cancellationToken = default)
|
|
{
|
|
var result = new TextPredictionStreamingResult();
|
|
|
|
_ = Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var context = opts.ToPromptContext();
|
|
|
|
_model.Prompt(text, context, responseCallback: e =>
|
|
{
|
|
if (e.IsError)
|
|
{
|
|
result.Success = false;
|
|
result.ErrorMessage = e.Response;
|
|
return false;
|
|
}
|
|
result.Append(e.Response);
|
|
return true;
|
|
}, cancellationToken: cancellationToken);
|
|
}
|
|
finally
|
|
{
|
|
result.Complete();
|
|
}
|
|
}, CancellationToken.None);
|
|
|
|
return Task.FromResult((ITextPredictionStreamingResult)result);
|
|
}
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
_model.Dispose();
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|