Deprecated Client#completion and removed redundant question parameter

This commit is contained in:
extio-de
2025-06-27 17:35:48 +02:00
parent 5b5f56fa15
commit bef3ef1468
9 changed files with 29 additions and 86 deletions

View File

@@ -5,7 +5,11 @@ import de.extio.lmlib.profile.ModelProfile.ModelProvider;
public interface Client { 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); Completion conversation(ModelCategory modelCategory, Conversation conversation);

View File

@@ -50,8 +50,8 @@ public class ClientService {
} }
@Override @Override
public Completion completion(final ModelCategory modelCategory, final String system, final String question, final String fullText) { public Completion completion(final ModelCategory modelCategory, final String system, final String text) {
return this.client.completion(modelCategory, system, question, fullText); return this.client.completion(modelCategory, system, text);
} }
@Override @Override

View File

@@ -90,21 +90,7 @@ public final class AzureAiClient implements Client {
} }
@Override @Override
public Completion completion(final ModelCategory modelCategory, final String system, final String question, final String fullText) { public Completion completion(final ModelCategory modelCategory, final String system, final String text) {
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");
}
return this.conversation(modelCategory, Conversation.create(system, text)); return this.conversation(modelCategory, Conversation.create(system, text));
} }

View File

@@ -31,8 +31,8 @@ public class CachedClient implements Client {
} }
@Override @Override
public Completion completion(final ModelCategory modelCategory, final String system, final String question, final String fullText) { public Completion completion(final ModelCategory modelCategory, final String system, final String text) {
return this.getCachedResponse(modelCategory, () -> this.client.completion(modelCategory, system, question, fullText), system, question, fullText); return this.getCachedResponse(modelCategory, () -> this.client.completion(modelCategory, system, text), system, text);
} }
@Override @Override

View File

@@ -68,21 +68,7 @@ public class TextCompletionClient implements Client, DisposableBean {
} }
@Override @Override
public Completion completion(final ModelCategory modelCategory, final String system, final String question, final String fullText) { public Completion completion(final ModelCategory modelCategory, final String system, final String text) {
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");
}
return this.conversation(modelCategory, Conversation.create(system, text)); return this.conversation(modelCategory, Conversation.create(system, text));
} }

View File

