mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 08:43:02 +08:00
refactor(framework): unify model invocation result and exception handling
This commit is contained in:
@@ -2,18 +2,22 @@ package work.slhaf.partner.framework.agent.model
|
||||
|
||||
import work.slhaf.partner.framework.agent.factory.component.abstracts.AbstractAgentModule
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message
|
||||
import work.slhaf.partner.framework.agent.support.Result
|
||||
|
||||
interface ActivateModel {
|
||||
|
||||
fun chat(messages: List<Message>): String {
|
||||
fun chat(messages: List<Message>): Result<String> {
|
||||
return ModelRuntimeRegistry.resolveProvider(modelKey()).chat(mergeMessages(messages))
|
||||
}
|
||||
|
||||
fun streamChat(messages: List<Message>, handler: StreamChatMessageConsumer) {
|
||||
ModelRuntimeRegistry.resolveProvider(modelKey()).streamChat(mergeMessages(messages), handler)
|
||||
fun streamChat(
|
||||
messages: List<Message>,
|
||||
handler: StreamChatMessageConsumer
|
||||
): work.slhaf.partner.framework.agent.support.Result<Unit> {
|
||||
return ModelRuntimeRegistry.resolveProvider(modelKey()).streamChat(mergeMessages(messages), handler)
|
||||
}
|
||||
|
||||
fun <T : Any> formattedChat(messages: List<Message>, responseType: Class<T>): T {
|
||||
fun <T : Any> formattedChat(messages: List<Message>, responseType: Class<T>): Result<T> {
|
||||
return ModelRuntimeRegistry.resolveProvider(modelKey()).formattedChat(mergeMessages(messages), responseType)
|
||||
}
|
||||
|
||||
@@ -39,4 +43,4 @@ interface ActivateModel {
|
||||
}
|
||||
|
||||
fun modulePrompt(): List<Message> = emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,13 @@ object ModelRuntimeRegistry : Configurable, ConfigRegistration<ModelRuntimeRegis
|
||||
private fun registerProvider(config: ProviderConfig) {
|
||||
when (config) {
|
||||
is OpenAiCompatibleProviderConfig -> baseProvider[config.name] =
|
||||
OpenAiCompatibleProvider(config.baseUrl, config.apiKey, config.defaultModel)
|
||||
OpenAiCompatibleProvider(
|
||||
config.name,
|
||||
DEFAULT_PROVIDER,
|
||||
config.baseUrl,
|
||||
config.apiKey,
|
||||
config.defaultModel
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,11 +67,7 @@ object ModelRuntimeRegistry : Configurable, ConfigRegistration<ModelRuntimeRegis
|
||||
val override = config.override
|
||||
|
||||
try {
|
||||
runtimeProvider[config.modelKey] = if (override != null) {
|
||||
provider.fork(override)
|
||||
} else {
|
||||
provider
|
||||
}
|
||||
runtimeProvider[config.modelKey] = provider.fork(config.modelKey, override)
|
||||
} catch (e: Exception) {
|
||||
throw runtimeModelException(
|
||||
"Failed to build runtime provider for model key ${config.modelKey}",
|
||||
|
||||
@@ -3,18 +3,21 @@ package work.slhaf.partner.framework.agent.model.provider
|
||||
import com.alibaba.fastjson2.JSONObject
|
||||
import work.slhaf.partner.framework.agent.model.StreamChatMessageConsumer
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message
|
||||
import work.slhaf.partner.framework.agent.support.Result
|
||||
|
||||
abstract class ModelProvider @JvmOverloads constructor(
|
||||
val providerName: String,
|
||||
val modelKey: String,
|
||||
val override: ProviderOverride? = null
|
||||
) {
|
||||
|
||||
abstract fun fork(override: ProviderOverride): ModelProvider
|
||||
abstract fun fork(modelKey: String, override: ProviderOverride? = null): ModelProvider
|
||||
|
||||
abstract fun streamChat(messages: List<Message>, consumer: StreamChatMessageConsumer)
|
||||
abstract fun streamChat(messages: List<Message>, consumer: StreamChatMessageConsumer): Result<Unit>
|
||||
|
||||
abstract fun chat(messages: List<Message>): String
|
||||
abstract fun chat(messages: List<Message>): Result<String>
|
||||
|
||||
abstract fun <T> formattedChat(messages: List<Message>, type: Class<T>): T
|
||||
abstract fun <T> formattedChat(messages: List<Message>, type: Class<T>): Result<T>
|
||||
}
|
||||
|
||||
data class ProviderOverride(
|
||||
@@ -24,4 +27,4 @@ data class ProviderOverride(
|
||||
val maxTokens: Int?,
|
||||
|
||||
val extras: JSONObject?
|
||||
)
|
||||
)
|
||||
|
||||
@@ -6,24 +6,33 @@ import com.openai.client.okhttp.OpenAIOkHttpClient;
|
||||
import com.openai.core.JsonValue;
|
||||
import com.openai.core.http.StreamResponse;
|
||||
import com.openai.models.chat.completions.*;
|
||||
import kotlin.Unit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import work.slhaf.partner.framework.agent.exception.ModelInvokeException;
|
||||
import work.slhaf.partner.framework.agent.model.StreamChatMessageConsumer;
|
||||
import work.slhaf.partner.framework.agent.model.pojo.Message;
|
||||
import work.slhaf.partner.framework.agent.model.provider.ModelProvider;
|
||||
import work.slhaf.partner.framework.agent.model.provider.ProviderOverride;
|
||||
import work.slhaf.partner.framework.agent.support.Result;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class OpenAiCompatibleProvider extends ModelProvider {
|
||||
|
||||
private static final int MAX_ATTEMPTS = 3;
|
||||
|
||||
private final String baseUrl;
|
||||
private final String apiKey;
|
||||
private final String model;
|
||||
|
||||
private final OpenAIClient client;
|
||||
|
||||
public OpenAiCompatibleProvider(String baseUrl, String apikey, String model) {
|
||||
public OpenAiCompatibleProvider(String providerName, String modelKey, String baseUrl, String apikey, String model) {
|
||||
super(providerName, modelKey, null);
|
||||
this.client = OpenAIOkHttpClient.builder()
|
||||
.baseUrl(baseUrl)
|
||||
.apiKey(apikey)
|
||||
@@ -34,8 +43,8 @@ public class OpenAiCompatibleProvider extends ModelProvider {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public OpenAiCompatibleProvider(String baseUrl, String apikey, String model, ProviderOverride override) {
|
||||
super(override);
|
||||
public OpenAiCompatibleProvider(String providerName, String modelKey, String baseUrl, String apikey, String model, ProviderOverride override) {
|
||||
super(providerName, modelKey, override);
|
||||
this.client = OpenAIOkHttpClient.builder()
|
||||
.baseUrl(baseUrl)
|
||||
.apiKey(apikey)
|
||||
@@ -46,27 +55,59 @@ public class OpenAiCompatibleProvider extends ModelProvider {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public @NotNull String chat(@NotNull List<Message> messages) {
|
||||
ChatCompletionCreateParams params = buildParams(messages);
|
||||
return extractText(client.chat().completions().create(params));
|
||||
@Override
|
||||
public @NotNull Result<String> chat(@NotNull List<Message> messages) {
|
||||
return executeWithRetry(
|
||||
"OpenAI-compatible provider failed to complete the chat request after 3 attempts.",
|
||||
() -> extractText(client.chat().completions().create(buildParams(messages)))
|
||||
);
|
||||
}
|
||||
|
||||
public void streamChat(@NotNull List<Message> messages, StreamChatMessageConsumer handler) {
|
||||
ChatCompletionCreateParams params = buildParams(messages);
|
||||
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(params)) {
|
||||
streamResponse.stream()
|
||||
.flatMap(completion -> completion.choices().stream())
|
||||
.flatMap(choice -> choice.delta().content().stream())
|
||||
.filter(delta -> !delta.isEmpty())
|
||||
.forEach(handler::onDelta);
|
||||
@Override
|
||||
public @NotNull Result<Unit> streamChat(@NotNull List<Message> messages, @NotNull StreamChatMessageConsumer handler) {
|
||||
Exception lastFailure = null;
|
||||
int remainingAttempts = MAX_ATTEMPTS;
|
||||
while (remainingAttempts > 0) {
|
||||
boolean emitted = false;
|
||||
try (StreamResponse<ChatCompletionChunk> streamResponse = client.chat().completions().createStreaming(buildParams(messages))) {
|
||||
Iterator<ChatCompletionChunk> iterator = streamResponse.stream().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
ChatCompletionChunk chunk = iterator.next();
|
||||
for (ChatCompletionChunk.Choice choice : chunk.choices()) {
|
||||
String delta = choice.delta().content().orElse("");
|
||||
if (delta.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
emitted = true;
|
||||
handler.onDelta(delta);
|
||||
}
|
||||
}
|
||||
return Result.success(Unit.INSTANCE);
|
||||
} catch (Exception e) {
|
||||
lastFailure = e;
|
||||
remainingAttempts--;
|
||||
if (emitted || remainingAttempts == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Result.failure(invokeException(
|
||||
"OpenAI-compatible provider failed to stream the chat response after 3 attempts.",
|
||||
lastFailure
|
||||
));
|
||||
}
|
||||
|
||||
public <T> T formattedChat(@NotNull List<Message> messages, @NotNull Class<T> responseType) {
|
||||
StructuredChatCompletionCreateParams<T> params = buildParams(messages).toBuilder()
|
||||
.responseFormat(responseType)
|
||||
.build();
|
||||
return extractStructured(client.chat().completions().create(params));
|
||||
@Override
|
||||
public <T> @NotNull Result<T> formattedChat(@NotNull List<Message> messages, @NotNull Class<T> responseType) {
|
||||
return executeWithRetry(
|
||||
"OpenAI-compatible provider failed to complete the structured chat request after 3 attempts.",
|
||||
() -> {
|
||||
StructuredChatCompletionCreateParams<T> params = buildParams(messages).toBuilder()
|
||||
.responseFormat(responseType)
|
||||
.build();
|
||||
return extractStructured(client.chat().completions().create(params));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private ChatCompletionCreateParams buildParams(List<Message> messages) {
|
||||
@@ -87,9 +128,7 @@ public class OpenAiCompatibleProvider extends ModelProvider {
|
||||
}
|
||||
JSONObject extras = override.getExtras();
|
||||
if (extras != null) {
|
||||
extras.forEach((key, value) -> {
|
||||
paramsBuilder.putAdditionalBodyProperty(key, JsonValue.from(value));
|
||||
});
|
||||
extras.forEach((key, value) -> paramsBuilder.putAdditionalBodyProperty(key, JsonValue.from(value)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,22 +137,81 @@ public class OpenAiCompatibleProvider extends ModelProvider {
|
||||
|
||||
private String extractText(ChatCompletion completion) {
|
||||
if (completion.choices().isEmpty()) {
|
||||
throw new IllegalStateException("OpenAI chat completion returned no choices.");
|
||||
throw invokeException("OpenAI chat completion returned no choices.", null);
|
||||
}
|
||||
return completion.choices().getFirst().message().content()
|
||||
.orElseThrow(() -> new IllegalStateException("OpenAI chat completion returned empty content."));
|
||||
.orElseThrow(() -> invokeException("OpenAI chat completion returned empty content.", null));
|
||||
}
|
||||
|
||||
private <T> T extractStructured(StructuredChatCompletion<T> completion) {
|
||||
if (completion.choices().isEmpty()) {
|
||||
throw new IllegalStateException("OpenAI structured chat completion returned no choices.");
|
||||
throw invokeException("OpenAI structured chat completion returned no choices.", null);
|
||||
}
|
||||
return completion.choices().getFirst().message().content()
|
||||
.orElseThrow(() -> new IllegalStateException("OpenAI structured chat completion returned empty content."));
|
||||
.orElseThrow(() -> invokeException("OpenAI structured chat completion returned empty content.", null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ModelProvider fork(@NotNull ProviderOverride override) {
|
||||
return new OpenAiCompatibleProvider(baseUrl, apiKey, override.getModel(), override);
|
||||
public @NotNull ModelProvider fork(@NotNull String modelKey, ProviderOverride override) {
|
||||
if (override == null) {
|
||||
return new OpenAiCompatibleProvider(getProviderName(), modelKey, baseUrl, apiKey, model, getOverride());
|
||||
}
|
||||
return new OpenAiCompatibleProvider(getProviderName(), modelKey, baseUrl, apiKey, override.getModel(), override);
|
||||
}
|
||||
|
||||
private <T> Result<T> executeWithRetry(String failureMessage, ThrowingSupplier<T> supplier) {
|
||||
Exception lastFailure = null;
|
||||
for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
||||
Result<T> result = Result.runCatching(supplier::get);
|
||||
if (result.isSuccess()) {
|
||||
return result;
|
||||
}
|
||||
Throwable throwable = result.exceptionOrNull();
|
||||
if (throwable instanceof Exception exception) {
|
||||
lastFailure = exception;
|
||||
continue;
|
||||
}
|
||||
if (throwable instanceof Error error) {
|
||||
throw error;
|
||||
}
|
||||
return Result.failure(invokeException(failureMessage, throwable));
|
||||
}
|
||||
return Result.failure(invokeException(failureMessage, lastFailure));
|
||||
}
|
||||
|
||||
private ModelInvokeException invokeException(String message, Throwable cause) {
|
||||
return new ModelInvokeException(
|
||||
message,
|
||||
getProviderName(),
|
||||
getModelKey(),
|
||||
baseUrl,
|
||||
model,
|
||||
getOverride() == null ? Map.of() : toOverrideReport(getOverride()),
|
||||
cause
|
||||
);
|
||||
}
|
||||
|
||||
private Map<String, String> toOverrideReport(ProviderOverride override) {
|
||||
Map<String, String> result = new LinkedHashMap<>();
|
||||
result.put("model", override.getModel());
|
||||
if (override.getTemperature() != null) {
|
||||
result.put("temperature", override.getTemperature().toString());
|
||||
}
|
||||
if (override.getTopP() != null) {
|
||||
result.put("topP", override.getTopP().toString());
|
||||
}
|
||||
if (override.getMaxTokens() != null) {
|
||||
result.put("maxTokens", override.getMaxTokens().toString());
|
||||
}
|
||||
JSONObject extras = override.getExtras();
|
||||
if (extras != null) {
|
||||
extras.forEach((key, value) -> result.put("extra." + key, value == null ? "null" : value.toString()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface ThrowingSupplier<T> {
|
||||
T get() throws Exception;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package work.slhaf.partner.framework.agent.support
|
||||
|
||||
import work.slhaf.partner.framework.agent.exception.AgentRuntimeException
|
||||
|
||||
class Result<T> private constructor(
|
||||
private val value: T?,
|
||||
private val exception: Throwable?
|
||||
) {
|
||||
|
||||
fun isSuccess(): Boolean = exception == null
|
||||
|
||||
fun isFailure(): Boolean = exception != null
|
||||
|
||||
fun getOrNull(): T? = value
|
||||
|
||||
fun exceptionOrNull(): Throwable? = exception
|
||||
|
||||
fun getOrThrow(): T {
|
||||
if (exception == null) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return value as T
|
||||
}
|
||||
when (exception) {
|
||||
is RuntimeException -> throw exception
|
||||
is Error -> throw exception
|
||||
else -> throw IllegalStateException(exception.message, exception)
|
||||
}
|
||||
}
|
||||
|
||||
fun getOrDefault(defaultValue: T): T {
|
||||
return if (exception == null) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
value as T
|
||||
} else {
|
||||
defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return if (exception == null) {
|
||||
"Result.success($value)"
|
||||
} else {
|
||||
"Result.failure($exception)"
|
||||
}
|
||||
}
|
||||
|
||||
fun interface ThrowingSupplier<T> {
|
||||
@Throws(Throwable::class)
|
||||
fun get(): T
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun <T> success(value: T): Result<T> = Result(value, null)
|
||||
|
||||
@JvmStatic
|
||||
fun <T> failure(exception: Throwable): Result<T> = Result(null, exception)
|
||||
|
||||
@JvmStatic
|
||||
fun <T> runCatching(block: ThrowingSupplier<T>): Result<T> {
|
||||
return try {
|
||||
success(block.get())
|
||||
} catch (throwable: Throwable) {
|
||||
failure(
|
||||
when (throwable) {
|
||||
is AgentRuntimeException, is Error -> throwable
|
||||
else -> AgentRuntimeException(
|
||||
throwable.message ?: "Unexpected runtime failure.",
|
||||
throwable
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user