quarkiverse / quarkus-langchain4j

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

handle collections as return types or error #943

Open maxandersen opened 1 month ago

maxandersen commented 1 month ago

if one use List, Set, Map etc. as return types the ask to genAI is: You must answer strictly in the following JSON format: {\n} - it should have the the json structure.

This fails:

    @RegisterAiService
    public interface jaidataAI {

        @SystemMessage(
            """
            IDENTITY

            You are a superintelligent AI that finds all mentions of data structures within an input and...
        """
        )
        @UserMessage("""
                    {data}
                """)
        List<FileInfo> generate(String data);
    }

    record FileInfo(String filename, String content) {};

but if change it to the following it works:

     FileInfos generate(String data);
    }
    record FileInfos(List<FileInfo>) {};
    record FileInfo(String filename, String content) {};

I would think it should be possible to handle - and if not, throw build error it needs to be a non-collection java type?

geoand commented 1 month ago

Are you using the latest version?

maxandersen commented 1 month ago
//DEPS io.quarkus:quarkus-bom:${quarkus.version:3.15.1}@pom
//DEPS io.quarkiverse.langchain4j:quarkus-langchain4j-openai:0.19.0.CR3

latest i could find in maven central at least.

geoand commented 1 month ago

Interesting, I remember fixing some related issues recently.

I'll have a look tomorrow

geoand commented 1 month ago

So indeed this is an issue with how upstream LangChain4j creates the json schema

geoand commented 1 month ago

@gsmet has run into similar so we probably need to rethink the whole approach

andreadimaio commented 1 month ago

It is also related to #671

gsmet commented 1 month ago

FWIW, using Map throws an error in 0.20.0 due to LangChain4j changes:

Caused by: dev.langchain4j.exception.IllegalConfigurationException: Illegal method return type: java.util.Map<java.lang.String, java.lang.Integer>
    at dev.langchain4j.exception.IllegalConfigurationException.illegalConfiguration(IllegalConfigurationException.java:12)
    at dev.langchain4j.service.output.ServiceOutputParser.validateJsonStructure(ServiceOutputParser.java:135)
    at dev.langchain4j.service.output.ServiceOutputParser.outputFormatInstructions(ServiceOutputParser.java:125)
    at io.quarkiverse.langchain4j.runtime.QuarkusServiceOutputParser.outputFormatInstructions(QuarkusServiceOutputParser.java:19)
    at io.quarkiverse.langchain4j.deployment.AiServicesProcessor.gatherMethodMetadata(AiServicesProcessor.java:1098)
    at io.quarkiverse.langchain4j.deployment.AiServicesProcessor.handleAiServices(AiServicesProcessor.java:932)
    at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:733)
    at io.quarkus.deployment.ExtensionLoader$3.execute(ExtensionLoader.java:856)
    at io.quarkus.builder.BuildContext.run(BuildContext.java:256)
    at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
    at org.jboss.threads.EnhancedQueueExecutor$Task.doRunWith(EnhancedQueueExecutor.java:2516)
    at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2495)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1521)
    at java.base/java.lang.Thread.run(Thread.java:1583)
    at org.jboss.threads.JBossThread.run(JBossThread.java:483)