InfoSphere/Spring AI 1.1.9-SNAPSHOT/ 返回书籍
OCI Generative AI

Cohere

qianmoQqianmoQ· 更新于 2026-09-19· 阅读 30 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录
This version is still in development and is not considered stable yet. For the latest stable version, please use Spring AI 2.0.1!

OCI GenAI Cohere Chat

OCI GenAI Service offers generative AI chat with on-demand models, or dedicated AI clusters.

The OCI Chat Models Page and OCI Generative AI Playground provide detailed information about using and hosting chat models on OCI.

Prerequisites

You will need an active Oracle Cloud Infrastructure (OCI) account to use the OCI GenAI Cohere Chat client. The client offers four different ways to connect, including simple authentication with a user and private key, workload identity, instance principal, or OCI configuration file authentication.

Add Repositories and BOM

Spring AI artifacts are published in Maven Central and Spring Snapshot repositories. Refer to the Artifact Repositories section to add these repositories to your build system.

To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the Dependency Management section to add the Spring AI BOM to your build system.

Auto-configuration

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the upgrade notes for more information.

Spring AI provides Spring Boot auto-configuration for the OCI GenAI Cohere Chat Client. To enable it add the following dependency to your project’s Maven pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-oci-genai</artifactId>
</dependency>

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-oci-genai'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Chat Properties

Connection Properties

The prefix spring.ai.oci.genai is the property prefix to configure the connection to OCI GenAI.

PropertyDescriptionDefault
spring.ai.oci.genai.authenticationTypeThe type of authentication to use when authenticating to OCI. May be file, instance-principal, workload-identity, or simple.file
spring.ai.oci.genai.regionOCI service region.us-chicago-1
spring.ai.oci.genai.tenantIdOCI tenant OCID, used when authenticating with simple auth.-
spring.ai.oci.genai.userIdOCI user OCID, used when authenticating with simple auth.-
spring.ai.oci.genai.fingerprintPrivate key fingerprint, used when authenticating with simple auth.-
spring.ai.oci.genai.privateKeyPrivate key content, used when authenticating with simple auth.-
spring.ai.oci.genai.passPhraseOptional private key passphrase, used when authenticating with simple auth and a passphrase protected private key.-
spring.ai.oci.genai.filePath to OCI config file. Used when authenticating with file auth.<user’s home directory>/.oci/config
spring.ai.oci.genai.profileOCI profile name. Used when authenticating with file auth.DEFAULT
spring.ai.oci.genai.endpointOptional OCI GenAI endpoint.-

Configuration Properties

Enabling and disabling of the chat auto-configurations are now configured via top level properties with the prefix spring.ai.model.chat. To enable, spring.ai.model.chat=oci-genai (It is enabled by default) To disable, spring.ai.model.chat=none (or any value which doesn’t match oci-genai) This change is done to allow configuration of multiple models.

The prefix spring.ai.oci.genai.cohere.chat is the property prefix that configures the ChatModel implementation for OCI GenAI Cohere Chat.

PropertyDescriptionDefault
spring.ai.model.chatEnable OCI GenAI Cohere chat model.oci-genai
spring.ai.oci.genai.cohere.chat.enabled (no longer valid)Enable OCI GenAI Cohere chat model.true
spring.ai.oci.genai.cohere.chat.options.modelModel OCID or endpoint-
spring.ai.oci.genai.cohere.chat.options.compartmentModel compartment OCID.-
spring.ai.oci.genai.cohere.chat.options.servingModeThe model serving mode to be used. May be on-demand, or dedicated.on-demand
spring.ai.oci.genai.cohere.chat.options.preambleOverrideOverride the chat model’s prompt preamble-
spring.ai.oci.genai.cohere.chat.options.temperatureInference temperature-
spring.ai.oci.genai.cohere.chat.options.topPTop P parameter-
spring.ai.oci.genai.cohere.chat.options.topKTop K parameter-
spring.ai.oci.genai.cohere.chat.options.frequencyPenaltyHigher values will reduce repeated tokens and outputs will be more random.-
spring.ai.oci.genai.cohere.chat.options.presencePenaltyHigher values encourage generating outputs with tokens that haven’t been used.-
spring.ai.oci.genai.cohere.chat.options.stopList of textual sequences that will end completions generation.-
spring.ai.oci.genai.cohere.chat.options.documentsList of documents used in chat context.-
All properties prefixed with spring.ai.oci.genai.cohere.chat.options can be overridden at runtime by adding a request specific Runtime Options to the Prompt call.

Runtime Options

The OCICohereChatOptions.java provides model configurations, such as the model to use, the temperature, the frequency penalty, etc.

On start-up, the default options can be configured with the OCICohereChatModel(api, options) constructor or the spring.ai.oci.genai.cohere.chat.options.* properties.

At run-time you can override the default options by adding new, request specific, options to the Prompt call. For example to override the default model and temperature for a specific request:

ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        OCICohereChatOptions.builder()
            .model("my-model-ocid")
            .compartment("my-compartment-ocid")
            .temperature(0.5)
        .build()
    ));

Sample Controller

Create a new Spring Boot project and add the spring-ai-starter-model-oci-genai to your pom (or gradle) dependencies.

Add a application.properties file, under the src/main/resources directory, to enable and configure the OCI GenAI Cohere chat model:

spring.ai.oci.genai.authenticationType=file
spring.ai.oci.genai.file=/path/to/oci/config/file
spring.ai.oci.genai.cohere.chat.options.compartment=my-compartment-ocid
spring.ai.oci.genai.cohere.chat.options.servingMode=on-demand
spring.ai.oci.genai.cohere.chat.options.model=my-chat-model-ocid
replace the file, compartment, and model with your values from your OCI account.

This will create a OCICohereChatModel implementation that you can inject into your class. Here is an example of a simple @Controller class that uses the chat model for text generations.

@RestController
public class ChatController {

    private final OCICohereChatModel chatModel;

    @Autowired
    public ChatController(OCICohereChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", chatModel.call(message));
    }

    @GetMapping("/ai/generateStream")
    public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        var prompt = new Prompt(new UserMessage(message));
        return chatModel.stream(prompt);
    }
}

Manual Configuration

The OCICohereChatModel implements the ChatModel and uses the OCI Java SDK to connect to the OCI GenAI service.

Add the spring-ai-oci-genai dependency to your project’s Maven pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-oci-genai</artifactId>
</dependency>

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-oci-genai'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Next, create a OCICohereChatModel and use it for text generations:

var CONFIG_FILE = Paths.get(System.getProperty("user.home"), ".oci", "config").toString();
var COMPARTMENT_ID = System.getenv("OCI_COMPARTMENT_ID");
var MODEL_ID = System.getenv("OCI_CHAT_MODEL_ID");

ConfigFileAuthenticationDetailsProvider authProvider = new ConfigFileAuthenticationDetailsProvider(
        CONFIG_FILE,
        "DEFAULT"
);
var genAi = GenerativeAiInferenceClient.builder()
        .region(Region.valueOf("us-chicago-1"))
        .build(authProvider);

var chatModel = new OCICohereChatModel(genAi, OCICohereChatOptions.builder()
        .model(MODEL_ID)
        .compartment(COMPARTMENT_ID)
        .servingMode("on-demand")
        .build());

ChatResponse response = chatModel.call(
        new Prompt("Generate the names of 5 famous pirates."));

The OCICohereChatOptions provides the configuration information for the chat requests. The OCICohereChatOptions.Builder is fluent options builder.

评论

登录后参与评论

正在加载评论…