quarkiverse / quarkus-langchain4j

Quarkus Langchain4j extension
https://docs.quarkiverse.io/quarkus-langchain4j/dev/index.html
Apache License 2.0
148 stars 89 forks source link

Provide an abstract output guardrails for json data extraction #1060

Closed mariofusco closed 1 week ago

mariofusco commented 2 weeks ago

This is a first implementation of @lordofthejars 's idea discussed here https://github.com/quarkiverse/quarkus-langchain4j/discussions/1022

I believe this could be basically used for each AiService trying to extract a POJO from the user prompt like

@RegisterAiService
@Singleton
public interface CustomerExtractor {

    @UserMessage("Extract information about a customer from this text '{text}'. The response must contain only the JSON with customer's data and without any other sentence.")
    @OutputGuardrails(CustomerExtractionOutputGuardrail.class)
    Customer extractData(String text);
}

implementing the output guardrail simply extending the provided AbstractJsonExtractorOutputGuardrail as it follows

@ApplicationScoped
public class CustomerExtractionOutputGuardrail extends AbstractJsonExtractorOutputGuardrail {

    @Override
    protected Class<?> getOutputClass() {
        return Customer.class;
    }
}

/cc @geoand @cescoffier

lordofthejars commented 2 weeks ago

Oh, wow, that is really nice. I have a list of guardrails that we could provide. I basically followed the LLM-guard project to check what guardrails they are offering and which of them we could implement.

mariofusco commented 2 weeks ago

Oh, wow, that is really nice.

This is only the more straightforward things that it comes me to mind. We could add more features and implementations incrementally with other commits.

I have a list of guardrails that we could provide. I basically followed the LLM-guard project to check what guardrails they are offering and which of them we could implement.

If you already have a list of guardrails that we could implement and provide out-of-the-box please feel free to share it, I'd be glad to collaborate on this.

lordofthejars commented 2 weeks ago

Mario are you up next week for a quick meeting and I explain my idea?

geoand commented 2 weeks ago

@gsmet when you have some time, I would love to chat with you about setting up a bot for this repo :) A good starting point would be providing the awesome CI summary we have in the main Quarkus repo

gsmet commented 2 weeks ago

I think we could install the Quarkus one here and only enable some of its features (that's something we can do).

I'll add to my TODO list to prepare your CI workflows for that.

geoand commented 2 weeks ago

That would be awesome, thanks!

mariofusco commented 1 week ago

@cescoffier I should have addressed all your remarks. For now I'm keeping the 2 commits separated only to make it easier for you to review my latest change. In particular now an OutputGuardrail can not only rewrite the textual LLM response, but also provide an explicit object result for the whole AiService invocation. In this way the object eventually deserialized from the json response can be added to the result and returned as it is instead of requiring a second deserialization.

quarkus-bot[bot] commented 1 week ago

:waning_crescent_moon: This workflow status is outdated as a new workflow run has been triggered.

Status for workflow Build (on pull request)

This is the status report for running Build (on pull request) on commit 634ada351aa3cefd15c4929e731a542878938569.

:white_check_mark: The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

mariofusco commented 1 week ago

I implemented all the requested changes and squashed all commits.

quarkus-bot[bot] commented 1 week ago

Status for workflow Build (on pull request)

This is the status report for running Build (on pull request) on commit 78fcaf4ec4f3b5e3d9d2bd7242d50e8b82e79b55.

:white_check_mark: The latest workflow run for the pull request has completed successfully.

It should be safe to merge provided you have a look at the other checks in the summary.

cescoffier commented 1 week ago

We should extend a bit the guardrail documentation to remove the json example and mention this one (with an example)

mariofusco commented 1 week ago

We should extend a bit the guardrail documentation to remove the json example and mention this one (with an example)

Agreed. I will rework docs as soon as this will be merged.