mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 03:47:13 +02:00
Makes changes to OllamaChatResult backwards compatible
This commit is contained in:
parent
25694a8bc9
commit
b6a293add7
@ -33,7 +33,7 @@ public class Main {
|
|||||||
// start conversation with model
|
// start conversation with model
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
|
|
||||||
System.out.println("First answer: " + chatResult.getResponse());
|
System.out.println("First answer: " + chatResult.getResponseModel().getMessage().getContent());
|
||||||
|
|
||||||
// create next userQuestion
|
// create next userQuestion
|
||||||
requestModel = builder.withMessages(chatResult.getChatHistory()).withMessage(OllamaChatMessageRole.USER, "And what is the second largest city?").build();
|
requestModel = builder.withMessages(chatResult.getChatHistory()).withMessage(OllamaChatMessageRole.USER, "And what is the second largest city?").build();
|
||||||
@ -41,7 +41,7 @@ public class Main {
|
|||||||
// "continue" conversation with model
|
// "continue" conversation with model
|
||||||
chatResult = ollamaAPI.chat(requestModel);
|
chatResult = ollamaAPI.chat(requestModel);
|
||||||
|
|
||||||
System.out.println("Second answer: " + chatResult.getResponse());
|
System.out.println("Second answer: " + chatResult.getResponseModel().getMessage().getContent());
|
||||||
|
|
||||||
System.out.println("Chat History: " + chatResult.getChatHistory());
|
System.out.println("Chat History: " + chatResult.getChatHistory());
|
||||||
}
|
}
|
||||||
@ -205,7 +205,7 @@ public class Main {
|
|||||||
// start conversation with model
|
// start conversation with model
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
|
|
||||||
System.out.println(chatResult.getResponse());
|
System.out.println(chatResult.getResponseModel());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,7 +244,7 @@ public class Main {
|
|||||||
new File("/path/to/image"))).build();
|
new File("/path/to/image"))).build();
|
||||||
|
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
System.out.println("First answer: " + chatResult.getResponse());
|
System.out.println("First answer: " + chatResult.getResponseModel());
|
||||||
|
|
||||||
builder.reset();
|
builder.reset();
|
||||||
|
|
||||||
@ -254,7 +254,7 @@ public class Main {
|
|||||||
.withMessage(OllamaChatMessageRole.USER, "What's the dogs breed?").build();
|
.withMessage(OllamaChatMessageRole.USER, "What's the dogs breed?").build();
|
||||||
|
|
||||||
chatResult = ollamaAPI.chat(requestModel);
|
chatResult = ollamaAPI.chat(requestModel);
|
||||||
System.out.println("Second answer: " + chatResult.getResponse());
|
System.out.println("Second answer: " + chatResult.getResponseModel());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -3,7 +3,6 @@ package io.github.ollama4j.models.chat;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import io.github.ollama4j.models.response.OllamaResult;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
import static io.github.ollama4j.utils.Utils.getObjectMapper;
|
import static io.github.ollama4j.utils.Utils.getObjectMapper;
|
||||||
@ -18,12 +17,12 @@ public class OllamaChatResult {
|
|||||||
|
|
||||||
private List<OllamaChatMessage> chatHistory;
|
private List<OllamaChatMessage> chatHistory;
|
||||||
|
|
||||||
private OllamaChatResponseModel response;
|
private OllamaChatResponseModel responseModel;
|
||||||
|
|
||||||
public OllamaChatResult(OllamaChatResponseModel response, List<OllamaChatMessage> chatHistory) {
|
public OllamaChatResult(OllamaChatResponseModel responseModel, List<OllamaChatMessage> chatHistory) {
|
||||||
this.chatHistory = chatHistory;
|
this.chatHistory = chatHistory;
|
||||||
this.response = response;
|
this.responseModel = responseModel;
|
||||||
appendAnswerToChatHistory(response);
|
appendAnswerToChatHistory(responseModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void appendAnswerToChatHistory(OllamaChatResponseModel response) {
|
private void appendAnswerToChatHistory(OllamaChatResponseModel response) {
|
||||||
@ -38,4 +37,19 @@ public class OllamaChatResult {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
public String getResponse(){
|
||||||
|
return responseModel != null ? responseModel.getMessage().getContent() : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
public int getHttpStatusCode(){
|
||||||
|
return 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
public long getResponseTime(){
|
||||||
|
return responseModel != null ? responseModel.getTotalDuration() : 0L;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,9 +194,9 @@ class TestRealAPIs {
|
|||||||
|
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponse());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
assertNotNull(chatResult.getResponse().getMessage());
|
assertNotNull(chatResult.getResponseModel().getMessage());
|
||||||
assertFalse(chatResult.getResponse().getMessage().getContent().isBlank());
|
assertFalse(chatResult.getResponseModel().getMessage().getContent().isBlank());
|
||||||
assertEquals(4, chatResult.getChatHistory().size());
|
assertEquals(4, chatResult.getChatHistory().size());
|
||||||
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
||||||
fail(e);
|
fail(e);
|
||||||
@ -217,10 +217,10 @@ class TestRealAPIs {
|
|||||||
|
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponse());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
assertNotNull(chatResult.getResponse().getMessage());
|
assertNotNull(chatResult.getResponseModel().getMessage());
|
||||||
assertFalse(chatResult.getResponse().getMessage().getContent().isBlank());
|
assertFalse(chatResult.getResponseModel().getMessage().getContent().isBlank());
|
||||||
assertTrue(chatResult.getResponse().getMessage().getContent().startsWith("NI"));
|
assertTrue(chatResult.getResponseModel().getMessage().getContent().startsWith("NI"));
|
||||||
assertEquals(3, chatResult.getChatHistory().size());
|
assertEquals(3, chatResult.getChatHistory().size());
|
||||||
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
||||||
fail(e);
|
fail(e);
|
||||||
@ -270,10 +270,10 @@ class TestRealAPIs {
|
|||||||
|
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponse());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
assertNotNull(chatResult.getResponse().getMessage());
|
assertNotNull(chatResult.getResponseModel().getMessage());
|
||||||
assertEquals(OllamaChatMessageRole.ASSISTANT.getRoleName(),chatResult.getResponse().getMessage().getRole().getRoleName());
|
assertEquals(OllamaChatMessageRole.ASSISTANT.getRoleName(),chatResult.getResponseModel().getMessage().getRole().getRoleName());
|
||||||
List<OllamaChatToolCalls> toolCalls = chatResult.getResponse().getMessage().getToolCalls();
|
List<OllamaChatToolCalls> toolCalls = chatResult.getResponseModel().getMessage().getToolCalls();
|
||||||
assertEquals(1, toolCalls.size());
|
assertEquals(1, toolCalls.size());
|
||||||
assertEquals("get-employee-details",toolCalls.get(0).getFunction().getName());
|
assertEquals("get-employee-details",toolCalls.get(0).getFunction().getName());
|
||||||
assertEquals(1, toolCalls.get(0).getFunction().getArguments().size());
|
assertEquals(1, toolCalls.get(0).getFunction().getArguments().size());
|
||||||
@ -305,10 +305,10 @@ class TestRealAPIs {
|
|||||||
sb.append(substring);
|
sb.append(substring);
|
||||||
});
|
});
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponse());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
assertNotNull(chatResult.getResponse().getMessage());
|
assertNotNull(chatResult.getResponseModel().getMessage());
|
||||||
assertNotNull(chatResult.getResponse().getMessage().getContent());
|
assertNotNull(chatResult.getResponseModel().getMessage().getContent());
|
||||||
assertEquals(sb.toString().trim(), chatResult.getResponse().getMessage().getContent().trim());
|
assertEquals(sb.toString().trim(), chatResult.getResponseModel().getMessage().getContent().trim());
|
||||||
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
||||||
fail(e);
|
fail(e);
|
||||||
}
|
}
|
||||||
@ -327,7 +327,7 @@ class TestRealAPIs {
|
|||||||
|
|
||||||
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponse());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
|
|
||||||
builder.reset();
|
builder.reset();
|
||||||
|
|
||||||
@ -337,7 +337,7 @@ class TestRealAPIs {
|
|||||||
|
|
||||||
chatResult = ollamaAPI.chat(requestModel);
|
chatResult = ollamaAPI.chat(requestModel);
|
||||||
assertNotNull(chatResult);
|
assertNotNull(chatResult);
|
||||||
assertNotNull(chatResult.getResponse());
|
assertNotNull(chatResult.getResponseModel());
|
||||||
|
|
||||||
|
|
||||||
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
} catch (IOException | OllamaBaseException | InterruptedException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user