## Change
In addition to `application/json`, Gemini also supports `text/x.enum` as
a structured output format, which is great for classification.
This PR covers both Google Vertex AI Gemini and Google AI Gemini models.
A minor internal change to create the `GeminiService` at construction
time rather than on each request.
+ Observability (ChatLanguageModel) for Google AI Gemini
## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] There are no breaking changes
- [X] I have added unit and integration tests for my change
- [X] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [/] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)
## Issue
#1506
## Change
Enabled Maven Enforcer Plugin on modules without existing version
conflicts to ensure they remain conflict-free. The Maven Enforcer Plugin
will now cause the build to fail if new conflicts are introduced
guarding against these.
## Tests
`mvn clean test` passed
Gemini updates:
* update to latest Java SDK version
* #1269
* #1270
* #1208
* #1399
* #1397
* #1182
* #828
* fixes parallel function calling which wasn't working properly in the
previous release
* refactored a bit the `generate()` method to have a single entry point
and less duplication
Vertex AI embedding model:
* add new task types (question answering and fact verification)
Imagen image model:
* support more configuration parameters
* #1367
## Issue
#1080
## Change
Adds a `dimension()` method in the `EmbeddingModel` interface that returns the dimension of the `Embedding`s produced by this `EmbeddingModel`.
If dimension is known (e.g., can be derived from the model name), it is returned. If not known, a "test" string is embedded and the embedding dimension is cached and returned.
## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes
- [x] I have added unit and integration tests for my change
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [x] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
## Checklist for adding new model integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added my new module in the
[BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml)
## Checklist for adding new embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreIT` that extends
from either `EmbeddingStoreIT` or `EmbeddingStoreWithFilteringIT`
- [ ] I have added my new module in the
[BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml)
## Checklist for changing existing embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have manually verified that the
`{NameOfIntegration}EmbeddingStore` works correctly with the data
persisted using the latest released version of LangChain4j
This is small refactoring
There are bunch of places where use deprecated methods.
These changes fix this issue.
## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes
- [ ] I have added unit and integration tests for my change
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [x] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- support system instructions
- support parallel function calling
- should avoid the allocation issue faced sometimes because of the
lifecycle of the underlying VertexAI client object
It's possible to customize different kinds of tasks for embedding:
- `RETRIEVAL_QUERY`
- `RETRIEVAL_DOCUMENT`
- `SEMANTIC_SIMILARITY`
- `CLASSIFICATION, CLUSTERING`
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **New Features**
- Introduced `TaskType` enum for specifying task types in embedding
instances, enhancing customization for retrieval, similarity,
classification, and clustering tasks.
- Added `taskType` parameter to the constructor for setting task type in
embedding instances.
- **Tests**
- Added tests for semantic similarity, text classification, and document
retrieval embeddings using different task types.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Hi! Noticed _almost all_ tests used AssertJ, but in some cases JUnit was
still used. In addition to that some tests don't use the most expressive
assertions. Figured clean that up such that you get better assertions if
any tests were to fail. Compare for instance
```diff
- assertThat(document.metadata().asMap().size()).isEqualTo(4);
+ assertThat(document.metadata().asMap()).hasSize(4);
```
The first one will print expected 5 to be equal to 4, whereas the second
one shows the contents of the map involved.
Being consistent with your test library also stops bad patterns from
repeating accidentally through copy-and-paste. If you want to enforce
these best practices through an automated pull request check that's also
an option. Let me know if you'd want that as well. Hope that helps!
With this change, the batching of embedding calls will be done
automatically, adjusting the number of text segments to calculate
embeddings for, according to the max number of tokens per batch call (up
to 20k) and the max number text segments per batch call (up to 250).
There are 2 new methods in the builder to specify the max number of
tokens and max number of text segments per call, as sometimes, the
calculation of tokens seems to not be correctly returned by the
underlying model (with textembedding-gecko@001 it's fine, but
textembedding-gecko@003 seems to calculate 5-10% less tokens on a per
segment basis, than it returns for the overall call)
This is a small prototype based on discussions originating from
https://github.com/ai-for-java/openai4j/pull/13
The approach I took here is to allow for decorating the models/builders
with additional functionality without having to extend model classes or
builders. I did it for a single model in this prototype - the
`OpenAiChatModel`, but this pattern could be applied to all of the other
models across Langchain4J.
That doesn't mean you couldn't extend the model classes if you wanted to
use inheritance. I just try to avoid it and use composition instead.
I also added a test which shows how it would be used. Downstream
libraries (like Spring Boot or Quarkus) could use this mechanism to
extend/enhance with their own capabilities which aren't necessarily part
of the model.
Let me know what you think @geoand / @langchain4j !
Happy to continue conversation and see where we can bring this!
This PR is a fix for #444
- Missing metadata for Gson serialization was added for
`langchain4j-vertex-ai` module
- `commons-logging` was excluded from dependencies as it causes troubles
during native build