This is an automated email from the ASF dual-hosted git repository.

aldettinger pushed a commit to branch data-extraction-example
in repository https://gitbox.apache.org/repos/asf/camel-quarkus-examples.git

commit c23b388c0835e531b98998007b3fa46f988bef25
Author: aldettinger <aldettin...@gmail.com>
AuthorDate: Wed Aug 14 15:42:32 2024 +0200

    Improved OllamaTestResource management of tags and modes
---
 .../org/acme/extraction/OllamaTestResource.java    | 33 ++++++++++++++--------
 ...chat-6942a475-c748-48c5-a955-20fb23d5abd9.json} | 10 +++----
 2 files changed, 26 insertions(+), 17 deletions(-)

diff --git 
a/data-extract-langchain4j/src/test/java/org/acme/extraction/OllamaTestResource.java
 
b/data-extract-langchain4j/src/test/java/org/acme/extraction/OllamaTestResource.java
index 18c42f8..8c70833 100644
--- 
a/data-extract-langchain4j/src/test/java/org/acme/extraction/OllamaTestResource.java
+++ 
b/data-extract-langchain4j/src/test/java/org/acme/extraction/OllamaTestResource.java
@@ -1,5 +1,6 @@
 package org.acme.extraction;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -16,7 +17,8 @@ import static java.lang.String.format;
 public class OllamaTestResource implements QuarkusTestResourceLifecycleManager 
{
 
