-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to rewrite LLM result in an OutputGuardrail #1021
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import jakarta.enterprise.inject.spi.CDI; | ||
|
||
import dev.langchain4j.agent.tool.ToolExecutionRequest; | ||
import dev.langchain4j.agent.tool.ToolSpecification; | ||
import dev.langchain4j.data.message.AiMessage; | ||
import dev.langchain4j.data.message.UserMessage; | ||
|
@@ -57,8 +58,9 @@ public static Response<AiMessage> invokeOutputGuardrails(AiServiceMethodCreateIn | |
if (max <= 0) { | ||
max = 1; | ||
} | ||
|
||
OutputGuardrailResult result = null; | ||
while (attempt < max) { | ||
OutputGuardrailResult result; | ||
try { | ||
result = invokeOutputGuardRails(methodCreateInfo, output); | ||
} catch (Exception e) { | ||
|
@@ -97,9 +99,20 @@ public static Response<AiMessage> invokeOutputGuardrails(AiServiceMethodCreateIn | |
if (attempt == max) { | ||
throw new GuardrailException("Output validation failed. The guardrails have reached the maximum number of retries"); | ||
} | ||
|
||
if (result.isRewrittenResult()) { | ||
response = rewriteResponseWithText(response, result.successfulResult()); | ||
} | ||
|
||
return response; | ||
} | ||
|
||
public static Response<AiMessage> rewriteResponseWithText(Response<AiMessage> response, String text) { | ||
List<ToolExecutionRequest> tools = response.content().toolExecutionRequests(); | ||
AiMessage content = tools != null && !tools.isEmpty() ? new AiMessage(text, tools) : new AiMessage(text); | ||
return new Response<>(content, response.tokenUsage(), response.finishReason(), response.metadata()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static OutputGuardrailResult invokeOutputGuardRails(AiServiceMethodCreateInfo methodCreateInfo, | ||
OutputGuardrailParams params) { | ||
|
@@ -160,25 +173,28 @@ private static <GR extends GuardrailResult> GR guardrailResult(GuardrailParams p | |
for (Class<? extends Guardrail> bean : classes) { | ||
GR result = (GR) CDI.current().select(bean).get().validate(params).validatedBy(bean); | ||
if (result.isFatal()) { | ||
return result; | ||
return accumulatedResults.isRewrittenResult() ? (GR) result.blockRetry() : result; | ||
} | ||
if (result.isRewrittenResult()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't remember if this method is invoked when using streamed responses. Streams make things slightly more convoluted. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this method is used only for streamed response. I'm keeping this rewriting here regardless, but now if I find that this rewriting happened while streaming I throw an exception as discussed. |
||
params = params.withText(result.successfulResult()); | ||
} | ||
accumulatedResults = compose(accumulatedResults, result, producer); | ||
} | ||
|
||
return accumulatedResults; | ||
} | ||
|
||
private static <GR extends GuardrailResult> GR compose(GR first, GR second, | ||
private static <GR extends GuardrailResult> GR compose(GR oldResult, GR newResult, | ||
Function<List<? extends GuardrailResult.Failure>, GR> producer) { | ||
if (first.isSuccess()) { | ||
return second; | ||
if (oldResult.isSuccess()) { | ||
return newResult; | ||
} | ||
if (second.isSuccess()) { | ||
return first; | ||
if (newResult.isSuccess()) { | ||
return oldResult; | ||
} | ||
List<? extends GuardrailResult.Failure> failures = new ArrayList<>(); | ||
failures.addAll(first.failures()); | ||
failures.addAll(second.failures()); | ||
failures.addAll(oldResult.failures()); | ||
failures.addAll(newResult.failures()); | ||
return producer.apply(failures); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question for @geoand - do you know if we can use
@ActivateRequestContext
on the class itself?