diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 5d868be..1fbf230 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -8,7 +8,7 @@ jobs:
steps:
- name: Checkout repo
- uses: actions/checkout@v4
+ uses: actions/checkout@v3
- name: Set up Java
id: setup-jre
@@ -25,7 +25,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
- uses: actions/checkout@v4
+ uses: actions/checkout@v3
- name: Set up Java
id: setup-jre
@@ -43,7 +43,7 @@ jobs:
steps:
- name: Checkout repo
- uses: actions/checkout@v4
+ uses: actions/checkout@v3
- name: Set up Java
id: setup-jre
diff --git a/build.gradle b/build.gradle
index 102bc1e..d94c1ff 100644
--- a/build.gradle
+++ b/build.gradle
@@ -46,7 +46,7 @@ java {
group = 'com.cohere'
-version = '1.8.0'
+version = '0.0.1'
jar {
dependsOn(":generatePomFileForMavenPublication")
@@ -77,7 +77,7 @@ publishing {
maven(MavenPublication) {
groupId = 'com.cohere'
artifactId = 'cohere-java'
- version = '1.8.0'
+ version = '0.0.1'
from components.java
pom {
name = 'cohere'
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
index 1b33c55..a4b76b9 100644
Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index d4081da..e2847c8 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
diff --git a/gradlew b/gradlew
index 23d15a9..f5feea6 100755
--- a/gradlew
+++ b/gradlew
@@ -86,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
-APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
+APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
+' "$PWD" ) || exit
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
@@ -114,7 +115,7 @@ case "$( uname )" in #(
NONSTOP* ) nonstop=true ;;
esac
-CLASSPATH="\\\"\\\""
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
@@ -205,7 +206,7 @@ fi
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Collect all arguments for the java command:
-# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
+# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
@@ -213,7 +214,7 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
- -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
+ org.gradle.wrapper.GradleWrapperMain \
"$@"
# Stop when "xargs" is not available.
diff --git a/gradlew.bat b/gradlew.bat
index 5eed7ee..9b42019 100644
--- a/gradlew.bat
+++ b/gradlew.bat
@@ -70,11 +70,11 @@ goto fail
:execute
@rem Setup the command line
-set CLASSPATH=
+set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
-"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
+"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
diff --git a/reference.md b/reference.md
deleted file mode 100644
index fb5cb69..0000000
--- a/reference.md
+++ /dev/null
@@ -1,4735 +0,0 @@
-# Reference
-client.chatStream(request) -> Optional<StreamedChatResponse>
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Generates a streamed text response to a user message.
-
-To learn how to use the Chat API and RAG follow our [Text Generation guides](https://docs.cohere.com/docs/chat-api).
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.chatStream(
- ChatStreamRequest
- .builder()
- .message("hello world!")
- .stream(true)
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**accepts:** `Optional` β Pass text/event-stream to receive the streamed response as server-sent events. The default is `\n` delimited events.
-
-
-
-
-
--
-
-**message:** `String`
-
-Text input for the model to respond to.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**model:** `Optional`
-
-The name of a compatible [Cohere model](https://docs.cohere.com/docs/models) or the ID of a [fine-tuned](https://docs.cohere.com/docs/chat-fine-tuning) model.
-
-Compatible Deployments: Cohere Platform, Private Deployments
-
-
-
-
-
--
-
-**stream:** `Boolean`
-
-Defaults to `false`.
-
-When `true`, the response will be a JSON stream of events. The final event will contain the complete response, and will have an `event_type` of `"stream-end"`.
-
-Streaming is beneficial for user interfaces that render the contents of the response piece by piece, as it gets generated.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**preamble:** `Optional`
-
-When specified, the default Cohere preamble will be replaced with the provided one. Preambles are a part of the prompt used to adjust the model's overall behavior and conversation style, and use the `SYSTEM` role.
-
-The `SYSTEM` role is also used for the contents of the optional `chat_history=` parameter. When used with the `chat_history=` parameter it adds content throughout a conversation. Conversely, when used with the `preamble=` parameter it adds content at the start of the conversation only.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**chatHistory:** `Optional
>`
-
-A list of previous messages between the user and the model, giving the model conversational context for responding to the user's `message`.
-
-Each item represents a single message in the chat history, excluding the current user turn. It has two properties: `role` and `message`. The `role` identifies the sender (`CHATBOT`, `SYSTEM`, or `USER`), while the `message` contains the text content.
-
-The chat_history parameter should not be used for `SYSTEM` messages in most cases. Instead, to add a `SYSTEM` role message at the beginning of a conversation, the `preamble` parameter should be used.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**conversationId:** `Optional`
-
-An alternative to `chat_history`.
-
-Providing a `conversation_id` creates or resumes a persisted conversation with the specified ID. The ID can be any non empty string.
-
-Compatible Deployments: Cohere Platform
-
-
-
-
-
--
-
-**promptTruncation:** `Optional`
-
-Defaults to `AUTO` when `connectors` are specified and `OFF` in all other cases.
-
-Dictates how the prompt will be constructed.
-
-With `prompt_truncation` set to "AUTO", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be changed and ranked by relevance.
-
-With `prompt_truncation` set to "AUTO_PRESERVE_ORDER", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be preserved as they are inputted into the API.
-
-With `prompt_truncation` set to "OFF", no elements will be dropped. If the sum of the inputs exceeds the model's context length limit, a `TooManyTokens` error will be returned.
-
-Compatible Deployments:
- - AUTO: Cohere Platform Only
- - AUTO_PRESERVE_ORDER: Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**connectors:** `Optional
>`
-
-Accepts `{"id": "web-search"}`, and/or the `"id"` for a custom [connector](https://docs.cohere.com/docs/connectors), if you've [created](https://docs.cohere.com/v1/docs/creating-and-deploying-a-connector) one.
-
-When specified, the model's reply will be enriched with information found by querying each of the connectors (RAG).
-
-Compatible Deployments: Cohere Platform
-
-
-
-
-
--
-
-**searchQueriesOnly:** `Optional`
-
-Defaults to `false`.
-
-When `true`, the response will only contain a list of generated search queries, but no search will take place, and no reply from the model to the user's `message` will be generated.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**documents:** `Optional
>>`
-
-A list of relevant documents that the model can cite to generate a more accurate reply. Each document is a string-string dictionary.
-
-Example:
-```
-[
- { "title": "Tall penguins", "text": "Emperor penguins are the tallest." },
- { "title": "Penguin habitats", "text": "Emperor penguins only live in Antarctica." },
-]
-```
-
-Keys and values from each document will be serialized to a string and passed to the model. The resulting generation will include citations that reference some of these documents.
-
-Some suggested keys are "text", "author", and "date". For better generation quality, it is recommended to keep the total word count of the strings in the dictionary to under 300 words.
-
-An `id` field (string) can be optionally supplied to identify the document in the citations. This field will not be passed to the model.
-
-An `_excludes` field (array of strings) can be optionally supplied to omit some key-value pairs from being shown to the model. The omitted fields will still show up in the citation object. The "_excludes" field will not be passed to the model.
-
-See ['Document Mode'](https://docs.cohere.com/docs/retrieval-augmented-generation-rag#document-mode) in the guide for more information.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**citationQuality:** `Optional`
-
-Defaults to `"accurate"`.
-
-Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**temperature:** `Optional`
-
-Defaults to `0.3`.
-
-A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations.
-
-Randomness can be further maximized by increasing the value of the `p` parameter.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**maxTokens:** `Optional`
-
-The maximum number of tokens the model will generate as part of the response. Note: Setting a low value may result in incomplete generations.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**maxInputTokens:** `Optional`
-
-The maximum number of input tokens to send to the model. If not specified, `max_input_tokens` is the model's context length limit minus a small buffer.
-
-Input will be truncated according to the `prompt_truncation` parameter.
-
-Compatible Deployments: Cohere Platform
-
-
-
-
-
--
-
-**k:** `Optional`
-
-Ensures only the top `k` most likely tokens are considered for generation at each step.
-Defaults to `0`, min value of `0`, max value of `500`.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**p:** `Optional`
-
-Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`.
-Defaults to `0.75`. min value of `0.01`, max value of `0.99`.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**seed:** `Optional`
-
-If specified, the backend will make a best effort to sample tokens
-deterministically, such that repeated requests with the same
-seed and parameters should return the same result. However,
-determinism cannot be totally guaranteed.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**stopSequences:** `Optional
>`
-
-A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**frequencyPenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-
-Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**presencePenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-
-Used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**tools:** `Optional
>`
-
-A list of available tools (functions) that the model may suggest invoking before producing a text response.
-
-When `tools` is passed (without `tool_results`), the `text` field in the response will be `""` and the `tool_calls` field in the response will be populated with a list of tool calls that need to be made. If no calls need to be made, the `tool_calls` array will be empty.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**toolResults:** `Optional
>`
-
-A list of results from invoking tools recommended by the model in the previous chat turn. Results are used to produce a text response and will be referenced in citations. When using `tool_results`, `tools` must be passed as well.
-Each tool_result contains information about how it was invoked, as well as a list of outputs in the form of dictionaries.
-
-**Note**: `outputs` must be a list of objects. If your tool returns a single object (eg `{"status": 200}`), make sure to wrap it in a list.
-```
-tool_results = [
- {
- "call": {
- "name": ,
- "parameters": {
- :
- }
- },
- "outputs": [{
- :
- }]
- },
- ...
-]
-```
-**Note**: Chat calls with `tool_results` should not be included in the Chat history to avoid duplication of the message text.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**forceSingleStep:** `Optional` β Forces the chat to be single step. Defaults to `false`.
-
-
-
-
-
--
-
-**responseFormat:** `Optional`
-
-
-
-
-
--
-
-**safetyMode:** `Optional`
-
-Used to select the [safety instruction](https://docs.cohere.com/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`.
-When `NONE` is specified, the safety instruction will be omitted.
-
-Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters.
-
-**Note**: This parameter is only compatible newer Cohere models, starting with [Command R 08-2024](https://docs.cohere.com/docs/command-r#august-2024-release) and [Command R+ 08-2024](https://docs.cohere.com/docs/command-r-plus#august-2024-release).
-
-**Note**: `command-r7b-12-2024` and newer models only support `"CONTEXTUAL"` and `"STRICT"` modes.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
-
-
-
-
-
-
-client.chat(request) -> NonStreamedChatResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Generates a text response to a user message.
-To learn how to use the Chat API and RAG follow our [Text Generation guides](https://docs.cohere.com/docs/chat-api).
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.chatStream(
- ChatStreamRequest
- .builder()
- .message("What year was he born?")
- .stream(false)
- .chatHistory(
- new ArrayList(
- Arrays.asList(
- Message.user(
- ChatMessage
- .builder()
- .message("Who discovered gravity?")
- .build()
- ),
- Message.chatbot(
- ChatMessage
- .builder()
- .message("The man who is widely credited with discovering gravity is Sir Isaac Newton")
- .build()
- )
- )
- )
- )
- .connectors(
- new ArrayList(
- Arrays.asList(
- ChatConnector
- .builder()
- .id("web-search")
- .build()
- )
- )
- )
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**accepts:** `Optional` β Pass text/event-stream to receive the streamed response as server-sent events. The default is `\n` delimited events.
-
-
-
-
-
--
-
-**message:** `String`
-
-Text input for the model to respond to.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**model:** `Optional`
-
-The name of a compatible [Cohere model](https://docs.cohere.com/docs/models) or the ID of a [fine-tuned](https://docs.cohere.com/docs/chat-fine-tuning) model.
-
-Compatible Deployments: Cohere Platform, Private Deployments
-
-
-
-
-
--
-
-**stream:** `Boolean`
-
-Defaults to `false`.
-
-When `true`, the response will be a JSON stream of events. The final event will contain the complete response, and will have an `event_type` of `"stream-end"`.
-
-Streaming is beneficial for user interfaces that render the contents of the response piece by piece, as it gets generated.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**preamble:** `Optional`
-
-When specified, the default Cohere preamble will be replaced with the provided one. Preambles are a part of the prompt used to adjust the model's overall behavior and conversation style, and use the `SYSTEM` role.
-
-The `SYSTEM` role is also used for the contents of the optional `chat_history=` parameter. When used with the `chat_history=` parameter it adds content throughout a conversation. Conversely, when used with the `preamble=` parameter it adds content at the start of the conversation only.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**chatHistory:** `Optional
>`
-
-A list of previous messages between the user and the model, giving the model conversational context for responding to the user's `message`.
-
-Each item represents a single message in the chat history, excluding the current user turn. It has two properties: `role` and `message`. The `role` identifies the sender (`CHATBOT`, `SYSTEM`, or `USER`), while the `message` contains the text content.
-
-The chat_history parameter should not be used for `SYSTEM` messages in most cases. Instead, to add a `SYSTEM` role message at the beginning of a conversation, the `preamble` parameter should be used.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**conversationId:** `Optional`
-
-An alternative to `chat_history`.
-
-Providing a `conversation_id` creates or resumes a persisted conversation with the specified ID. The ID can be any non empty string.
-
-Compatible Deployments: Cohere Platform
-
-
-
-
-
--
-
-**promptTruncation:** `Optional`
-
-Defaults to `AUTO` when `connectors` are specified and `OFF` in all other cases.
-
-Dictates how the prompt will be constructed.
-
-With `prompt_truncation` set to "AUTO", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be changed and ranked by relevance.
-
-With `prompt_truncation` set to "AUTO_PRESERVE_ORDER", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be preserved as they are inputted into the API.
-
-With `prompt_truncation` set to "OFF", no elements will be dropped. If the sum of the inputs exceeds the model's context length limit, a `TooManyTokens` error will be returned.
-
-Compatible Deployments:
- - AUTO: Cohere Platform Only
- - AUTO_PRESERVE_ORDER: Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**connectors:** `Optional
>`
-
-Accepts `{"id": "web-search"}`, and/or the `"id"` for a custom [connector](https://docs.cohere.com/docs/connectors), if you've [created](https://docs.cohere.com/v1/docs/creating-and-deploying-a-connector) one.
-
-When specified, the model's reply will be enriched with information found by querying each of the connectors (RAG).
-
-Compatible Deployments: Cohere Platform
-
-
-
-
-
--
-
-**searchQueriesOnly:** `Optional`
-
-Defaults to `false`.
-
-When `true`, the response will only contain a list of generated search queries, but no search will take place, and no reply from the model to the user's `message` will be generated.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**documents:** `Optional
>>`
-
-A list of relevant documents that the model can cite to generate a more accurate reply. Each document is a string-string dictionary.
-
-Example:
-```
-[
- { "title": "Tall penguins", "text": "Emperor penguins are the tallest." },
- { "title": "Penguin habitats", "text": "Emperor penguins only live in Antarctica." },
-]
-```
-
-Keys and values from each document will be serialized to a string and passed to the model. The resulting generation will include citations that reference some of these documents.
-
-Some suggested keys are "text", "author", and "date". For better generation quality, it is recommended to keep the total word count of the strings in the dictionary to under 300 words.
-
-An `id` field (string) can be optionally supplied to identify the document in the citations. This field will not be passed to the model.
-
-An `_excludes` field (array of strings) can be optionally supplied to omit some key-value pairs from being shown to the model. The omitted fields will still show up in the citation object. The "_excludes" field will not be passed to the model.
-
-See ['Document Mode'](https://docs.cohere.com/docs/retrieval-augmented-generation-rag#document-mode) in the guide for more information.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**citationQuality:** `Optional`
-
-Defaults to `"accurate"`.
-
-Dictates the approach taken to generating citations as part of the RAG flow by allowing the user to specify whether they want `"accurate"` results, `"fast"` results or no results.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**temperature:** `Optional`
-
-Defaults to `0.3`.
-
-A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations.
-
-Randomness can be further maximized by increasing the value of the `p` parameter.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**maxTokens:** `Optional`
-
-The maximum number of tokens the model will generate as part of the response. Note: Setting a low value may result in incomplete generations.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**maxInputTokens:** `Optional`
-
-The maximum number of input tokens to send to the model. If not specified, `max_input_tokens` is the model's context length limit minus a small buffer.
-
-Input will be truncated according to the `prompt_truncation` parameter.
-
-Compatible Deployments: Cohere Platform
-
-
-
-
-
--
-
-**k:** `Optional`
-
-Ensures only the top `k` most likely tokens are considered for generation at each step.
-Defaults to `0`, min value of `0`, max value of `500`.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**p:** `Optional`
-
-Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`.
-Defaults to `0.75`. min value of `0.01`, max value of `0.99`.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**seed:** `Optional`
-
-If specified, the backend will make a best effort to sample tokens
-deterministically, such that repeated requests with the same
-seed and parameters should return the same result. However,
-determinism cannot be totally guaranteed.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**stopSequences:** `Optional
>`
-
-A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**frequencyPenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-
-Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**presencePenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-
-Used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**tools:** `Optional
>`
-
-A list of available tools (functions) that the model may suggest invoking before producing a text response.
-
-When `tools` is passed (without `tool_results`), the `text` field in the response will be `""` and the `tool_calls` field in the response will be populated with a list of tool calls that need to be made. If no calls need to be made, the `tool_calls` array will be empty.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**toolResults:** `Optional
>`
-
-A list of results from invoking tools recommended by the model in the previous chat turn. Results are used to produce a text response and will be referenced in citations. When using `tool_results`, `tools` must be passed as well.
-Each tool_result contains information about how it was invoked, as well as a list of outputs in the form of dictionaries.
-
-**Note**: `outputs` must be a list of objects. If your tool returns a single object (eg `{"status": 200}`), make sure to wrap it in a list.
-```
-tool_results = [
- {
- "call": {
- "name": ,
- "parameters": {
- :
- }
- },
- "outputs": [{
- :
- }]
- },
- ...
-]
-```
-**Note**: Chat calls with `tool_results` should not be included in the Chat history to avoid duplication of the message text.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**forceSingleStep:** `Optional` β Forces the chat to be single step. Defaults to `false`.
-
-
-
-
-
--
-
-**responseFormat:** `Optional`
-
-
-
-
-
--
-
-**safetyMode:** `Optional`
-
-Used to select the [safety instruction](https://docs.cohere.com/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`.
-When `NONE` is specified, the safety instruction will be omitted.
-
-Safety modes are not yet configurable in combination with `tools`, `tool_results` and `documents` parameters.
-
-**Note**: This parameter is only compatible newer Cohere models, starting with [Command R 08-2024](https://docs.cohere.com/docs/command-r#august-2024-release) and [Command R+ 08-2024](https://docs.cohere.com/docs/command-r-plus#august-2024-release).
-
-**Note**: `command-r7b-12-2024` and newer models only support `"CONTEXTUAL"` and `"STRICT"` modes.
-
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
-
-
-
-
-
-
-client.generateStream(request) -> Optional<GenerateStreamedResponse>
-
--
-
-#### π Description
-
-
--
-
-
--
-
-
-This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat with Streaming API.
-
-Generates realistic text conditioned on a given input.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.generateStream(
- GenerateStreamRequest
- .builder()
- .prompt("Please explain to me how LLMs work")
- .stream(true)
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**prompt:** `String`
-
-The input text that serves as the starting point for generating the response.
-Note: The prompt will be pre-processed and modified before reaching the model.
-
-
-
-
-
--
-
-**model:** `Optional`
-
-The identifier of the model to generate with. Currently available models are `command` (default), `command-nightly` (experimental), `command-light`, and `command-light-nightly` (experimental).
-Smaller, "light" models are faster, while larger models will perform better. [Custom models](https://docs.cohere.com/docs/training-custom-models) can also be supplied with their full ID.
-
-
-
-
-
--
-
-**numGenerations:** `Optional` β The maximum number of generations that will be returned. Defaults to `1`, min value of `1`, max value of `5`.
-
-
-
-
-
--
-
-**stream:** `Boolean`
-
-When `true`, the response will be a JSON stream of events. Streaming is beneficial for user interfaces that render the contents of the response piece by piece, as it gets generated.
-
-The final event will contain the complete response, and will contain an `is_finished` field set to `true`. The event will also contain a `finish_reason`, which can be one of the following:
-- `COMPLETE` - the model sent back a finished reply
-- `MAX_TOKENS` - the reply was cut off because the model reached the maximum number of tokens for its context length
-- `ERROR` - something went wrong when generating the reply
-- `ERROR_TOXIC` - the model generated a reply that was deemed toxic
-
-
-
-
-
--
-
-**maxTokens:** `Optional`
-
-The maximum number of tokens the model will generate as part of the response. Note: Setting a low value may result in incomplete generations.
-
-This parameter is off by default, and if it's not specified, the model will continue generating until it emits an EOS completion token. See [BPE Tokens](/bpe-tokens-wiki) for more details.
-
-Can only be set to `0` if `return_likelihoods` is set to `ALL` to get the likelihood of the prompt.
-
-
-
-
-
--
-
-**truncate:** `Optional`
-
-One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length.
-
-Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model.
-
-If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned.
-
-
-
-
-
--
-
-**temperature:** `Optional`
-
-A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations. See [Temperature](/temperature-wiki) for more details.
-Defaults to `0.75`, min value of `0.0`, max value of `5.0`.
-
-
-
-
-
--
-
-**seed:** `Optional`
-
-If specified, the backend will make a best effort to sample tokens
-deterministically, such that repeated requests with the same
-seed and parameters should return the same result. However,
-determinism cannot be totally guaranteed.
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**preset:** `Optional`
-
-Identifier of a custom preset. A preset is a combination of parameters, such as prompt, temperature etc. You can create presets in the [playground](https://dashboard.cohere.com/playground/generate).
-When a preset is specified, the `prompt` parameter becomes optional, and any included parameters will override the preset's parameters.
-
-
-
-
-
--
-
-**endSequences:** `Optional
>` β The generated text will be cut at the beginning of the earliest occurrence of an end sequence. The sequence will be excluded from the text.
-
-
-
-
-
--
-
-**stopSequences:** `Optional
>` β The generated text will be cut at the end of the earliest occurrence of a stop sequence. The sequence will be included the text.
-
-
-
-
-
--
-
-**k:** `Optional`
-
-Ensures only the top `k` most likely tokens are considered for generation at each step.
-Defaults to `0`, min value of `0`, max value of `500`.
-
-
-
-
-
--
-
-**p:** `Optional`
-
-Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`.
-Defaults to `0.75`. min value of `0.01`, max value of `0.99`.
-
-
-
-
-
--
-
-**frequencyPenalty:** `Optional`
-
-Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
-
-Using `frequency_penalty` in combination with `presence_penalty` is not supported on newer models.
-
-
-
-
-
--
-
-**presencePenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-
-Can be used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
-
-Using `frequency_penalty` in combination with `presence_penalty` is not supported on newer models.
-
-
-
-
-
--
-
-**returnLikelihoods:** `Optional`
-
-One of `GENERATION|NONE` to specify how and if the token likelihoods are returned with the response. Defaults to `NONE`.
-
-If `GENERATION` is selected, the token likelihoods will only be provided for generated text.
-
-WARNING: `ALL` is deprecated, and will be removed in a future release.
-
-
-
-
-
--
-
-**rawPrompting:** `Optional` β When enabled, the user's prompt will be sent to the model without any pre-processing.
-
-
-
-
-
-
-
-
-
-
-
-client.generate(request) -> Generation
-
--
-
-#### π Description
-
-
--
-
-
--
-
-
-This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
-
-Generates realistic text conditioned on a given input.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.generateStream(
- GenerateStreamRequest
- .builder()
- .prompt("Please explain to me how LLMs work")
- .stream(false)
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**prompt:** `String`
-
-The input text that serves as the starting point for generating the response.
-Note: The prompt will be pre-processed and modified before reaching the model.
-
-
-
-
-
--
-
-**model:** `Optional`
-
-The identifier of the model to generate with. Currently available models are `command` (default), `command-nightly` (experimental), `command-light`, and `command-light-nightly` (experimental).
-Smaller, "light" models are faster, while larger models will perform better. [Custom models](https://docs.cohere.com/docs/training-custom-models) can also be supplied with their full ID.
-
-
-
-
-
--
-
-**numGenerations:** `Optional` β The maximum number of generations that will be returned. Defaults to `1`, min value of `1`, max value of `5`.
-
-
-
-
-
--
-
-**stream:** `Boolean`
-
-When `true`, the response will be a JSON stream of events. Streaming is beneficial for user interfaces that render the contents of the response piece by piece, as it gets generated.
-
-The final event will contain the complete response, and will contain an `is_finished` field set to `true`. The event will also contain a `finish_reason`, which can be one of the following:
-- `COMPLETE` - the model sent back a finished reply
-- `MAX_TOKENS` - the reply was cut off because the model reached the maximum number of tokens for its context length
-- `ERROR` - something went wrong when generating the reply
-- `ERROR_TOXIC` - the model generated a reply that was deemed toxic
-
-
-
-
-
--
-
-**maxTokens:** `Optional`
-
-The maximum number of tokens the model will generate as part of the response. Note: Setting a low value may result in incomplete generations.
-
-This parameter is off by default, and if it's not specified, the model will continue generating until it emits an EOS completion token. See [BPE Tokens](/bpe-tokens-wiki) for more details.
-
-Can only be set to `0` if `return_likelihoods` is set to `ALL` to get the likelihood of the prompt.
-
-
-
-
-
--
-
-**truncate:** `Optional`
-
-One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length.
-
-Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model.
-
-If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned.
-
-
-
-
-
--
-
-**temperature:** `Optional`
-
-A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations. See [Temperature](/temperature-wiki) for more details.
-Defaults to `0.75`, min value of `0.0`, max value of `5.0`.
-
-
-
-
-
--
-
-**seed:** `Optional`
-
-If specified, the backend will make a best effort to sample tokens
-deterministically, such that repeated requests with the same
-seed and parameters should return the same result. However,
-determinism cannot be totally guaranteed.
-Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
-
-
-
-
-
--
-
-**preset:** `Optional`
-
-Identifier of a custom preset. A preset is a combination of parameters, such as prompt, temperature etc. You can create presets in the [playground](https://dashboard.cohere.com/playground/generate).
-When a preset is specified, the `prompt` parameter becomes optional, and any included parameters will override the preset's parameters.
-
-
-
-
-
--
-
-**endSequences:** `Optional
>` β The generated text will be cut at the beginning of the earliest occurrence of an end sequence. The sequence will be excluded from the text.
-
-
-
-
-
--
-
-**stopSequences:** `Optional
>` β The generated text will be cut at the end of the earliest occurrence of a stop sequence. The sequence will be included the text.
-
-
-
-
-
--
-
-**k:** `Optional`
-
-Ensures only the top `k` most likely tokens are considered for generation at each step.
-Defaults to `0`, min value of `0`, max value of `500`.
-
-
-
-
-
--
-
-**p:** `Optional`
-
-Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`.
-Defaults to `0.75`. min value of `0.01`, max value of `0.99`.
-
-
-
-
-
--
-
-**frequencyPenalty:** `Optional`
-
-Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
-
-Using `frequency_penalty` in combination with `presence_penalty` is not supported on newer models.
-
-
-
-
-
--
-
-**presencePenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-
-Can be used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
-
-Using `frequency_penalty` in combination with `presence_penalty` is not supported on newer models.
-
-
-
-
-
--
-
-**returnLikelihoods:** `Optional`
-
-One of `GENERATION|NONE` to specify how and if the token likelihoods are returned with the response. Defaults to `NONE`.
-
-If `GENERATION` is selected, the token likelihoods will only be provided for generated text.
-
-WARNING: `ALL` is deprecated, and will be removed in a future release.
-
-
-
-
-
--
-
-**rawPrompting:** `Optional` β When enabled, the user's prompt will be sent to the model without any pre-processing.
-
-
-
-
-
-
-
-
-
-
-
-client.embed(request) -> EmbedResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
-
-Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
-
-If you want to learn more how to use the embedding model, have a look at the [Semantic Search Guide](https://docs.cohere.com/docs/semantic-search).
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.embed(
- EmbedRequest
- .builder()
- .texts(
- new ArrayList(
- Arrays.asList("hello", "goodbye")
- )
- )
- .model("embed-v4.0")
- .inputType(EmbedInputType.CLASSIFICATION)
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**texts:** `Optional
>` β An array of strings for the model to embed. Maximum number of texts per call is `96`.
-
-
-
-
-
--
-
-**images:** `Optional
>`
-
-An array of image data URIs for the model to embed. Maximum number of images per call is `1`.
-
-The image must be a valid [data URI](https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data). The image must be in either `image/jpeg` or `image/png` format and has a maximum size of 5MB.
-
-Images are only supported with Embed v3.0 and newer models.
-
-
-
-
-
--
-
-**model:** `Optional` β ID of one of the available [Embedding models](https://docs.cohere.com/docs/cohere-embed).
-
-
-
-
-
--
-
-**inputType:** `Optional`
-
-
-
-
-
--
-
-**embeddingTypes:** `Optional
>`
-
-Specifies the types of embeddings you want to get back. Not required and default is None, which returns the Embed Floats response type. Can be one or more of the following types.
-
-* `"float"`: Use this when you want to get back the default float embeddings. Supported with all Embed models.
-* `"int8"`: Use this when you want to get back signed int8 embeddings. Supported with Embed v3.0 and newer Embed models.
-* `"uint8"`: Use this when you want to get back unsigned int8 embeddings. Supported with Embed v3.0 and newer Embed models.
-* `"binary"`: Use this when you want to get back signed binary embeddings. Supported with Embed v3.0 and newer Embed models.
-* `"ubinary"`: Use this when you want to get back unsigned binary embeddings. Supported with Embed v3.0 and newer Embed models.
-
-
-
-
-
--
-
-**truncate:** `Optional`
-
-One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length.
-
-Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model.
-
-If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned.
-
-
-
-
-
-
-
-
-
-
-
-client.rerank(request) -> RerankResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.rerank(
- RerankRequest
- .builder()
- .documents(
- new ArrayList(
- Arrays.asList(
- RerankRequestDocumentsItem.of(),
- RerankRequestDocumentsItem.of(),
- RerankRequestDocumentsItem.of(),
- RerankRequestDocumentsItem.of(),
- RerankRequestDocumentsItem.of()
- )
- )
- )
- .query("What is the capital of the United States?")
- .topN(3)
- .model("rerank-v3.5")
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**model:** `Optional` β The identifier of the model to use, eg `rerank-v3.5`.
-
-
-
-
-
--
-
-**query:** `String` β The search query
-
-
-
-
-
--
-
-**documents:** `List`
-
-A list of document objects or strings to rerank.
-If a document is provided the text fields is required and all other fields will be preserved in the response.
-
-The total max chunks (length of documents * max_chunks_per_doc) must be less than 10000.
-
-We recommend a maximum of 1,000 documents for optimal endpoint performance.
-
-
-
-
-
--
-
-**topN:** `Optional` β The number of most relevant documents or indices to return, defaults to the length of the documents
-
-
-
-
-
--
-
-**rankFields:** `Optional
>` β If a JSON object is provided, you can specify which keys you would like to have considered for reranking. The model will rerank based on order of the fields passed in (i.e. rank_fields=['title','author','text'] will rerank using the values in title, author, text sequentially. If the length of title, author, and text exceeds the context length of the model, the chunking will not re-consider earlier fields). If not provided, the model will use the default text field for ranking.
-
-
-
-
-
--
-
-**returnDocuments:** `Optional`
-
-- If false, returns results without the doc text - the api will return a list of {index, relevance score} where index is inferred from the list passed into the request.
-- If true, returns results with the doc text passed in - the api will return an ordered list of {index, text, relevance score} where index + text refers to the list passed into the request.
-
-
-
-
-
--
-
-**maxChunksPerDoc:** `Optional` β The maximum number of chunks to produce internally from a document
-
-
-
-
-
-
-
-
-
-
-
-client.classify(request) -> ClassifyResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided `examples` of text + label pairs as a reference.
-Note: [Fine-tuned models](https://docs.cohere.com/docs/classify-fine-tuning) trained on classification examples don't require the `examples` parameter to be passed in explicitly.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.classify(
- ClassifyRequest
- .builder()
- .inputs(
- new ArrayList(
- Arrays.asList("Confirm your email address", "hey i need u to send some $")
- )
- )
- .examples(
- new ArrayList(
- Arrays.asList(
- ClassifyExample
- .builder()
- .text("Dermatologists don't like her!")
- .label("Spam")
- .build(),
- ClassifyExample
- .builder()
- .text("'Hello, open to this?'")
- .label("Spam")
- .build(),
- ClassifyExample
- .builder()
- .text("I need help please wire me $1000 right now")
- .label("Spam")
- .build(),
- ClassifyExample
- .builder()
- .text("Nice to know you ;)")
- .label("Spam")
- .build(),
- ClassifyExample
- .builder()
- .text("Please help me?")
- .label("Spam")
- .build(),
- ClassifyExample
- .builder()
- .text("Your parcel will be delivered today")
- .label("Not spam")
- .build(),
- ClassifyExample
- .builder()
- .text("Review changes to our Terms and Conditions")
- .label("Not spam")
- .build(),
- ClassifyExample
- .builder()
- .text("Weekly sync notes")
- .label("Not spam")
- .build(),
- ClassifyExample
- .builder()
- .text("'Re: Follow up from today's meeting'")
- .label("Not spam")
- .build(),
- ClassifyExample
- .builder()
- .text("Pre-read for tomorrow")
- .label("Not spam")
- .build()
- )
- )
- )
- .model("YOUR-FINE-TUNED-MODEL-ID")
- .build()
- );
- }
-}
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**inputs:** `List`
-
-A list of up to 96 texts to be classified. Each one must be a non-empty string.
-There is, however, no consistent, universal limit to the length a particular input can be. We perform classification on the first `x` tokens of each input, and `x` varies depending on which underlying model is powering classification. The maximum token length for each model is listed in the "max tokens" column [here](https://docs.cohere.com/docs/models).
-Note: by default the `truncate` parameter is set to `END`, so tokens exceeding the limit will be automatically dropped. This behavior can be disabled by setting `truncate` to `NONE`, which will result in validation errors for longer texts.
-
-
-
-
-
--
-
-**examples:** `Optional
>`
-
-An array of examples to provide context to the model. Each example is a text string and its associated label/class. Each unique label requires at least 2 examples associated with it; the maximum number of examples is 2500, and each example has a maximum length of 512 tokens. The values should be structured as `{text: "...",label: "..."}`.
-Note: [Fine-tuned Models](https://docs.cohere.com/docs/classify-fine-tuning) trained on classification examples don't require the `examples` parameter to be passed in explicitly.
-
-
-
-
-
--
-
-**model:** `Optional` β ID of a [Fine-tuned](https://docs.cohere.com/v2/docs/classify-starting-the-training) Classify model
-
-
-
-
-
--
-
-**preset:** `Optional` β The ID of a custom playground preset. You can create presets in the [playground](https://dashboard.cohere.com/playground). If you use a preset, all other parameters become optional, and any included parameters will override the preset's parameters.
-
-
-
-
-
--
-
-**truncate:** `Optional`
-
-One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length.
-Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model.
-If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned.
-
-
-
-
-
-
-
-
-
-
-
-client.summarize(request) -> SummarizeResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-
-This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
-
-Generates a summary in English for a given text.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.summarize(
- SummarizeRequest
- .builder()
- .text("Ice cream is a sweetened frozen food typically eaten as a snack or dessert. It may be made from milk or cream and is flavoured with a sweetener, either sugar or an alternative, and a spice, such as cocoa or vanilla, or with fruit such as strawberries or peaches. It can also be made by whisking a flavored cream base and liquid nitrogen together. Food coloring is sometimes added, in addition to stabilizers. The mixture is cooled below the freezing point of water and stirred to incorporate air spaces and to prevent detectable ice crystals from forming. The result is a smooth, semi-solid foam that is solid at very low temperatures (below 2 Β°C or 35 Β°F). It becomes more malleable as its temperature increases.\n\nThe meaning of the name \"ice cream\" varies from one country to another. In some countries, such as the United States, \"ice cream\" applies only to a specific variety, and most governments regulate the commercial use of the various terms according to the relative quantities of the main ingredients, notably the amount of cream. Products that do not meet the criteria to be called ice cream are sometimes labelled \"frozen dairy dessert\" instead. In other countries, such as Italy and Argentina, one word is used fo\r all variants. Analogues made from dairy alternatives, such as goat's or sheep's milk, or milk substitutes (e.g., soy, cashew, coconut, almond milk or tofu), are available for those who are lactose intolerant, allergic to dairy protein or vegan.")
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**text:** `String` β The text to generate a summary for. Can be up to 100,000 characters long. Currently the only supported language is English.
-
-
-
-
-
--
-
-**length:** `Optional` β One of `short`, `medium`, `long`, or `auto` defaults to `auto`. Indicates the approximate length of the summary. If `auto` is selected, the best option will be picked based on the input text.
-
-
-
-
-
--
-
-**format:** `Optional` β One of `paragraph`, `bullets`, or `auto`, defaults to `auto`. Indicates the style in which the summary will be delivered - in a free form paragraph or in bullet points. If `auto` is selected, the best option will be picked based on the input text.
-
-
-
-
-
--
-
-**model:** `Optional` β The identifier of the model to generate the summary with. Currently available models are `command` (default), `command-nightly` (experimental), `command-light`, and `command-light-nightly` (experimental). Smaller, "light" models are faster, while larger models will perform better.
-
-
-
-
-
--
-
-**extractiveness:** `Optional` β One of `low`, `medium`, `high`, or `auto`, defaults to `auto`. Controls how close to the original text the summary is. `high` extractiveness summaries will lean towards reusing sentences verbatim, while `low` extractiveness summaries will tend to paraphrase more. If `auto` is selected, the best option will be picked based on the input text.
-
-
-
-
-
--
-
-**temperature:** `Optional` β Ranges from 0 to 5. Controls the randomness of the output. Lower values tend to generate more βpredictableβ output, while higher values tend to generate more βcreativeβ output. The sweet spot is typically between 0 and 1.
-
-
-
-
-
--
-
-**additionalCommand:** `Optional` β A free-form instruction for modifying how the summaries get generated. Should complete the sentence "Generate a summary _". Eg. "focusing on the next steps" or "written by Yoda"
-
-
-
-
-
-
-
-
-
-
-
-client.tokenize(request) -> TokenizeResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.tokenize(
- TokenizeRequest
- .builder()
- .text("tokenize me! :D")
- .model("command")
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**text:** `String` β The string to be tokenized, the minimum text length is 1 character, and the maximum text length is 65536 characters.
-
-
-
-
-
--
-
-**model:** `String` β The input will be tokenized by the tokenizer that is used by this model.
-
-
-
-
-
-
-
-
-
-
-
-client.detokenize(request) -> DetokenizeResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.detokenize(
- DetokenizeRequest
- .builder()
- .tokens(
- new ArrayList(
- Arrays.asList(10002, 2261, 2012, 8, 2792, 43)
- )
- )
- .model("command")
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**tokens:** `List` β The list of tokens to be detokenized.
-
-
-
-
-
--
-
-**model:** `String` β An optional parameter to provide the model name. This will ensure that the detokenization is done by the tokenizer used by that model.
-
-
-
-
-
-
-
-
-
-
-
-client.checkApiKey() -> CheckApiKeyResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Checks that the api key in the Authorization header is valid and active
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.checkApiKey();
-```
-
-
-
-
-
-
-
-
-
-
-## V2
-client.v2.chatStream(request) -> Optional<V2ChatStreamResponse>
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Generates a text response to a user message. To learn how to use the Chat API and RAG follow our [Text Generation guides](https://docs.cohere.com/v2/docs/chat-api).
-
-Follow the [Migration Guide](https://docs.cohere.com/v2/docs/migrating-v1-to-v2) for instructions on moving from API v1 to API v2.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.v2().chatStream(
- V2ChatStreamRequest
- .builder()
- .model("command-r")
- .messages(
- new ArrayList(
- Arrays.asList(
- ChatMessageV2.user(
- UserMessageV2
- .builder()
- .content(
- UserMessageV2Content.of("Hello!")
- )
- .build()
- )
- )
- )
- )
- .stream(true)
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**stream:** `Boolean`
-
-Defaults to `false`.
-
-When `true`, the response will be a SSE stream of events.
-
-Streaming is beneficial for user interfaces that render the contents of the response piece by piece, as it gets generated.
-
-
-
-
-
--
-
-**model:** `String` β The name of a compatible [Cohere model](https://docs.cohere.com/v2/docs/models) or the ID of a [fine-tuned](https://docs.cohere.com/v2/docs/chat-fine-tuning) model.
-
-
-
-
-
--
-
-**messages:** `List`
-
-
-
-
-
--
-
-**tools:** `Optional
>`
-
-A list of tools (functions) available to the model. The model response may contain 'tool_calls' to the specified tools.
-
-Learn more in the [Tool Use guide](https://docs.cohere.com/docs/tools).
-
-
-
-
-
--
-
-**strictTools:** `Optional`
-
-When set to `true`, tool calls in the Assistant message will be forced to follow the tool definition strictly. Learn more in the [Structured Outputs (Tools) guide](https://docs.cohere.com/docs/structured-outputs-json#structured-outputs-tools).
-
-**Note**: The first few requests with a new set of tools will take longer to process.
-
-
-
-
-
--
-
-**documents:** `Optional
>` β A list of relevant documents that the model can cite to generate a more accurate reply. Each document is either a string or document object with content and metadata.
-
-
-
-
-
--
-
-**citationOptions:** `Optional`
-
-
-
-
-
--
-
-**responseFormat:** `Optional`
-
-
-
-
-
--
-
-**safetyMode:** `Optional`
-
-Used to select the [safety instruction](https://docs.cohere.com/v2/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`.
-When `OFF` is specified, the safety instruction will be omitted.
-
-Safety modes are not yet configurable in combination with `tools` and `documents` parameters.
-
-**Note**: This parameter is only compatible newer Cohere models, starting with [Command R 08-2024](https://docs.cohere.com/docs/command-r#august-2024-release) and [Command R+ 08-2024](https://docs.cohere.com/docs/command-r-plus#august-2024-release).
-
-**Note**: `command-r7b-12-2024` and newer models only support `"CONTEXTUAL"` and `"STRICT"` modes.
-
-
-
-
-
--
-
-**maxTokens:** `Optional`
-
-The maximum number of tokens the model will generate as part of the response.
-
-**Note**: Setting a low value may result in incomplete generations.
-
-
-
-
-
--
-
-**stopSequences:** `Optional
>` β A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence.
-
-
-
-
-
--
-
-**temperature:** `Optional`
-
-Defaults to `0.3`.
-
-A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations.
-
-Randomness can be further maximized by increasing the value of the `p` parameter.
-
-
-
-
-
--
-
-**seed:** `Optional`
-
-If specified, the backend will make a best effort to sample tokens
-deterministically, such that repeated requests with the same
-seed and parameters should return the same result. However,
-determinism cannot be totally guaranteed.
-
-
-
-
-
--
-
-**frequencyPenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
-
-
-
-
-
--
-
-**presencePenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-Used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
-
-
-
-
-
--
-
-**k:** `Optional`
-
-Ensures that only the top `k` most likely tokens are considered for generation at each step. When `k` is set to `0`, k-sampling is disabled.
-Defaults to `0`, min value of `0`, max value of `500`.
-
-
-
-
-
--
-
-**p:** `Optional`
-
-Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`.
-Defaults to `0.75`. min value of `0.01`, max value of `0.99`.
-
-
-
-
-
--
-
-**logprobs:** `Optional` β Defaults to `false`. When set to `true`, the log probabilities of the generated tokens will be included in the response.
-
-
-
-
-
--
-
-**toolChoice:** `Optional`
-
-Used to control whether or not the model will be forced to use a tool when answering. When `REQUIRED` is specified, the model will be forced to use at least one of the user-defined tools, and the `tools` parameter must be passed in the request.
-When `NONE` is specified, the model will be forced **not** to use one of the specified tools, and give a direct response.
-If tool_choice isn't specified, then the model is free to choose whether to use the specified tools or not.
-
-**Note**: This parameter is only compatible with models [Command-r7b](https://docs.cohere.com/v2/docs/command-r7b) and newer.
-
-**Note**: The same functionality can be achieved in `/v1/chat` using the `force_single_step` parameter. If `force_single_step=true`, this is equivalent to specifying `REQUIRED`. While if `force_single_step=true` and `tool_results` are passed, this is equivalent to specifying `NONE`.
-
-
-
-
-
-
-
-
-
-
-
-client.v2.chat(request) -> V2ChatResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Generates a text response to a user message and streams it down, token by token. To learn how to use the Chat API with streaming follow our [Text Generation guides](https://docs.cohere.com/v2/docs/chat-api).
-
-Follow the [Migration Guide](https://docs.cohere.com/v2/docs/migrating-v1-to-v2) for instructions on moving from API v1 to API v2.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.v2().chatStream(
- V2ChatStreamRequest
- .builder()
- .model("command-a-03-2025")
- .messages(
- new ArrayList(
- Arrays.asList(
- ChatMessageV2.user(
- UserMessageV2
- .builder()
- .content(
- UserMessageV2Content.of("Tell me about LLMs")
- )
- .build()
- )
- )
- )
- )
- .stream(false)
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**stream:** `Boolean`
-
-Defaults to `false`.
-
-When `true`, the response will be a SSE stream of events.
-
-Streaming is beneficial for user interfaces that render the contents of the response piece by piece, as it gets generated.
-
-
-
-
-
--
-
-**model:** `String` β The name of a compatible [Cohere model](https://docs.cohere.com/v2/docs/models) or the ID of a [fine-tuned](https://docs.cohere.com/v2/docs/chat-fine-tuning) model.
-
-
-
-
-
--
-
-**messages:** `List`
-
-
-
-
-
--
-
-**tools:** `Optional
>`
-
-A list of tools (functions) available to the model. The model response may contain 'tool_calls' to the specified tools.
-
-Learn more in the [Tool Use guide](https://docs.cohere.com/docs/tools).
-
-
-
-
-
--
-
-**strictTools:** `Optional`
-
-When set to `true`, tool calls in the Assistant message will be forced to follow the tool definition strictly. Learn more in the [Structured Outputs (Tools) guide](https://docs.cohere.com/docs/structured-outputs-json#structured-outputs-tools).
-
-**Note**: The first few requests with a new set of tools will take longer to process.
-
-
-
-
-
--
-
-**documents:** `Optional
>` β A list of relevant documents that the model can cite to generate a more accurate reply. Each document is either a string or document object with content and metadata.
-
-
-
-
-
--
-
-**citationOptions:** `Optional`
-
-
-
-
-
--
-
-**responseFormat:** `Optional`
-
-
-
-
-
--
-
-**safetyMode:** `Optional`
-
-Used to select the [safety instruction](https://docs.cohere.com/v2/docs/safety-modes) inserted into the prompt. Defaults to `CONTEXTUAL`.
-When `OFF` is specified, the safety instruction will be omitted.
-
-Safety modes are not yet configurable in combination with `tools` and `documents` parameters.
-
-**Note**: This parameter is only compatible newer Cohere models, starting with [Command R 08-2024](https://docs.cohere.com/docs/command-r#august-2024-release) and [Command R+ 08-2024](https://docs.cohere.com/docs/command-r-plus#august-2024-release).
-
-**Note**: `command-r7b-12-2024` and newer models only support `"CONTEXTUAL"` and `"STRICT"` modes.
-
-
-
-
-
--
-
-**maxTokens:** `Optional`
-
-The maximum number of tokens the model will generate as part of the response.
-
-**Note**: Setting a low value may result in incomplete generations.
-
-
-
-
-
--
-
-**stopSequences:** `Optional
>` β A list of up to 5 strings that the model will use to stop generation. If the model generates a string that matches any of the strings in the list, it will stop generating tokens and return the generated text up to that point not including the stop sequence.
-
-
-
-
-
--
-
-**temperature:** `Optional`
-
-Defaults to `0.3`.
-
-A non-negative float that tunes the degree of randomness in generation. Lower temperatures mean less random generations, and higher temperatures mean more random generations.
-
-Randomness can be further maximized by increasing the value of the `p` parameter.
-
-
-
-
-
--
-
-**seed:** `Optional`
-
-If specified, the backend will make a best effort to sample tokens
-deterministically, such that repeated requests with the same
-seed and parameters should return the same result. However,
-determinism cannot be totally guaranteed.
-
-
-
-
-
--
-
-**frequencyPenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-Used to reduce repetitiveness of generated tokens. The higher the value, the stronger a penalty is applied to previously present tokens, proportional to how many times they have already appeared in the prompt or prior generation.
-
-
-
-
-
--
-
-**presencePenalty:** `Optional`
-
-Defaults to `0.0`, min value of `0.0`, max value of `1.0`.
-Used to reduce repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied equally to all tokens that have already appeared, regardless of their exact frequencies.
-
-
-
-
-
--
-
-**k:** `Optional`
-
-Ensures that only the top `k` most likely tokens are considered for generation at each step. When `k` is set to `0`, k-sampling is disabled.
-Defaults to `0`, min value of `0`, max value of `500`.
-
-
-
-
-
--
-
-**p:** `Optional`
-
-Ensures that only the most likely tokens, with total probability mass of `p`, are considered for generation at each step. If both `k` and `p` are enabled, `p` acts after `k`.
-Defaults to `0.75`. min value of `0.01`, max value of `0.99`.
-
-
-
-
-
--
-
-**logprobs:** `Optional` β Defaults to `false`. When set to `true`, the log probabilities of the generated tokens will be included in the response.
-
-
-
-
-
--
-
-**toolChoice:** `Optional`
-
-Used to control whether or not the model will be forced to use a tool when answering. When `REQUIRED` is specified, the model will be forced to use at least one of the user-defined tools, and the `tools` parameter must be passed in the request.
-When `NONE` is specified, the model will be forced **not** to use one of the specified tools, and give a direct response.
-If tool_choice isn't specified, then the model is free to choose whether to use the specified tools or not.
-
-**Note**: This parameter is only compatible with models [Command-r7b](https://docs.cohere.com/v2/docs/command-r7b) and newer.
-
-**Note**: The same functionality can be achieved in `/v1/chat` using the `force_single_step` parameter. If `force_single_step=true`, this is equivalent to specifying `REQUIRED`. While if `force_single_step=true` and `tool_results` are passed, this is equivalent to specifying `NONE`.
-
-
-
-
-
-
-
-
-
-
-
-client.v2.embed(request) -> EmbedByTypeResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This endpoint returns text embeddings. An embedding is a list of floating point numbers that captures semantic information about the text that it represents.
-
-Embeddings can be used to create text classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
-
-If you want to learn more how to use the embedding model, have a look at the [Semantic Search Guide](https://docs.cohere.com/docs/semantic-search).
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.v2().embed(
- V2EmbedRequest
- .builder()
- .model("embed-v4.0")
- .inputType(EmbedInputType.CLASSIFICATION)
- .texts(
- new ArrayList(
- Arrays.asList("hello", "goodbye")
- )
- )
- .embeddingTypes(
- new ArrayList(
- Arrays.asList(EmbeddingType.FLOAT)
- )
- )
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**texts:** `Optional
>` β An array of strings for the model to embed. Maximum number of texts per call is `96`.
-
-
-
-
-
--
-
-**images:** `Optional
>`
-
-An array of image data URIs for the model to embed. Maximum number of images per call is `1`.
-
-The image must be a valid [data URI](https://developer.mozilla.org/en-US/docs/Web/URI/Schemes/data). The image must be in either `image/jpeg` or `image/png` format and has a maximum size of 5MB.
-
-Image embeddings are supported with Embed v3.0 and newer models.
-
-
-
-
-
--
-
-**model:** `String` β ID of one of the available [Embedding models](https://docs.cohere.com/docs/cohere-embed).
-
-
-
-
-
--
-
-**inputType:** `EmbedInputType`
-
-
-
-
-
--
-
-**inputs:** `Optional
>` β An array of inputs for the model to embed. Maximum number of inputs per call is `96`. An input can contain a mix of text and image components.
-
-
-
-
-
--
-
-**maxTokens:** `Optional` β The maximum number of tokens to embed per input. If the input text is longer than this, it will be truncated according to the `truncate` parameter.
-
-
-
-
-
--
-
-**outputDimension:** `Optional`
-
-The number of dimensions of the output embedding. This is only available for `embed-v4` and newer models.
-Possible values are `256`, `512`, `1024`, and `1536`. The default is `1536`.
-
-
-
-
-
--
-
-**embeddingTypes:** `Optional
>`
-
-Specifies the types of embeddings you want to get back. Can be one or more of the following types.
-
-* `"float"`: Use this when you want to get back the default float embeddings. Supported with all Embed models.
-* `"int8"`: Use this when you want to get back signed int8 embeddings. Supported with Embed v3.0 and newer Embed models.
-* `"uint8"`: Use this when you want to get back unsigned int8 embeddings. Supported with Embed v3.0 and newer Embed models.
-* `"binary"`: Use this when you want to get back signed binary embeddings. Supported with Embed v3.0 and newer Embed models.
-* `"ubinary"`: Use this when you want to get back unsigned binary embeddings. Supported with Embed v3.0 and newer Embed models.
-
-
-
-
-
--
-
-**truncate:** `Optional`
-
-One of `NONE|START|END` to specify how the API will handle inputs longer than the maximum token length.
-
-Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model.
-
-If `NONE` is selected, when the input exceeds the maximum input token length an error will be returned.
-
-
-
-
-
-
-
-
-
-
-
-client.v2.rerank(request) -> V2RerankResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.v2().rerank(
- V2RerankRequest
- .builder()
- .documents(
- new ArrayList(
- Arrays.asList("Carson City is the capital city of the American state of Nevada.", "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.", "Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.", "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.", "Capital punishment has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states.")
- )
- )
- .query("What is the capital of the United States?")
- .model("rerank-v3.5")
- .topN(3)
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**model:** `String` β The identifier of the model to use, eg `rerank-v3.5`.
-
-
-
-
-
--
-
-**query:** `String` β The search query
-
-
-
-
-
--
-
-**documents:** `List`
-
-A list of texts that will be compared to the `query`.
-For optimal performance we recommend against sending more than 1,000 documents in a single request.
-
-**Note**: long documents will automatically be truncated to the value of `max_tokens_per_doc`.
-
-**Note**: structured data should be formatted as YAML strings for best performance.
-
-
-
-
-
--
-
-**topN:** `Optional` β Limits the number of returned rerank results to the specified value. If not passed, all the rerank results will be returned.
-
-
-
-
-
--
-
-**maxTokensPerDoc:** `Optional` β Defaults to `4096`. Long documents will be automatically truncated to the specified number of tokens.
-
-
-
-
-
-
-
-
-
-
-
-## EmbedJobs
-client.embedJobs.list() -> ListEmbedJobResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-The list embed job endpoint allows users to view all embed jobs history for that specific user.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.embedJobs().list();
-```
-
-
-
-
-
-
-
-
-
-
-client.embedJobs.create(request) -> CreateEmbedJobResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This API launches an async Embed job for a [Dataset](https://docs.cohere.com/docs/datasets) of type `embed-input`. The result of a completed embed job is new Dataset of type `embed-output`, which contains the original text entries and the corresponding embeddings.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.embedJobs().create(
- CreateEmbedJobRequest
- .builder()
- .model("model")
- .datasetId("dataset_id")
- .inputType(EmbedInputType.SEARCH_DOCUMENT)
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**model:** `String`
-
-ID of the embedding model.
-
-Available models and corresponding embedding dimensions:
-
-- `embed-english-v3.0` : 1024
-- `embed-multilingual-v3.0` : 1024
-- `embed-english-light-v3.0` : 384
-- `embed-multilingual-light-v3.0` : 384
-
-
-
-
-
--
-
-**datasetId:** `String` β ID of a [Dataset](https://docs.cohere.com/docs/datasets). The Dataset must be of type `embed-input` and must have a validation status `Validated`
-
-
-
-
-
--
-
-**inputType:** `EmbedInputType`
-
-
-
-
-
--
-
-**name:** `Optional` β The name of the embed job.
-
-
-
-
-
--
-
-**embeddingTypes:** `Optional
>`
-
-Specifies the types of embeddings you want to get back. Not required and default is None, which returns the Embed Floats response type. Can be one or more of the following types.
-
-* `"float"`: Use this when you want to get back the default float embeddings. Valid for all models.
-* `"int8"`: Use this when you want to get back signed int8 embeddings. Valid for v3 and newer model versions.
-* `"uint8"`: Use this when you want to get back unsigned int8 embeddings. Valid for v3 and newer model versions.
-* `"binary"`: Use this when you want to get back signed binary embeddings. Valid for v3 and newer model versions.
-* `"ubinary"`: Use this when you want to get back unsigned binary embeddings. Valid for v3 and newer model versions.
-
-
-
-
-
--
-
-**truncate:** `Optional`
-
-One of `START|END` to specify how the API will handle inputs longer than the maximum token length.
-
-Passing `START` will discard the start of the input. `END` will discard the end of the input. In both cases, input is discarded until the remaining input is exactly the maximum input token length for the model.
-
-
-
-
-
-
-
-
-
-
-
-client.embedJobs.get(id) -> EmbedJob
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This API retrieves the details about an embed job started by the same user.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.embedJobs().get("id");
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String` β The ID of the embed job to retrieve.
-
-
-
-
-
-
-
-
-
-
-
-client.embedJobs.cancel(id)
-
--
-
-#### π Description
-
-
--
-
-
--
-
-This API allows users to cancel an active embed job. Once invoked, the embedding process will be terminated, and users will be charged for the embeddings processed up to the cancellation point. It's important to note that partial results will not be available to users after cancellation.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.embedJobs().cancel("id");
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String` β The ID of the embed job to cancel.
-
-
-
-
-
-
-
-
-
-
-
-## Datasets
-client.datasets.list() -> DatasetsListResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-List datasets that have been created.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.datasets().list(
- DatasetsListRequest
- .builder()
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**datasetType:** `Optional` β optional filter by dataset type
-
-
-
-
-
--
-
-**before:** `Optional` β optional filter before a date
-
-
-
-
-
--
-
-**after:** `Optional` β optional filter after a date
-
-
-
-
-
--
-
-**limit:** `Optional` β optional limit to number of results
-
-
-
-
-
--
-
-**offset:** `Optional` β optional offset to start of results
-
-
-
-
-
--
-
-**validationStatus:** `Optional` β optional filter by validation status
-
-
-
-
-
-
-
-
-
-
-
-client.datasets.create(request) -> DatasetsCreateResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Create a dataset by uploading a file. See ['Dataset Creation'](https://docs.cohere.com/docs/datasets#dataset-creation) for more information.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.datasets().create(
- DatasetsCreateRequest
- .builder()
- .name("name")
- .type(DatasetType.EMBED_INPUT)
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**name:** `String` β The name of the uploaded dataset.
-
-
-
-
-
--
-
-**type:** `DatasetType` β The dataset type, which is used to validate the data. Valid types are `embed-input`, `reranker-finetune-input`, `single-label-classification-finetune-input`, `chat-finetune-input`, and `multi-label-classification-finetune-input`.
-
-
-
-
-
--
-
-**keepOriginalFile:** `Optional` β Indicates if the original file should be stored.
-
-
-
-
-
--
-
-**skipMalformedInput:** `Optional` β Indicates whether rows with malformed input should be dropped (instead of failing the validation check). Dropped rows will be returned in the warnings field.
-
-
-
-
-
--
-
-**keepFields:** `Optional` β List of names of fields that will be persisted in the Dataset. By default the Dataset will retain only the required fields indicated in the [schema for the corresponding Dataset type](https://docs.cohere.com/docs/datasets#dataset-types). For example, datasets of type `embed-input` will drop all fields other than the required `text` field. If any of the fields in `keep_fields` are missing from the uploaded file, Dataset validation will fail.
-
-
-
-
-
--
-
-**optionalFields:** `Optional` β List of names of fields that will be persisted in the Dataset. By default the Dataset will retain only the required fields indicated in the [schema for the corresponding Dataset type](https://docs.cohere.com/docs/datasets#dataset-types). For example, Datasets of type `embed-input` will drop all fields other than the required `text` field. If any of the fields in `optional_fields` are missing from the uploaded file, Dataset validation will pass.
-
-
-
-
-
--
-
-**textSeparator:** `Optional` β Raw .txt uploads will be split into entries using the text_separator value.
-
-
-
-
-
--
-
-**csvDelimiter:** `Optional` β The delimiter used for .csv uploads.
-
-
-
-
-
-
-
-
-
-
-
-client.datasets.getUsage() -> DatasetsGetUsageResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-View the dataset storage usage for your Organization. Each Organization can have up to 10GB of storage across all their users.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.datasets().getUsage();
-```
-
-
-
-
-
-
-
-
-
-
-client.datasets.get(id) -> DatasetsGetResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Retrieve a dataset by ID. See ['Datasets'](https://docs.cohere.com/docs/datasets) for more information.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.datasets().get("id");
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String`
-
-
-
-
-
-
-
-
-
-
-
-client.datasets.delete(id) -> Map<String, Object>
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Delete a dataset by ID. Datasets are automatically deleted after 30 days, but they can also be deleted manually.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.datasets().delete("id");
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String`
-
-
-
-
-
-
-
-
-
-
-
-## Connectors
-client.connectors.list() -> ListConnectorsResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Returns a list of connectors ordered by descending creation date (newer first). See ['Managing your Connector'](https://docs.cohere.com/docs/managing-your-connector) for more information.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.connectors().list(
- ConnectorsListRequest
- .builder()
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**limit:** `Optional` β Maximum number of connectors to return [0, 100].
-
-
-
-
-
--
-
-**offset:** `Optional` β Number of connectors to skip before returning results [0, inf].
-
-
-
-
-
-
-
-
-
-
-
-client.connectors.create(request) -> CreateConnectorResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Creates a new connector. The connector is tested during registration and will cancel registration when the test is unsuccessful. See ['Creating and Deploying a Connector'](https://docs.cohere.com/v1/docs/creating-and-deploying-a-connector) for more information.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.connectors().create(
- CreateConnectorRequest
- .builder()
- .name("name")
- .url("url")
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**name:** `String` β A human-readable name for the connector.
-
-
-
-
-
--
-
-**description:** `Optional` β A description of the connector.
-
-
-
-
-
--
-
-**url:** `String` β The URL of the connector that will be used to search for documents.
-
-
-
-
-
--
-
-**excludes:** `Optional
>` β A list of fields to exclude from the prompt (fields remain in the document).
-
-
-
-
-
--
-
-**oauth:** `Optional` β The OAuth 2.0 configuration for the connector. Cannot be specified if service_auth is specified.
-
-
-
-
-
--
-
-**active:** `Optional` β Whether the connector is active or not.
-
-
-
-
-
--
-
-**continueOnFailure:** `Optional` β Whether a chat request should continue or not if the request to this connector fails.
-
-
-
-
-
--
-
-**serviceAuth:** `Optional` β The service to service authentication configuration for the connector. Cannot be specified if oauth is specified.
-
-
-
-
-
-
-
-
-
-
-
-client.connectors.get(id) -> GetConnectorResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Retrieve a connector by ID. See ['Connectors'](https://docs.cohere.com/docs/connectors) for more information.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.connectors().get("id");
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String` β The ID of the connector to retrieve.
-
-
-
-
-
-
-
-
-
-
-
-client.connectors.delete(id) -> Map<String, Object>
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Delete a connector by ID. See ['Connectors'](https://docs.cohere.com/docs/connectors) for more information.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.connectors().delete("id");
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String` β The ID of the connector to delete.
-
-
-
-
-
-
-
-
-
-
-
-client.connectors.update(id, request) -> UpdateConnectorResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Update a connector by ID. Omitted fields will not be updated. See ['Managing your Connector'](https://docs.cohere.com/docs/managing-your-connector) for more information.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.connectors().update(
- "id",
- UpdateConnectorRequest
- .builder()
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String` β The ID of the connector to update.
-
-
-
-
-
--
-
-**name:** `Optional` β A human-readable name for the connector.
-
-
-
-
-
--
-
-**url:** `Optional` β The URL of the connector that will be used to search for documents.
-
-
-
-
-
--
-
-**excludes:** `Optional
>` β A list of fields to exclude from the prompt (fields remain in the document).
-
-
-
-
-
--
-
-**oauth:** `Optional` β The OAuth 2.0 configuration for the connector. Cannot be specified if service_auth is specified.
-
-
-
-
-
--
-
-**active:** `Optional`
-
-
-
-
-
--
-
-**continueOnFailure:** `Optional`
-
-
-
-
-
--
-
-**serviceAuth:** `Optional` β The service to service authentication configuration for the connector. Cannot be specified if oauth is specified.
-
-
-
-
-
-
-
-
-
-
-
-client.connectors.oAuthAuthorize(id) -> OAuthAuthorizeResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Authorize the connector with the given ID for the connector oauth app. See ['Connector Authentication'](https://docs.cohere.com/docs/connector-authentication) for more information.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.connectors().oAuthAuthorize(
- "id",
- ConnectorsOAuthAuthorizeRequest
- .builder()
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String` β The ID of the connector to authorize.
-
-
-
-
-
--
-
-**afterTokenRedirect:** `Optional` β The URL to redirect to after the connector has been authorized.
-
-
-
-
-
-
-
-
-
-
-
-## Models
-client.models.get(model) -> GetModelResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Returns the details of a model, provided its name.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.models().get("command-a-03-2025");
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**model:** `String`
-
-
-
-
-
-
-
-
-
-
-
-client.models.list() -> ListModelsResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Returns a list of models available for use. The list contains models from Cohere as well as your fine-tuned models.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.models().list(
- ModelsListRequest
- .builder()
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**pageSize:** `Optional`
-
-Maximum number of models to include in a page
-Defaults to `20`, min value of `1`, max value of `1000`.
-
-
-
-
-
--
-
-**pageToken:** `Optional` β Page token provided in the `next_page_token` field of a previous response.
-
-
-
-
-
--
-
-**endpoint:** `Optional` β When provided, filters the list of models to only those that are compatible with the specified endpoint.
-
-
-
-
-
--
-
-**defaultOnly:** `Optional` β When provided, filters the list of models to only the default model to the endpoint. This parameter is only valid when `endpoint` is provided.
-
-
-
-
-
-
-
-
-
-
-
-## /finetuning
-client.finetuning.listFinetunedModels() -> ListFinetunedModelsResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Returns a list of fine-tuned models that the user has access to.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.finetuning().listFinetunedModels(
- FinetuningListFinetunedModelsRequest
- .builder()
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**pageSize:** `Optional`
-
-Maximum number of results to be returned by the server. If 0, defaults to
-50.
-
-
-
-
-
--
-
-**pageToken:** `Optional` β Request a specific page of the list results.
-
-
-
-
-
--
-
-**orderBy:** `Optional`
-
-Comma separated list of fields. For example: "created_at,name". The default
-sorting order is ascending. To specify descending order for a field, append
-" desc" to the field name. For example: "created_at desc,name".
-
-Supported sorting fields:
- - created_at (default)
-
-
-
-
-
-
-
-
-
-
-
-client.finetuning.createFinetunedModel(request) -> CreateFinetunedModelResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Creates a new fine-tuned model. The model will be trained on the dataset specified in the request body. The training process may take some time, and the model will be available once the training is complete.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.finetuning().createFinetunedModel(
- FinetunedModel
- .builder()
- .name("api-test")
- .settings(
- Settings
- .builder()
- .baseModel(
- BaseModel
- .builder()
- .baseType(BaseType.BASE_TYPE_CHAT)
- .build()
- )
- .datasetId("my-dataset-id")
- .build()
- )
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**request:** `FinetunedModel`
-
-
-
-
-
-
-
-
-
-
-
-client.finetuning.getFinetunedModel(id) -> GetFinetunedModelResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Retrieve a fine-tuned model by its ID.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.finetuning().getFinetunedModel("id");
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String` β The fine-tuned model ID.
-
-
-
-
-
-
-
-
-
-
-
-client.finetuning.deleteFinetunedModel(id) -> Map<String, Object>
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Deletes a fine-tuned model. The model will be removed from the system and will no longer be available for use.
-This operation is irreversible.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.finetuning().deleteFinetunedModel("id");
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String` β The fine-tuned model ID.
-
-
-
-
-
-
-
-
-
-
-
-client.finetuning.updateFinetunedModel(id, request) -> UpdateFinetunedModelResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Updates the fine-tuned model with the given ID. The model will be updated with the new settings and name provided in the request body.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.finetuning().updateFinetunedModel(
- "id",
- FinetuningUpdateFinetunedModelRequest
- .builder()
- .name("name")
- .settings(
- Settings
- .builder()
- .baseModel(
- BaseModel
- .builder()
- .baseType(BaseType.BASE_TYPE_UNSPECIFIED)
- .build()
- )
- .datasetId("dataset_id")
- .build()
- )
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**id:** `String` β FinetunedModel ID.
-
-
-
-
-
--
-
-**name:** `String` β FinetunedModel name (e.g. `foobar`).
-
-
-
-
-
--
-
-**settings:** `Settings` β FinetunedModel settings such as dataset, hyperparameters...
-
-
-
-
-
--
-
-**status:** `Optional` β Current stage in the life-cycle of the fine-tuned model.
-
-
-
-
-
-
-
-
-
-
-
-client.finetuning.listEvents(finetunedModelId) -> ListEventsResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Returns a list of events that occurred during the life-cycle of the fine-tuned model.
-The events are ordered by creation time, with the most recent event first.
-The list can be paginated using `page_size` and `page_token` parameters.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.finetuning().listEvents(
- "finetuned_model_id",
- FinetuningListEventsRequest
- .builder()
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**finetunedModelId:** `String` β The parent fine-tuned model ID.
-
-
-
-
-
--
-
-**pageSize:** `Optional`
-
-Maximum number of results to be returned by the server. If 0, defaults to
-50.
-
-
-
-
-
--
-
-**pageToken:** `Optional` β Request a specific page of the list results.
-
-
-
-
-
--
-
-**orderBy:** `Optional`
-
-Comma separated list of fields. For example: "created_at,name". The default
-sorting order is ascending. To specify descending order for a field, append
-" desc" to the field name. For example: "created_at desc,name".
-
-Supported sorting fields:
- - created_at (default)
-
-
-
-
-
-
-
-
-
-
-
-client.finetuning.listTrainingStepMetrics(finetunedModelId) -> ListTrainingStepMetricsResponse
-
--
-
-#### π Description
-
-
--
-
-
--
-
-Returns a list of metrics measured during the training of a fine-tuned model.
-The metrics are ordered by step number, with the most recent step first.
-The list can be paginated using `page_size` and `page_token` parameters.
-
-
-
-
-
-#### π Usage
-
-
--
-
-
--
-
-```java
-client.finetuning().listTrainingStepMetrics(
- "finetuned_model_id",
- FinetuningListTrainingStepMetricsRequest
- .builder()
- .build()
-);
-```
-
-
-
-
-
-#### βοΈ Parameters
-
-
--
-
-
--
-
-**finetunedModelId:** `String` β The parent fine-tuned model ID.
-
-
-
-
-
--
-
-**pageSize:** `Optional`
-
-Maximum number of results to be returned by the server. If 0, defaults to
-50.
-
-
-
-
-
--
-
-**pageToken:** `Optional` β Request a specific page of the list results.
-
-
-
-
-
-
-
-
-
-
diff --git a/sample-app/src/main/java/sample/App.java b/sample-app/src/main/java/sample/App.java
index 265a292..038ddb0 100644
--- a/sample-app/src/main/java/sample/App.java
+++ b/sample-app/src/main/java/sample/App.java
@@ -8,6 +8,6 @@
public final class App {
public static void main(String[] args) {
- // import com.cohere.api.AsyncCohere
+ // import com.cohere.api.Cohere
}
}
diff --git a/src/main/java/com/cohere/api/AsyncCohere.java b/src/main/java/com/cohere/api/AsyncCohere.java
deleted file mode 100644
index 5623a66..0000000
--- a/src/main/java/com/cohere/api/AsyncCohere.java
+++ /dev/null
@@ -1,294 +0,0 @@
-/**
- * This file was auto-generated by Fern from our API Definition.
- */
-package com.cohere.api;
-
-import com.cohere.api.core.ClientOptions;
-import com.cohere.api.core.RequestOptions;
-import com.cohere.api.core.Suppliers;
-import com.cohere.api.requests.ChatRequest;
-import com.cohere.api.requests.ChatStreamRequest;
-import com.cohere.api.requests.ClassifyRequest;
-import com.cohere.api.requests.DetokenizeRequest;
-import com.cohere.api.requests.EmbedRequest;
-import com.cohere.api.requests.GenerateRequest;
-import com.cohere.api.requests.GenerateStreamRequest;
-import com.cohere.api.requests.RerankRequest;
-import com.cohere.api.requests.SummarizeRequest;
-import com.cohere.api.requests.TokenizeRequest;
-import com.cohere.api.resources.connectors.AsyncConnectorsClient;
-import com.cohere.api.resources.datasets.AsyncDatasetsClient;
-import com.cohere.api.resources.embedjobs.AsyncEmbedJobsClient;
-import com.cohere.api.resources.finetuning.AsyncFinetuningClient;
-import com.cohere.api.resources.models.AsyncModelsClient;
-import com.cohere.api.resources.v2.AsyncV2Client;
-import com.cohere.api.types.CheckApiKeyResponse;
-import com.cohere.api.types.ClassifyResponse;
-import com.cohere.api.types.DetokenizeResponse;
-import com.cohere.api.types.EmbedResponse;
-import com.cohere.api.types.GenerateStreamedResponse;
-import com.cohere.api.types.Generation;
-import com.cohere.api.types.NonStreamedChatResponse;
-import com.cohere.api.types.RerankResponse;
-import com.cohere.api.types.StreamedChatResponse;
-import com.cohere.api.types.SummarizeResponse;
-import com.cohere.api.types.TokenizeResponse;
-import java.util.concurrent.CompletableFuture;
-import java.util.function.Supplier;
-
-public class AsyncCohere {
- protected final ClientOptions clientOptions;
-
- private final AsyncRawCohere rawClient;
-
- protected final Supplier v2Client;
-
- protected final Supplier embedJobsClient;
-
- protected final Supplier datasetsClient;
-
- protected final Supplier connectorsClient;
-
- protected final Supplier modelsClient;
-
- protected final Supplier finetuningClient;
-
- public AsyncCohere(ClientOptions clientOptions) {
- this.clientOptions = clientOptions;
- this.rawClient = new AsyncRawCohere(clientOptions);
- this.v2Client = Suppliers.memoize(() -> new AsyncV2Client(clientOptions));
- this.embedJobsClient = Suppliers.memoize(() -> new AsyncEmbedJobsClient(clientOptions));
- this.datasetsClient = Suppliers.memoize(() -> new AsyncDatasetsClient(clientOptions));
- this.connectorsClient = Suppliers.memoize(() -> new AsyncConnectorsClient(clientOptions));
- this.modelsClient = Suppliers.memoize(() -> new AsyncModelsClient(clientOptions));
- this.finetuningClient = Suppliers.memoize(() -> new AsyncFinetuningClient(clientOptions));
- }
-
- /**
- * Get responses with HTTP metadata like headers
- */
- public AsyncRawCohere withRawResponse() {
- return this.rawClient;
- }
-
- /**
- * Generates a streamed text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CompletableFuture> chatStream(ChatStreamRequest request) {
- return this.rawClient.chatStream(request).thenApply(response -> response.body());
- }
-
- /**
- * Generates a streamed text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CompletableFuture> chatStream(
- ChatStreamRequest request, RequestOptions requestOptions) {
- return this.rawClient.chatStream(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * Generates a text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CompletableFuture chat(ChatRequest request) {
- return this.rawClient.chat(request).thenApply(response -> response.body());
- }
-
- /**
- * Generates a text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CompletableFuture chat(ChatRequest request, RequestOptions requestOptions) {
- return this.rawClient.chat(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat with Streaming API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CompletableFuture> generateStream(GenerateStreamRequest request) {
- return this.rawClient.generateStream(request).thenApply(response -> response.body());
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat with Streaming API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CompletableFuture> generateStream(
- GenerateStreamRequest request, RequestOptions requestOptions) {
- return this.rawClient.generateStream(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CompletableFuture generate(GenerateRequest request) {
- return this.rawClient.generate(request).thenApply(response -> response.body());
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CompletableFuture generate(GenerateRequest request, RequestOptions requestOptions) {
- return this.rawClient.generate(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
- * Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
- * If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
- */
- public CompletableFuture embed() {
- return this.rawClient.embed().thenApply(response -> response.body());
- }
-
- /**
- * This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
- * Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
- * If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
- */
- public CompletableFuture embed(EmbedRequest request) {
- return this.rawClient.embed(request).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
- * Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
- * If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
- */
- public CompletableFuture embed(EmbedRequest request, RequestOptions requestOptions) {
- return this.rawClient.embed(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
- */
- public CompletableFuture rerank(RerankRequest request) {
- return this.rawClient.rerank(request).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
- */
- public CompletableFuture rerank(RerankRequest request, RequestOptions requestOptions) {
- return this.rawClient.rerank(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided examples of text + label pairs as a reference.
- * Note: Fine-tuned models trained on classification examples don't require the examples parameter to be passed in explicitly.
- */
- public CompletableFuture classify(ClassifyRequest request) {
- return this.rawClient.classify(request).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided examples of text + label pairs as a reference.
- * Note: Fine-tuned models trained on classification examples don't require the examples parameter to be passed in explicitly.
- */
- public CompletableFuture classify(ClassifyRequest request, RequestOptions requestOptions) {
- return this.rawClient.classify(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates a summary in English for a given text.
- */
- public CompletableFuture summarize(SummarizeRequest request) {
- return this.rawClient.summarize(request).thenApply(response -> response.body());
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates a summary in English for a given text.
- */
- public CompletableFuture summarize(SummarizeRequest request, RequestOptions requestOptions) {
- return this.rawClient.summarize(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CompletableFuture tokenize(TokenizeRequest request) {
- return this.rawClient.tokenize(request).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CompletableFuture tokenize(TokenizeRequest request, RequestOptions requestOptions) {
- return this.rawClient.tokenize(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CompletableFuture detokenize(DetokenizeRequest request) {
- return this.rawClient.detokenize(request).thenApply(response -> response.body());
- }
-
- /**
- * This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CompletableFuture detokenize(DetokenizeRequest request, RequestOptions requestOptions) {
- return this.rawClient.detokenize(request, requestOptions).thenApply(response -> response.body());
- }
-
- /**
- * Checks that the api key in the Authorization header is valid and active
- */
- public CompletableFuture checkApiKey() {
- return this.rawClient.checkApiKey().thenApply(response -> response.body());
- }
-
- /**
- * Checks that the api key in the Authorization header is valid and active
- */
- public CompletableFuture checkApiKey(RequestOptions requestOptions) {
- return this.rawClient.checkApiKey(requestOptions).thenApply(response -> response.body());
- }
-
- public AsyncV2Client v2() {
- return this.v2Client.get();
- }
-
- public AsyncEmbedJobsClient embedJobs() {
- return this.embedJobsClient.get();
- }
-
- public AsyncDatasetsClient datasets() {
- return this.datasetsClient.get();
- }
-
- public AsyncConnectorsClient connectors() {
- return this.connectorsClient.get();
- }
-
- public AsyncModelsClient models() {
- return this.modelsClient.get();
- }
-
- public AsyncFinetuningClient finetuning() {
- return this.finetuningClient.get();
- }
-
- public static AsyncCohereBuilder builder() {
- return new AsyncCohereBuilder();
- }
-}
diff --git a/src/main/java/com/cohere/api/AsyncCohereBuilder.java b/src/main/java/com/cohere/api/AsyncCohereBuilder.java
deleted file mode 100644
index 8d89214..0000000
--- a/src/main/java/com/cohere/api/AsyncCohereBuilder.java
+++ /dev/null
@@ -1,218 +0,0 @@
-/**
- * This file was auto-generated by Fern from our API Definition.
- */
-package com.cohere.api;
-
-import com.cohere.api.core.ClientOptions;
-import com.cohere.api.core.Environment;
-import java.util.Optional;
-import okhttp3.OkHttpClient;
-
-public class AsyncCohereBuilder {
- private Optional timeout = Optional.empty();
-
- private Optional maxRetries = Optional.empty();
-
- private String token = System.getenv("CO_API_KEY");
-
- private String clientName = null;
-
- private Environment environment = Environment.PRODUCTION;
-
- private OkHttpClient httpClient;
-
- /**
- * Sets token.
- * Defaults to the CO_API_KEY environment variable.
- */
- public AsyncCohereBuilder token(String token) {
- this.token = token;
- return this;
- }
-
- /**
- * Sets clientName
- */
- public AsyncCohereBuilder clientName(String clientName) {
- this.clientName = clientName;
- return this;
- }
-
- public AsyncCohereBuilder environment(Environment environment) {
- this.environment = environment;
- return this;
- }
-
- public AsyncCohereBuilder url(String url) {
- this.environment = Environment.custom(url);
- return this;
- }
-
- /**
- * Sets the timeout (in seconds) for the client. Defaults to 60 seconds.
- */
- public AsyncCohereBuilder timeout(int timeout) {
- this.timeout = Optional.of(timeout);
- return this;
- }
-
- /**
- * Sets the maximum number of retries for the client. Defaults to 2 retries.
- */
- public AsyncCohereBuilder maxRetries(int maxRetries) {
- this.maxRetries = Optional.of(maxRetries);
- return this;
- }
-
- /**
- * Sets the underlying OkHttp client
- */
- public AsyncCohereBuilder httpClient(OkHttpClient httpClient) {
- this.httpClient = httpClient;
- return this;
- }
-
- protected ClientOptions buildClientOptions() {
- ClientOptions.Builder builder = ClientOptions.builder();
- setEnvironment(builder);
- setAuthentication(builder);
- setCustomHeaders(builder);
- setHttpClient(builder);
- setTimeouts(builder);
- setRetries(builder);
- setAdditional(builder);
- return builder.build();
- }
-
- /**
- * Sets the environment configuration for the client.
- * Override this method to modify URLs or add environment-specific logic.
- *
- * @param builder The ClientOptions.Builder to configure
- */
- protected void setEnvironment(ClientOptions.Builder builder) {
- builder.environment(this.environment);
- }
-
- /**
- * Override this method to customize authentication.
- * This method is called during client options construction to set up authentication headers.
- *
- * @param builder The ClientOptions.Builder to configure
- *
- * Example:
- * {@code
- * @Override
- * protected void setAuthentication(ClientOptions.Builder builder) {
- * super.setAuthentication(builder); // Keep existing auth
- * builder.addHeader("X-API-Key", this.apiKey);
- * }
- * }
- */
- protected void setAuthentication(ClientOptions.Builder builder) {
- if (this.token != null) {
- builder.addHeader("Authorization", "Bearer " + this.token);
- }
- }
-
- /**
- * Override this method to add or modify custom headers.
- * This method is called during client options construction to set up custom headers defined in the API.
- *
- * @param builder The ClientOptions.Builder to configure
- *
- * Example:
- * {@code
- * @Override
- * protected void setCustomHeaders(ClientOptions.Builder builder) {
- * super.setCustomHeaders(builder); // Keep existing headers
- * builder.addHeader("X-Trace-ID", generateTraceId());
- * }
- * }
- */
- protected void setCustomHeaders(ClientOptions.Builder builder) {
- if (this.clientName != null) {
- builder.addHeader("X-Client-Name", this.clientName);
- }
- }
-
- /**
- * Sets the request timeout configuration.
- * Override this method to customize timeout behavior.
- *
- * @param builder The ClientOptions.Builder to configure
- */
- protected void setTimeouts(ClientOptions.Builder builder) {
- if (this.timeout.isPresent()) {
- builder.timeout(this.timeout.get());
- }
- }
-
- /**
- * Sets the retry configuration for failed requests.
- * Override this method to implement custom retry strategies.
- *
- * @param builder The ClientOptions.Builder to configure
- */
- protected void setRetries(ClientOptions.Builder builder) {
- if (this.maxRetries.isPresent()) {
- builder.maxRetries(this.maxRetries.get());
- }
- }
-
- /**
- * Sets the OkHttp client configuration.
- * Override this method to customize HTTP client behavior (interceptors, connection pools, etc).
- *
- * @param builder The ClientOptions.Builder to configure
- */
- protected void setHttpClient(ClientOptions.Builder builder) {
- if (this.httpClient != null) {
- builder.httpClient(this.httpClient);
- }
- }
-
- /**
- * Override this method to add any additional configuration to the client.
- * This method is called at the end of the configuration chain, allowing you to add
- * custom headers, modify settings, or perform any other client customization.
- *
- * @param builder The ClientOptions.Builder to configure
- *
- * Example:
- * {@code
- * @Override
- * protected void setAdditional(ClientOptions.Builder builder) {
- * builder.addHeader("X-Request-ID", () -> UUID.randomUUID().toString());
- * builder.addHeader("X-Client-Version", "1.0.0");
- * }
- * }
- */
- protected void setAdditional(ClientOptions.Builder builder) {}
-
- /**
- * Override this method to add custom validation logic before the client is built.
- * This method is called at the beginning of the build() method to ensure the configuration is valid.
- * Throw an exception to prevent client creation if validation fails.
- *
- * Example:
- * {@code
- * @Override
- * protected void validateConfiguration() {
- * super.validateConfiguration(); // Run parent validations
- * if (tenantId == null || tenantId.isEmpty()) {
- * throw new IllegalStateException("tenantId is required");
- * }
- * }
- * }
- */
- protected void validateConfiguration() {}
-
- public AsyncCohere build() {
- if (token == null) {
- throw new RuntimeException("Please provide token or set the CO_API_KEY environment variable.");
- }
- validateConfiguration();
- return new AsyncCohere(buildClientOptions());
- }
-}
diff --git a/src/main/java/com/cohere/api/AsyncRawCohere.java b/src/main/java/com/cohere/api/AsyncRawCohere.java
deleted file mode 100644
index c3728c4..0000000
--- a/src/main/java/com/cohere/api/AsyncRawCohere.java
+++ /dev/null
@@ -1,1694 +0,0 @@
-/**
- * This file was auto-generated by Fern from our API Definition.
- */
-package com.cohere.api;
-
-import com.cohere.api.core.ClientOptions;
-import com.cohere.api.core.CohereApiException;
-import com.cohere.api.core.CohereException;
-import com.cohere.api.core.CohereHttpResponse;
-import com.cohere.api.core.MediaTypes;
-import com.cohere.api.core.ObjectMappers;
-import com.cohere.api.core.RequestOptions;
-import com.cohere.api.core.ResponseBodyReader;
-import com.cohere.api.core.Stream;
-import com.cohere.api.errors.BadRequestError;
-import com.cohere.api.errors.ClientClosedRequestError;
-import com.cohere.api.errors.ForbiddenError;
-import com.cohere.api.errors.GatewayTimeoutError;
-import com.cohere.api.errors.InternalServerError;
-import com.cohere.api.errors.InvalidTokenError;
-import com.cohere.api.errors.NotFoundError;
-import com.cohere.api.errors.NotImplementedError;
-import com.cohere.api.errors.ServiceUnavailableError;
-import com.cohere.api.errors.TooManyRequestsError;
-import com.cohere.api.errors.UnauthorizedError;
-import com.cohere.api.errors.UnprocessableEntityError;
-import com.cohere.api.requests.ChatRequest;
-import com.cohere.api.requests.ChatStreamRequest;
-import com.cohere.api.requests.ClassifyRequest;
-import com.cohere.api.requests.DetokenizeRequest;
-import com.cohere.api.requests.EmbedRequest;
-import com.cohere.api.requests.GenerateRequest;
-import com.cohere.api.requests.GenerateStreamRequest;
-import com.cohere.api.requests.RerankRequest;
-import com.cohere.api.requests.SummarizeRequest;
-import com.cohere.api.requests.TokenizeRequest;
-import com.cohere.api.types.CheckApiKeyResponse;
-import com.cohere.api.types.ClassifyResponse;
-import com.cohere.api.types.DetokenizeResponse;
-import com.cohere.api.types.EmbedResponse;
-import com.cohere.api.types.GenerateStreamedResponse;
-import com.cohere.api.types.Generation;
-import com.cohere.api.types.NonStreamedChatResponse;
-import com.cohere.api.types.RerankResponse;
-import com.cohere.api.types.StreamedChatResponse;
-import com.cohere.api.types.SummarizeResponse;
-import com.cohere.api.types.TokenizeResponse;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.CompletableFuture;
-import okhttp3.Call;
-import okhttp3.Callback;
-import okhttp3.Headers;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-import org.jetbrains.annotations.NotNull;
-
-public class AsyncRawCohere {
- protected final ClientOptions clientOptions;
-
- public AsyncRawCohere(ClientOptions clientOptions) {
- this.clientOptions = clientOptions;
- }
-
- /**
- * Generates a streamed text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CompletableFuture>> chatStream(ChatStreamRequest request) {
- return chatStream(request, null);
- }
-
- /**
- * Generates a streamed text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CompletableFuture>> chatStream(
- ChatStreamRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/chat")
- .build();
- Map properties = new HashMap<>();
- properties.put("message", request.getMessage());
- if (request.getModel().isPresent()) {
- properties.put("model", request.getModel());
- }
- properties.put("stream", request.getStream());
- if (request.getPreamble().isPresent()) {
- properties.put("preamble", request.getPreamble());
- }
- if (request.getChatHistory().isPresent()) {
- properties.put("chat_history", request.getChatHistory());
- }
- if (request.getConversationId().isPresent()) {
- properties.put("conversation_id", request.getConversationId());
- }
- if (request.getPromptTruncation().isPresent()) {
- properties.put("prompt_truncation", request.getPromptTruncation());
- }
- if (request.getConnectors().isPresent()) {
- properties.put("connectors", request.getConnectors());
- }
- if (request.getSearchQueriesOnly().isPresent()) {
- properties.put("search_queries_only", request.getSearchQueriesOnly());
- }
- if (request.getDocuments().isPresent()) {
- properties.put("documents", request.getDocuments());
- }
- if (request.getCitationQuality().isPresent()) {
- properties.put("citation_quality", request.getCitationQuality());
- }
- if (request.getTemperature().isPresent()) {
- properties.put("temperature", request.getTemperature());
- }
- if (request.getMaxTokens().isPresent()) {
- properties.put("max_tokens", request.getMaxTokens());
- }
- if (request.getMaxInputTokens().isPresent()) {
- properties.put("max_input_tokens", request.getMaxInputTokens());
- }
- if (request.getK().isPresent()) {
- properties.put("k", request.getK());
- }
- if (request.getP().isPresent()) {
- properties.put("p", request.getP());
- }
- if (request.getSeed().isPresent()) {
- properties.put("seed", request.getSeed());
- }
- if (request.getStopSequences().isPresent()) {
- properties.put("stop_sequences", request.getStopSequences());
- }
- if (request.getFrequencyPenalty().isPresent()) {
- properties.put("frequency_penalty", request.getFrequencyPenalty());
- }
- if (request.getPresencePenalty().isPresent()) {
- properties.put("presence_penalty", request.getPresencePenalty());
- }
- if (request.getTools().isPresent()) {
- properties.put("tools", request.getTools());
- }
- if (request.getToolResults().isPresent()) {
- properties.put("tool_results", request.getToolResults());
- }
- if (request.getForceSingleStep().isPresent()) {
- properties.put("force_single_step", request.getForceSingleStep());
- }
- if (request.getResponseFormat().isPresent()) {
- properties.put("response_format", request.getResponseFormat());
- }
- if (request.getSafetyMode().isPresent()) {
- properties.put("safety_mode", request.getSafetyMode());
- }
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaTypes.APPLICATION_JSON);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- if (request.getAccepts().isPresent()) {
- _requestBuilder.addHeader("Accepts", request.getAccepts().get());
- }
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture>> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- Stream.fromJson(StreamedChatResponse.class, new ResponseBodyReader(response), "\\n"),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * Generates a text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CompletableFuture> chat(ChatRequest request) {
- return chat(request, null);
- }
-
- /**
- * Generates a text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CompletableFuture> chat(
- ChatRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/chat")
- .build();
- Map properties = new HashMap<>();
- properties.put("message", request.getMessage());
- if (request.getModel().isPresent()) {
- properties.put("model", request.getModel());
- }
- properties.put("stream", request.getStream());
- if (request.getPreamble().isPresent()) {
- properties.put("preamble", request.getPreamble());
- }
- if (request.getChatHistory().isPresent()) {
- properties.put("chat_history", request.getChatHistory());
- }
- if (request.getConversationId().isPresent()) {
- properties.put("conversation_id", request.getConversationId());
- }
- if (request.getPromptTruncation().isPresent()) {
- properties.put("prompt_truncation", request.getPromptTruncation());
- }
- if (request.getConnectors().isPresent()) {
- properties.put("connectors", request.getConnectors());
- }
- if (request.getSearchQueriesOnly().isPresent()) {
- properties.put("search_queries_only", request.getSearchQueriesOnly());
- }
- if (request.getDocuments().isPresent()) {
- properties.put("documents", request.getDocuments());
- }
- if (request.getCitationQuality().isPresent()) {
- properties.put("citation_quality", request.getCitationQuality());
- }
- if (request.getTemperature().isPresent()) {
- properties.put("temperature", request.getTemperature());
- }
- if (request.getMaxTokens().isPresent()) {
- properties.put("max_tokens", request.getMaxTokens());
- }
- if (request.getMaxInputTokens().isPresent()) {
- properties.put("max_input_tokens", request.getMaxInputTokens());
- }
- if (request.getK().isPresent()) {
- properties.put("k", request.getK());
- }
- if (request.getP().isPresent()) {
- properties.put("p", request.getP());
- }
- if (request.getSeed().isPresent()) {
- properties.put("seed", request.getSeed());
- }
- if (request.getStopSequences().isPresent()) {
- properties.put("stop_sequences", request.getStopSequences());
- }
- if (request.getFrequencyPenalty().isPresent()) {
- properties.put("frequency_penalty", request.getFrequencyPenalty());
- }
- if (request.getPresencePenalty().isPresent()) {
- properties.put("presence_penalty", request.getPresencePenalty());
- }
- if (request.getTools().isPresent()) {
- properties.put("tools", request.getTools());
- }
- if (request.getToolResults().isPresent()) {
- properties.put("tool_results", request.getToolResults());
- }
- if (request.getForceSingleStep().isPresent()) {
- properties.put("force_single_step", request.getForceSingleStep());
- }
- if (request.getResponseFormat().isPresent()) {
- properties.put("response_format", request.getResponseFormat());
- }
- if (request.getSafetyMode().isPresent()) {
- properties.put("safety_mode", request.getSafetyMode());
- }
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaTypes.APPLICATION_JSON);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- if (request.getAccepts().isPresent()) {
- _requestBuilder.addHeader("Accepts", request.getAccepts().get());
- }
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(
- responseBody.string(), NonStreamedChatResponse.class),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat with Streaming API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CompletableFuture>> generateStream(
- GenerateStreamRequest request) {
- return generateStream(request, null);
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat with Streaming API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CompletableFuture>> generateStream(
- GenerateStreamRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/generate")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture>> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- Stream.fromJson(
- GenerateStreamedResponse.class, new ResponseBodyReader(response), "\\n"),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CompletableFuture> generate(GenerateRequest request) {
- return generate(request, null);
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CompletableFuture> generate(
- GenerateRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/generate")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Generation.class),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
- * Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
- * If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
- */
- public CompletableFuture> embed() {
- return embed(EmbedRequest.builder().build());
- }
-
- /**
- * This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
- * Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
- * If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
- */
- public CompletableFuture> embed(EmbedRequest request) {
- return embed(request, null);
- }
-
- /**
- * This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
- * Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
- * If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
- */
- public CompletableFuture> embed(
- EmbedRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/embed")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), EmbedResponse.class),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
- */
- public CompletableFuture> rerank(RerankRequest request) {
- return rerank(request, null);
- }
-
- /**
- * This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
- */
- public CompletableFuture> rerank(
- RerankRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/rerank")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RerankResponse.class),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided examples of text + label pairs as a reference.
- * Note: Fine-tuned models trained on classification examples don't require the examples parameter to be passed in explicitly.
- */
- public CompletableFuture> classify(ClassifyRequest request) {
- return classify(request, null);
- }
-
- /**
- * This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided examples of text + label pairs as a reference.
- * Note: Fine-tuned models trained on classification examples don't require the examples parameter to be passed in explicitly.
- */
- public CompletableFuture> classify(
- ClassifyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/classify")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ClassifyResponse.class),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates a summary in English for a given text.
- */
- public CompletableFuture> summarize(SummarizeRequest request) {
- return summarize(request, null);
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates a summary in English for a given text.
- */
- public CompletableFuture> summarize(
- SummarizeRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/summarize")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SummarizeResponse.class),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CompletableFuture> tokenize(TokenizeRequest request) {
- return tokenize(request, null);
- }
-
- /**
- * This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CompletableFuture> tokenize(
- TokenizeRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/tokenize")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), TokenizeResponse.class),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CompletableFuture> detokenize(DetokenizeRequest request) {
- return detokenize(request, null);
- }
-
- /**
- * This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CompletableFuture> detokenize(
- DetokenizeRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/detokenize")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DetokenizeResponse.class),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-
- /**
- * Checks that the api key in the Authorization header is valid and active
- */
- public CompletableFuture> checkApiKey() {
- return checkApiKey(null);
- }
-
- /**
- * Checks that the api key in the Authorization header is valid and active
- */
- public CompletableFuture> checkApiKey(RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/check-api-key")
- .build();
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", RequestBody.create("", null))
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- CompletableFuture> future = new CompletableFuture<>();
- client.newCall(okhttpRequest).enqueue(new Callback() {
- @Override
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
- try (ResponseBody responseBody = response.body()) {
- if (response.isSuccessful()) {
- future.complete(new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CheckApiKeyResponse.class),
- response));
- return;
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- future.completeExceptionally(new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 401:
- future.completeExceptionally(new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 403:
- future.completeExceptionally(new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 404:
- future.completeExceptionally(new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 422:
- future.completeExceptionally(new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 429:
- future.completeExceptionally(new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 498:
- future.completeExceptionally(new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 499:
- future.completeExceptionally(new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 500:
- future.completeExceptionally(new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 501:
- future.completeExceptionally(new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 503:
- future.completeExceptionally(new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- case 504:
- future.completeExceptionally(new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- future.completeExceptionally(new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response));
- return;
- } catch (IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- }
-
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- future.completeExceptionally(new CohereException("Network error executing HTTP request", e));
- }
- });
- return future;
- }
-}
diff --git a/src/main/java/com/cohere/api/Cohere.java b/src/main/java/com/cohere/api/Cohere.java
index aa2a15e..e1eb266 100644
--- a/src/main/java/com/cohere/api/Cohere.java
+++ b/src/main/java/com/cohere/api/Cohere.java
@@ -4,8 +4,26 @@
package com.cohere.api;
import com.cohere.api.core.ClientOptions;
+import com.cohere.api.core.CohereApiException;
+import com.cohere.api.core.CohereException;
+import com.cohere.api.core.MediaTypes;
+import com.cohere.api.core.ObjectMappers;
import com.cohere.api.core.RequestOptions;
+import com.cohere.api.core.ResponseBodyReader;
+import com.cohere.api.core.Stream;
import com.cohere.api.core.Suppliers;
+import com.cohere.api.errors.BadRequestError;
+import com.cohere.api.errors.ClientClosedRequestError;
+import com.cohere.api.errors.ForbiddenError;
+import com.cohere.api.errors.GatewayTimeoutError;
+import com.cohere.api.errors.InternalServerError;
+import com.cohere.api.errors.InvalidTokenError;
+import com.cohere.api.errors.NotFoundError;
+import com.cohere.api.errors.NotImplementedError;
+import com.cohere.api.errors.ServiceUnavailableError;
+import com.cohere.api.errors.TooManyRequestsError;
+import com.cohere.api.errors.UnauthorizedError;
+import com.cohere.api.errors.UnprocessableEntityError;
import com.cohere.api.requests.ChatRequest;
import com.cohere.api.requests.ChatStreamRequest;
import com.cohere.api.requests.ClassifyRequest;
@@ -33,13 +51,22 @@
import com.cohere.api.types.StreamedChatResponse;
import com.cohere.api.types.SummarizeResponse;
import com.cohere.api.types.TokenizeResponse;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
import java.util.function.Supplier;
+import okhttp3.Headers;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
public class Cohere {
protected final ClientOptions clientOptions;
- private final RawCohere rawClient;
-
protected final Supplier v2Client;
protected final Supplier embedJobsClient;
@@ -54,7 +81,6 @@ public class Cohere {
public Cohere(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
- this.rawClient = new RawCohere(clientOptions);
this.v2Client = Suppliers.memoize(() -> new V2Client(clientOptions));
this.embedJobsClient = Suppliers.memoize(() -> new EmbedJobsClient(clientOptions));
this.datasetsClient = Suppliers.memoize(() -> new DatasetsClient(clientOptions));
@@ -63,19 +89,12 @@ public Cohere(ClientOptions clientOptions) {
this.finetuningClient = Suppliers.memoize(() -> new FinetuningClient(clientOptions));
}
- /**
- * Get responses with HTTP metadata like headers
- */
- public RawCohere withRawResponse() {
- return this.rawClient;
- }
-
/**
* Generates a streamed text response to a user message.
* To learn how to use the Chat API and RAG follow our Text Generation guides.
*/
public Iterable chatStream(ChatStreamRequest request) {
- return this.rawClient.chatStream(request).body();
+ return chatStream(request, null);
}
/**
@@ -83,7 +102,163 @@ public Iterable chatStream(ChatStreamRequest request) {
* To learn how to use the Chat API and RAG follow our Text Generation guides.
*/
public Iterable chatStream(ChatStreamRequest request, RequestOptions requestOptions) {
- return this.rawClient.chatStream(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/chat")
+ .build();
+ Map properties = new HashMap<>();
+ properties.put("message", request.getMessage());
+ if (request.getModel().isPresent()) {
+ properties.put("model", request.getModel());
+ }
+ properties.put("stream", request.getStream());
+ if (request.getPreamble().isPresent()) {
+ properties.put("preamble", request.getPreamble());
+ }
+ if (request.getChatHistory().isPresent()) {
+ properties.put("chat_history", request.getChatHistory());
+ }
+ if (request.getConversationId().isPresent()) {
+ properties.put("conversation_id", request.getConversationId());
+ }
+ if (request.getPromptTruncation().isPresent()) {
+ properties.put("prompt_truncation", request.getPromptTruncation());
+ }
+ if (request.getConnectors().isPresent()) {
+ properties.put("connectors", request.getConnectors());
+ }
+ if (request.getSearchQueriesOnly().isPresent()) {
+ properties.put("search_queries_only", request.getSearchQueriesOnly());
+ }
+ if (request.getDocuments().isPresent()) {
+ properties.put("documents", request.getDocuments());
+ }
+ if (request.getCitationQuality().isPresent()) {
+ properties.put("citation_quality", request.getCitationQuality());
+ }
+ if (request.getTemperature().isPresent()) {
+ properties.put("temperature", request.getTemperature());
+ }
+ if (request.getMaxTokens().isPresent()) {
+ properties.put("max_tokens", request.getMaxTokens());
+ }
+ if (request.getMaxInputTokens().isPresent()) {
+ properties.put("max_input_tokens", request.getMaxInputTokens());
+ }
+ if (request.getK().isPresent()) {
+ properties.put("k", request.getK());
+ }
+ if (request.getP().isPresent()) {
+ properties.put("p", request.getP());
+ }
+ if (request.getSeed().isPresent()) {
+ properties.put("seed", request.getSeed());
+ }
+ if (request.getStopSequences().isPresent()) {
+ properties.put("stop_sequences", request.getStopSequences());
+ }
+ if (request.getFrequencyPenalty().isPresent()) {
+ properties.put("frequency_penalty", request.getFrequencyPenalty());
+ }
+ if (request.getPresencePenalty().isPresent()) {
+ properties.put("presence_penalty", request.getPresencePenalty());
+ }
+ if (request.getRawPrompting().isPresent()) {
+ properties.put("raw_prompting", request.getRawPrompting());
+ }
+ if (request.getReturnPrompt().isPresent()) {
+ properties.put("return_prompt", request.getReturnPrompt());
+ }
+ if (request.getTools().isPresent()) {
+ properties.put("tools", request.getTools());
+ }
+ if (request.getToolResults().isPresent()) {
+ properties.put("tool_results", request.getToolResults());
+ }
+ if (request.getForceSingleStep().isPresent()) {
+ properties.put("force_single_step", request.getForceSingleStep());
+ }
+ if (request.getResponseFormat().isPresent()) {
+ properties.put("response_format", request.getResponseFormat());
+ }
+ if (request.getSafetyMode().isPresent()) {
+ properties.put("safety_mode", request.getSafetyMode());
+ }
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaTypes.APPLICATION_JSON);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json");
+ if (request.getAccepts().isPresent()) {
+ _requestBuilder.addHeader("Accepts", request.getAccepts().get());
+ }
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try {
+ Response response = client.newCall(okhttpRequest).execute();
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new Stream(
+ StreamedChatResponse.class, new ResponseBodyReader(response), "\n");
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
@@ -91,7 +266,7 @@ public Iterable chatStream(ChatStreamRequest request, Requ
* To learn how to use the Chat API and RAG follow our Text Generation guides.
*/
public NonStreamedChatResponse chat(ChatRequest request) {
- return this.rawClient.chat(request).body();
+ return chat(request, null);
}
/**
@@ -99,7 +274,161 @@ public NonStreamedChatResponse chat(ChatRequest request) {
* To learn how to use the Chat API and RAG follow our Text Generation guides.
*/
public NonStreamedChatResponse chat(ChatRequest request, RequestOptions requestOptions) {
- return this.rawClient.chat(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/chat")
+ .build();
+ Map properties = new HashMap<>();
+ properties.put("message", request.getMessage());
+ if (request.getModel().isPresent()) {
+ properties.put("model", request.getModel());
+ }
+ properties.put("stream", request.getStream());
+ if (request.getPreamble().isPresent()) {
+ properties.put("preamble", request.getPreamble());
+ }
+ if (request.getChatHistory().isPresent()) {
+ properties.put("chat_history", request.getChatHistory());
+ }
+ if (request.getConversationId().isPresent()) {
+ properties.put("conversation_id", request.getConversationId());
+ }
+ if (request.getPromptTruncation().isPresent()) {
+ properties.put("prompt_truncation", request.getPromptTruncation());
+ }
+ if (request.getConnectors().isPresent()) {
+ properties.put("connectors", request.getConnectors());
+ }
+ if (request.getSearchQueriesOnly().isPresent()) {
+ properties.put("search_queries_only", request.getSearchQueriesOnly());
+ }
+ if (request.getDocuments().isPresent()) {
+ properties.put("documents", request.getDocuments());
+ }
+ if (request.getCitationQuality().isPresent()) {
+ properties.put("citation_quality", request.getCitationQuality());
+ }
+ if (request.getTemperature().isPresent()) {
+ properties.put("temperature", request.getTemperature());
+ }
+ if (request.getMaxTokens().isPresent()) {
+ properties.put("max_tokens", request.getMaxTokens());
+ }
+ if (request.getMaxInputTokens().isPresent()) {
+ properties.put("max_input_tokens", request.getMaxInputTokens());
+ }
+ if (request.getK().isPresent()) {
+ properties.put("k", request.getK());
+ }
+ if (request.getP().isPresent()) {
+ properties.put("p", request.getP());
+ }
+ if (request.getSeed().isPresent()) {
+ properties.put("seed", request.getSeed());
+ }
+ if (request.getStopSequences().isPresent()) {
+ properties.put("stop_sequences", request.getStopSequences());
+ }
+ if (request.getFrequencyPenalty().isPresent()) {
+ properties.put("frequency_penalty", request.getFrequencyPenalty());
+ }
+ if (request.getPresencePenalty().isPresent()) {
+ properties.put("presence_penalty", request.getPresencePenalty());
+ }
+ if (request.getRawPrompting().isPresent()) {
+ properties.put("raw_prompting", request.getRawPrompting());
+ }
+ if (request.getReturnPrompt().isPresent()) {
+ properties.put("return_prompt", request.getReturnPrompt());
+ }
+ if (request.getTools().isPresent()) {
+ properties.put("tools", request.getTools());
+ }
+ if (request.getToolResults().isPresent()) {
+ properties.put("tool_results", request.getToolResults());
+ }
+ if (request.getForceSingleStep().isPresent()) {
+ properties.put("force_single_step", request.getForceSingleStep());
+ }
+ if (request.getResponseFormat().isPresent()) {
+ properties.put("response_format", request.getResponseFormat());
+ }
+ if (request.getSafetyMode().isPresent()) {
+ properties.put("safety_mode", request.getSafetyMode());
+ }
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaTypes.APPLICATION_JSON);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ Request.Builder _requestBuilder = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json");
+ if (request.getAccepts().isPresent()) {
+ _requestBuilder.addHeader("Accepts", request.getAccepts().get());
+ }
+ Request okhttpRequest = _requestBuilder.build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), NonStreamedChatResponse.class);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
@@ -109,7 +438,7 @@ public NonStreamedChatResponse chat(ChatRequest request, RequestOptions requestO
* Generates realistic text conditioned on a given input.
*/
public Iterable generateStream(GenerateStreamRequest request) {
- return this.rawClient.generateStream(request).body();
+ return generateStream(request, null);
}
/**
@@ -120,7 +449,82 @@ public Iterable generateStream(GenerateStreamRequest r
*/
public Iterable generateStream(
GenerateStreamRequest request, RequestOptions requestOptions) {
- return this.rawClient.generateStream(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/generate")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new CohereException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try {
+ Response response = client.newCall(okhttpRequest).execute();
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return new Stream(
+ GenerateStreamedResponse.class, new ResponseBodyReader(response), "\n");
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
@@ -130,7 +534,7 @@ public Iterable generateStream(
* Generates realistic text conditioned on a given input.
*/
public Generation generate(GenerateRequest request) {
- return this.rawClient.generate(request).body();
+ return generate(request, null);
}
/**
@@ -140,7 +544,80 @@ public Generation generate(GenerateRequest request) {
* Generates realistic text conditioned on a given input.
*/
public Generation generate(GenerateRequest request, RequestOptions requestOptions) {
- return this.rawClient.generate(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/generate")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new CohereException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Generation.class);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
@@ -149,7 +626,7 @@ public Generation generate(GenerateRequest request, RequestOptions requestOption
* If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
*/
public EmbedResponse embed() {
- return this.rawClient.embed().body();
+ return embed(EmbedRequest.builder().build());
}
/**
@@ -158,7 +635,7 @@ public EmbedResponse embed() {
* If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
*/
public EmbedResponse embed(EmbedRequest request) {
- return this.rawClient.embed(request).body();
+ return embed(request, null);
}
/**
@@ -167,21 +644,167 @@ public EmbedResponse embed(EmbedRequest request) {
* If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
*/
public EmbedResponse embed(EmbedRequest request, RequestOptions requestOptions) {
- return this.rawClient.embed(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/embed")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new CohereException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), EmbedResponse.class);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
* This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
*/
public RerankResponse rerank(RerankRequest request) {
- return this.rawClient.rerank(request).body();
+ return rerank(request, null);
}
/**
* This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
*/
public RerankResponse rerank(RerankRequest request, RequestOptions requestOptions) {
- return this.rawClient.rerank(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/rerank")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new CohereException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RerankResponse.class);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
@@ -189,7 +812,7 @@ public RerankResponse rerank(RerankRequest request, RequestOptions requestOption
* Note: Fine-tuned models trained on classification examples don't require the examples parameter to be passed in explicitly.
*/
public ClassifyResponse classify(ClassifyRequest request) {
- return this.rawClient.classify(request).body();
+ return classify(request, null);
}
/**
@@ -197,7 +820,80 @@ public ClassifyResponse classify(ClassifyRequest request) {
* Note: Fine-tuned models trained on classification examples don't require the examples parameter to be passed in explicitly.
*/
public ClassifyResponse classify(ClassifyRequest request, RequestOptions requestOptions) {
- return this.rawClient.classify(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/classify")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new CohereException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ClassifyResponse.class);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
@@ -207,7 +903,7 @@ public ClassifyResponse classify(ClassifyRequest request, RequestOptions request
* Generates a summary in English for a given text.
*/
public SummarizeResponse summarize(SummarizeRequest request) {
- return this.rawClient.summarize(request).body();
+ return summarize(request, null);
}
/**
@@ -217,49 +913,334 @@ public SummarizeResponse summarize(SummarizeRequest request) {
* Generates a summary in English for a given text.
*/
public SummarizeResponse summarize(SummarizeRequest request, RequestOptions requestOptions) {
- return this.rawClient.summarize(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/summarize")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new CohereException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SummarizeResponse.class);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
* This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
*/
public TokenizeResponse tokenize(TokenizeRequest request) {
- return this.rawClient.tokenize(request).body();
+ return tokenize(request, null);
}
/**
* This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
*/
public TokenizeResponse tokenize(TokenizeRequest request, RequestOptions requestOptions) {
- return this.rawClient.tokenize(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/tokenize")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new CohereException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), TokenizeResponse.class);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
* This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
*/
public DetokenizeResponse detokenize(DetokenizeRequest request) {
- return this.rawClient.detokenize(request).body();
+ return detokenize(request, null);
}
/**
* This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
*/
public DetokenizeResponse detokenize(DetokenizeRequest request, RequestOptions requestOptions) {
- return this.rawClient.detokenize(request, requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/detokenize")
+ .build();
+ RequestBody body;
+ try {
+ body = RequestBody.create(
+ ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
+ } catch (JsonProcessingException e) {
+ throw new CohereException("Failed to serialize request", e);
+ }
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", body)
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DetokenizeResponse.class);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
/**
* Checks that the api key in the Authorization header is valid and active
*/
public CheckApiKeyResponse checkApiKey() {
- return this.rawClient.checkApiKey().body();
+ return checkApiKey(null);
}
/**
* Checks that the api key in the Authorization header is valid and active
*/
public CheckApiKeyResponse checkApiKey(RequestOptions requestOptions) {
- return this.rawClient.checkApiKey(requestOptions).body();
+ HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
+ .newBuilder()
+ .addPathSegments("v1/check-api-key")
+ .build();
+ Request okhttpRequest = new Request.Builder()
+ .url(httpUrl)
+ .method("POST", RequestBody.create("", null))
+ .headers(Headers.of(clientOptions.headers(requestOptions)))
+ .addHeader("Content-Type", "application/json")
+ .build();
+ OkHttpClient client = clientOptions.httpClient();
+ if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
+ client = clientOptions.httpClientWithTimeout(requestOptions);
+ }
+ try (Response response = client.newCall(okhttpRequest).execute()) {
+ ResponseBody responseBody = response.body();
+ if (response.isSuccessful()) {
+ return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CheckApiKeyResponse.class);
+ }
+ String responseBodyString = responseBody != null ? responseBody.string() : "{}";
+ try {
+ switch (response.code()) {
+ case 400:
+ throw new BadRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 401:
+ throw new UnauthorizedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 403:
+ throw new ForbiddenError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 404:
+ throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 422:
+ throw new UnprocessableEntityError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 429:
+ throw new TooManyRequestsError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 498:
+ throw new InvalidTokenError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 499:
+ throw new ClientClosedRequestError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 500:
+ throw new InternalServerError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 501:
+ throw new NotImplementedError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 503:
+ throw new ServiceUnavailableError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ case 504:
+ throw new GatewayTimeoutError(
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ }
+ } catch (JsonProcessingException ignored) {
+ // unable to map error response, throwing generic error
+ }
+ throw new CohereApiException(
+ "Error with status code " + response.code(),
+ response.code(),
+ ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class));
+ } catch (IOException e) {
+ throw new CohereException("Network error executing HTTP request", e);
+ }
}
public V2Client v2() {
diff --git a/src/main/java/com/cohere/api/CohereBuilder.java b/src/main/java/com/cohere/api/CohereBuilder.java
index 2050387..133039f 100644
--- a/src/main/java/com/cohere/api/CohereBuilder.java
+++ b/src/main/java/com/cohere/api/CohereBuilder.java
@@ -5,13 +5,9 @@
import com.cohere.api.core.ClientOptions;
import com.cohere.api.core.Environment;
-import java.util.Optional;
-import okhttp3.OkHttpClient;
-public class CohereBuilder {
- private Optional timeout = Optional.empty();
-
- private Optional maxRetries = Optional.empty();
+public final class CohereBuilder {
+ private ClientOptions.Builder clientOptionsBuilder = ClientOptions.builder();
private String token = System.getenv("CO_API_KEY");
@@ -19,8 +15,6 @@ public class CohereBuilder {
private Environment environment = Environment.PRODUCTION;
- private OkHttpClient httpClient;
-
/**
* Sets token.
* Defaults to the CO_API_KEY environment variable.
@@ -48,171 +42,15 @@ public CohereBuilder url(String url) {
return this;
}
- /**
- * Sets the timeout (in seconds) for the client. Defaults to 60 seconds.
- */
- public CohereBuilder timeout(int timeout) {
- this.timeout = Optional.of(timeout);
- return this;
- }
-
- /**
- * Sets the maximum number of retries for the client. Defaults to 2 retries.
- */
- public CohereBuilder maxRetries(int maxRetries) {
- this.maxRetries = Optional.of(maxRetries);
- return this;
- }
-
- /**
- * Sets the underlying OkHttp client
- */
- public CohereBuilder httpClient(OkHttpClient httpClient) {
- this.httpClient = httpClient;
- return this;
- }
-
- protected ClientOptions buildClientOptions() {
- ClientOptions.Builder builder = ClientOptions.builder();
- setEnvironment(builder);
- setAuthentication(builder);
- setCustomHeaders(builder);
- setHttpClient(builder);
- setTimeouts(builder);
- setRetries(builder);
- setAdditional(builder);
- return builder.build();
- }
-
- /**
- * Sets the environment configuration for the client.
- * Override this method to modify URLs or add environment-specific logic.
- *
- * @param builder The ClientOptions.Builder to configure
- */
- protected void setEnvironment(ClientOptions.Builder builder) {
- builder.environment(this.environment);
- }
-
- /**
- * Override this method to customize authentication.
- * This method is called during client options construction to set up authentication headers.
- *
- * @param builder The ClientOptions.Builder to configure
- *
- * Example:
- * {@code
- * @Override
- * protected void setAuthentication(ClientOptions.Builder builder) {
- * super.setAuthentication(builder); // Keep existing auth
- * builder.addHeader("X-API-Key", this.apiKey);
- * }
- * }
- */
- protected void setAuthentication(ClientOptions.Builder builder) {
- if (this.token != null) {
- builder.addHeader("Authorization", "Bearer " + this.token);
- }
- }
-
- /**
- * Override this method to add or modify custom headers.
- * This method is called during client options construction to set up custom headers defined in the API.
- *
- * @param builder The ClientOptions.Builder to configure
- *
- * Example:
- * {@code
- * @Override
- * protected void setCustomHeaders(ClientOptions.Builder builder) {
- * super.setCustomHeaders(builder); // Keep existing headers
- * builder.addHeader("X-Trace-ID", generateTraceId());
- * }
- * }
- */
- protected void setCustomHeaders(ClientOptions.Builder builder) {
- if (this.clientName != null) {
- builder.addHeader("X-Client-Name", this.clientName);
- }
- }
-
- /**
- * Sets the request timeout configuration.
- * Override this method to customize timeout behavior.
- *
- * @param builder The ClientOptions.Builder to configure
- */
- protected void setTimeouts(ClientOptions.Builder builder) {
- if (this.timeout.isPresent()) {
- builder.timeout(this.timeout.get());
- }
- }
-
- /**
- * Sets the retry configuration for failed requests.
- * Override this method to implement custom retry strategies.
- *
- * @param builder The ClientOptions.Builder to configure
- */
- protected void setRetries(ClientOptions.Builder builder) {
- if (this.maxRetries.isPresent()) {
- builder.maxRetries(this.maxRetries.get());
- }
- }
-
- /**
- * Sets the OkHttp client configuration.
- * Override this method to customize HTTP client behavior (interceptors, connection pools, etc).
- *
- * @param builder The ClientOptions.Builder to configure
- */
- protected void setHttpClient(ClientOptions.Builder builder) {
- if (this.httpClient != null) {
- builder.httpClient(this.httpClient);
- }
- }
-
- /**
- * Override this method to add any additional configuration to the client.
- * This method is called at the end of the configuration chain, allowing you to add
- * custom headers, modify settings, or perform any other client customization.
- *
- * @param builder The ClientOptions.Builder to configure
- *
- * Example:
- * {@code
- * @Override
- * protected void setAdditional(ClientOptions.Builder builder) {
- * builder.addHeader("X-Request-ID", () -> UUID.randomUUID().toString());
- * builder.addHeader("X-Client-Version", "1.0.0");
- * }
- * }
- */
- protected void setAdditional(ClientOptions.Builder builder) {}
-
- /**
- * Override this method to add custom validation logic before the client is built.
- * This method is called at the beginning of the build() method to ensure the configuration is valid.
- * Throw an exception to prevent client creation if validation fails.
- *
- * Example:
- * {@code
- * @Override
- * protected void validateConfiguration() {
- * super.validateConfiguration(); // Run parent validations
- * if (tenantId == null || tenantId.isEmpty()) {
- * throw new IllegalStateException("tenantId is required");
- * }
- * }
- * }
- */
- protected void validateConfiguration() {}
-
public Cohere build() {
if (token == null) {
throw new RuntimeException("Please provide token or set the CO_API_KEY environment variable.");
}
- validateConfiguration();
- return new Cohere(buildClientOptions());
+ this.clientOptionsBuilder.addHeader("Authorization", "Bearer " + this.token);
+ if (clientName != null) {
+ this.clientOptionsBuilder.addHeader("X-Client-Name", this.clientName);
+ }
+ clientOptionsBuilder.environment(this.environment);
+ return new Cohere(clientOptionsBuilder.build());
}
}
diff --git a/src/main/java/com/cohere/api/RawCohere.java b/src/main/java/com/cohere/api/RawCohere.java
deleted file mode 100644
index 3f3ec4e..0000000
--- a/src/main/java/com/cohere/api/RawCohere.java
+++ /dev/null
@@ -1,1264 +0,0 @@
-/**
- * This file was auto-generated by Fern from our API Definition.
- */
-package com.cohere.api;
-
-import com.cohere.api.core.ClientOptions;
-import com.cohere.api.core.CohereApiException;
-import com.cohere.api.core.CohereException;
-import com.cohere.api.core.CohereHttpResponse;
-import com.cohere.api.core.MediaTypes;
-import com.cohere.api.core.ObjectMappers;
-import com.cohere.api.core.RequestOptions;
-import com.cohere.api.core.ResponseBodyReader;
-import com.cohere.api.core.Stream;
-import com.cohere.api.errors.BadRequestError;
-import com.cohere.api.errors.ClientClosedRequestError;
-import com.cohere.api.errors.ForbiddenError;
-import com.cohere.api.errors.GatewayTimeoutError;
-import com.cohere.api.errors.InternalServerError;
-import com.cohere.api.errors.InvalidTokenError;
-import com.cohere.api.errors.NotFoundError;
-import com.cohere.api.errors.NotImplementedError;
-import com.cohere.api.errors.ServiceUnavailableError;
-import com.cohere.api.errors.TooManyRequestsError;
-import com.cohere.api.errors.UnauthorizedError;
-import com.cohere.api.errors.UnprocessableEntityError;
-import com.cohere.api.requests.ChatRequest;
-import com.cohere.api.requests.ChatStreamRequest;
-import com.cohere.api.requests.ClassifyRequest;
-import com.cohere.api.requests.DetokenizeRequest;
-import com.cohere.api.requests.EmbedRequest;
-import com.cohere.api.requests.GenerateRequest;
-import com.cohere.api.requests.GenerateStreamRequest;
-import com.cohere.api.requests.RerankRequest;
-import com.cohere.api.requests.SummarizeRequest;
-import com.cohere.api.requests.TokenizeRequest;
-import com.cohere.api.types.CheckApiKeyResponse;
-import com.cohere.api.types.ClassifyResponse;
-import com.cohere.api.types.DetokenizeResponse;
-import com.cohere.api.types.EmbedResponse;
-import com.cohere.api.types.GenerateStreamedResponse;
-import com.cohere.api.types.Generation;
-import com.cohere.api.types.NonStreamedChatResponse;
-import com.cohere.api.types.RerankResponse;
-import com.cohere.api.types.StreamedChatResponse;
-import com.cohere.api.types.SummarizeResponse;
-import com.cohere.api.types.TokenizeResponse;
-import com.fasterxml.jackson.core.JsonProcessingException;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-import okhttp3.Headers;
-import okhttp3.HttpUrl;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-
-public class RawCohere {
- protected final ClientOptions clientOptions;
-
- public RawCohere(ClientOptions clientOptions) {
- this.clientOptions = clientOptions;
- }
-
- /**
- * Generates a streamed text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CohereHttpResponse> chatStream(ChatStreamRequest request) {
- return chatStream(request, null);
- }
-
- /**
- * Generates a streamed text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CohereHttpResponse> chatStream(
- ChatStreamRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/chat")
- .build();
- Map properties = new HashMap<>();
- properties.put("message", request.getMessage());
- if (request.getModel().isPresent()) {
- properties.put("model", request.getModel());
- }
- properties.put("stream", request.getStream());
- if (request.getPreamble().isPresent()) {
- properties.put("preamble", request.getPreamble());
- }
- if (request.getChatHistory().isPresent()) {
- properties.put("chat_history", request.getChatHistory());
- }
- if (request.getConversationId().isPresent()) {
- properties.put("conversation_id", request.getConversationId());
- }
- if (request.getPromptTruncation().isPresent()) {
- properties.put("prompt_truncation", request.getPromptTruncation());
- }
- if (request.getConnectors().isPresent()) {
- properties.put("connectors", request.getConnectors());
- }
- if (request.getSearchQueriesOnly().isPresent()) {
- properties.put("search_queries_only", request.getSearchQueriesOnly());
- }
- if (request.getDocuments().isPresent()) {
- properties.put("documents", request.getDocuments());
- }
- if (request.getCitationQuality().isPresent()) {
- properties.put("citation_quality", request.getCitationQuality());
- }
- if (request.getTemperature().isPresent()) {
- properties.put("temperature", request.getTemperature());
- }
- if (request.getMaxTokens().isPresent()) {
- properties.put("max_tokens", request.getMaxTokens());
- }
- if (request.getMaxInputTokens().isPresent()) {
- properties.put("max_input_tokens", request.getMaxInputTokens());
- }
- if (request.getK().isPresent()) {
- properties.put("k", request.getK());
- }
- if (request.getP().isPresent()) {
- properties.put("p", request.getP());
- }
- if (request.getSeed().isPresent()) {
- properties.put("seed", request.getSeed());
- }
- if (request.getStopSequences().isPresent()) {
- properties.put("stop_sequences", request.getStopSequences());
- }
- if (request.getFrequencyPenalty().isPresent()) {
- properties.put("frequency_penalty", request.getFrequencyPenalty());
- }
- if (request.getPresencePenalty().isPresent()) {
- properties.put("presence_penalty", request.getPresencePenalty());
- }
- if (request.getTools().isPresent()) {
- properties.put("tools", request.getTools());
- }
- if (request.getToolResults().isPresent()) {
- properties.put("tool_results", request.getToolResults());
- }
- if (request.getForceSingleStep().isPresent()) {
- properties.put("force_single_step", request.getForceSingleStep());
- }
- if (request.getResponseFormat().isPresent()) {
- properties.put("response_format", request.getResponseFormat());
- }
- if (request.getSafetyMode().isPresent()) {
- properties.put("safety_mode", request.getSafetyMode());
- }
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaTypes.APPLICATION_JSON);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- if (request.getAccepts().isPresent()) {
- _requestBuilder.addHeader("Accepts", request.getAccepts().get());
- }
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try {
- Response response = client.newCall(okhttpRequest).execute();
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- Stream.fromJson(StreamedChatResponse.class, new ResponseBodyReader(response), "\\n"), response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * Generates a text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CohereHttpResponse chat(ChatRequest request) {
- return chat(request, null);
- }
-
- /**
- * Generates a text response to a user message.
- * To learn how to use the Chat API and RAG follow our Text Generation guides.
- */
- public CohereHttpResponse chat(ChatRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/chat")
- .build();
- Map properties = new HashMap<>();
- properties.put("message", request.getMessage());
- if (request.getModel().isPresent()) {
- properties.put("model", request.getModel());
- }
- properties.put("stream", request.getStream());
- if (request.getPreamble().isPresent()) {
- properties.put("preamble", request.getPreamble());
- }
- if (request.getChatHistory().isPresent()) {
- properties.put("chat_history", request.getChatHistory());
- }
- if (request.getConversationId().isPresent()) {
- properties.put("conversation_id", request.getConversationId());
- }
- if (request.getPromptTruncation().isPresent()) {
- properties.put("prompt_truncation", request.getPromptTruncation());
- }
- if (request.getConnectors().isPresent()) {
- properties.put("connectors", request.getConnectors());
- }
- if (request.getSearchQueriesOnly().isPresent()) {
- properties.put("search_queries_only", request.getSearchQueriesOnly());
- }
- if (request.getDocuments().isPresent()) {
- properties.put("documents", request.getDocuments());
- }
- if (request.getCitationQuality().isPresent()) {
- properties.put("citation_quality", request.getCitationQuality());
- }
- if (request.getTemperature().isPresent()) {
- properties.put("temperature", request.getTemperature());
- }
- if (request.getMaxTokens().isPresent()) {
- properties.put("max_tokens", request.getMaxTokens());
- }
- if (request.getMaxInputTokens().isPresent()) {
- properties.put("max_input_tokens", request.getMaxInputTokens());
- }
- if (request.getK().isPresent()) {
- properties.put("k", request.getK());
- }
- if (request.getP().isPresent()) {
- properties.put("p", request.getP());
- }
- if (request.getSeed().isPresent()) {
- properties.put("seed", request.getSeed());
- }
- if (request.getStopSequences().isPresent()) {
- properties.put("stop_sequences", request.getStopSequences());
- }
- if (request.getFrequencyPenalty().isPresent()) {
- properties.put("frequency_penalty", request.getFrequencyPenalty());
- }
- if (request.getPresencePenalty().isPresent()) {
- properties.put("presence_penalty", request.getPresencePenalty());
- }
- if (request.getTools().isPresent()) {
- properties.put("tools", request.getTools());
- }
- if (request.getToolResults().isPresent()) {
- properties.put("tool_results", request.getToolResults());
- }
- if (request.getForceSingleStep().isPresent()) {
- properties.put("force_single_step", request.getForceSingleStep());
- }
- if (request.getResponseFormat().isPresent()) {
- properties.put("response_format", request.getResponseFormat());
- }
- if (request.getSafetyMode().isPresent()) {
- properties.put("safety_mode", request.getSafetyMode());
- }
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaTypes.APPLICATION_JSON);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- Request.Builder _requestBuilder = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json");
- if (request.getAccepts().isPresent()) {
- _requestBuilder.addHeader("Accepts", request.getAccepts().get());
- }
- Request okhttpRequest = _requestBuilder.build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), NonStreamedChatResponse.class),
- response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat with Streaming API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CohereHttpResponse> generateStream(GenerateStreamRequest request) {
- return generateStream(request, null);
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat with Streaming API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CohereHttpResponse> generateStream(
- GenerateStreamRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/generate")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try {
- Response response = client.newCall(okhttpRequest).execute();
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- Stream.fromJson(GenerateStreamedResponse.class, new ResponseBodyReader(response), "\\n"),
- response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CohereHttpResponse generate(GenerateRequest request) {
- return generate(request, null);
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates realistic text conditioned on a given input.
- */
- public CohereHttpResponse generate(GenerateRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/generate")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), Generation.class), response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
- * Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
- * If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
- */
- public CohereHttpResponse embed() {
- return embed(EmbedRequest.builder().build());
- }
-
- /**
- * This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
- * Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
- * If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
- */
- public CohereHttpResponse embed(EmbedRequest request) {
- return embed(request, null);
- }
-
- /**
- * This endpoint returns text and image embeddings. An embedding is a list of floating point numbers that captures semantic information about the content that it represents.
- * Embeddings can be used to create classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
- * If you want to learn more how to use the embedding model, have a look at the Semantic Search Guide.
- */
- public CohereHttpResponse embed(EmbedRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/embed")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), EmbedResponse.class), response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
- */
- public CohereHttpResponse rerank(RerankRequest request) {
- return rerank(request, null);
- }
-
- /**
- * This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
- */
- public CohereHttpResponse rerank(RerankRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/rerank")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RerankResponse.class), response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided examples of text + label pairs as a reference.
- * Note: Fine-tuned models trained on classification examples don't require the examples parameter to be passed in explicitly.
- */
- public CohereHttpResponse classify(ClassifyRequest request) {
- return classify(request, null);
- }
-
- /**
- * This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided examples of text + label pairs as a reference.
- * Note: Fine-tuned models trained on classification examples don't require the examples parameter to be passed in explicitly.
- */
- public CohereHttpResponse classify(ClassifyRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/classify")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), ClassifyResponse.class), response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates a summary in English for a given text.
- */
- public CohereHttpResponse summarize(SummarizeRequest request) {
- return summarize(request, null);
- }
-
- /**
- * <Warning>
- * This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](https://docs.cohere.com/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
- * </Warning>
- * Generates a summary in English for a given text.
- */
- public CohereHttpResponse summarize(SummarizeRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/summarize")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), SummarizeResponse.class), response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CohereHttpResponse tokenize(TokenizeRequest request) {
- return tokenize(request, null);
- }
-
- /**
- * This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CohereHttpResponse tokenize(TokenizeRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/tokenize")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), TokenizeResponse.class), response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CohereHttpResponse detokenize(DetokenizeRequest request) {
- return detokenize(request, null);
- }
-
- /**
- * This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
- */
- public CohereHttpResponse detokenize(DetokenizeRequest request, RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/detokenize")
- .build();
- RequestBody body;
- try {
- body = RequestBody.create(
- ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON);
- } catch (JsonProcessingException e) {
- throw new CohereException("Failed to serialize request", e);
- }
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", body)
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Content-Type", "application/json")
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), DetokenizeResponse.class), response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-
- /**
- * Checks that the api key in the Authorization header is valid and active
- */
- public CohereHttpResponse checkApiKey() {
- return checkApiKey(null);
- }
-
- /**
- * Checks that the api key in the Authorization header is valid and active
- */
- public CohereHttpResponse checkApiKey(RequestOptions requestOptions) {
- HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
- .newBuilder()
- .addPathSegments("v1/check-api-key")
- .build();
- Request okhttpRequest = new Request.Builder()
- .url(httpUrl)
- .method("POST", RequestBody.create("", null))
- .headers(Headers.of(clientOptions.headers(requestOptions)))
- .addHeader("Accept", "application/json")
- .build();
- OkHttpClient client = clientOptions.httpClient();
- if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
- client = clientOptions.httpClientWithTimeout(requestOptions);
- }
- try (Response response = client.newCall(okhttpRequest).execute()) {
- ResponseBody responseBody = response.body();
- if (response.isSuccessful()) {
- return new CohereHttpResponse<>(
- ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), CheckApiKeyResponse.class),
- response);
- }
- String responseBodyString = responseBody != null ? responseBody.string() : "{}";
- try {
- switch (response.code()) {
- case 400:
- throw new BadRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 401:
- throw new UnauthorizedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 403:
- throw new ForbiddenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 404:
- throw new NotFoundError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 422:
- throw new UnprocessableEntityError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 429:
- throw new TooManyRequestsError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 498:
- throw new InvalidTokenError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 499:
- throw new ClientClosedRequestError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 500:
- throw new InternalServerError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 501:
- throw new NotImplementedError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 503:
- throw new ServiceUnavailableError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- case 504:
- throw new GatewayTimeoutError(
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response);
- }
- } catch (JsonProcessingException ignored) {
- // unable to map error response, throwing generic error
- }
- throw new CohereApiException(
- "Error with status code " + response.code(),
- response.code(),
- ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class),
- response);
- } catch (IOException e) {
- throw new CohereException("Network error executing HTTP request", e);
- }
- }
-}
diff --git a/src/main/java/com/cohere/api/core/ClientOptions.java b/src/main/java/com/cohere/api/core/ClientOptions.java
index 07e7379..0d1a22f 100644
--- a/src/main/java/com/cohere/api/core/ClientOptions.java
+++ b/src/main/java/com/cohere/api/core/ClientOptions.java
@@ -5,7 +5,6 @@
import java.util.HashMap;
import java.util.Map;
-import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import okhttp3.OkHttpClient;
@@ -19,28 +18,23 @@ public final class ClientOptions {
private final OkHttpClient httpClient;
- private final int timeout;
-
private ClientOptions(
Environment environment,
Map headers,
Map> headerSuppliers,
- OkHttpClient httpClient,
- int timeout) {
+ OkHttpClient httpClient) {
this.environment = environment;
this.headers = new HashMap<>();
this.headers.putAll(headers);
this.headers.putAll(new HashMap() {
{
- put("User-Agent", "com.cohere:cohere-java/1.8.0");
put("X-Fern-Language", "JAVA");
put("X-Fern-SDK-Name", "com.cohere.fern:api-sdk");
- put("X-Fern-SDK-Version", "1.8.0");
+ put("X-Fern-SDK-Version", "0.0.1");
}
});
this.headerSuppliers = headerSuppliers;
this.httpClient = httpClient;
- this.timeout = timeout;
}
public Environment environment() {
@@ -58,13 +52,6 @@ public Map headers(RequestOptions requestOptions) {
return values;
}
- public int timeout(RequestOptions requestOptions) {
- if (requestOptions == null) {
- return this.timeout;
- }
- return requestOptions.getTimeout().orElse(this.timeout);
- }
-
public OkHttpClient httpClient() {
return this.httpClient;
}
@@ -86,19 +73,13 @@ public static Builder builder() {
return new Builder();
}
- public static class Builder {
+ public static final class Builder {
private Environment environment;
private final Map headers = new HashMap<>();
private final Map> headerSuppliers = new HashMap<>();
- private int maxRetries = 2;
-
- private Optional timeout = Optional.empty();
-
- private OkHttpClient httpClient = null;
-
public Builder environment(Environment environment) {
this.environment = environment;
return this;
@@ -114,69 +95,11 @@ public Builder addHeader(String key, Supplier value) {
return this;
}
- /**
- * Override the timeout in seconds. Defaults to 60 seconds.
- */
- public Builder timeout(int timeout) {
- this.timeout = Optional.of(timeout);
- return this;
- }
-
- /**
- * Override the timeout in seconds. Defaults to 60 seconds.
- */
- public Builder timeout(Optional timeout) {
- this.timeout = timeout;
- return this;
- }
-
- /**
- * Override the maximum number of retries. Defaults to 2 retries.
- */
- public Builder maxRetries(int maxRetries) {
- this.maxRetries = maxRetries;
- return this;
- }
-
- public Builder httpClient(OkHttpClient httpClient) {
- this.httpClient = httpClient;
- return this;
- }
-
public ClientOptions build() {
- OkHttpClient.Builder httpClientBuilder =
- this.httpClient != null ? this.httpClient.newBuilder() : new OkHttpClient.Builder();
-
- if (this.httpClient != null) {
- timeout.ifPresent(timeout -> httpClientBuilder
- .callTimeout(timeout, TimeUnit.SECONDS)
- .connectTimeout(0, TimeUnit.SECONDS)
- .writeTimeout(0, TimeUnit.SECONDS)
- .readTimeout(0, TimeUnit.SECONDS));
- } else {
- httpClientBuilder
- .callTimeout(this.timeout.orElse(60), TimeUnit.SECONDS)
- .connectTimeout(0, TimeUnit.SECONDS)
- .writeTimeout(0, TimeUnit.SECONDS)
- .readTimeout(0, TimeUnit.SECONDS)
- .addInterceptor(new RetryInterceptor(this.maxRetries));
- }
-
- this.httpClient = httpClientBuilder.build();
- this.timeout = Optional.of(httpClient.callTimeoutMillis() / 1000);
-
- return new ClientOptions(environment, headers, headerSuppliers, httpClient, this.timeout.get());
- }
-
- /**
- * Create a new Builder initialized with values from an existing ClientOptions
- */
- public static Builder from(ClientOptions clientOptions) {
- Builder builder = new Builder();
- builder.environment = clientOptions.environment();
- builder.timeout = Optional.of(clientOptions.timeout(null));
- builder.httpClient = clientOptions.httpClient();
- return builder;
+ OkHttpClient okhttpClient = new OkHttpClient.Builder()
+ .addInterceptor(new RetryInterceptor(3))
+ .build();
+ return new ClientOptions(environment, headers, headerSuppliers, okhttpClient);
}
}
}
diff --git a/src/main/java/com/cohere/api/core/CohereApiException.java b/src/main/java/com/cohere/api/core/CohereApiException.java
index 46c8bb9..c3f60b6 100644
--- a/src/main/java/com/cohere/api/core/CohereApiException.java
+++ b/src/main/java/com/cohere/api/core/CohereApiException.java
@@ -3,12 +3,6 @@
*/
package com.cohere.api.core;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import okhttp3.Response;
-
/**
* This exception type will be thrown for any non-2XX API responses.
*/
@@ -23,25 +17,10 @@ public class CohereApiException extends CohereException {
*/
private final Object body;
- private final Map> headers;
-
public CohereApiException(String message, int statusCode, Object body) {
super(message);
this.statusCode = statusCode;
this.body = body;
- this.headers = new HashMap<>();
- }
-
- public CohereApiException(String message, int statusCode, Object body, Response rawResponse) {
- super(message);
- this.statusCode = statusCode;
- this.body = body;
- this.headers = new HashMap<>();
- rawResponse.headers().forEach(header -> {
- String key = header.component1();
- String value = header.component2();
- this.headers.computeIfAbsent(key, _str -> new ArrayList<>()).add(value);
- });
}
/**
@@ -58,13 +37,6 @@ public Object body() {
return this.body;
}
- /**
- * @return the headers
- */
- public Map> headers() {
- return this.headers;
- }
-
@java.lang.Override
public String toString() {
return "CohereApiException{" + "message: " + getMessage() + ", statusCode: " + statusCode + ", body: " + body
diff --git a/src/main/java/com/cohere/api/core/CohereHttpResponse.java b/src/main/java/com/cohere/api/core/CohereHttpResponse.java
deleted file mode 100644
index 8518c92..0000000
--- a/src/main/java/com/cohere/api/core/CohereHttpResponse.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
- * This file was auto-generated by Fern from our API Definition.
- */
-package com.cohere.api.core;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import okhttp3.Response;
-
-public final class CohereHttpResponse {
-
- private final T body;
-
- private final Map> headers;
-
- public CohereHttpResponse(T body, Response rawResponse) {
- this.body = body;
-
- Map> headers = new HashMap<>();
- rawResponse.headers().forEach(header -> {
- String key = header.component1();
- String value = header.component2();
- headers.computeIfAbsent(key, _str -> new ArrayList<>()).add(value);
- });
- this.headers = headers;
- }
-
- public T body() {
- return this.body;
- }
-
- public Map> headers() {
- return headers;
- }
-}
diff --git a/src/main/java/com/cohere/api/core/InputStreamRequestBody.java b/src/main/java/com/cohere/api/core/InputStreamRequestBody.java
index 9c141b5..cdc9a13 100644
--- a/src/main/java/com/cohere/api/core/InputStreamRequestBody.java
+++ b/src/main/java/com/cohere/api/core/InputStreamRequestBody.java
@@ -8,6 +8,7 @@
import java.util.Objects;
import okhttp3.MediaType;
import okhttp3.RequestBody;
+import okhttp3.internal.Util;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;
@@ -67,8 +68,12 @@ public long contentLength() throws IOException {
*/
@Override
public void writeTo(BufferedSink sink) throws IOException {
- try (Source source = Okio.source(inputStream)) {
+ Source source = null;
+ try {
+ source = Okio.source(inputStream);
sink.writeAll(source);
+ } finally {
+ Util.closeQuietly(Objects.requireNonNull(source));
}
}
}
diff --git a/src/main/java/com/cohere/api/core/Nullable.java b/src/main/java/com/cohere/api/core/Nullable.java
deleted file mode 100644
index 1a50d35..0000000
--- a/src/main/java/com/cohere/api/core/Nullable.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/**
- * This file was auto-generated by Fern from our API Definition.
- */
-package com.cohere.api.core;
-
-import java.util.Optional;
-import java.util.function.Function;
-
-public final class Nullable {
-
- private final Either, Null> value;
-
- private Nullable() {
- this.value = Either.left(Optional.empty());
- }
-
- private Nullable(T value) {
- if (value == null) {
- this.value = Either.right(Null.INSTANCE);
- } else {
- this.value = Either.left(Optional.of(value));
- }
- }
-
- public static Nullable ofNull() {
- return new Nullable<>(null);
- }
-
- public static Nullable of(T value) {
- return new Nullable<>(value);
- }
-
- public static Nullable empty() {
- return new Nullable<>();
- }
-
- public static Nullable ofOptional(Optional value) {
- if (value.isPresent()) {
- return of(value.get());
- } else {
- return empty();
- }
- }
-
- public boolean isNull() {
- return this.value.isRight();
- }
-
- public boolean isEmpty() {
- return this.value.isLeft() && !this.value.getLeft().isPresent();
- }
-
- public T get() {
- if (this.isNull()) {
- return null;
- }
-
- return this.value.getLeft().get();
- }
-
- public Nullable map(Function super T, ? extends U> mapper) {
- if (this.isNull()) {
- return Nullable.ofNull();
- }
-
- return Nullable.ofOptional(this.value.getLeft().map(mapper));
- }
-
- @Override
- public boolean equals(Object other) {
- if (!(other instanceof Nullable)) {
- return false;
- }
-
- if (((Nullable>) other).isNull() && this.isNull()) {
- return true;
- }
-
- return this.value.getLeft().equals(((Nullable>) other).value.getLeft());
- }
-
- private static final class Either {
- private L left = null;
- private R right = null;
-
- private Either(L left, R right) {
- if (left != null && right != null) {
- throw new IllegalArgumentException("Left and right argument cannot both be non-null.");
- }
-
- if (left == null && right == null) {
- throw new IllegalArgumentException("Left and right argument cannot both be null.");
- }
-
- if (left != null) {
- this.left = left;
- }
-
- if (right != null) {
- this.right = right;
- }
- }
-
- public static Either left(L left) {
- return new Either<>(left, null);
- }
-
- public static Either right(R right) {
- return new Either<>(null, right);
- }
-
- public boolean isLeft() {
- return this.left != null;
- }
-
- public boolean isRight() {
- return this.right != null;
- }
-
- public L getLeft() {
- if (!this.isLeft()) {
- throw new IllegalArgumentException("Cannot get left from right Either.");
- }
- return this.left;
- }
-
- public R getRight() {
- if (!this.isRight()) {
- throw new IllegalArgumentException("Cannot get right from left Either.");
- }
- return this.right;
- }
- }
-
- private static final class Null {
- private static final Null INSTANCE = new Null();
-
- private Null() {}
- }
-}
diff --git a/src/main/java/com/cohere/api/core/NullableNonemptyFilter.java b/src/main/java/com/cohere/api/core/NullableNonemptyFilter.java
deleted file mode 100644
index 62644da..0000000
--- a/src/main/java/com/cohere/api/core/NullableNonemptyFilter.java
+++ /dev/null
@@ -1,19 +0,0 @@
-/**
- * This file was auto-generated by Fern from our API Definition.
- */
-package com.cohere.api.core;
-
-import java.util.Optional;
-
-public final class NullableNonemptyFilter {
- @Override
- public boolean equals(Object o) {
- boolean isOptionalEmpty = isOptionalEmpty(o);
-
- return isOptionalEmpty;
- }
-
- private boolean isOptionalEmpty(Object o) {
- return o instanceof Optional && !((Optional>) o).isPresent();
- }
-}
diff --git a/src/main/java/com/cohere/api/core/QueryStringMapper.java b/src/main/java/com/cohere/api/core/QueryStringMapper.java
deleted file mode 100644
index a28f971..0000000
--- a/src/main/java/com/cohere/api/core/QueryStringMapper.java
+++ /dev/null
@@ -1,142 +0,0 @@
-/**
- * This file was auto-generated by Fern from our API Definition.
- */
-package com.cohere.api.core;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import java.util.AbstractMap;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import okhttp3.HttpUrl;
-import okhttp3.MultipartBody;
-
-public class QueryStringMapper {
-
- private static final ObjectMapper MAPPER = ObjectMappers.JSON_MAPPER;
-
- public static void addQueryParameter(HttpUrl.Builder httpUrl, String key, Object value, boolean arraysAsRepeats) {
- JsonNode valueNode = MAPPER.valueToTree(value);
-
- List> flat;
- if (valueNode.isObject()) {
- flat = flattenObject((ObjectNode) valueNode, arraysAsRepeats);
- } else if (valueNode.isArray()) {
- flat = flattenArray((ArrayNode) valueNode, "", arraysAsRepeats);
- } else {
- if (valueNode.isTextual()) {
- httpUrl.addQueryParameter(key, valueNode.textValue());
- } else {
- httpUrl.addQueryParameter(key, valueNode.toString());
- }
- return;
- }
-
- for (Map.Entry field : flat) {
- if (field.getValue().isTextual()) {
- httpUrl.addQueryParameter(key + field.getKey(), field.getValue().textValue());
- } else {
- httpUrl.addQueryParameter(key + field.getKey(), field.getValue().toString());
- }
- }
- }
-
- public static void addFormDataPart(
- MultipartBody.Builder multipartBody, String key, Object value, boolean arraysAsRepeats) {
- JsonNode valueNode = MAPPER.valueToTree(value);
-
- List> flat;
- if (valueNode.isObject()) {
- flat = flattenObject((ObjectNode) valueNode, arraysAsRepeats);
- } else if (valueNode.isArray()) {
- flat = flattenArray((ArrayNode) valueNode, "", arraysAsRepeats);
- } else {
- if (valueNode.isTextual()) {
- multipartBody.addFormDataPart(key, valueNode.textValue());
- } else {
- multipartBody.addFormDataPart(key, valueNode.toString());
- }
- return;
- }
-
- for (Map.Entry field : flat) {
- if (field.getValue().isTextual()) {
- multipartBody.addFormDataPart(
- key + field.getKey(), field.getValue().textValue());
- } else {
- multipartBody.addFormDataPart(
- key + field.getKey(), field.getValue().toString());
- }
- }
- }
-
- public static List> flattenObject(ObjectNode object, boolean arraysAsRepeats) {
- List> flat = new ArrayList<>();
-
- Iterator> fields = object.fields();
- while (fields.hasNext()) {
- Map.Entry field = fields.next();
-
- String key = "[" + field.getKey() + "]";
-
- if (field.getValue().isObject()) {
- List> flatField =
- flattenObject((ObjectNode) field.getValue(), arraysAsRepeats);
- addAll(flat, flatField, key);
- } else if (field.getValue().isArray()) {
- List> flatField =
- flattenArray((ArrayNode) field.getValue(), key, arraysAsRepeats);
- addAll(flat, flatField, "");
- } else {
- flat.add(new AbstractMap.SimpleEntry<>(key, field.getValue()));
- }
- }
-
- return flat;
- }
-
- private static List> flattenArray(
- ArrayNode array, String key, boolean arraysAsRepeats) {
- List> flat = new ArrayList<>();
-
- Iterator elements = array.elements();
-
- int index = 0;
- while (elements.hasNext()) {
- JsonNode element = elements.next();
-
- String indexKey = key + "[" + index + "]";
-
- if (arraysAsRepeats) {
- indexKey = key;
- }
-
- if (element.isObject()) {
- List> flatField = flattenObject((ObjectNode) element, arraysAsRepeats);
- addAll(flat, flatField, indexKey);
- } else if (element.isArray()) {
- List> flatField = flattenArray((ArrayNode) element, "", arraysAsRepeats);
- addAll(flat, flatField, indexKey);
- } else {
- flat.add(new AbstractMap.SimpleEntry<>(indexKey, element));
- }
-
- index++;
- }
-
- return flat;
- }
-
- private static void addAll(
- List> target, List> source, String prefix) {
- for (Map.Entry entry : source) {
- Map.Entry entryToAdd =
- new AbstractMap.SimpleEntry<>(prefix + entry.getKey(), entry.getValue());
- target.add(entryToAdd);
- }
- }
-}
diff --git a/src/main/java/com/cohere/api/core/RequestOptions.java b/src/main/java/com/cohere/api/core/RequestOptions.java
index fa9e403..d88c8e9 100644
--- a/src/main/java/com/cohere/api/core/RequestOptions.java
+++ b/src/main/java/com/cohere/api/core/RequestOptions.java
@@ -7,7 +7,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
-import java.util.function.Supplier;
public final class RequestOptions {
private final String token;
@@ -18,23 +17,11 @@ public final class RequestOptions {
private final TimeUnit timeoutTimeUnit;
- private final Map headers;
-
- private final Map> headerSuppliers;
-
- private RequestOptions(
- String token,
- String clientName,
- Optional timeout,
- TimeUnit timeoutTimeUnit,
- Map headers,
- Map> headerSuppliers) {
+ private RequestOptions(String token, String clientName, Optional timeout, TimeUnit timeoutTimeUnit) {
this.token = token;
this.clientName = clientName;
this.timeout = timeout;
this.timeoutTimeUnit = timeoutTimeUnit;
- this.headers = headers;
- this.headerSuppliers = headerSuppliers;
}
public Optional getTimeout() {
@@ -53,10 +40,6 @@ public Map getHeaders() {
if (this.clientName != null) {
headers.put("X-Client-Name", this.clientName);
}
- headers.putAll(this.headers);
- this.headerSuppliers.forEach((key, supplier) -> {
- headers.put(key, supplier.get());
- });
return headers;
}
@@ -64,7 +47,7 @@ public static Builder builder() {
return new Builder();
}
- public static class Builder {
+ public static final class Builder {
private String token = null;
private String clientName = null;
@@ -73,10 +56,6 @@ public static class Builder {
private TimeUnit timeoutTimeUnit = TimeUnit.SECONDS;
- private final Map headers = new HashMap<>();
-
- private final Map> headerSuppliers = new HashMap<>();
-
public Builder token(String token) {
this.token = token;
return this;
@@ -98,18 +77,8 @@ public Builder timeout(Integer timeout, TimeUnit timeoutTimeUnit) {
return this;
}
- public Builder addHeader(String key, String value) {
- this.headers.put(key, value);
- return this;
- }
-
- public Builder addHeader(String key, Supplier value) {
- this.headerSuppliers.put(key, value);
- return this;
- }
-
public RequestOptions build() {
- return new RequestOptions(token, clientName, timeout, timeoutTimeUnit, headers, headerSuppliers);
+ return new RequestOptions(token, clientName, timeout, timeoutTimeUnit);
}
}
}
diff --git a/src/main/java/com/cohere/api/core/RetryInterceptor.java b/src/main/java/com/cohere/api/core/RetryInterceptor.java
index 5b17329..97336fa 100644
--- a/src/main/java/com/cohere/api/core/RetryInterceptor.java
+++ b/src/main/java/com/cohere/api/core/RetryInterceptor.java
@@ -52,7 +52,7 @@ private Response retryChain(Response response, Chain chain) throws IOException {
}
private static boolean shouldRetry(int statusCode) {
- return statusCode == 408 || statusCode == 429 || statusCode >= 500;
+ return statusCode == 408 || statusCode == 409 || statusCode == 429 || statusCode >= 500;
}
private final class ExponentialBackoff {
diff --git a/src/main/java/com/cohere/api/core/Stream.java b/src/main/java/com/cohere/api/core/Stream.java
index 9b1c240..6309a2e 100644
--- a/src/main/java/com/cohere/api/core/Stream.java
+++ b/src/main/java/com/cohere/api/core/Stream.java
@@ -3,41 +3,29 @@
*/
package com.cohere.api.core;
-import java.io.Closeable;
-import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
- * The {@code Stream} class implements {@link Iterable} to provide a simple mechanism for reading and parsing
+ * The {@code Stream} class implmenets {@link Iterable} to provide a simple mechanism for reading and parsing
* objects of a given type from data streamed via a {@link Reader} using a specified delimiter.
*
* {@code Stream} assumes that data is being pushed to the provided {@link Reader} asynchronously and utilizes a
* {@code Scanner} to block during iteration if the next object is not available.
- * Iterable stream for parsing JSON and Server-Sent Events (SSE) data.
- * Supports both newline-delimited JSON and SSE with optional stream termination.
*
* @param The type of objects in the stream.
*/
-public final class Stream implements Iterable, Closeable {
-
- private static final String NEWLINE = "\n";
- private static final String DATA_PREFIX = "data:";
-
- public enum StreamType {
- JSON,
- SSE
- }
-
+public final class Stream implements Iterable {
+ /**
+ * The {@link Class} of the objects in the stream.
+ */
private final Class valueType;
+ /**
+ * The {@link Scanner} used for reading from the input stream and blocking when neede during iteration.
+ */
private final Scanner scanner;
- private final StreamType streamType;
- private final String messageTerminator;
- private final String streamTerminator;
- private final Reader sseReader;
- private boolean isClosed = false;
/**
* Constructs a new {@code Stream} with the specified value type, reader, and delimiter.
@@ -47,61 +35,8 @@ public enum StreamType {
* @param delimiter The delimiter used to separate elements in the stream.
*/
public Stream(Class valueType, Reader reader, String delimiter) {
- this.valueType = valueType;
this.scanner = new Scanner(reader).useDelimiter(delimiter);
- this.streamType = StreamType.JSON;
- this.messageTerminator = delimiter;
- this.streamTerminator = null;
- this.sseReader = null;
- }
-
- private Stream(Class valueType, StreamType type, Reader reader, String terminator) {
this.valueType = valueType;
- this.streamType = type;
- if (type == StreamType.JSON) {
- this.scanner = new Scanner(reader).useDelimiter(terminator);
- this.messageTerminator = terminator;
- this.streamTerminator = null;
- this.sseReader = null;
- } else {
- this.scanner = null;
- this.messageTerminator = NEWLINE;
- this.streamTerminator = terminator;
- this.sseReader = reader;
- }
- }
-
- public static Stream fromJson(Class valueType, Reader reader, String delimiter) {
- return new Stream<>(valueType, reader, delimiter);
- }
-
- public static Stream fromJson(Class valueType, Reader reader) {
- return new Stream<>(valueType, reader, NEWLINE);
- }
-
- public static Stream fromSse(Class valueType, Reader sseReader) {
- return new Stream<>(valueType, StreamType.SSE, sseReader, null);
- }
-
- public static Stream fromSse(Class valueType, Reader sseReader, String streamTerminator) {
- return new Stream<>(valueType, StreamType.SSE, sseReader, streamTerminator);
- }
-
- @Override
- public void close() throws IOException {
- if (!isClosed) {
- isClosed = true;
- if (scanner != null) {
- scanner.close();
- }
- if (sseReader != null) {
- sseReader.close();
- }
- }
- }
-
- private boolean isStreamClosed() {
- return isClosed;
}
/**
@@ -112,161 +47,51 @@ private boolean isStreamClosed() {
*/
@Override
public Iterator iterator() {
- if (streamType == StreamType.SSE) {
- return new SSEIterator();
- } else {
- return new JsonIterator();
- }
- }
-
- private final class JsonIterator implements Iterator {
-
- /**
- * Returns {@code true} if there are more elements in the stream.
- *
- * Will block and wait for input if the stream has not ended and the next object is not yet available.
- *
- * @return {@code true} if there are more elements, {@code false} otherwise.
- */
- @Override
- public boolean hasNext() {
- if (isStreamClosed()) {
- return false;
+ return new Iterator() {
+ /**
+ * Returns {@code true} if there are more elements in the stream.
+ *
+ * Will block and wait for input if the stream has not ended and the next object is not yet available.
+ *
+ * @return {@code true} if there are more elements, {@code false} otherwise.
+ */
+ @Override
+ public boolean hasNext() {
+ return scanner.hasNext();
}
- return scanner.hasNext();
- }
- /**
- * Returns the next element in the stream.
- *
- * Will block and wait for input if the stream has not ended and the next object is not yet available.
- *
- * @return The next element in the stream.
- * @throws NoSuchElementException If there are no more elements in the stream.
- */
- @Override
- public T next() {
- if (isStreamClosed()) {
- throw new NoSuchElementException("Stream is closed");
- }
-
- if (!scanner.hasNext()) {
- throw new NoSuchElementException();
- } else {
- try {
- T parsedResponse =
- ObjectMappers.JSON_MAPPER.readValue(scanner.next().trim(), valueType);
- return parsedResponse;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
- }
-
- private final class SSEIterator implements Iterator {
- private Scanner sseScanner;
- private T nextItem;
- private boolean hasNextItem = false;
- private boolean endOfStream = false;
- private StringBuilder buffer = new StringBuilder();
- private boolean prefixSeen = false;
-
- private SSEIterator() {
- if (sseReader != null && !isStreamClosed()) {
- this.sseScanner = new Scanner(sseReader);
- } else {
- this.endOfStream = true;
- }
- }
-
- @Override
- public boolean hasNext() {
- if (isStreamClosed() || endOfStream) {
- return false;
- }
-
- if (hasNextItem) {
- return true;
- }
-
- return readNextMessage();
- }
-
- @Override
- public T next() {
- if (!hasNext()) {
- throw new NoSuchElementException("No more elements in stream");
- }
-
- T result = nextItem;
- nextItem = null;
- hasNextItem = false;
- return result;
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- private boolean readNextMessage() {
- if (sseScanner == null || isStreamClosed()) {
- endOfStream = true;
- return false;
- }
-
- try {
- while (sseScanner.hasNextLine()) {
- String chunk = sseScanner.nextLine();
- buffer.append(chunk).append(NEWLINE);
-
- int terminatorIndex;
- while ((terminatorIndex = buffer.indexOf(messageTerminator)) >= 0) {
- String line = buffer.substring(0, terminatorIndex + messageTerminator.length());
- buffer.delete(0, terminatorIndex + messageTerminator.length());
-
- line = line.trim();
- if (line.isEmpty()) {
- continue;
- }
-
- if (!prefixSeen && line.startsWith(DATA_PREFIX)) {
- prefixSeen = true;
- line = line.substring(DATA_PREFIX.length()).trim();
- } else if (!prefixSeen) {
- continue;
- }
-
- if (streamTerminator != null && line.contains(streamTerminator)) {
- endOfStream = true;
- return false;
- }
-
- try {
- nextItem = ObjectMappers.JSON_MAPPER.readValue(line, valueType);
- hasNextItem = true;
- prefixSeen = false;
- return true;
- } catch (Exception parseEx) {
- continue;
- }
+ /**
+ * Returns the next element in the stream.
+ *
+ * Will block and wait for input if the stream has not ended and the next object is not yet available.
+ *
+ * @return The next element in the stream.
+ * @throws NoSuchElementException If there are no more elements in the stream.
+ */
+ @Override
+ public T next() {
+ if (!scanner.hasNext()) {
+ throw new NoSuchElementException();
+ } else {
+ try {
+ T parsedResponse = ObjectMappers.JSON_MAPPER.readValue(
+ scanner.next().trim(), valueType);
+ return parsedResponse;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
}
}
+ }
- endOfStream = true;
- return false;
-
- } catch (Exception e) {
- System.err.println("Failed to parse SSE stream: " + e.getMessage());
- endOfStream = true;
- return false;
+ /**
+ * Removing elements from {@code Stream} is not supported.
+ *
+ * @throws UnsupportedOperationException Always, as removal is not supported.
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
}
- }
+ };
}
}
diff --git a/src/main/java/com/cohere/api/errors/BadRequestError.java b/src/main/java/com/cohere/api/errors/BadRequestError.java
index 60abf96..6546e91 100644
--- a/src/main/java/com/cohere/api/errors/BadRequestError.java
+++ b/src/main/java/com/cohere/api/errors/BadRequestError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class BadRequestError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public BadRequestError(Object body) {
this.body = body;
}
- public BadRequestError(Object body, Response rawResponse) {
- super("BadRequestError", 400, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/ClientClosedRequestError.java b/src/main/java/com/cohere/api/errors/ClientClosedRequestError.java
index cc78e15..89a0fc4 100644
--- a/src/main/java/com/cohere/api/errors/ClientClosedRequestError.java
+++ b/src/main/java/com/cohere/api/errors/ClientClosedRequestError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class ClientClosedRequestError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public ClientClosedRequestError(Object body) {
this.body = body;
}
- public ClientClosedRequestError(Object body, Response rawResponse) {
- super("ClientClosedRequestError", 499, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/ForbiddenError.java b/src/main/java/com/cohere/api/errors/ForbiddenError.java
index a139acc..23c461b 100644
--- a/src/main/java/com/cohere/api/errors/ForbiddenError.java
+++ b/src/main/java/com/cohere/api/errors/ForbiddenError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class ForbiddenError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public ForbiddenError(Object body) {
this.body = body;
}
- public ForbiddenError(Object body, Response rawResponse) {
- super("ForbiddenError", 403, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/GatewayTimeoutError.java b/src/main/java/com/cohere/api/errors/GatewayTimeoutError.java
index 3e28c9d..bf513ef 100644
--- a/src/main/java/com/cohere/api/errors/GatewayTimeoutError.java
+++ b/src/main/java/com/cohere/api/errors/GatewayTimeoutError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class GatewayTimeoutError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public GatewayTimeoutError(Object body) {
this.body = body;
}
- public GatewayTimeoutError(Object body, Response rawResponse) {
- super("GatewayTimeoutError", 504, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/InternalServerError.java b/src/main/java/com/cohere/api/errors/InternalServerError.java
index 3e5fbdb..5a64c17 100644
--- a/src/main/java/com/cohere/api/errors/InternalServerError.java
+++ b/src/main/java/com/cohere/api/errors/InternalServerError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class InternalServerError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public InternalServerError(Object body) {
this.body = body;
}
- public InternalServerError(Object body, Response rawResponse) {
- super("InternalServerError", 500, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/InvalidTokenError.java b/src/main/java/com/cohere/api/errors/InvalidTokenError.java
index eb1caa1..b889d1b 100644
--- a/src/main/java/com/cohere/api/errors/InvalidTokenError.java
+++ b/src/main/java/com/cohere/api/errors/InvalidTokenError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class InvalidTokenError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public InvalidTokenError(Object body) {
this.body = body;
}
- public InvalidTokenError(Object body, Response rawResponse) {
- super("InvalidTokenError", 498, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/NotFoundError.java b/src/main/java/com/cohere/api/errors/NotFoundError.java
index 6ec9e4c..37f7e9d 100644
--- a/src/main/java/com/cohere/api/errors/NotFoundError.java
+++ b/src/main/java/com/cohere/api/errors/NotFoundError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class NotFoundError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public NotFoundError(Object body) {
this.body = body;
}
- public NotFoundError(Object body, Response rawResponse) {
- super("NotFoundError", 404, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/NotImplementedError.java b/src/main/java/com/cohere/api/errors/NotImplementedError.java
index dc436b9..7ada371 100644
--- a/src/main/java/com/cohere/api/errors/NotImplementedError.java
+++ b/src/main/java/com/cohere/api/errors/NotImplementedError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class NotImplementedError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public NotImplementedError(Object body) {
this.body = body;
}
- public NotImplementedError(Object body, Response rawResponse) {
- super("NotImplementedError", 501, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/ServiceUnavailableError.java b/src/main/java/com/cohere/api/errors/ServiceUnavailableError.java
index 95dbf3e..a615b11 100644
--- a/src/main/java/com/cohere/api/errors/ServiceUnavailableError.java
+++ b/src/main/java/com/cohere/api/errors/ServiceUnavailableError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class ServiceUnavailableError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public ServiceUnavailableError(Object body) {
this.body = body;
}
- public ServiceUnavailableError(Object body, Response rawResponse) {
- super("ServiceUnavailableError", 503, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/TooManyRequestsError.java b/src/main/java/com/cohere/api/errors/TooManyRequestsError.java
index bbff67a..d1f4f4b 100644
--- a/src/main/java/com/cohere/api/errors/TooManyRequestsError.java
+++ b/src/main/java/com/cohere/api/errors/TooManyRequestsError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class TooManyRequestsError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public TooManyRequestsError(Object body) {
this.body = body;
}
- public TooManyRequestsError(Object body, Response rawResponse) {
- super("TooManyRequestsError", 429, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/UnauthorizedError.java b/src/main/java/com/cohere/api/errors/UnauthorizedError.java
index cb6815d..6a3df32 100644
--- a/src/main/java/com/cohere/api/errors/UnauthorizedError.java
+++ b/src/main/java/com/cohere/api/errors/UnauthorizedError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class UnauthorizedError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public UnauthorizedError(Object body) {
this.body = body;
}
- public UnauthorizedError(Object body, Response rawResponse) {
- super("UnauthorizedError", 401, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/errors/UnprocessableEntityError.java b/src/main/java/com/cohere/api/errors/UnprocessableEntityError.java
index 83fa51f..96d95e1 100644
--- a/src/main/java/com/cohere/api/errors/UnprocessableEntityError.java
+++ b/src/main/java/com/cohere/api/errors/UnprocessableEntityError.java
@@ -4,7 +4,6 @@
package com.cohere.api.errors;
import com.cohere.api.core.CohereApiException;
-import okhttp3.Response;
public final class UnprocessableEntityError extends CohereApiException {
/**
@@ -17,11 +16,6 @@ public UnprocessableEntityError(Object body) {
this.body = body;
}
- public UnprocessableEntityError(Object body, Response rawResponse) {
- super("UnprocessableEntityError", 422, body, rawResponse);
- this.body = body;
- }
-
/**
* @return the body
*/
diff --git a/src/main/java/com/cohere/api/requests/ChatRequest.java b/src/main/java/com/cohere/api/requests/ChatRequest.java
index 5c4e8d7..4125d21 100644
--- a/src/main/java/com/cohere/api/requests/ChatRequest.java
+++ b/src/main/java/com/cohere/api/requests/ChatRequest.java
@@ -70,6 +70,10 @@ public final class ChatRequest {
private final Optional presencePenalty;
+ private final Optional rawPrompting;
+
+ private final Optional returnPrompt;
+
private final Optional> tools;
private final Optional> toolResults;
@@ -103,6 +107,8 @@ private ChatRequest(
Optional> stopSequences,
Optional frequencyPenalty,
Optional presencePenalty,
+ Optional rawPrompting,
+ Optional returnPrompt,
Optional> tools,
Optional> toolResults,
Optional forceSingleStep,
@@ -129,6 +135,8 @@ private ChatRequest(
this.stopSequences = stopSequences;
this.frequencyPenalty = frequencyPenalty;
this.presencePenalty = presencePenalty;
+ this.rawPrompting = rawPrompting;
+ this.returnPrompt = returnPrompt;
this.tools = tools;
this.toolResults = toolResults;
this.forceSingleStep = forceSingleStep;
@@ -363,6 +371,24 @@ public Optional getPresencePenalty() {
return presencePenalty;
}
+ /**
+ * @return When enabled, the user's prompt will be sent to the model without
+ * any pre-processing.
+ * Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
+ */
+ @JsonProperty("raw_prompting")
+ public Optional getRawPrompting() {
+ return rawPrompting;
+ }
+
+ /**
+ * @return The prompt is returned in the prompt response field when this is enabled.
+ */
+ @JsonProperty("return_prompt")
+ public Optional getReturnPrompt() {
+ return returnPrompt;
+ }
+
/**
* @return A list of available tools (functions) that the model may suggest invoking before producing a text response.
* When tools is passed (without tool_results), the text field in the response will be "" and the tool_calls field in the response will be populated with a list of tool calls that need to be made. If no calls need to be made, the tool_calls array will be empty.
@@ -458,6 +484,8 @@ private boolean equalTo(ChatRequest other) {
&& stopSequences.equals(other.stopSequences)
&& frequencyPenalty.equals(other.frequencyPenalty)
&& presencePenalty.equals(other.presencePenalty)
+ && rawPrompting.equals(other.rawPrompting)
+ && returnPrompt.equals(other.returnPrompt)
&& tools.equals(other.tools)
&& toolResults.equals(other.toolResults)
&& forceSingleStep.equals(other.forceSingleStep)
@@ -488,6 +516,8 @@ public int hashCode() {
this.stopSequences,
this.frequencyPenalty,
this.presencePenalty,
+ this.rawPrompting,
+ this.returnPrompt,
this.tools,
this.toolResults,
this.forceSingleStep,
@@ -505,10 +535,6 @@ public static MessageStage builder() {
}
public interface MessageStage {
- /**
- * Text input for the model to respond to.
- * Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
- */
_FinalStage message(@NotNull String message);
Builder from(ChatRequest other);
@@ -517,231 +543,98 @@ public interface MessageStage {
public interface _FinalStage {
ChatRequest build();
- /**
- * Pass text/event-stream to receive the streamed response as server-sent events. The default is \n delimited events.
- */
_FinalStage accepts(Optional accepts);
_FinalStage accepts(String accepts);
- /**
- * The name of a compatible Cohere model or the ID of a fine-tuned model.
- * Compatible Deployments: Cohere Platform, Private Deployments
- */
_FinalStage model(Optional model);
_FinalStage model(String model);
- /**
- * When specified, the default Cohere preamble will be replaced with the provided one. Preambles are a part of the prompt used to adjust the model's overall behavior and conversation style, and use the SYSTEM role.
- * The SYSTEM role is also used for the contents of the optional chat_history= parameter. When used with the chat_history= parameter it adds content throughout a conversation. Conversely, when used with the preamble= parameter it adds content at the start of the conversation only.
- * Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
- */
_FinalStage preamble(Optional preamble);
_FinalStage preamble(String preamble);
- /**
- * A list of previous messages between the user and the model, giving the model conversational context for responding to the user's message.
- * Each item represents a single message in the chat history, excluding the current user turn. It has two properties: role and message. The role identifies the sender (CHATBOT, SYSTEM, or USER), while the message contains the text content.
- * The chat_history parameter should not be used for SYSTEM messages in most cases. Instead, to add a SYSTEM role message at the beginning of a conversation, the preamble parameter should be used.
- * Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
- */
_FinalStage chatHistory(Optional> chatHistory);
_FinalStage chatHistory(List chatHistory);
- /**
- * An alternative to chat_history.
- * Providing a conversation_id creates or resumes a persisted conversation with the specified ID. The ID can be any non empty string.
- * Compatible Deployments: Cohere Platform
- */
_FinalStage conversationId(Optional conversationId);
_FinalStage conversationId(String conversationId);
- /**
- * Defaults to AUTO when connectors are specified and OFF in all other cases.
- * Dictates how the prompt will be constructed.
- * With prompt_truncation set to "AUTO", some elements from chat_history and documents will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be changed and ranked by relevance.
- * With prompt_truncation set to "AUTO_PRESERVE_ORDER", some elements from chat_history and documents will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be preserved as they are inputted into the API.
- * With prompt_truncation set to "OFF", no elements will be dropped. If the sum of the inputs exceeds the model's context length limit, a TooManyTokens error will be returned.
- * Compatible Deployments:
- *
- * - AUTO: Cohere Platform Only
- * - AUTO_PRESERVE_ORDER: Azure, AWS Sagemaker/Bedrock, Private Deployments
- *
- */
_FinalStage promptTruncation(Optional promptTruncation);
_FinalStage promptTruncation(ChatRequestPromptTruncation promptTruncation);
- /**
- * Accepts {"id": "web-search"}, and/or the "id" for a custom connector, if you've created one.
- * When specified, the model's reply will be enriched with information found by querying each of the connectors (RAG).
- * Compatible Deployments: Cohere Platform
- */
_FinalStage connectors(Optional> connectors);
_FinalStage connectors(List connectors);
- /**
- * Defaults to false.
- * When true, the response will only contain a list of generated search queries, but no search will take place, and no reply from the model to the user's message will be generated.
- * Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
- */
_FinalStage searchQueriesOnly(Optional searchQueriesOnly);
_FinalStage searchQueriesOnly(Boolean searchQueriesOnly);
- /**
- * A list of relevant documents that the model can cite to generate a more accurate reply. Each document is a string-string dictionary.
- * Example:
- * [
- * { "title": "Tall penguins", "text": "Emperor penguins are the tallest." },
- * { "title": "Penguin habitats", "text": "Emperor penguins only live in Antarctica." },
- * ]
- *
- * Keys and values from each document will be serialized to a string and passed to the model. The resulting generation will include citations that reference some of these documents.
- * Some suggested keys are "text", "author", and "date". For better generation quality, it is recommended to keep the total word count of the strings in the dictionary to under 300 words.
- * An id field (string) can be optionally supplied to identify the document in the citations. This field will not be passed to the model.
- * An _excludes field (array of strings) can be optionally supplied to omit some key-value pairs from being shown to the model. The omitted fields will still show up in the citation object. The "_excludes" field will not be passed to the model.
- * See 'Document Mode' in the guide for more information.
- * Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments
- */
_FinalStage documents(Optional>> documents);
_FinalStage documents(List