From bef3ef1468d1cf9a9d81441114cd74a214d05cca Mon Sep 17 00:00:00 2001 From: extio-de Date: Fri, 27 Jun 2025 17:35:48 +0200 Subject: [PATCH] Deprecated Client#completion and removed redundant question parameter --- .../java/de/extio/lmlib/client/Client.java | 6 ++- .../de/extio/lmlib/client/ClientService.java | 4 +- .../lmlib/client/azureai/AzureAiClient.java | 16 +------- .../lmlib/client/cached/CachedClient.java | 4 +- .../textcompletion/TextCompletionClient.java | 16 +------- .../java/de/extio/lmlib/grader/Grader.java | 14 ++++--- .../client/azureai/AzureAiClientTest.java | 7 +--- .../lmlib/client/cached/CachedClientTest.java | 9 ++--- .../client/oai/TextCompletionClientTest.java | 39 ++----------------- 9 files changed, 29 insertions(+), 86 deletions(-) diff --git a/src/main/java/de/extio/lmlib/client/Client.java b/src/main/java/de/extio/lmlib/client/Client.java index 6c00339..6fb57c3 100755 --- a/src/main/java/de/extio/lmlib/client/Client.java +++ b/src/main/java/de/extio/lmlib/client/Client.java @@ -5,7 +5,11 @@ import de.extio.lmlib.profile.ModelProfile.ModelProvider; public interface Client { - Completion completion(ModelCategory modelCategory, String system, String question, String fullText); + /** + * @deprecated use {@link #conversation(ModelCategory, Conversation)} instead. Conversation can be converted from a system and text by Conversation.create(system, text) + */ + @Deprecated + Completion completion(ModelCategory modelCategory, String system, String text); Completion conversation(ModelCategory modelCategory, Conversation conversation); diff --git a/src/main/java/de/extio/lmlib/client/ClientService.java b/src/main/java/de/extio/lmlib/client/ClientService.java index 9bf2c0f..39082a3 100755 --- a/src/main/java/de/extio/lmlib/client/ClientService.java +++ b/src/main/java/de/extio/lmlib/client/ClientService.java @@ -50,8 +50,8 @@ public class ClientService { } @Override - public Completion completion(final ModelCategory modelCategory, final String system, final String question, final String fullText) { - return this.client.completion(modelCategory, system, question, fullText); + public Completion completion(final ModelCategory modelCategory, final String system, final String text) { + return this.client.completion(modelCategory, system, text); } @Override diff --git a/src/main/java/de/extio/lmlib/client/azureai/AzureAiClient.java b/src/main/java/de/extio/lmlib/client/azureai/AzureAiClient.java index 35ee50e..e32e281 100755 --- a/src/main/java/de/extio/lmlib/client/azureai/AzureAiClient.java +++ b/src/main/java/de/extio/lmlib/client/azureai/AzureAiClient.java @@ -90,21 +90,7 @@ public final class AzureAiClient implements Client { } @Override - public Completion completion(final ModelCategory modelCategory, final String system, final String question, final String fullText) { - String text = ""; - if (!question.isEmpty() && !fullText.isEmpty()) { - text = question + "\n" + fullText; - } - else if (!question.isEmpty()) { - text = question; - } - else if (!fullText.isEmpty()) { - text = fullText; - } - else { - throw new IllegalArgumentException("Either question or fullText must be set"); - } - + public Completion completion(final ModelCategory modelCategory, final String system, final String text) { return this.conversation(modelCategory, Conversation.create(system, text)); } diff --git a/src/main/java/de/extio/lmlib/client/cached/CachedClient.java b/src/main/java/de/extio/lmlib/client/cached/CachedClient.java index 303d21c..a0ae201 100644 --- a/src/main/java/de/extio/lmlib/client/cached/CachedClient.java +++ b/src/main/java/de/extio/lmlib/client/cached/CachedClient.java @@ -31,8 +31,8 @@ public class CachedClient implements Client { } @Override - public Completion completion(final ModelCategory modelCategory, final String system, final String question, final String fullText) { - return this.getCachedResponse(modelCategory, () -> this.client.completion(modelCategory, system, question, fullText), system, question, fullText); + public Completion completion(final ModelCategory modelCategory, final String system, final String text) { + return this.getCachedResponse(modelCategory, () -> this.client.completion(modelCategory, system, text), system, text); } @Override diff --git a/src/main/java/de/extio/lmlib/client/oai/textcompletion/TextCompletionClient.java b/src/main/java/de/extio/lmlib/client/oai/textcompletion/TextCompletionClient.java index 74800c9..97d57c2 100755 --- a/src/main/java/de/extio/lmlib/client/oai/textcompletion/TextCompletionClient.java +++ b/src/main/java/de/extio/lmlib/client/oai/textcompletion/TextCompletionClient.java @@ -68,21 +68,7 @@ public class TextCompletionClient implements Client, DisposableBean { } @Override - public Completion completion(final ModelCategory modelCategory, final String system, final String question, final String fullText) { - String text = ""; - if (!question.isEmpty() && !fullText.isEmpty()) { - text = question + "\n" + fullText; - } - else if (!question.isEmpty()) { - text = question; - } - else if (!fullText.isEmpty()) { - text = fullText; - } - else { - throw new IllegalArgumentException("Either question or fullText must be set"); - } - + public Completion completion(final ModelCategory modelCategory, final String system, final String text) { return this.conversation(modelCategory, Conversation.create(system, text)); } diff --git a/src/main/java/de/extio/lmlib/grader/Grader.java b/src/main/java/de/extio/lmlib/grader/Grader.java index 3ab405b..8363e90 100644 --- a/src/main/java/de/extio/lmlib/grader/Grader.java +++ b/src/main/java/de/extio/lmlib/grader/Grader.java @@ -3,17 +3,21 @@ package de.extio.lmlib.grader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import de.extio.lmlib.client.Client; import de.extio.lmlib.client.ClientService; import de.extio.lmlib.profile.ModelCategory; public class Grader { - private static final Logger LOGGER = LoggerFactory.getLogger(Grader.class); + private static final Logger LOGGER = LoggerFactory.getLogger(Grader.class); public static boolean assessScoreBinary(final String question, final String text, final ClientService clientService) { - LOGGER.info(question); - final var client = clientService.getClient(ModelCategory.MEDIUM); + return assessScoreBinary(question, text, client); + } + + public static boolean assessScoreBinary(final String question, final String text, final Client client) { + LOGGER.info(question); var score = 0; while (Math.abs(score) < 3) { @@ -24,8 +28,8 @@ public class Grader { Please provide a binary response 'true' or 'false' for the following text. 'true' means that the text provides a truthful answer to the question, while 'false' means that it does not. Only provide the response as a single word and no preamble and no explanation.""", - "Here is the text:\n" + text, - "\nHere is the user question: " + question); + "Here is the text:\n" + text + "\n\n" + + "Here is the user question: " + question); LOGGER.info(completion.response()); score += Boolean.parseBoolean(completion.response().strip()) ? 1 : -1; if (Math.abs(score) == 2) { diff --git a/src/test/java/de/extio/lmlib/client/azureai/AzureAiClientTest.java b/src/test/java/de/extio/lmlib/client/azureai/AzureAiClientTest.java index ccb54c4..fb1dccb 100755 --- a/src/test/java/de/extio/lmlib/client/azureai/AzureAiClientTest.java +++ b/src/test/java/de/extio/lmlib/client/azureai/AzureAiClientTest.java @@ -33,8 +33,7 @@ public class AzureAiClientTest { final var completion = this.client.completion( ModelCategory.MEDIUM, "You are a clever story teller", - "Continue this story:", - "Once upon a time, there was a spaceship travelling to a black hole. The crew were cats!"); + "Continue this story:\nOnce upon a time, there was a spaceship travelling to a black hole. The crew were cats!"); LOGGER.info(completion.toString()); } @@ -43,8 +42,7 @@ public class AzureAiClientTest { final var completion = this.client.completion( ModelCategory.LARGE, "You are a clever story teller", - "Continue this story:", - "Once upon a time, there was a spaceship travelling to a black hole. The crew were cats!"); + "Continue this story:\nOnce upon a time, there was a spaceship travelling to a black hole. The crew were cats!"); LOGGER.info(completion.response()); } @@ -64,7 +62,6 @@ public class AzureAiClientTest { final var completion = this.client.completion( null, "You are a helpful assistant", - "", "Count the numbers: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 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5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9"); LOGGER.info(completion.response()); } diff --git a/src/test/java/de/extio/lmlib/client/cached/CachedClientTest.java b/src/test/java/de/extio/lmlib/client/cached/CachedClientTest.java index 06d8b3f..6910065 100755 --- a/src/test/java/de/extio/lmlib/client/cached/CachedClientTest.java +++ b/src/test/java/de/extio/lmlib/client/cached/CachedClientTest.java @@ -48,8 +48,7 @@ public class CachedClientTest { var completion = this.clientService.getClient(ModelCategory.MEDIUM).completion( ModelCategory.MEDIUM, "You are a funny person", - "Tell me a cat joke", - ""); + "Tell me a cat joke"); LOGGER.info(completion.toString()); assertFalse(completion.cached()); assertEquals(1, completion.requests()); @@ -59,8 +58,7 @@ public class CachedClientTest { completion = this.clientService.getClient(ModelCategory.MEDIUM).completion( ModelCategory.MEDIUM, "You are a funny person", - "Tell me a cat joke", - ""); + "Tell me a cat joke"); LOGGER.info(completion.toString()); assertTrue(completion.cached()); assertEquals(0, completion.requests()); @@ -69,8 +67,7 @@ public class CachedClientTest { completion = this.clientService.getClient(ModelCategory.MEDIUM).completion( ModelCategory.MEDIUM, "You are a funny person", - "Tell me a dog joke", - ""); + "Tell me a dog joke"); LOGGER.info(completion.toString()); assertFalse(completion.cached()); assertEquals(1, completion.requests()); diff --git a/src/test/java/de/extio/lmlib/client/oai/TextCompletionClientTest.java b/src/test/java/de/extio/lmlib/client/oai/TextCompletionClientTest.java index 0327159..1620ed4 100755 --- a/src/test/java/de/extio/lmlib/client/oai/TextCompletionClientTest.java +++ b/src/test/java/de/extio/lmlib/client/oai/TextCompletionClientTest.java @@ -22,6 +22,7 @@ import de.extio.lmlib.client.Conversation; import de.extio.lmlib.client.Conversation.Turn; import de.extio.lmlib.client.Conversation.TurnType; import de.extio.lmlib.client.oai.textcompletion.TextCompletionClient; +import de.extio.lmlib.grader.Grader; import de.extio.lmlib.profile.ModelCategory; //@Disabled("This test requires a running Llama server") @@ -41,12 +42,11 @@ public class TextCompletionClientTest { final var completion = this.client.completion( null, "You are a helpful assistant", - "", "Say the color is green"); LOGGER.info(completion.response()); - assertTrue(this.assessScoreBinary("Does the text mention the color green?", completion.response())); + assertTrue(Grader.assessScoreBinary("Does the text mention the color green?", completion.response(), this.client)); } @Test @@ -58,7 +58,7 @@ public class TextCompletionClientTest { LOGGER.info(completion.response()); - assertTrue(this.assessScoreBinary("Does the text mention a color?", completion.response())); + assertTrue(Grader.assessScoreBinary("Does the text mention a color?", completion.response(), this.client)); } @Disabled @@ -74,8 +74,7 @@ public class TextCompletionClientTest { final var completion = this.client.completion( null, "You are counting machine", - "How many numbers do you count?", - sb.toString()); + "How many numbers do you count?\n" + sb.toString()); LOGGER.info(completion.response()); } @@ -123,7 +122,6 @@ public class TextCompletionClientTest { final var completion = this.client.completion( null, "You are a helpful assistant", - "", "How do I calculate the annual profit margin?"); LOGGER.info(completion.response()); } @@ -135,7 +133,6 @@ public class TextCompletionClientTest { final var completion = this.client.completion( null, "You are a helpful assistant", - "", "How do I calculate the operating profit ratio?"); LOGGER.info(completion.response()); } @@ -147,7 +144,6 @@ public class TextCompletionClientTest { final var completion = this.client.completion( null, "You are a helpful assistant", - "", "How do I calculate the return of investment?"); LOGGER.info(completion.response()); } @@ -159,7 +155,6 @@ public class TextCompletionClientTest { final var completion = this.client.completion( null, "You are a helpful assistant", - "", "How do I calculate the return on net worth?"); LOGGER.info(completion.response()); } @@ -171,30 +166,4 @@ public class TextCompletionClientTest { static record Tasks(Runnable task1, Runnable task2, Runnable task3, Runnable task4) { } - - private boolean assessScoreBinary(final String question, final String text) { - LOGGER.info(question); - - var score = 0; - while (Math.abs(score) < 3) { - final var completion = this.client.completion( - ModelCategory.MEDIUM, - """ - You are a grader assessing the truthfulness of a given text to a user question. - Please provide a binary response 'true' or 'false' for the following text. - 'true' means that the text provides a truthful answer to the question, while 'false' means that it does not. - Only provide the response as a single word and no preamble and no explanation.""", - "Here is the text:\n" + text, - "\nHere is the user question: " + question); - LOGGER.info(completion.response()); - score += Boolean.parseBoolean(completion.response().strip()) ? 1 : -1; - if (Math.abs(score) == 2) { - break; - } - } - - final var result = score > 0; - LOGGER.info("->" + String.valueOf(result)); - return result; - } }