Merged lm-lib
git-svn-id: svn://home0/lm-lib@3737 71dcc7fd-b1f2-4e5d-bb8e-274146e81173
This commit is contained in:
@@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
|
||||
import de.extio.lmlib.client.cached.CachedClient;
|
||||
import de.extio.lmlib.client.cached.CachedClientRepository;
|
||||
import de.extio.lmlib.profile.ModelCategory;
|
||||
import de.extio.lmlib.profile.ModelProfile.ModelProvider;
|
||||
import de.extio.lmlib.profile.ModelProfileService;
|
||||
|
||||
@Service
|
||||
@@ -23,6 +24,9 @@ public class ClientService {
|
||||
@Autowired(required = false)
|
||||
private CachedClientRepository cachedClientRepository;
|
||||
|
||||
@Autowired(required = false)
|
||||
private List<CompletionInterceptor> completionInterceptors;
|
||||
|
||||
@Cacheable("clients")
|
||||
public Client getClient(final ModelCategory category) {
|
||||
final var modelProfile = this.modelProfileService.getModelProfile(category.getModelProfile());
|
||||
@@ -30,7 +34,44 @@ public class ClientService {
|
||||
.filter(client -> client.getModelProvider() == modelProfile.modelProvider())
|
||||
.findFirst()
|
||||
.map(client -> this.cachedClientRepository == null ? client : new CachedClient(this.cachedClientRepository, this.modelProfileService, client))
|
||||
.map(client -> this.completionInterceptors == null || this.completionInterceptors.isEmpty() ? client : new InterceptingClient(client, this.completionInterceptors))
|
||||
.orElseThrow(() -> new IllegalStateException("No client found for model profile " + category));
|
||||
}
|
||||
|
||||
private static class InterceptingClient implements Client {
|
||||
|
||||
private final Client client;
|
||||
|
||||
private final List<CompletionInterceptor> completionInterceptors;
|
||||
|
||||
public InterceptingClient(final Client client, final List<CompletionInterceptor> completionInterceptors) {
|
||||
this.client = client;
|
||||
this.completionInterceptors = completionInterceptors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Completion completion(final ModelCategory modelCategory, final String system, final String question, final String fullText) {
|
||||
return this.client.completion(modelCategory, system, question, fullText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Completion conversation(final ModelCategory modelCategory, final Conversation conversation_) {
|
||||
Conversation conversation = conversation_;
|
||||
for (final var interceptor : this.completionInterceptors) {
|
||||
conversation = interceptor.before(modelCategory, conversation);
|
||||
}
|
||||
var completion = this.client.conversation(modelCategory, conversation);
|
||||
for (final var interceptor : this.completionInterceptors) {
|
||||
completion = interceptor.after(modelCategory, completion);
|
||||
}
|
||||
return completion;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelProvider getModelProvider() {
|
||||
return this.client.getModelProvider();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user