Merged with tp lmlib

This commit is contained in:
extio-de
2025-06-27 16:36:46 +02:00
parent dcd6eeee41
commit 94894aee85
10 changed files with 279 additions and 33 deletions

View File

@@ -9,6 +9,7 @@ import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -28,6 +29,7 @@ import io.netty.util.internal.ThreadLocalRandom;
/**
* These tests have been verified with Llama 3.1 8B and Gemma2 27B models
*/
@Disabled("This test requires a running Llama server or an Azure subscription (setup azure key in model profile)")
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@SpringBootConfiguration
@ComponentScan(basePackages = "de.extio.lmlib")

View File

@@ -16,7 +16,7 @@ import de.extio.lmlib.client.Conversation.Turn;
import de.extio.lmlib.client.Conversation.TurnType;
import de.extio.lmlib.profile.ModelCategory;
@Disabled
@Disabled("This test requires an Azure subscription (setup azure key in model profile)")
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@SpringBootConfiguration
@ComponentScan(basePackages = "de.extio.lmlib")
@@ -35,7 +35,7 @@ public class AzureAiClientTest {
"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!");
LOGGER.info(completion.response());
LOGGER.info(completion.toString());
}
@Test

View File

@@ -0,0 +1,106 @@
package de.extio.lmlib.client.cached;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import de.extio.lmlib.client.ClientService;
import de.extio.lmlib.client.Conversation;
import de.extio.lmlib.client.Conversation.Turn;
import de.extio.lmlib.client.Conversation.TurnType;
import de.extio.lmlib.profile.ModelCategory;
@Disabled("This test requires a running Llama server or an Azure subscription (setup azure key in model profile)")
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@SpringBootConfiguration
@ComponentScan(basePackages = "de.extio.lmlib")
@PropertySource("classpath:/application-test.properties")
public class CachedClientTest {
private static final Logger LOGGER = LoggerFactory.getLogger(CachedClientTest.class);
@Autowired
private TestCachedClientRepository cachedClientRepository;
@Autowired
private ClientService clientService;
@AfterEach
void afterEach() {
this.cachedClientRepository.reset();
}
@Test
void cacheCompletion() throws Exception {
var completion = this.clientService.getClient(ModelCategory.MEDIUM).completion(
ModelCategory.MEDIUM,
"You are a funny person",
"Tell me a cat joke",
"");
LOGGER.info(completion.toString());
assertFalse(completion.cached());
assertEquals(1, completion.requests());
final var firstResponse = completion.response();
assertFalse(firstResponse.isBlank());
completion = this.clientService.getClient(ModelCategory.MEDIUM).completion(
ModelCategory.MEDIUM,
"You are a funny person",
"Tell me a cat joke",
"");
LOGGER.info(completion.toString());
assertTrue(completion.cached());
assertEquals(0, completion.requests());
assertEquals(firstResponse, completion.response());
completion = this.clientService.getClient(ModelCategory.MEDIUM).completion(
ModelCategory.MEDIUM,
"You are a funny person",
"Tell me a dog joke",
"");
LOGGER.info(completion.toString());
assertFalse(completion.cached());
assertEquals(1, completion.requests());
assertNotEquals(firstResponse, completion.response());
}
@Test
void cacheChat() throws Exception {
final var conversation = Conversation.create("You are an assistant who stutters", "Hi there, I would like to know the climate in Munich.");
var completion = this.clientService.getClient(ModelCategory.MEDIUM).conversation(ModelCategory.MEDIUM, conversation);
LOGGER.info(completion.toString());
assertFalse(completion.cached());
assertEquals(1, completion.requests());
final var firstResponse = completion.response();
assertFalse(firstResponse.isBlank());
completion = this.clientService.getClient(ModelCategory.MEDIUM).conversation(ModelCategory.MEDIUM, conversation);
LOGGER.info(completion.toString());
assertTrue(completion.cached());
assertEquals(0, completion.requests());
assertEquals(firstResponse, completion.response());
conversation.addTurn(new Turn(TurnType.ASSISTANT,
"T-t-the climate in B-b-berlin, um, it's, uh, quite, c-c-cool, actually. B-b-berlin has a, um, temperate climate, with, uh, four distinct s-s-seasons. S-s-spring is, um, pretty mild, with, uh, temperatures ranging from, uh, 10 to 20 degrees Celsius. S-s-summer is, um, warm, with, uh, highs around 25 degrees Celsius. F-f-fall is, um, cool, with, uh, temperatures ranging from 5 to 15 degrees Celsius. And, um, w-w-winter is, uh, quite, c-c-cold, with, uh, temperatures ranging from -2 to 5 degrees Celsius."));
conversation.addTurn(new Turn(TurnType.USER, "Great! How is the weather in summer?"));
completion = this.clientService.getClient(ModelCategory.MEDIUM).conversation(ModelCategory.MEDIUM, conversation);
LOGGER.info(completion.toString());
assertFalse(completion.cached());
assertEquals(1, completion.requests());
assertNotEquals(firstResponse, completion.response());
}
}

View File

@@ -0,0 +1,27 @@
package de.extio.lmlib.client.cached;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.stereotype.Component;
@Component
class TestCachedClientRepository implements CachedClientRepository {
private final ConcurrentMap<String, CachedCompletion> cache = new ConcurrentHashMap<>();
@Override
public CachedCompletion get(final String key) {
return this.cache.get(key);
}
@Override
public void put(final String key, final CachedCompletion completion) {
this.cache.put(key, completion);
}
void reset() {
this.cache.clear();
}
}

View File

@@ -23,6 +23,7 @@ import de.extio.lmlib.client.Conversation.Turn;
import de.extio.lmlib.client.Conversation.TurnType;
import de.extio.lmlib.profile.ModelCategory;
@Disabled("This test requires a running Llama server")
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
@SpringBootConfiguration
@ComponentScan(basePackages = "de.extio.lmlib")

View File

@@ -2,6 +2,7 @@ package de.extio.lmlib.token;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -42,8 +43,9 @@ public class TokenizerTest {
}
/**
* Tested with a Llama 3.1 8B model. The test will likely fail if you load a different model.
* Tested with a Llama 3.1 8B model. The test will likely fail if you load a different model.
*/
@Disabled("Requires llama.cpp model profile")
@Test
void llamaServer() throws Exception {
final var tokenizer = new LlamaServerTokenizer(this.webClientBuilder);