@@ -3,17 +3,21 @@ package de.extio.lmlib.grader;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import de.extio.lmlib.client.Client;
import de.extio.lmlib.client.ClientService; import de.extio.lmlib.client.ClientService;
import de.extio.lmlib.profile.ModelCategory; import de.extio.lmlib.profile.ModelCategory;
public class Grader { 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) { public static boolean assessScoreBinary(final String question, final String text, final ClientService clientService) {
LOGGER.info(question);
final var client = clientService.getClient(ModelCategory.MEDIUM); 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; var score = 0;
while (Math.abs(score) < 3) { while (Math.abs(score) < 3) {
@@ -24,8 +28,8 @@ public class Grader {
Please provide a binary response 'true' or 'false' for the following text. 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. '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.""", Only provide the response as a single word and no preamble and no explanation.""",
"Here is the text:\n" + text, "Here is the text:\n" + text + "\n\n" +
"\nHere is the user question: " + question); "Here is the user question: " + question);
LOGGER.info(completion.response()); LOGGER.info(completion.response());
score += Boolean.parseBoolean(completion.response().strip()) ? 1 : -1; score += Boolean.parseBoolean(completion.response().strip()) ? 1 : -1;
if (Math.abs(score) == 2) { if (Math.abs(score) == 2) {

View File

@@ -33,8 +33,7 @@ public class AzureAiClientTest {
final var completion = this.client.completion( final var completion = this.client.completion(
ModelCategory.MEDIUM, ModelCategory.MEDIUM,
"You are a clever story teller", "You are a clever story teller",
"Continue this story:", "Continue this story:\nOnce upon a time, there was a spaceship travelling to a black hole. The crew were cats!");
"Once upon a time, there was a spaceship travelling to a black hole. The crew were cats!");
LOGGER.info(completion.toString()); LOGGER.info(completion.toString());
} }
@@ -43,8 +42,7 @@ public class AzureAiClientTest {
final var completion = this.client.completion( final var completion = this.client.completion(
ModelCategory.LARGE, ModelCategory.LARGE,
"You are a clever story teller", "You are a clever story teller",
"Continue this story:", "Continue this story:\nOnce upon a time, there was a spaceship travelling to a black hole. The crew were cats!");
"Once upon a time, there was a spaceship travelling to a black hole. The crew were cats!");
LOGGER.info(completion.response()); LOGGER.info(completion.response());
} }
@@ -64,7 +62,6 @@ public class AzureAiClientTest {
final var completion = this.client.completion( final var completion = this.client.completion(
null, null,
"You are a helpful assistant", "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 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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 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()); LOGGER.info(completion.response());
} }

View File

@@ -48,8 +48,7 @@ public class CachedClientTest {
var completion = this.clientService.getClient(ModelCategory.MEDIUM).completion( var completion = this.clientService.getClient(ModelCategory.MEDIUM).completion(
ModelCategory.MEDIUM, ModelCategory.MEDIUM,
"You are a funny person", "You are a funny person",
"Tell me a cat joke", "Tell me a cat joke");
"");
LOGGER.info(completion.toString()); LOGGER.info(completion.toString());
assertFalse(completion.cached()); assertFalse(completion.cached());
assertEquals(1, completion.requests()); assertEquals(1, completion.requests());
@@ -59,8 +58,7 @@ public class CachedClientTest {
completion = this.clientService.getClient(ModelCategory.MEDIUM).completion( completion = this.clientService.getClient(ModelCategory.MEDIUM).completion(
ModelCategory.MEDIUM, ModelCategory.MEDIUM,
"You are a funny person", "You are a funny person",
"Tell me a cat joke", "Tell me a cat joke");
"");
LOGGER.info(completion.toString()); LOGGER.info(completion.toString());
assertTrue(completion.cached()); assertTrue(completion.cached());
assertEquals(0, completion.requests()); assertEquals(0, completion.requests());
@@ -69,8 +67,7 @@ public class CachedClientTest {
completion = this.clientService.getClient(ModelCategory.MEDIUM).completion( completion = this.clientService.getClient(ModelCategory.MEDIUM).completion(
ModelCategory.MEDIUM, ModelCategory.MEDIUM,
"You are a funny person", "You are a funny person",
"Tell me a dog joke", "Tell me a dog joke");
"");
LOGGER.info(completion.toString()); LOGGER.info(completion.toString());
assertFalse(completion.cached()); assertFalse(completion.cached());
assertEquals(1, completion.requests()); assertEquals(1, completion.requests());

View File

@@ -22,6 +22,7 @@ import de.extio.lmlib.client.Conversation;
import de.extio.lmlib.client.Conversation.Turn; import de.extio.lmlib.client.Conversation.Turn;
import de.extio.lmlib.client.Conversation.TurnType; import de.extio.lmlib.client.Conversation.TurnType;
import de.extio.lmlib.client.oai.textcompletion.TextCompletionClient; import de.extio.lmlib.client.oai.textcompletion.TextCompletionClient;
import de.extio.lmlib.grader.Grader;
import de.extio.lmlib.profile.ModelCategory; import de.extio.lmlib.profile.ModelCategory;
//@Disabled("This test requires a running Llama server") //@Disabled("This test requires a running Llama server")
@@ -41,12 +42,11 @@ public class TextCompletionClientTest {
final var completion = this.client.completion( final var completion = this.client.completion(
null, null,
"You are a helpful assistant", "You are a helpful assistant",
"",
"Say the color is green"); "Say the color is green");
LOGGER.info(completion.response()); 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 @Test
@@ -58,7 +58,7 @@ public class TextCompletionClientTest {
LOGGER.info(completion.response()); 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 @Disabled
@@ -74,8 +74,7 @@ public class TextCompletionClientTest {
final var completion = this.client.completion( final var completion = this.client.completion(
null, null,
"You are counting machine", "You are counting machine",
"How many numbers do you count?", "How many numbers do you count?\n" + sb.toString());
sb.toString());
LOGGER.info(completion.response()); LOGGER.info(completion.response());
} }
@@ -123,7 +122,6 @@ public class TextCompletionClientTest {
final var completion = this.client.completion( final var completion = this.client.completion(
null, null,
"You are a helpful assistant", "You are a helpful assistant",
"",
"How do I calculate the annual profit margin?"); "How do I calculate the annual profit margin?");
LOGGER.info(completion.response()); LOGGER.info(completion.response());
} }
@@ -135,7 +133,6 @@ public class TextCompletionClientTest {
final var completion = this.client.completion( final var completion = this.client.completion(
null, null,
"You are a helpful assistant", "You are a helpful assistant",
"",
"How do I calculate the operating profit ratio?"); "How do I calculate the operating profit ratio?");
LOGGER.info(completion.response()); LOGGER.info(completion.response());
} }
@@ -147,7 +144,6 @@ public class TextCompletionClientTest {
final var completion = this.client.completion( final var completion = this.client.completion(
null, null,
"You are a helpful assistant", "You are a helpful assistant",
"",
"How do I calculate the return of investment?"); "How do I calculate the return of investment?");
LOGGER.info(completion.response()); LOGGER.info(completion.response());
} }
@@ -159,7 +155,6 @@ public class TextCompletionClientTest {
final var completion = this.client.completion( final var completion = this.client.completion(
null, null,
"You are a helpful assistant", "You are a helpful assistant",
"",
"How do I calculate the return on net worth?"); "How do I calculate the return on net worth?");
LOGGER.info(completion.response()); LOGGER.info(completion.response());
} }
@@ -171,30 +166,4 @@ public class TextCompletionClientTest {
static record Tasks(Runnable task1, Runnable task2, Runnable task3, Runnable task4) { 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;
}
} }