com.cohere.api.requests.ChatRequest Maven / Gradle / Ivy
Show all versions of cohere-java Show documentation
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.cohere.api.requests;
import com.cohere.api.core.ObjectMappers;
import com.cohere.api.types.ChatConnector;
import com.cohere.api.types.ChatRequestCitationQuality;
import com.cohere.api.types.ChatRequestPromptTruncation;
import com.cohere.api.types.Message;
import com.cohere.api.types.Tool;
import com.cohere.api.types.ToolResult;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonDeserialize(builder = ChatRequest.Builder.class)
public final class ChatRequest {
private final String message;
private final Optional model;
private final Optional preamble;
private final Optional> chatHistory;
private final Optional conversationId;
private final Optional promptTruncation;
private final Optional> connectors;
private final Optional searchQueriesOnly;
private final Optional>> documents;
private final Optional citationQuality;
private final Optional temperature;
private final Optional maxTokens;
private final Optional maxInputTokens;
private final Optional k;
private final Optional p;
private final Optional seed;
private final Optional> stopSequences;
private final Optional frequencyPenalty;
private final Optional presencePenalty;
private final Optional rawPrompting;
private final Optional returnPrompt;
private final Optional> tools;
private final Optional> toolResults;
private final Map additionalProperties;
private ChatRequest(
String message,
Optional model,
Optional preamble,
Optional> chatHistory,
Optional conversationId,
Optional promptTruncation,
Optional> connectors,
Optional searchQueriesOnly,
Optional>> documents,
Optional citationQuality,
Optional temperature,
Optional maxTokens,
Optional maxInputTokens,
Optional k,
Optional p,
Optional seed,
Optional> stopSequences,
Optional frequencyPenalty,
Optional presencePenalty,
Optional rawPrompting,
Optional returnPrompt,
Optional> tools,
Optional> toolResults,
Map additionalProperties) {
this.message = message;
this.model = model;
this.preamble = preamble;
this.chatHistory = chatHistory;
this.conversationId = conversationId;
this.promptTruncation = promptTruncation;
this.connectors = connectors;
this.searchQueriesOnly = searchQueriesOnly;
this.documents = documents;
this.citationQuality = citationQuality;
this.temperature = temperature;
this.maxTokens = maxTokens;
this.maxInputTokens = maxInputTokens;
this.k = k;
this.p = p;
this.seed = seed;
this.stopSequences = stopSequences;
this.frequencyPenalty = frequencyPenalty;
this.presencePenalty = presencePenalty;
this.rawPrompting = rawPrompting;
this.returnPrompt = returnPrompt;
this.tools = tools;
this.toolResults = toolResults;
this.additionalProperties = additionalProperties;
}
/**
* @return Text input for the model to respond to.
* Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker, Private Deployments
*/
@JsonProperty("message")
public String getMessage() {
return message;
}
/**
* @return Defaults to command-r-plus
.
* The name of a compatible Cohere model or the ID of a fine-tuned model.
* Compatible Deployments: Cohere Platform, Private Deployments
*/
@JsonProperty("model")
public Optional getModel() {
return model;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("stream")
public Boolean getStream() {
return false;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("preamble")
public Optional getPreamble() {
return preamble;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("chat_history")
public Optional> getChatHistory() {
return chatHistory;
}
/**
* @return 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
*/
@JsonProperty("conversation_id")
public Optional getConversationId() {
return conversationId;
}
/**
* @return 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: Cohere Platform Only AUTO_PRESERVE_ORDER: Azure, AWS Sagemaker, Private Deployments
*/
@JsonProperty("prompt_truncation")
public Optional getPromptTruncation() {
return promptTruncation;
}
/**
* @return 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 quering each of the connectors (RAG).
* Compatible Deployments: Cohere Platform
*/
@JsonProperty("connectors")
public Optional> getConnectors() {
return connectors;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("search_queries_only")
public Optional getSearchQueriesOnly() {
return searchQueriesOnly;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("documents")
public Optional>> getDocuments() {
return documents;
}
/**
* @return 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 or "fast"
results.
* Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker, Private Deployments
*/
@JsonProperty("citation_quality")
public Optional getCitationQuality() {
return citationQuality;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("temperature")
public Optional getTemperature() {
return temperature;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("max_tokens")
public Optional getMaxTokens() {
return maxTokens;
}
/**
* @return 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
*/
@JsonProperty("max_input_tokens")
public Optional getMaxInputTokens() {
return maxInputTokens;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("k")
public Optional getK() {
return k;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("p")
public Optional getP() {
return p;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("seed")
public Optional getSeed() {
return seed;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("stop_sequences")
public Optional> getStopSequences() {
return stopSequences;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("frequency_penalty")
public Optional getFrequencyPenalty() {
return frequencyPenalty;
}
/**
* @return 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, Private Deployments
*/
@JsonProperty("presence_penalty")
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, 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.
* Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker, Private Deployments
*/
@JsonProperty("tools")
public Optional> getTools() {
return tools;
}
/**
* @return 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": <tool name>,
* "parameters": {
* <param name>: <param value>
* }
* },
* "outputs": [{
* <key>: <value>
* }]
* },
* ...
* ]
*
* 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, Private Deployments
*/
@JsonProperty("tool_results")
public Optional> getToolResults() {
return toolResults;
}
@java.lang.Override
public boolean equals(Object other) {
if (this == other) return true;
return other instanceof ChatRequest && equalTo((ChatRequest) other);
}
@JsonAnyGetter
public Map getAdditionalProperties() {
return this.additionalProperties;
}
private boolean equalTo(ChatRequest other) {
return message.equals(other.message)
&& model.equals(other.model)
&& preamble.equals(other.preamble)
&& chatHistory.equals(other.chatHistory)
&& conversationId.equals(other.conversationId)
&& promptTruncation.equals(other.promptTruncation)
&& connectors.equals(other.connectors)
&& searchQueriesOnly.equals(other.searchQueriesOnly)
&& documents.equals(other.documents)
&& citationQuality.equals(other.citationQuality)
&& temperature.equals(other.temperature)
&& maxTokens.equals(other.maxTokens)
&& maxInputTokens.equals(other.maxInputTokens)
&& k.equals(other.k)
&& p.equals(other.p)
&& seed.equals(other.seed)
&& 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);
}
@java.lang.Override
public int hashCode() {
return Objects.hash(
this.message,
this.model,
this.preamble,
this.chatHistory,
this.conversationId,
this.promptTruncation,
this.connectors,
this.searchQueriesOnly,
this.documents,
this.citationQuality,
this.temperature,
this.maxTokens,
this.maxInputTokens,
this.k,
this.p,
this.seed,
this.stopSequences,
this.frequencyPenalty,
this.presencePenalty,
this.rawPrompting,
this.returnPrompt,
this.tools,
this.toolResults);
}
@java.lang.Override
public String toString() {
return ObjectMappers.stringify(this);
}
public static MessageStage builder() {
return new Builder();
}
public interface MessageStage {
_FinalStage message(String message);
Builder from(ChatRequest other);
}
public interface _FinalStage {
ChatRequest build();
_FinalStage model(Optional model);
_FinalStage model(String model);
_FinalStage preamble(Optional preamble);
_FinalStage preamble(String preamble);
_FinalStage chatHistory(Optional> chatHistory);
_FinalStage chatHistory(List chatHistory);
_FinalStage conversationId(Optional conversationId);
_FinalStage conversationId(String conversationId);
_FinalStage promptTruncation(Optional promptTruncation);
_FinalStage promptTruncation(ChatRequestPromptTruncation promptTruncation);
_FinalStage connectors(Optional> connectors);
_FinalStage connectors(List connectors);
_FinalStage searchQueriesOnly(Optional searchQueriesOnly);
_FinalStage searchQueriesOnly(Boolean searchQueriesOnly);
_FinalStage documents(Optional>> documents);
_FinalStage documents(List