Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌿 Fern Regeneration -- September 13, 2024 #30

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ publishing {
maven(MavenPublication) {
groupId = 'com.cohere'
artifactId = 'cohere-java'
version = '1.3.1'
version = '1.3.2'
from components.java
pom {
name = 'cohere'
Expand Down
197 changes: 185 additions & 12 deletions src/main/java/com/cohere/api/Cohere.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import com.cohere.api.types.UnprocessableEntityErrorBody;
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;
Expand Down Expand Up @@ -103,19 +105,100 @@ public Iterable<StreamedChatResponse> chatStream(ChatStreamRequest request, Requ
.newBuilder()
.addPathSegments("v1/chat")
.build();
Map<String, Object> 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(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new CohereApiError("Failed to serialize request", e);
ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaTypes.APPLICATION_JSON);
} catch (Exception e) {
throw new RuntimeException(e);
}
Request okhttpRequest = new Request.Builder()
Request.Builder _requestBuilder = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.build();
.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);
Expand Down Expand Up @@ -191,19 +274,100 @@ public NonStreamedChatResponse chat(ChatRequest request, RequestOptions requestO
.newBuilder()
.addPathSegments("v1/chat")
.build();
Map<String, Object> 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(request), MediaTypes.APPLICATION_JSON);
} catch (JsonProcessingException e) {
throw new CohereApiError("Failed to serialize request", e);
ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaTypes.APPLICATION_JSON);
} catch (Exception e) {
throw new RuntimeException(e);
}
Request okhttpRequest = new Request.Builder()
Request.Builder _requestBuilder = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.build();
.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);
Expand Down Expand Up @@ -448,6 +612,15 @@ public Generation generate(GenerateRequest request, RequestOptions requestOption
}
}

/**
* This endpoint returns text embeddings. An embedding is a list of floating point numbers that captures semantic information about the text that it represents.
* <p>Embeddings can be used to create text classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.</p>
* <p>If you want to learn more how to use the embedding model, have a look at the <a href="/docs/semantic-search">Semantic Search Guide</a>.</p>
*/
public EmbedResponse embed() {
return embed(EmbedRequest.builder().build());
}

/**
* This endpoint returns text embeddings. An embedding is a list of floating point numbers that captures semantic information about the text that it represents.
* <p>Embeddings can be used to create text classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.</p>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cohere/api/core/ClientOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private ClientOptions(
{
put("X-Fern-Language", "JAVA");
put("X-Fern-SDK-Name", "com.cohere.fern:api-sdk");
put("X-Fern-SDK-Version", "1.3.1");
put("X-Fern-SDK-Version", "1.3.2");
}
});
this.headerSuppliers = headerSuppliers;
Expand Down
41 changes: 40 additions & 1 deletion src/main/java/com/cohere/api/requests/ChatRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonDeserialize(builder = ChatRequest.Builder.class)
public final class ChatRequest {
private final Optional<String> accepts;

private final String message;

private final Optional<String> model;
Expand Down Expand Up @@ -84,6 +86,7 @@ public final class ChatRequest {
private final Map<String, Object> additionalProperties;

private ChatRequest(
Optional<String> accepts,
String message,
Optional<String> model,
Optional<String> preamble,
Expand Down Expand Up @@ -111,6 +114,7 @@ private ChatRequest(
Optional<ResponseFormat> responseFormat,
Optional<ChatRequestSafetyMode> safetyMode,
Map<String, Object> additionalProperties) {
this.accepts = accepts;
this.message = message;
this.model = model;
this.preamble = preamble;
Expand Down Expand Up @@ -140,6 +144,14 @@ private ChatRequest(
this.additionalProperties = additionalProperties;
}

/**
* @return Pass text/event-stream to receive the streamed response as server-sent events. The default is <code>\n</code> delimited events.
*/
@JsonProperty("Accepts")
public Optional<String> getAccepts() {
return accepts;
}

/**
* @return Text input for the model to respond to.
* <p>Compatible Deployments: Cohere Platform, Azure, AWS Sagemaker/Bedrock, Private Deployments</p>
Expand Down Expand Up @@ -451,7 +463,8 @@ public Map<String, Object> getAdditionalProperties() {
}

private boolean equalTo(ChatRequest other) {
return message.equals(other.message)
return accepts.equals(other.accepts)
&& message.equals(other.message)
&& model.equals(other.model)
&& preamble.equals(other.preamble)
&& chatHistory.equals(other.chatHistory)
Expand Down Expand Up @@ -482,6 +495,7 @@ private boolean equalTo(ChatRequest other) {
@java.lang.Override
public int hashCode() {
return Objects.hash(
this.accepts,
this.message,
this.model,
this.preamble,
Expand Down Expand Up @@ -528,6 +542,10 @@ public interface MessageStage {
public interface _FinalStage {
ChatRequest build();

_FinalStage accepts(Optional<String> accepts);

_FinalStage accepts(String accepts);

_FinalStage model(Optional<String> model);

_FinalStage model(String model);
Expand Down Expand Up @@ -683,13 +701,16 @@ public static final class Builder implements MessageStage, _FinalStage {

private Optional<String> model = Optional.empty();

private Optional<String> accepts = Optional.empty();

@JsonAnySetter
private Map<String, Object> additionalProperties = new HashMap<>();

private Builder() {}

@java.lang.Override
public Builder from(ChatRequest other) {
accepts(other.getAccepts());
message(other.getMessage());
model(other.getModel());
preamble(other.getPreamble());
Expand Down Expand Up @@ -1234,9 +1255,27 @@ public _FinalStage model(Optional<String> model) {
return this;
}

/**
* <p>Pass text/event-stream to receive the streamed response as server-sent events. The default is <code>\n</code> delimited events.</p>
* @return Reference to {@code this} so that method calls can be chained together.
*/
@java.lang.Override
public _FinalStage accepts(String accepts) {
this.accepts = Optional.of(accepts);
return this;
}

@java.lang.Override
@JsonSetter(value = "Accepts", nulls = Nulls.SKIP)
public _FinalStage accepts(Optional<String> accepts) {
this.accepts = accepts;
return this;
}

@java.lang.Override
public ChatRequest build() {
return new ChatRequest(
accepts,
message,
model,
preamble,
Expand Down
Loading
Loading