lm-lib: Merged with tp fork;

git-svn-id: svn://home0/lm-lib@3725 71dcc7fd-b1f2-4e5d-bb8e-274146e81173
This commit is contained in:
(no author)
2025-02-23 20:22:10 +00:00
parent 3c412077bb
commit 776ab01e85
3 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
package de.extio.lmlib.client.cached;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.HexFormat;
import java.util.Objects;
import java.util.function.Supplier;
import de.extio.lmlib.client.Client;
import de.extio.lmlib.client.Completion;
import de.extio.lmlib.client.Conversation;
import de.extio.lmlib.profile.ModelCategory;
import de.extio.lmlib.profile.ModelProfile.ModelProvider;
import de.extio.lmlib.profile.ModelProfileService;
public class CachedClient implements Client {
private final CachedClientRepository cacheRepository;
private final ModelProfileService modelProfileService;
private final Client client;
public CachedClient(final CachedClientRepository cacheRepository, final ModelProfileService modelProfileService, final Client client) {
this.cacheRepository = cacheRepository;
this.modelProfileService = modelProfileService;
this.client = 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);
}
@Override
public Completion conversation(final ModelCategory modelCategory, final Conversation conversation) {
return this.getCachedResponse(modelCategory, () -> this.client.conversation(modelCategory, conversation), conversation.getConversation().toString());
}
@Override
public ModelProvider getModelProvider() {
return this.client.getModelProvider();
}
private Completion getCachedResponse(final ModelCategory modelCategory, final Supplier<Completion> supplier, final String... keys) {
final var key = this.createCacheKey(modelCategory, keys);
final var cachedCompletion = this.cacheRepository.get(key);
if (cachedCompletion != null) {
return new Completion(cachedCompletion.response(), 0, Duration.ZERO, cachedCompletion.inTokens(), cachedCompletion.outTokens(), BigDecimal.ZERO, true);
}
final var completion = supplier.get();
if (completion != null) {
this.cacheRepository.put(key, new CachedCompletion(completion.response(), completion.inTokens(), completion.outTokens(), OffsetDateTime.now()));
}
return completion;
}
private String createCacheKey(ModelCategory modelCategory, final String... keys) {
if (modelCategory == null) {
modelCategory = ModelCategory.MEDIUM;
}
final var modelProfile = this.modelProfileService.getModelProfile(modelCategory.getModelProfile());
if (modelProfile == null) {
throw new IllegalArgumentException("ModelProfile " + modelCategory.getModelProfile());
}
final var key = new StringBuilder();
key.append(Objects.requireNonNullElse(modelProfile.modelName(), modelCategory.name()));
key.append("_");
key.append(modelProfile.maxTokens());
key.append("_");
key.append(modelProfile.maxContextLength());
key.append("_");
key.append(modelProfile.maxContinuations());
key.append("_");
key.append(modelProfile.temperature());
key.append("_");
key.append(modelProfile.topP());
key.append("_");
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
for (final var k : keys) {
if (k != null && !k.isEmpty()) {
digest.update(k.getBytes());
}
}
key.append(HexFormat.of().formatHex(digest.digest()));
}
catch (final NoSuchAlgorithmException e) {
throw new RuntimeException("Error calculating hash", e);
}
return key.toString();
}
}

View File

@@ -0,0 +1,9 @@
package de.extio.lmlib.client.cached;
public interface CachedClientRepository {
CachedCompletion get(String key);
void put(String key, CachedCompletion completion);
}

View File

@@ -0,0 +1,7 @@
package de.extio.lmlib.client.cached;
import java.time.OffsetDateTime;
public record CachedCompletion(String response, long inTokens, long outTokens, OffsetDateTime created) {
}