langchain4j [特性]一种用于AI服务声明式定义1-N个少量样本的方法

kjthegm6  于 2个月前  发布在  其他
关注(0)|答案(2)|浏览(34)

用户应该能够轻松地通过声明式/编程方式在AI服务上定义一个或多个少量的示例。
这些示例应包括来自LLM的文本和工具调用响应。
在将它们发送给LLM之前,应自动将这些示例添加到消息列表中。
这也应该与聊天记忆很好地配合使用,以便示例不会存储在内存中。
对于函数调用的本地模型进行微调并与OpenAI、Anthropic和Gemini一起测试工具调用示例。

wwtsj6pe

wwtsj6pe1#

我在article中实现了不同的方法来进行少量样本的提示。使用经典的大型字符串提示模板,以及用户/ AI消息列表,然后还有AiServices。我还没有调查工具调用方面。但我希望看到类似于TextClassification类的东西,但用于少量样本的提示。那可能会很有趣。

ctzwtxfj

ctzwtxfj2#

可能的选择有限:

声明式:

interface SentimentAnalyzer {

        @UserMessage("Analyze sentiment of:\n|||{{it}}|||")
        @Examples(
                @Example(user = "This is fantastic news!", ai = "POSITIVE"),
                @Example(user = "Pi is roughly equal to 3.14", ai = "NEUTRAL"),
                @Example(user = "I really disliked the pizza.", ai = "NEGATIVE")
        )
        Sentiment analyzeSentimentOf(String text);

// or

        @Example(user = "This is fantastic news!", ai = "POSITIVE")
        @Example(user = "Pi is roughly equal to 3.14", ai = "NEUTRAL")
        @Example(user = "I really disliked the pizza.", ai = "NEGATIVE")
        @UserMessage("Analyze sentiment of:\n|||{{it}}|||")
        Sentiment analyzeSentimentOf(String text);
}

编程式:

AiServices.builder(SentimentAnalyzer.class)
                .chatLanguageModel(chatLanguageModel)
                .examplesProvider((userMessage) -> // one could dynamically select examples depending on the (current) user message
                    List.of(UserMessage.from(...), AiMessage.from(...), ...)
                )
                .build();

我们还需要考虑如何提供带有工具使用说明的示例,例如在这个例子中应该有4条消息:
用户 - AI - 工具 - AI。
另外一件事:是否支持在 @Example 中使用模板?

interface SentimentAnalyzer {

        @UserMessage("Analyze sentiment of:\n|||{{text}}|||")
        @Examples(
                @Example(user = "I really disliked {{thing}}.", ai = "NEGATIVE")
        )
        Sentiment analyzeSentimentOf(String text, String thing);
}

另外一件事:如何指定带有结构化输出的示例(例如 POJO):作为原始 JSON 字符串还是作为 POJO 对象?

相关问题