interop

christos 2025-01-22T12:05:42.665819Z

I am trying to use java interop with langchain4j, more specificaly the Aiservices. My problem is that it requires an interface with one method for the Aiservices builder. So i have the following clojure protocol implemented:

(defprotocol Assistant
  (^String answer [this ^String query]))
Not sure if the hint do anything here, it seems not, i use it with Aiservices in the following way:
(defn make-assistant [model retriever]
 (try
  (.build (doto
            (AiServices/builder rag_base.core.Assistant)
            (.chatLanguageModel model)
            (.contentRetriever retriever)))
  (catch Throwable t (.println System/out t))))
Where rag-base.core is the namespace, hence rag_base.core.Assistant is the java interface of the corresponding protocol. However when i call make-assistant the type of the class i get is jdk.proxy4.$Proxy46 and wean a call the answear method i get
; eval (current-form): (answer assistant "test")
; (err) Execution error (IllegalConfigurationException) at dev.langchain4j.exception.IllegalConfigurationException/illegalConfiguration (IllegalConfigurationException.java:12).
; (err) Illegal method return type: class java.lang.Object
Indicating that is something wrong with the return type, so type hints dont work what can i do here ?

seancorfield 2025-01-22T18:17:44.642989Z

I would have expected you need definterface here, not defprotocol, but I'd be curious about what AiServices.builder() really requires here. If it truly accepts an interface, it has to return a proxy object since it cannot instantiate an interface...?

christos 2025-01-22T19:14:53.952919Z

correct it returns a proxy object

christos 2025-01-22T19:16:16.131579Z

i have totally forgotten that definterface existed, thanks

seancorfield 2025-01-22T19:17:09.559859Z

The docs seem to suggest builder takes no arguments and that you create via the interface: https://docs.langchain4j.dev/tutorials/ai-services/#simplest-ai-service

christos 2025-01-22T19:18:40.509409Z

That's not true, search for "naive rag" and see the implementation

seancorfield 2025-01-22T19:19:22.372339Z

Ah, mixed up build which doesn't and builder that does. I've never used any of this stuff before...

👍 1