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 -- January 15, 2024 #9

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 @@ -46,7 +46,7 @@ publishing {
maven(MavenPublication) {
groupId = 'com.cohere'
artifactId = 'cohere-java'
version = '1.0.3'
version = '1.0.5'
from components.java
}
}
Expand Down
78 changes: 66 additions & 12 deletions src/main/java/com/cohere/api/Cohere.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
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.ConnectorsClient;
import com.cohere.api.resources.datasets.DatasetsClient;
import com.cohere.api.types.ClassifyResponse;
import com.cohere.api.types.DetectLanguageResponse;
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;
Expand All @@ -42,10 +45,13 @@
public class Cohere {
protected final ClientOptions clientOptions;

protected final Supplier<DatasetsClient> datasetsClient;

protected final Supplier<ConnectorsClient> connectorsClient;

public Cohere(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
this.datasetsClient = Suppliers.memoize(() -> new DatasetsClient(clientOptions));
this.connectorsClient = Suppliers.memoize(() -> new ConnectorsClient(clientOptions));
}

Expand All @@ -56,7 +62,7 @@ public Cohere(ClientOptions clientOptions) {
public Iterable<StreamedChatResponse> chatStream(ChatStreamRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("chat")
.addPathSegments("v1/chat")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -101,7 +107,7 @@ public Iterable<StreamedChatResponse> chatStream(ChatStreamRequest request) {
public NonStreamedChatResponse chat(ChatRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("chat")
.addPathSegments("v1/chat")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -138,13 +144,57 @@ public NonStreamedChatResponse chat(ChatRequest request) {
return chat(request, null);
}

/**
* This endpoint generates realistic text conditioned on a given input.
*/
public Iterable<GenerateStreamedResponse> 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), MediaType.parse("application/json"));
} catch (Exception e) {
throw new RuntimeException(e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.build();
try {
Response response =
clientOptions.httpClient().newCall(okhttpRequest).execute();
if (response.isSuccessful()) {
return new Stream<GenerateStreamedResponse>(
GenerateStreamedResponse.class, response.body().charStream(), "\n");
}
throw new ApiError(
response.code(),
ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
* This endpoint generates realistic text conditioned on a given input.
*/
public Iterable<GenerateStreamedResponse> generateStream(GenerateStreamRequest request) {
return generateStream(request, null);
}

/**
* This endpoint generates realistic text conditioned on a given input.
*/
public Generation generate(GenerateRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("generate")
.addPathSegments("v1/generate")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -188,7 +238,7 @@ public Generation generate(GenerateRequest request) {
public EmbedResponse embed(EmbedRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("embed")
.addPathSegments("v1/embed")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -232,7 +282,7 @@ public EmbedResponse embed(EmbedRequest request) {
public RerankResponse rerank(RerankRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("rerank")
.addPathSegments("v1/rerank")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -270,12 +320,12 @@ public RerankResponse rerank(RerankRequest request) {

/**
* This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided <code>examples</code> of text + label pairs as a reference.
* Note: <a href="/training-representation-models">Custom Models</a> trained on classification examples don't require the <code>examples</code> parameter to be passed in explicitly.
* Note: <a href="https://docs.cohere.com/docs/classify-fine-tuning">Fine-tuned models</a> trained on classification examples don't require the <code>examples</code> parameter to be passed in explicitly.
*/
public ClassifyResponse classify(ClassifyRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("classify")
.addPathSegments("v1/classify")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -306,7 +356,7 @@ public ClassifyResponse classify(ClassifyRequest request, RequestOptions request

/**
* This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided <code>examples</code> of text + label pairs as a reference.
* Note: <a href="/training-representation-models">Custom Models</a> trained on classification examples don't require the <code>examples</code> parameter to be passed in explicitly.
* Note: <a href="https://docs.cohere.com/docs/classify-fine-tuning">Fine-tuned models</a> trained on classification examples don't require the <code>examples</code> parameter to be passed in explicitly.
*/
public ClassifyResponse classify(ClassifyRequest request) {
return classify(request, null);
Expand All @@ -318,7 +368,7 @@ public ClassifyResponse classify(ClassifyRequest request) {
public DetectLanguageResponse detectLanguage(DetectLanguageRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("detect-language")
.addPathSegments("v1/detect-language")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -360,7 +410,7 @@ public DetectLanguageResponse detectLanguage(DetectLanguageRequest request) {
public SummarizeResponse summarize(SummarizeRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("summarize")
.addPathSegments("v1/summarize")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -402,7 +452,7 @@ public SummarizeResponse summarize(SummarizeRequest request) {
public TokenizeResponse tokenize(TokenizeRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("tokenize")
.addPathSegments("v1/tokenize")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -444,7 +494,7 @@ public TokenizeResponse tokenize(TokenizeRequest request) {
public DetokenizeResponse detokenize(DetokenizeRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("detokenize")
.addPathSegments("v1/detokenize")
.build();
RequestBody body;
try {
Expand Down Expand Up @@ -480,6 +530,10 @@ public DetokenizeResponse detokenize(DetokenizeRequest request) {
return detokenize(request, null);
}

public DatasetsClient datasets() {
return this.datasetsClient.get();
}

public ConnectorsClient connectors() {
return this.connectorsClient.get();
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/cohere/api/CohereBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public CohereBuilder token(String token) {
return this;
}

public CohereBuilder clientName(String clientName) {
this.clientOptionsBuilder.addHeader("X-Client-Name", clientName);
return this;
}

public CohereBuilder environment(Environment environment) {
this.environment = environment;
return this;
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 @@ -29,7 +29,7 @@ private ClientOptions(
"X-Fern-SDK-Name",
"com.cohere.fern:api-sdk",
"X-Fern-SDK-Version",
"1.0.3",
"1.0.5",
"X-Fern-Language",
"JAVA"));
this.headerSuppliers = headerSuppliers;
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/cohere/api/core/RequestOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
public final class RequestOptions {
private final String token;

private RequestOptions(String token) {
private final String clientName;

private RequestOptions(String token, String clientName) {
this.token = token;
this.clientName = clientName;
}

public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
if (this.token != null) {
headers.put("Authorization", "Bearer " + this.token);
}
if (this.clientName != null) {
headers.put("X-Client-Name", this.clientName);
}
return headers;
}

Expand All @@ -28,13 +34,20 @@ public static Builder builder() {
public static final class Builder {
private String token = null;

private String clientName = null;

public Builder token(String token) {
this.token = token;
return this;
}

public Builder clientName(String clientName) {
this.clientName = clientName;
return this;
}

public RequestOptions build() {
return new RequestOptions(token);
return new RequestOptions(token, clientName);
}
}
}
32 changes: 3 additions & 29 deletions src/main/java/com/cohere/api/requests/ChatRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public final class ChatRequest {

private final Optional<String> model;

private final Boolean stream;

private final Optional<String> preambleOverride;

private final Optional<List<ChatMessage>> chatHistory;
Expand All @@ -54,7 +52,6 @@ public final class ChatRequest {
private ChatRequest(
String message,
Optional<String> model,
Boolean stream,
Optional<String> preambleOverride,
Optional<List<ChatMessage>> chatHistory,
Optional<String> conversationId,
Expand All @@ -67,7 +64,6 @@ private ChatRequest(
Map<String, Object> additionalProperties) {
this.message = message;
this.model = model;
this.stream = stream;
this.preambleOverride = preambleOverride;
this.chatHistory = chatHistory;
this.conversationId = conversationId;
Expand Down Expand Up @@ -99,11 +95,6 @@ public Optional<String> getModel() {
return model;
}

@JsonProperty("stream")
public Boolean getStream() {
return stream;
}

/**
* @return When specified, the default Cohere preamble will be replaced with the provided one.
*/
Expand Down Expand Up @@ -198,7 +189,6 @@ public Map<String, Object> getAdditionalProperties() {
private boolean equalTo(ChatRequest other) {
return message.equals(other.message)
&& model.equals(other.model)
&& stream.equals(other.stream)
&& preambleOverride.equals(other.preambleOverride)
&& chatHistory.equals(other.chatHistory)
&& conversationId.equals(other.conversationId)
Expand All @@ -215,7 +205,6 @@ public int hashCode() {
return Objects.hash(
this.message,
this.model,
this.stream,
this.preambleOverride,
this.chatHistory,
this.conversationId,
Expand All @@ -237,15 +226,11 @@ public static MessageStage builder() {
}

public interface MessageStage {
StreamStage message(String message);
_FinalStage message(String message);

Builder from(ChatRequest other);
}

public interface StreamStage {
_FinalStage stream(Boolean stream);
}

public interface _FinalStage {
ChatRequest build();

Expand Down Expand Up @@ -291,11 +276,9 @@ public interface _FinalStage {
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static final class Builder implements MessageStage, StreamStage, _FinalStage {
public static final class Builder implements MessageStage, _FinalStage {
private String message;

private Boolean stream;

private Optional<Double> temperature = Optional.empty();

private Optional<ChatRequestCitationQuality> citationQuality = Optional.empty();
Expand Down Expand Up @@ -325,7 +308,6 @@ private Builder() {}
public Builder from(ChatRequest other) {
message(other.getMessage());
model(other.getModel());
stream(other.getStream());
preambleOverride(other.getPreambleOverride());
chatHistory(other.getChatHistory());
conversationId(other.getConversationId());
Expand All @@ -345,18 +327,11 @@ public Builder from(ChatRequest other) {
*/
@Override
@JsonSetter("message")
public StreamStage message(String message) {
public _FinalStage message(String message) {
this.message = message;
return this;
}

@Override
@JsonSetter("stream")
public _FinalStage stream(Boolean stream) {
this.stream = stream;
return this;
}

/**
* <p>Defaults to <code>0.3</code>
* 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.</p>
Expand Down Expand Up @@ -542,7 +517,6 @@ public ChatRequest build() {
return new ChatRequest(
message,
model,
stream,
preambleOverride,
chatHistory,
conversationId,
Expand Down
Loading
Loading