     private static final Logger LOG = 
LoggerFactory.getLogger(OllamaTestResource.class);
-    // TODO: don't use latest version, switch to explicit version
+
+    // langchain4j offers only latest tag for ollama-codellama, hence we do 
use latest until more tags are introduced
     private static final String OLLAMA_IMAGE = 
"langchain4j/ollama-codellama:latest";
     private static final int OLLAMA_SERVER_PORT = 11434;
 
@@ -27,15 +29,17 @@ public class OllamaTestResource implements 
QuarkusTestResourceLifecycleManager {
 
     private static final String MODE_MOCK = "mock";
     private static final String MODE_RECORDING = "record";
+    private static final String MODE_CONTAINER = "container";
 
     /**
      * The testMode value could be defined, for instance by invoking: mvn 
clean test -DtestMode=mock. With the default
      * value "mock", the llm is faked based on the last recorded run. With the 
value "record", tests are run against a
-     * containerized llm while the HTTP interactions with the llm are 
recorded. Any other value would run the test
-     * against a containzeried llm without recording.
+     * containerized llm while the HTTP interactions with the llm are 
recorded. With the value "container" tests are run
+     * against a containerized llm without recording. With any other value, an 
exception is thrown.
      */
-    private boolean mock;
-    private boolean recording;
+    private boolean isMockMode;
+    private boolean isRecordingMode;
+    private boolean isContainerMode;
 
     private static final String BASE_URL_FORMAT = "http://%s:%s";;
 
@@ -44,12 +48,17 @@ public class OllamaTestResource implements 
QuarkusTestResourceLifecycleManager {
 
         // Check the test running mode
         String testMode = System.getProperty("testMode", MODE_MOCK);
-        mock = MODE_MOCK.equals(testMode);
-        recording = MODE_RECORDING.equals(testMode);
+        isMockMode = MODE_MOCK.equals(testMode);
+        isRecordingMode = MODE_RECORDING.equals(testMode);
+        isContainerMode = MODE_CONTAINER.equals(testMode);
+        if (!isMockMode && !isRecordingMode && !isContainerMode) {
+            throw new IllegalArgumentException(
+                    "testMode value should be one of " + 
Arrays.asList(MODE_MOCK, MODE_RECORDING, MODE_CONTAINER));
+        }
 
         Map<String, String> properties = new HashMap<>();
 
-        if (mock) {
+        if (isMockMode) {
             LOG.info("Starting a fake Ollama server backed by wiremock");
             initWireMockServer();
         } else {
@@ -62,7 +71,7 @@ public class OllamaTestResource implements 
QuarkusTestResourceLifecycleManager {
 
             baseUrl = format(BASE_URL_FORMAT, ollamaContainer.getHost(), 
ollamaContainer.getMappedPort(OLLAMA_SERVER_PORT));
 
-            if (recording) {
+            if (isRecordingMode) {
                 LOG.info("Recording interactions with the Ollama server backed 
by testcontainers");
                 initWireMockServer();
             }
@@ -76,7 +85,7 @@ public class OllamaTestResource implements 
QuarkusTestResourceLifecycleManager {
     private void initWireMockServer() {
         wireMockServer = new WireMockServer();
         wireMockServer.start();
-        if (recording) {
+        if (isRecordingMode) {
             wireMockServer.resetMappings();
             wireMockServer.startRecording(baseUrl);
         }
@@ -93,9 +102,9 @@ public class OllamaTestResource implements 
QuarkusTestResourceLifecycleManager {
             LOG.error("An issue occurred while stopping " + 
ollamaContainer.getNetworkAliases(), ex);
         }
 
-        if (mock) {
+        if (isMockMode) {
             wireMockServer.stop();
-        } else if (recording) {
+        } else if (isRecordingMode) {
             wireMockServer.stopRecording();
             wireMockServer.saveMappings();
         }
diff --git 
a/data-extract-langchain4j/src/test/resources/mappings/api_chat-64441899-7a8b-456d-9dba-8dcf21cdc6e6.json
 
b/data-extract-langchain4j/src/test/resources/mappings/api_chat-6942a475-c748-48c5-a955-20fb23d5abd9.json
similarity index 78%
rename from 
data-extract-langchain4j/src/test/resources/mappings/api_chat-64441899-7a8b-456d-9dba-8dcf21cdc6e6.json
rename to 
data-extract-langchain4j/src/test/resources/mappings/api_chat-6942a475-c748-48c5-a955-20fb23d5abd9.json
index 6477e57..7aaa638 100644
--- 
a/data-extract-langchain4j/src/test/resources/mappings/api_chat-64441899-7a8b-456d-9dba-8dcf21cdc6e6.json
+++ 
b/data-extract-langchain4j/src/test/resources/mappings/api_chat-6942a475-c748-48c5-a955-20fb23d5abd9.json
@@ -1,24 +1,24 @@
 {
-  "id" : "64441899-7a8b-456d-9dba-8dcf21cdc6e6",
+  "id" : "6942a475-c748-48c5-a955-20fb23d5abd9",
   "name" : "api_chat",
   "request" : {
     "url" : "/api/chat",
     "method" : "POST",
     "bodyPatterns" : [ {
-      "equalToJson" : "{\n  \"model\" : \"codellama\",\n  \"messages\" : [ {\n 
   \"role\" : \"user\",\n    \"content\" : \"Extract information about a 
customer from the text delimited by triple backticks: ```Operator: Hello, how 
may I help you?\\nCustomer: Hello, I am currently at the police station because 
I've got an accident. The police would need a proof that I have an insurance. 
Could you please help me?\\nOperator: Sure, could you please remind me your 
name and birth date?\\nCusto [...]
+      "equalToJson" : "{\n  \"model\" : \"codellama\",\n  \"messages\" : [ {\n 
   \"role\" : \"user\",\n    \"content\" : \"Extract information about a 
customer from the text delimited by triple backticks: ```Operator: Hello, how 
may I help you?\\nCustomer: Hello, I am currently at the police station because 
I've got an accident. The police would need a proof that I have an insurance. 
Could you please help me?\\nOperator: Sure, could you please remind me your 
name and birth date?\\nCusto [...]
       "ignoreArrayOrder" : true,
       "ignoreExtraElements" : true
     } ]
   },
   "response" : {
     "status" : 200,
-    "body" : 
"{\"model\":\"codellama\",\"created_at\":\"2024-07-26T13:48:19.801608167Z\",\"message\":{\"role\":\"assistant\",\"content\":\"{\\n\\\"customerSatisfied\\\":
 true,\\n\\\"customerName\\\": \\\"Kate Boss\\\",\\n\\\"customerBirthday\\\": 
\\\"1999-08-13\\\",\\n\\\"summary\\\": \\\"Customer Kate Boss is satisfied with 
the assistance provided by the operator. The customer was able to provide their 
name and birth date correctly, and the operator was able to locate their 
insurance co [...]
+    "body" : 
"{\"model\":\"codellama\",\"created_at\":\"2024-08-14T13:38:15.143402928Z\",\"message\":{\"role\":\"assistant\",\"content\":\"{\\n\\\"customerSatisfied\\\":
 true,\\n\\\"customerName\\\": \\\"Kate Boss\\\",\\n\\\"customerBirthday\\\": 
\\\"1999-08-13\\\",\\n\\\"summary\\\": \\\"Customer Kate Boss is satisfied with 
the assistance provided by the operator. The customer was able to provide their 
name and birth date correctly, and the operator was able to locate their 
insurance co [...]
     "headers" : {
-      "Date" : "Fri, 26 Jul 2024 13:48:19 GMT",
+      "Date" : "Wed, 14 Aug 2024 13:38:15 GMT",
       "Content-Type" : "application/json; charset=utf-8"
     }
   },
-  "uuid" : "64441899-7a8b-456d-9dba-8dcf21cdc6e6",
+  "uuid" : "6942a475-c748-48c5-a955-20fb23d5abd9",
   "persistent" : true,
   "insertionIndex" : 2
 }
\ No newline at end of file

Reply via email to