Class OllamaApiClient
- Namespace
- OllamaSharp
- Assembly
- OllamaSharp.dll
The default client to use the Ollama API conveniently. https://github.com/jmorganca/ollama/blob/main/docs/api.md
public class OllamaApiClient : IOllamaApiClient, IChatClient, IEmbeddingGenerator<string, Embedding<float>>, IEmbeddingGenerator, IDisposable
- Inheritance
-
OllamaApiClient
- Implements
-
IChatClientIEmbeddingGenerator
- Inherited Members
- Extension Methods
Constructors
OllamaApiClient(Configuration)
Creates a new instance of the Ollama API client.
public OllamaApiClient(OllamaApiClient.Configuration config)
Parameters
configOllamaApiClient.ConfigurationThe configuration for the Ollama API client.
OllamaApiClient(HttpClient, string, JsonSerializerContext?)
Creates a new instance of the Ollama API client.
public OllamaApiClient(HttpClient client, string defaultModel = "", JsonSerializerContext? jsonSerializerContext = null)
Parameters
clientHttpClientThe HTTP client to access the Ollama API with.
defaultModelstringThe default model that should be used with Ollama.
jsonSerializerContextJsonSerializerContextThe JSON serializer context for source generation (optional, for NativeAOT scenarios).
Exceptions
OllamaApiClient(string, string)
Creates a new instance of the Ollama API client.
public OllamaApiClient(string uriString, string defaultModel = "")
Parameters
uriStringstringThe URI of the Ollama API endpoint.
defaultModelstringThe default model that should be used with Ollama.
OllamaApiClient(Uri, string)
Creates a new instance of the Ollama API client.
public OllamaApiClient(Uri uri, string defaultModel = "")
Parameters
uriUriThe URI of the Ollama API endpoint.
defaultModelstringThe default model that should be used with Ollama.
Properties
Config
Gets the current configuration of the API client.
public OllamaApiClient.Configuration Config { get; }
Property Value
DefaultRequestHeaders
Gets the default request headers that are sent to the Ollama API.
public Dictionary<string, string> DefaultRequestHeaders { get; }
Property Value
IncomingJsonSerializerOptions
Gets the serializer options used for deserializing HTTP responses.
public JsonSerializerOptions IncomingJsonSerializerOptions { get; }
Property Value
OutgoingJsonSerializerOptions
Gets the serializer options for outgoing web requests like Post or Delete.
public JsonSerializerOptions OutgoingJsonSerializerOptions { get; }
Property Value
SelectedModel
Gets or sets the name of the model to run requests on.
public string SelectedModel { get; set; }
Property Value
Uri
Gets the endpoint URI used by the API client.
public Uri Uri { get; }
Property Value
Methods
ChatAsync(ChatRequest, CancellationToken)
Sends a request to the /api/chat endpoint and streams the response of the chat.
public IAsyncEnumerable<ChatResponseStream?> ChatAsync(ChatRequest request, CancellationToken cancellationToken = default)
Parameters
requestChatRequestThe request to send to Ollama.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- IAsyncEnumerable<ChatResponseStream>
An asynchronous enumerable that yields ChatResponseStream. Each item represents a message in the chat response stream. Returns null when the stream is completed.
Remarks
This is the method to call the Ollama endpoint /api/chat. You might not want to do this manually. To implement a fully interactive chat, you should make use of the Chat class with "new Chat(...)"
CopyModelAsync(CopyModelRequest, CancellationToken)
Sends a request to the /api/copy endpoint to copy a model.
public Task CopyModelAsync(CopyModelRequest request, CancellationToken cancellationToken = default)
Parameters
requestCopyModelRequestThe parameters required to copy a model.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
CreateModelAsync(CreateModelRequest, CancellationToken)
Sends a request to the /api/create endpoint to create a model.
public IAsyncEnumerable<CreateModelResponse?> CreateModelAsync(CreateModelRequest request, CancellationToken cancellationToken = default)
Parameters
requestCreateModelRequestThe request object containing the model details.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- IAsyncEnumerable<CreateModelResponse>
An asynchronous enumerable of the model creation status.
DeleteModelAsync(DeleteModelRequest, CancellationToken)
Sends a request to the /api/delete endpoint to delete a model.
public Task DeleteModelAsync(DeleteModelRequest request, CancellationToken cancellationToken = default)
Parameters
requestDeleteModelRequestThe request containing the model to delete.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
Dispose()
Releases the resources used by the OllamaApiClient instance. Disposes the internal HTTP client if it was created internally.
public void Dispose()
EmbedAsync(EmbedRequest, CancellationToken)
Sends a request to the /api/embed endpoint to generate embeddings.
public Task<EmbedResponse> EmbedAsync(EmbedRequest request, CancellationToken cancellationToken = default)
Parameters
requestEmbedRequestThe parameters to generate embeddings for.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- Task<EmbedResponse>
A task that represents the asynchronous operation. The task result contains the EmbedResponse.
GenerateAsync(GenerateRequest, CancellationToken)
Streams completion responses from the /api/generate endpoint on the Ollama API based on the provided request.
public IAsyncEnumerable<GenerateResponseStream?> GenerateAsync(GenerateRequest request, CancellationToken cancellationToken = default)
Parameters
requestGenerateRequestThe request containing the parameters for the completion.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- IAsyncEnumerable<GenerateResponseStream>
An asynchronous enumerable of GenerateResponseStream.
GetVersionAsync(CancellationToken)
Gets the version of Ollama.
public Task<string> GetVersionAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- Task<string>
A task that represents the asynchronous operation. The task result contains the Version.
IsBlobExistsAsync(string, CancellationToken)
Ensures that the file blob (Binary Large Object) used with create a model exists on the server. This checks your Ollama server and not ollama.com.
public Task<bool> IsBlobExistsAsync(string digest, CancellationToken cancellationToken = default)
Parameters
digeststringThe expected SHA256 digest of the file.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
IsRunningAsync(CancellationToken)
Sends a query to check whether the Ollama API is running or not.
public Task<bool> IsRunningAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result contains a boolean indicating whether the API is running.
ListLocalModelsAsync(CancellationToken)
Sends a request to the /api/tags endpoint to get all models that are available locally.
public Task<IEnumerable<Model>> ListLocalModelsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- Task<IEnumerable<Model>>
A task that represents the asynchronous operation. The task result contains a collection of Model.
ListRunningModelsAsync(CancellationToken)
Sends a request to the /api/ps endpoint to get the running models.
public Task<IEnumerable<RunningModel>> ListRunningModelsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- Task<IEnumerable<RunningModel>>
A task that represents the asynchronous operation. The task result contains a collection of RunningModel.
PullModelAsync(PullModelRequest, CancellationToken)
Sends a request to the /api/pull endpoint to pull a new model.
public IAsyncEnumerable<PullModelResponse?> PullModelAsync(PullModelRequest request, CancellationToken cancellationToken = default)
Parameters
requestPullModelRequestThe request specifying the model name and whether to use an insecure connection.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- IAsyncEnumerable<PullModelResponse>
An asynchronous enumerable of PullModelResponse objects representing the status of the model pull operation.
PushBlobAsync(string, byte[], CancellationToken)
Push a file to the Ollama server to create a "blob" (Binary Large Object).
public Task PushBlobAsync(string digest, byte[] bytes, CancellationToken cancellationToken = default)
Parameters
digeststringThe expected SHA256 digest of the file.
bytesbyte[]The bytes data of the file.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
PushModelAsync(PushModelRequest, CancellationToken)
Pushes a model to the Ollama API endpoint.
public IAsyncEnumerable<PushModelResponse?> PushModelAsync(PushModelRequest request, CancellationToken cancellationToken = default)
Parameters
requestPushModelRequestThe request containing the model information to push.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- IAsyncEnumerable<PushModelResponse>
An asynchronous enumerable of push status updates. Use the enumerator to retrieve the push status updates.
SendToOllamaAsync(HttpRequestMessage, OllamaRequest?, HttpCompletionOption, CancellationToken)
Sends an HTTP request message to the Ollama API.
protected virtual Task<HttpResponseMessage> SendToOllamaAsync(HttpRequestMessage requestMessage, OllamaRequest? ollamaRequest, HttpCompletionOption completionOption, CancellationToken cancellationToken)
Parameters
requestMessageHttpRequestMessageThe HTTP request message to send.
ollamaRequestOllamaRequestThe request containing custom HTTP request headers.
completionOptionHttpCompletionOptionWhen the operation should complete (as soon as a response is available or after reading the whole response content).
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
ShowModelAsync(ShowModelRequest, CancellationToken)
Sends a request to the /api/show endpoint to show the information of a model.
public Task<ShowModelResponse> ShowModelAsync(ShowModelRequest request, CancellationToken cancellationToken = default)
Parameters
requestShowModelRequestThe request containing the name of the model to get the information for.
cancellationTokenCancellationTokenThe token to cancel the operation with.
Returns
- Task<ShowModelResponse>
A task that represents the asynchronous operation. The task result contains the ShowModelResponse.