Skip to content

Commit

Permalink
Adds support for Langchain4j Cohere provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-langer committed Feb 7, 2025
1 parent 66ba5be commit 497f47f
Show file tree
Hide file tree
Showing 17 changed files with 809 additions and 4 deletions.
4 changes: 4 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,10 @@
<groupId>io.helidon.integrations.langchain4j.providers</groupId>
<artifactId>helidon-integrations-langchain4j-providers-open-ai</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.integrations.langchain4j.providers</groupId>
<artifactId>helidon-integrations-langchain4j-providers-cohere</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.openapi</groupId>
<artifactId>helidon-openapi</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,11 @@
<artifactId>helidon-integrations-langchain4j-providers-open-ai</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.integrations.langchain4j.providers</groupId>
<artifactId>helidon-integrations-langchain4j-providers-cohere</artifactId>
<version>${helidon.version}</version>
</dependency>

<!-- OpenAPI support -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public final class Ai {
* Name that is used to qualify moderation models.
*/
public static final String MODERATION_MODEL_NAME = "moderation-model";
/**
* Name that is used to qualify scoring models.
*/
public static final String SCORING_MODEL_NAME = "scoring-model";

private Ai() {
}
Expand Down
166 changes: 166 additions & 0 deletions integrations/langchain4j/providers/cohere/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2025 Oracle and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.integrations.langchain4j.providers</groupId>
<artifactId>helidon-integrations-langchain4j-providers-project</artifactId>
<version>4.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>helidon-integrations-langchain4j-providers-cohere</artifactId>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-config</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-types</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.service</groupId>
<artifactId>helidon-service-registry</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.builder</groupId>
<artifactId>helidon-builder-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.integrations.langchain4j</groupId>
<artifactId>helidon-integrations-langchain4j</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-core</artifactId>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-cohere</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common.features</groupId>
<artifactId>helidon-common-features-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.helidon.config.metadata</groupId>
<artifactId>helidon-config-metadata-codegen</artifactId>
<version>${helidon.version}</version>
</path>
<path>
<groupId>io.helidon.codegen</groupId>
<artifactId>helidon-codegen-apt</artifactId>
<version>${helidon.version}</version>
</path>
<path>
<groupId>io.helidon.builder</groupId>
<artifactId>helidon-builder-codegen</artifactId>
<version>${helidon.version}</version>
</path>
<path>
<groupId>io.helidon.service</groupId>
<artifactId>helidon-service-codegen</artifactId>
<version>${helidon.version}</version>
</path>
<path>
<groupId>io.helidon.codegen</groupId>
<artifactId>helidon-codegen-helidon-copyright</artifactId>
<version>${helidon.version}</version>
</path>
<path>
<groupId>io.helidon.common.features</groupId>
<artifactId>helidon-common-features-processor</artifactId>
<version>${helidon.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
<dependencies>
<dependency>
<groupId>io.helidon.config.metadata</groupId>
<artifactId>helidon-config-metadata-codegen</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.codegen</groupId>
<artifactId>helidon-codegen-apt</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.builder</groupId>
<artifactId>helidon-builder-codegen</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.service</groupId>
<artifactId>helidon-service-codegen</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.codegen</groupId>
<artifactId>helidon-codegen-helidon-copyright</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.common.features</groupId>
<artifactId>helidon-common-features-processor</artifactId>
<version>${helidon.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.langchain4j.providers.cohere;

import io.helidon.common.Weighted;
import io.helidon.integrations.langchain4j.Ai;
import io.helidon.service.registry.Qualifier;

final class Cohere {
static final String COHERE = "cohere";
static final String SCORING_MODEL = COHERE + "." + Ai.SCORING_MODEL_NAME;
static final Qualifier COHERE_QUALIFIER = Qualifier.createNamed(COHERE);
static final double WEIGHT = Weighted.DEFAULT_WEIGHT - 20;

private Cohere() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.langchain4j.providers.cohere;

import java.time.Duration;
import java.util.Map;
import java.util.Optional;

import io.helidon.builder.api.Option;

interface CohereCommonConfig {
/**
* If set to {@code false} (default), Cohere model will not be available even if configured.
*
* @return whether Cohere model is enabled, defaults to {@code false}
*/
@Option.Configured
boolean enabled();

/**
* The base URL for the Cohere API.
*
* @return the base URL
*/
@Option.Configured
Optional<String> baseUrl();

/**
* The API key used to authenticate requests to the Cohere API.
*
* @return an {@link java.util.Optional} containing the API key
*/
@Option.Configured
Optional<String> apiKey();

/**
* Whether to log API requests.
*
* @return an {@link java.util.Optional} containing true if requests should be logged, false otherwise
*/
@Option.Configured
Optional<Boolean> logRequests();

/**
* Whether to log API responses.
*
* @return an {@link java.util.Optional} containing true if responses should be logged, false otherwise
*/
@Option.Configured
Optional<Boolean> logResponses();

/**
* A map containing custom headers.
*
* @return custom headers map
*/
@Option.Configured
@Option.Singular
Map<String, String> customHeaders();

/**
* The timeout setting for API requests.
*
* @return the timeout setting in {@link java.time.Duration#parse} format
*/
@Option.Configured
Optional<Duration> timeout();

/**
* The model name to use (e.g., "gpt-3.5-turbo").
*
* @return the model name
*/
@Option.Configured
Optional<String> modelName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.integrations.langchain4j.providers.cohere;

import java.util.Optional;

import io.helidon.builder.api.Option;
import io.helidon.builder.api.Prototype;

/**
* Configuration for the Cohere embedding model, {@link dev.langchain4j.model.cohere.CohereEmbeddingModel}.
*
* @see dev.langchain4j.model.cohere.CohereEmbeddingModel
*/
@Prototype.Configured(CohereEmbeddingModelConfigBlueprint.CONFIG_ROOT)
@Prototype.Blueprint
interface CohereEmbeddingModelConfigBlueprint extends CohereCommonConfig {
/**
* Default configuration prefix.
*/
String CONFIG_ROOT = "langchain4j.cohere.embedding-model";

/**
* Gets the input type.
*
* @return an {@link java.util.Optional} containing the input type
*/
@Option.Configured
Optional<String> inputType();

/**
* Gets the maximum number of segments per batch.
*
* @return an {@link java.util.Optional} containing the maximum number of segments per batch
*/
@Option.Configured
Optional<Integer> maxSegmentsPerBatch();
}
Loading

0 comments on commit 497f47f

Please sign in to comment.