Skip to content

Commit

Permalink
Ollama: added method to delete a model (langchain4j#1557)
Browse files Browse the repository at this point in the history
## Change
Added a new method to `OllamaModels.deleteModel(...)`. It is now possible to delete an
Ollama model through the API.


## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [X] There are no breaking changes
- [X] I have added unit and integration tests for my change
- [X] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [X] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)
  • Loading branch information
bidek authored Aug 5, 2024
1 parent 38e025f commit e40b53c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.langchain4j.model.ollama;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
class DeleteModelRequest {

private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ interface OllamaApi {
@POST("api/show")
@Headers({"Content-Type: application/json"})
Call<OllamaModelCard> showInformation(@Body ShowModelInformationRequest modelDetailsRequest);

@HTTP(method = "DELETE", path = "/api/delete", hasBody = true)
@Headers({"Content-Type: application/json"})
Call<Void> deleteModel(@Body DeleteModelRequest deleteModelRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ public OllamaModelCard showInformation(ShowModelInformationRequest showInformati
}
}

public Void deleteModel(DeleteModelRequest deleteModelRequest) {
try {
retrofit2.Response<Void> retrofitResponse = ollamaApi.deleteModel(deleteModelRequest).execute();
if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private RuntimeException toException(retrofit2.Response<?> response) throws IOException {
int code = response.code();
String body = response.errorBody().string();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ public Response<OllamaModelCard> modelCard(String modelName) {
), maxRetries);
return Response.from(response);
}

public void deleteModel(OllamaModel ollamaModel) {
deleteModel(ollamaModel.getName());
}

public void deleteModel(String ollamaModelName) {
withRetry(() -> client.deleteModel(
DeleteModelRequest.builder()
.name(ollamaModelName)
.build()
), maxRetries);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,28 @@ void should_respond_with_model_information() {
assertThat(modelDetailsResponse.getDetails().getFormat()).isEqualTo("gguf");
assertThat(modelDetailsResponse.getDetails().getFamily()).isEqualTo("llama");
}

@Test
void should_delete_model() {
// given AbstractOllamaInfrastructure

OllamaClient ollamaClient = OllamaClient.builder()
.baseUrl(ollama.getEndpoint())
.logRequests(true)
.logResponses(true)
.timeout(Duration.ofMinutes(1))
.build();

ModelsListResponse beforeDeleteModelList = ollamaClient.listModels();
assertThat(beforeDeleteModelList.getModels().size()).isPositive();

// when
ollamaClient.deleteModel(DeleteModelRequest.builder()
.name("tinydolphin")
.build());

// then
ModelsListResponse afterDeleteModelList = ollamaClient.listModels();
assertThat(afterDeleteModelList.getModels().size()).isZero();
}
}

0 comments on commit e40b53c

Please sign in to comment.