Skip to content

Commit

Permalink
chore: tdding ollama client helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nickchecan committed Jan 16, 2025
1 parent f8b7e88 commit c170a0d
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.developer.nefarious.zjoule.plugin.login.api;

import java.util.List;

import com.developer.nefarious.zjoule.plugin.models.OllamaModel;

public class GetOllamaModelsResponse {

private List<OllamaModel> models;

// Getters and Setters
public List<OllamaModel> getModels() {
return models;
}

public void setModels(final List<OllamaModel> models) {
this.models = models;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.developer.nefarious.zjoule.plugin.login.api;

public interface IOllamaLoginClient {

GetOllamaModelsResponse getModels();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.developer.nefarious.zjoule.plugin.login.api;

import java.net.URI;

public interface IOllamaLoginClientHelper {

URI createUri(final String endpoint);

GetOllamaModelsResponse parseOllamaModelsResponseToObject(final String responseBody);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.developer.nefarious.zjoule.plugin.login.api;

public class OllamaLoginClient implements IOllamaLoginClient {

@Override
public GetOllamaModelsResponse getModels() {
// TODO Auto-generated method stub
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.developer.nefarious.zjoule.plugin.login.api;

import java.net.URI;

import com.google.gson.Gson;

public class OllamaLoginClientHelper implements IOllamaLoginClientHelper {

private Gson gson;

public OllamaLoginClientHelper() {
gson = new Gson();
}

@Override
public URI createUri(final String endpoint) {
return URI.create(endpoint);
}

@Override
public GetOllamaModelsResponse parseOllamaModelsResponseToObject(final String responseBody) {
return gson.fromJson(responseBody, GetOllamaModelsResponse.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public SecondOllamaLoginPage() {
}

@Override
public void createControl(Composite parent) {
public void createControl(final Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout(2, false));

Expand All @@ -46,7 +46,7 @@ private void selectModel() {
setPageComplete(true);
}

public void setModelsForSelection(List<String> modelsForSelection) {
public void setModelsForSelection(final List<String> modelsForSelection) {
modelDropdown.setItems(modelsForSelection.toArray(new String[0]));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.developer.nefarious.zjoule.plugin.models;

import java.util.List;

import com.google.gson.annotations.SerializedName;

public class OllamaModel {

private String name;

private String model;

@SerializedName("modified_at")
private String modifiedAt;

private long size;

private String digest;

private OllamaModelDetails details;

// Getters and Setters
public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}

public String getModel() {
return model;
}

public void setModel(final String model) {
this.model = model;
}

public String getModifiedAt() {
return modifiedAt;
}

public void setModifiedAt(final String modifiedAt) {
this.modifiedAt = modifiedAt;
}

public long getSize() {
return size;
}

public void setSize(final long size) {
this.size = size;
}

public String getDigest() {
return digest;
}

public void setDigest(final String digest) {
this.digest = digest;
}

public OllamaModelDetails getDetails() {
return details;
}

public void setDetails(final OllamaModelDetails details) {
this.details = details;
}

}

class OllamaModelDetails {

@SerializedName("parent_model")
private String parentModel;

private String format;

private String family;

private List<String> families;

@SerializedName("parameter_size")
private String parameterSize;

@SerializedName("quantization_level")
private String quantizationLevel;

// Getters and Setters
public String getParentModel() {
return parentModel;
}

public void setParentModel(final String parentModel) {
this.parentModel = parentModel;
}

public String getFormat() {
return format;
}

public void setFormat(final String format) {
this.format = format;
}

public String getFamily() {
return family;
}

public void setFamily(final String family) {
this.family = family;
}

public List<String> getFamilies() {
return families;
}

public void setFamilies(final List<String> families) {
this.families = families;
}

public String getParameterSize() {
return parameterSize;
}

public void setParameterSize(final String parameterSize) {
this.parameterSize = parameterSize;
}

public String getQuantizationLevel() {
return quantizationLevel;
}

public void setQuantizationLevel(final String quantizationLevel) {
this.quantizationLevel = quantizationLevel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.developer.nefarious.zjoule.test.login.api;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;

import java.net.URI;
import java.util.concurrent.ThreadLocalRandom;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import com.developer.nefarious.zjoule.plugin.login.api.GetOllamaModelsResponse;
import com.developer.nefarious.zjoule.plugin.login.api.OllamaLoginClientHelper;
import com.google.gson.Gson;

public class OllamaLoginClientHelperTest {

private OllamaLoginClientHelper cut;

private String randomWord() {
final String[] WORDS = { "apple", "banana", "grape" };
int randomIndex = ThreadLocalRandom.current().nextInt(WORDS.length);
return WORDS[randomIndex];
}

@BeforeEach
public void setUp() {
cut = new OllamaLoginClientHelper();
}

@Test
public void shouldConvertTheModelsResponseBodyToObject() {
// Arrange
Gson gson = new Gson();
String mockResponseBody = "{\"models\": [{\"name\": \"llama3.2:latest\"}]}";
GetOllamaModelsResponse expectedObject = gson.fromJson(mockResponseBody, GetOllamaModelsResponse.class);
// Act
GetOllamaModelsResponse returnObject = cut.parseOllamaModelsResponseToObject(mockResponseBody);
// Assert
assertEquals(returnObject.getModels().getFirst().getName(), expectedObject.getModels().getFirst().getName());
}

@Test
public void shouldCreateTheUri() {
// Arrange
URI expectedObject = mock(URI.class);
String mockEndpoint = randomWord();
try (MockedStatic<URI> uriStatic = mockStatic(URI.class)) {
uriStatic.when(() -> URI.create(mockEndpoint)).thenReturn(expectedObject);
// Act
URI returnObject = cut.createUri(mockEndpoint);
// Assert
assertEquals(returnObject, expectedObject);
}
}

}

0 comments on commit c170a0d

Please sign in to comment.