Reputation: 157
I am trying to use Langchain4J to connect to Open AI models and Anthropic model that are hosted behind my organization's base URL. The OpenAI Model is working fine. However, the Anthropic Model always throws the error "Required Authorization Header is missing". Below is the code snippet of both the model testing that I did.
//THIS code works fine without any issues
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.chat.ChatMessage;
import dev.langchain4j.model.input.Prompt;
public class OpenAIChatExample {
public static void main(String[] args) {
String clientId = "myclientid";
String clientId = "myclientsecret";
TokenAuthenticator authenticator = new TokenAuthenticator(clientId, clientSecret);
ChatLanguageModel model = OpenAiChatModel.builder()
.baseUrl("https://mycustombaseurl")
.apiKey(authenticator.getValidToken())
.modelName("gpt-4o")
.build();
try {
String response = model.generate("What is LLM?");
System.out.println("Simple Response: " + response);
} catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
e.printStackTrace();
}
}
}
//THIS code fails with the error "Required Authorization header is missing"
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.anthropic.AnthropicChatModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.chat.ChatMessage;
import dev.langchain4j.model.input.Prompt;
public class AnthropicChatExample {
public static void main(String[] args) {
String clientId = "myclientid";
String clientSecret = "myclientsecret";
TokenAuthenticator authenticator = new TokenAuthenticator(clientId, clientSecret);
AnthropicChatModel model = AnthropicChatModel.builder()
.baseUrl("https://mycustombaseurl")
.apiKey(authenticator.getValidToken())
.modelName("claude-3-5-sonnet")
.build();
try {
String response = model.generate("What is LLM?");
System.out.println("Simple Response: " + response);
} catch (Exception e) {
System.err.println("Error occurred: " + e.getMessage());
e.printStackTrace();
}
}
}
Below is the error message I get for the failing code snippet above (AnthropicChatExample)
Caused by: dev.langchain4j.model.anthropic.internal.client.AnthropicHttpException: {"Error": "Required Authorization header is missing"}
I tried running the above examples using the below Langchain4j versions that can be found in my build.gradle file below, the java version I am using in Java 21.
plugins {
id 'java'
}
group = 'org.example'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation 'dev.langchain4j:langchain4j:0.36.2'
implementation 'dev.langchain4j:langchain4j-core:0.36.2'
implementation 'dev.langchain4j:langchain4j-open-ai:0.36.2'
implementation group: 'dev.langchain4j', name: 'langchain4j-anthropic', version: '0.36.2'
implementation 'dev.langchain4j:langchain4j-embeddings-all-minilm-l6-v2:0.36.2'
implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
}
test {
useJUnitPlatform()
}
Please let me know if I am missing something here.
Upvotes: 0
Views: 76
Reputation: 157
A simpler solution to this is to use the below approach. Since the Anthropic Models are hosted behind OpenAI's API this would be the appropriate approach without adding any unnecessary complexities,
OpenAiChatModel model = OpenAiChatModel.builder()
.baseUrl("https://mycustombaseurl")
.apiKey(authenticator.getValidToken())
.modelName("claude-3-5-sonnet")
.build();
The Github issue link to the solution
Upvotes: 0