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

Cohere: Add CohereClient and CohereChatModel to support Chat #917

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.langchain4j.model.cohere;


import lombok.Builder;
import lombok.NonNull;

@Builder
public class ChatHistory {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: we are now changing the way classes are organized in the modules. Could you please use the following structure (inside the internal package)?


@NonNull
CohereRole role;

@NonNull
String message;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.langchain4j.model.cohere;

import lombok.Getter;
import lombok.ToString;

import java.util.List;

@Getter
@ToString
public class Citation {
Integer start;

Integer end;

String text;

List<String> documentIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

interface CohereApi {

@POST("chat")
@Headers({"Content-Type: application/json"})
Call<CohereChatResponse> chat(@Body CohereChatRequest request, @Header("Authorization") String authorizationHeader);

@POST("rerank")
@Headers({"Content-Type: application/json"})
Call<RerankResponse> rerank(@Body RerankRequest request, @Header("Authorization") String authorizationHeader);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package dev.langchain4j.model.cohere;

import lombok.Builder;
import lombok.NonNull;

import java.util.List;
import java.util.Map;

@Builder
public class CohereChatRequest {

@NonNull
String message;

String model;

boolean stream;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason primitives are used in this class?


String preamble;

List<ChatHistory> chatHistory;

String conversationId;

String promptTruncation;

List<Connector> connectors;

boolean searchQueriesOnly;

List<Map<String, String>> documents;

Double temperature;

int maxTokens;

Integer k;

Double p;

Double seed;

List<String> stopSequences;

Double frequencyPenalty;

Double presencePenalty;

List<Tool> tools;

List<ToolResult> toolResults;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.langchain4j.model.cohere;

import lombok.Getter;

import java.util.List;
import java.util.Map;

@Getter
public class CohereChatResponse {

String text;

String generationId;

List<Citation> citations;

List<Map<String, String>> documents;

boolean isSearchRequired;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason primitives are used in this class?


List<SearchQuery> searchQueries;

List<SearchResult> searchResults;

String finishReason;

List<ToolCall> toolCalls;

List<ChatHistory> chatHistory;

Meta meta;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.GsonBuilder;
import lombok.Builder;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

Expand Down Expand Up @@ -49,10 +50,18 @@ class CohereClient {
this.authorizationHeader = "Bearer " + ensureNotBlank(apiKey, "apiKey");
}

public CohereChatResponse chat(CohereChatRequest request) {
return execute(cohereApi.chat(request, authorizationHeader));
}

public RerankResponse rerank(RerankRequest request) {
return execute(cohereApi.rerank(request, authorizationHeader));
}

private <T> T execute(Call<T> retrofitCall) {
try {
retrofit2.Response<RerankResponse> retrofitResponse
= cohereApi.rerank(request, authorizationHeader).execute();
retrofit2.Response<T> retrofitResponse
= retrofitCall.execute();

if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.langchain4j.model.cohere;

public enum CohereRole {

CHATBOX,

SYSTEM,

USER

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.langchain4j.model.cohere;

import lombok.Builder;
import lombok.NonNull;

import java.util.Map;

@Builder
public class Connector {

@NonNull
String id;

String userAccessToken;

boolean continueOnFailure;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason primitives are used in this class?


Map<String, String> options;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.langchain4j.model.cohere;

import lombok.Builder;
import lombok.NonNull;

@Builder
public class ParameterDefinition {

String description;

@NonNull
String type;

boolean required;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason primitives are used in this class?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.langchain4j.model.cohere;

import lombok.Getter;

@Getter
public class SearchQuery {

String text;

String generationId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.langchain4j.model.cohere;

import lombok.Getter;

import java.util.List;

@Getter
public class SearchResult {

SearchQuery searchQuery;

Connector connector;

List<String> documentIds;

String errorMessage;

boolean continueOnFailure;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason primitives are used in this class?


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.langchain4j.model.cohere;

import lombok.Builder;
import lombok.NonNull;

import java.util.Map;

@Builder
public class Tool {

@NonNull
String name;

@NonNull
String description;

Map<String, ParameterDefinition> parameterDefinitions;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.langchain4j.model.cohere;

import lombok.Builder;

import java.util.Map;

@Builder
public class ToolCall {
String name;
Map<String, String> parameters;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.langchain4j.model.cohere;

import lombok.Builder;

import java.util.List;
import java.util.Map;

@Builder
public class ToolResult {

ToolCall call;
List<Map<String, String>> outputs;
}