Skip to content

Commit

Permalink
fix: Fixed latest commit. Added more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed May 12, 2024
1 parent a916951 commit d601ce8
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
4 changes: 2 additions & 2 deletions docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ components:
type: array
description: List of models available locally.
items:
$ref: '#/components/schemas/Model'
Model:
$ref: '#/components/schemas/LocalModel'
LocalModel:
type: object
description: A model available locally.
properties:
Expand Down
2 changes: 1 addition & 1 deletion src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<PropertyGroup Label="Nuget">
<Version>0.9.5</Version>
<Version>1.0.0</Version>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>tryAGI and contributors</Authors>
Expand Down
6 changes: 3 additions & 3 deletions src/libs/Ollama/OllamaApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public async Task DeleteModelAsync(
{
using var request = new HttpRequestMessage(HttpMethod.Delete, "api/delete");
request.Content = new StringContent(
JsonSerializer.Serialize(new DeleteModelRequest { Name = model }, SourceGenerationContext.Default.DeleteModelRequest),
JsonSerializer.Serialize(new DeleteModelRequest { Model = model }, SourceGenerationContext.Default.DeleteModelRequest),
Encoding.UTF8,
"application/json");

Expand All @@ -94,7 +94,7 @@ public async Task DeleteModelAsync(
/// Sends a request to the /api/tags endpoint to get all models that are available locally
/// </summary>
/// <param name="cancellationToken">The token to cancel the operation with</param>
public async Task<IReadOnlyList<Model>> ListModelsAsync(
public async Task<IReadOnlyList<LocalModel>> ListLocalModelsAsync(
CancellationToken cancellationToken = default)
{
var data = await GetAsync(
Expand All @@ -120,7 +120,7 @@ public async Task<ModelInfo> ShowModelInformationAsync(
{
return await PostAsync(
"api/show",
new ModelInfoRequest { Name = model },
new ModelInfoRequest { Model = model },
SourceGenerationContext.Default.ModelInfoRequest,
SourceGenerationContext.Default.ModelInfo,
cancellationToken).ConfigureAwait(false);
Expand Down
23 changes: 20 additions & 3 deletions src/libs/Ollama/OllamaApiClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static async IAsyncEnumerable<CreateModelResponse> CreateModelAsync(

var enumerable = client.CreateModelAsync(new CreateModelRequest
{
Name = name,
Model = name,
Modelfile = modelFileContent,
Path = path,
Stream = true,
Expand All @@ -101,7 +101,24 @@ public static async IAsyncEnumerable<CreateModelResponse> CreateModelAsync(
yield return response;
}
}

/// <summary>
/// Throws an InvalidOperationException if the response is not successful.
/// </summary>
/// <param name="response"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public static void EnsureSuccess(
this PullModelResponse response)
{
response = response ?? throw new ArgumentNullException(nameof(response));

if (response.Status != "success")
{
throw new InvalidOperationException($"Failed to pull model with status {response.Status}");
}
}

/// <summary>
/// Sends a request to the /api/pull endpoint to pull a new model
/// </summary>
Expand All @@ -117,7 +134,7 @@ public static async IAsyncEnumerable<PullModelResponse> PullModelAsync(

var enumerable = client.PullModelAsync(new PullModelRequest
{
Name = model,
Model = model,
}, cancellationToken).ConfigureAwait(false);

await foreach (var response in enumerable)
Expand All @@ -141,7 +158,7 @@ public static async IAsyncEnumerable<PushModelResponse> PushModelAsync(

var enumerable = client.PushModelAsync(new PushModelRequest
{
Name = name,
Model = name,
Stream = true,
}, cancellationToken).ConfigureAwait(false);

Expand Down
25 changes: 24 additions & 1 deletion src/tests/Ollama.IntegrationTests/Tests.Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,15 @@ public async Task ListModels()
{
await using var container = await PrepareEnvironmentAsync(EnvironmentType.Container);

var models = await container.ApiClient.ListModelsAsync();
var models = await container.ApiClient.ListLocalModelsAsync();
models.Should().NotBeNull();
models.Should().BeEmpty();

await container.ApiClient.PullModelAsync("nomic-embed-text").WaitAsync();

models = await container.ApiClient.ListLocalModelsAsync();
models.Should().HaveCount(1);
models[0].Model.Should().Be("nomic-embed-text:latest");
}

[TestMethod]
Expand All @@ -78,6 +84,23 @@ public async Task PullModel()
await container.ApiClient.PullModelAsync("nomic-embed-text").WaitAsync();
}

[TestMethod]
public async Task Embeddings()
{
await using var container = await PrepareEnvironmentAsync(EnvironmentType.Container);

var pullResponse = await container.ApiClient.PullModelAsync("nomic-embed-text").WaitAsync();
pullResponse.EnsureSuccess();

var embeddingResponse = await container.ApiClient.GenerateEmbeddingsAsync(new GenerateEmbeddingRequest
{
Model = "nomic-embed-text",
Prompt = "hello",
});

embeddingResponse.Embedding.Should().NotBeNullOrEmpty();
}

[TestMethod]
public async Task GetCompletionWithOptions()
{
Expand Down
6 changes: 3 additions & 3 deletions src/tests/Ollama.IntegrationTests/Tests.Streaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ public async Task Returns_Deserialized_Models()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(
"{\r\n\"models\": [\r\n{\r\n\"name\": \"codellama:latest\",\r\n\"modified_at\": \"2023-10-12T14:17:04.967950259+02:00\",\r\n\"size\": 3791811617,\r\n\"digest\": \"36893bf9bc7ff7ace56557cd28784f35f834290c85d39115c6b91c00a031cfad\"\r\n},\r\n{\r\n\"name\": \"llama2:latest\",\r\n\"modified_at\": \"2023-10-02T14:10:14.78152065+02:00\",\r\n\"size\": 3791737662,\r\n\"digest\": \"d5611f7c428cf71fb05660257d18e043477f8b46cf561bf86940c687c1a59f70\"\r\n},\r\n{\r\n\"name\": \"mistral:latest\",\r\n\"modified_at\": \"2023-10-02T14:16:24.841447764+02:00\",\r\n\"size\": 4108916688,\r\n\"digest\": \"8aa307f73b2622af521e8f22d46e4b777123c4df91898dcb2e4079dc8fdf579e\"\r\n},\r\n{\r\n\"name\": \"vicuna:latest\",\r\n\"modified_at\": \"2023-10-06T09:44:16.936312659+02:00\",\r\n\"size\": 3825517709,\r\n\"digest\": \"675fa173a76abc48325d395854471961abf74b664d91e92ffb4fc03e0bde652b\"\r\n}\r\n]\r\n}\r\n")
"{\r\n\"models\": [\r\n{\r\n\"model\": \"codellama:latest\",\r\n\"modified_at\": \"2023-10-12T14:17:04.967950259+02:00\",\r\n\"size\": 3791811617,\r\n\"digest\": \"36893bf9bc7ff7ace56557cd28784f35f834290c85d39115c6b91c00a031cfad\"\r\n},\r\n{\r\n\"name\": \"llama2:latest\",\r\n\"modified_at\": \"2023-10-02T14:10:14.78152065+02:00\",\r\n\"size\": 3791737662,\r\n\"digest\": \"d5611f7c428cf71fb05660257d18e043477f8b46cf561bf86940c687c1a59f70\"\r\n},\r\n{\r\n\"name\": \"mistral:latest\",\r\n\"modified_at\": \"2023-10-02T14:16:24.841447764+02:00\",\r\n\"size\": 4108916688,\r\n\"digest\": \"8aa307f73b2622af521e8f22d46e4b777123c4df91898dcb2e4079dc8fdf579e\"\r\n},\r\n{\r\n\"name\": \"vicuna:latest\",\r\n\"modified_at\": \"2023-10-06T09:44:16.936312659+02:00\",\r\n\"size\": 3825517709,\r\n\"digest\": \"675fa173a76abc48325d395854471961abf74b664d91e92ffb4fc03e0bde652b\"\r\n}\r\n]\r\n}\r\n")
});

var models = await client.ListModelsAsync(CancellationToken.None);
var models = await client.ListLocalModelsAsync(CancellationToken.None);
models.Count().Should().Be(4);

var first = models.First();
first.Name.Should().Be("codellama:latest");
first.Model.Should().Be("codellama:latest");
first.ModifiedAt.Date.Should().Be(new DateTime(2023, 10, 12));
first.Size.Should().Be(3791811617);
first.Digest.Should().StartWith("36893bf9bc7ff7ace5655");
Expand Down

0 comments on commit d601ce8

Please sign in to comment.