Skip to content
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

Seeking feedback #1

Open
langchain4j opened this issue Nov 13, 2024 · 9 comments
Open

Seeking feedback #1

langchain4j opened this issue Nov 13, 2024 · 9 comments

Comments

@langchain4j
Copy link
Contributor

Hi @michaelisvy, sorry for pinging you here - I couldn’t find another way to reach out. I hope you don’t mind.

I came across https://speakerdeck.com/michaelisvy/spring-ai-vs-langchain4j?slide=20 and wanted to get some feedback, especially on the "Support for Entities" category. Could you please share what problems you encountered there?

Thank you!

@michaelisvy
Copy link
Owner

hi @langchain4j ,
sure! Nice to be in touch.
Regarding entities, I was referring to this: https://speakerdeck.com/michaelisvy/spring-ai-vs-langchain4j?slide=32
I know it's also possible to do Structured Outputs in LangChain4J as shown here, but I find it complicated.

Hope that helps :)

@langchain4j
Copy link
Contributor Author

@michaelisvy thank you for taking the time to provide feedback, I really appreciate! Would you mind elaborating a bit further? I'd love to better understand which specific aspects you found complicated.

There are currently 2 ways to implement this in LC4J:

Using AI Services

langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}
langchain4j.open-ai.chat-model.model-name=gpt-4o-mini
@AiService
public interface ActorFilmsGenerator {

    ActorFilms generate(String query);
}
@RestController
public class ActorFilmsController {

    private final ActorFilmsGenerator actorFilmsGenerator;

    public ActorFilmsController(ActorFilmsGenerator actorFilmsGenerator) {
        this.actorFilmsGenerator = actorFilmsGenerator;
    }

    @GetMapping("/generateActorFilms")
    public ActorFilms generateActorFilms() {
        return actorFilmsGenerator.generate("Generate the 10 most popular movies starring Bruce Willis");
    }
}

Using low-level API

langchain4j.open-ai.chat-model.api-key=${OPENAI_API_KEY}
langchain4j.open-ai.chat-model.model-name=gpt-4o-mini
@RestController
public class ActorFilmsController {

    private final ChatLanguageModel chatModel;

    public ActorFilmsController(ChatLanguageModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/generateActorFilms")
    public String generateActorFilms() {
        ChatRequest chatRequest = ChatRequest.builder()
                .messages(UserMessage.from("Generate the 10 most popular movies starring Bruce Willis"))
                .responseFormat(ResponseFormat.builder()
                        .type(JSON)
                        .jsonSchema(JsonSchemas.jsonSchemaFrom(ActorFilms.class).get())
                        .build())
                .build();
        ChatResponse chatResponse = chatModel.chat(chatRequest);
        return chatResponse.aiMessage().text();
    }
}

@michaelisvy
Copy link
Owner

understood thanks! I had missed it from the documentation, I'll add it to my sample application.

By the way, one small other thing which I thought was missing: I didn't find a way to load an image from th classpath. See here: https://speakerdeck.com/michaelisvy/spring-ai-vs-langchain4j?slide=33

Cheers,
Michael.

@langchain4j
Copy link
Contributor Author

@michaelisvy sure, this can be done as well:

@Service
class ImageService {

    @Value("classpath:images/scientist.jpg")
    private Resource imageResourceScientist;

    private final ChatLanguageModel chatModel;

    public ImageService(ChatLanguageModel chatModel) {
        this.chatModel = chatModel;
    }

    public String describeScientist() throws IOException {
        UserMessage userMessage = UserMessage.from(
                TextContent.from("can you describe this person? And what is written on top of his head?"),
                ImageContent.from(encodeInBase64(imageResourceScientist), "image/jpg")
        );
        ChatResponse chatResponse = chatModel.chat(ChatRequest.builder().messages(userMessage).build());
        return chatResponse.aiMessage().text();
    }

    private String encodeInBase64(Resource resource) throws IOException {
        return Base64.getEncoder().encodeToString(resource.getContentAsByteArray());
    }
}

Thank you for pointing this out, I will add more examples to the documentation!

@michaelisvy
Copy link
Owner

thanks a lot for your help @langchain4j !

I implemented all the changes you suggested. Only thing I couldn't do is this:

interface BookExtractor {

    Book generate(String query);

    //List<Book> generateAll(String query);
    //doesn't seem to work yet
}

It seems that returning a collection of objects is not yet supported? (I couldn't find any information on it).

Regards,
Michael.

@langchain4j
Copy link
Contributor Author

@michaelisvy great, thanks a lot!

Yes, returning a List of objects is being finalized in langchain4j/langchain4j#1938 and will be available in the next (0.37.0) release.
For now one can wrap the list of objects into an object and return this wrapper:

record Books(List<Book> books) {}

interface BookExtractor {

    Books generateAll(String query);
}

@langchain4j
Copy link
Contributor Author

Do you mind If I'll submit a PR to implement a List<Book> case and adjust a few things?

@langchain4j
Copy link
Contributor Author

I have opened #2, I hope you don't mind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant