Package com.tnsai.llm.observability
Class ObservableLLMClient
java.lang.Object
com.tnsai.llm.observability.ObservableLLMClient
- All Implemented Interfaces:
com.tnsai.llm.LLMClient
Wrapper that adds observability to any LLMClient.
ObservableLLMClient wraps an existing LLMClient and notifies registered observers about all requests, responses, and errors. This enables non-invasive monitoring of LLM operations.
Usage Example
// Create base client
LLMClient baseClient = new OpenAIClient("gpt-4o");
// Create metrics collector
LLMMetrics metrics = new LLMMetrics();
// Wrap with observability
LLMClient observedClient = new ObservableLLMClient(baseClient, metrics);
// Use normally - all calls are tracked
observedClient.chat("Hello!");
// Get metrics
LLMMetrics.Report report = metrics.getReport();
System.out.println("Requests: " + report.totalRequests());
System.out.println("Avg latency: " + report.avgLatencyMs() + "ms");
Multiple Observers
// Create multiple observers
LLMMetrics metrics = new LLMMetrics();
LoggingObserver logger = new LoggingObserver();
// Combine into one
LLMObserver combined = CompositeObserver.of(metrics, logger);
// Wrap client
LLMClient client = new ObservableLLMClient(baseClient, combined);
Custom Logging
LLMObserver customObserver = new LLMObserver() {
@Override
public void onRequest(LLMClient client, String message, ...) {
System.out.println("[" + Instant.now() + "] Request: " + message);
}
@Override
public void onResponse(LLMClient client, ChatResponse response, long latencyMs) {
System.out.println("[" + Instant.now() + "] Response (" + latencyMs + "ms)");
}
};
LLMClient client = new ObservableLLMClient(baseClient, customObserver);
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classComposes multiple observers into one. -
Constructor Summary
ConstructorsConstructorDescriptionObservableLLMClient(com.tnsai.llm.LLMClient delegate, LLMObserver observer) Creates an observable client with a single observer.ObservableLLMClient(com.tnsai.llm.LLMClient delegate, LLMObserver... observers) Creates an observable client with multiple observers. -
Method Summary
Modifier and TypeMethodDescriptioncom.tnsai.llm.ChatResponsechat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) com.tnsai.llm.ChatResponsechat(List<com.tnsai.models.chat.ContentPart> messageParts, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) com.tnsai.llm.LLMCapabilitiescom.tnsai.llm.LLMClientReturns the underlying client.getModel()Returns the observer.floatstreamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Stream<com.tnsai.llm.ChatChunk> streamChatWithSpec(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, getProviderName, getTopP, listModels, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec
-
Constructor Details
-
ObservableLLMClient
Creates an observable client with a single observer.- Parameters:
delegate- The underlying LLM clientobserver- The observer to notify
-
ObservableLLMClient
Creates an observable client with multiple observers.- Parameters:
delegate- The underlying LLM clientobservers- The observers to notify
-
-
Method Details
-
getDelegate
public com.tnsai.llm.LLMClient getDelegate()Returns the underlying client. -
getObserver
Returns the observer. -
getModel
- Specified by:
getModelin interfacecom.tnsai.llm.LLMClient
-
getTemperature
public float getTemperature()- Specified by:
getTemperaturein interfacecom.tnsai.llm.LLMClient
-
getMaxTokens
- Specified by:
getMaxTokensin interfacecom.tnsai.llm.LLMClient
-
getCapabilities
public com.tnsai.llm.LLMCapabilities getCapabilities()- Specified by:
getCapabilitiesin interfacecom.tnsai.llm.LLMClient
-
chat
public com.tnsai.llm.ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
chatin interfacecom.tnsai.llm.LLMClient
-
chat
public com.tnsai.llm.ChatResponse chat(List<com.tnsai.models.chat.ContentPart> messageParts, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
chatin interfacecom.tnsai.llm.LLMClient
-
streamChat
public Stream<String> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatin interfacecom.tnsai.llm.LLMClient
-
streamChatWithSpec
public Stream<com.tnsai.llm.ChatChunk> streamChatWithSpec(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatWithSpecin interfacecom.tnsai.llm.LLMClient
-