Skip to content

Commit 2b0853c

Browse files
committed
fix: java fallback
1 parent 831e584 commit 2b0853c

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

docs/sections/java-quarkus/06-chat-api.md

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,29 @@ Before we can create the clients, we need to retrieve the credentials to access
2525
Add this code under the `TODO:` to retrieve the token to build the `AzureOpenAIChatModel`:
2626

2727
```java
28-
AzureOpenAiChatModel model;
29-
30-
try {
31-
// Use the current user identity to authenticate with Azure OpenAI.
32-
// (no secrets needed, just use `az login` or `azd auth login` locally, and managed identity when deployed on Azure).
33-
model = AzureOpenAiChatModel.builder()
34-
.tokenCredential(new DefaultAzureCredentialBuilder().build())
35-
.endpoint(azureOpenAiEndpoint)
36-
.deploymentName(azureOpenAiDeploymentName)
37-
.timeout(ofSeconds(60))
38-
.logRequestsAndResponses(true)
39-
.build();
40-
} catch (Exception e) {
41-
// default value for local execution
42-
log.info("### Using fallback configuration for OpenAI");
43-
}
28+
AzureOpenAiChatModel model;
29+
30+
try {
31+
// Use the current user identity to authenticate with Azure OpenAI.
32+
// (no secrets needed, just use `az login` or `azd auth login` locally, and managed identity when deployed on Azure).
33+
DefaultAzureCredential credentials = new DefaultAzureCredentialBuilder().build();
34+
35+
// Try requesting a token, so we can fallback to the other builder if it doesn't work
36+
TokenRequestContext request = new TokenRequestContext();
37+
request.addScopes("https://cognitiveservices.azure.com/.default");
38+
credentials.getTokenSync(request);
39+
40+
model = AzureOpenAiChatModel.builder()
41+
.tokenCredential(credentials)
42+
.endpoint(azureOpenAiEndpoint)
43+
.deploymentName(azureOpenAiDeploymentName)
44+
.timeout(ofSeconds(60))
45+
.logRequestsAndResponses(true)
46+
.build();
47+
} catch (Exception e) {
48+
// Default value for local execution
49+
// ...
50+
}
4451
```
4552

4653
This will use the current user identity to authenticate with Azure OpenAI and AI Search. We don't need to provide any secrets, just use `az login` (or `azd auth login`) locally, and [managed identity](https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/overview) when deployed on Azure.
@@ -58,7 +65,7 @@ To use the fallback, add the following code in the catch statement and return th
5865

5966
```java
6067
} catch (Exception e) {
61-
// default value for local execution
68+
// Default value for local execution
6269
log.info("### Using fallback configuration for OpenAI");
6370
model = AzureOpenAiChatModel.builder()
6471
.apiKey("__dummy")

scripts/repo/create-github-template.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ import dev.langchain4j.model.azure.AzureOpenAiChatModel;
268268
import dev.langchain4j.model.chat.ChatLanguageModel;
269269
import jakarta.enterprise.inject.Produces;
270270
import org.eclipse.microprofile.config.inject.ConfigProperty;
271+
272+
import com.azure.core.credential.TokenRequestContext;
273+
import com.azure.identity.DefaultAzureCredential;
271274
import com.azure.identity.DefaultAzureCredentialBuilder;
272275
273276
import static java.time.Duration.ofSeconds;

src/backend-java-quarkus/src/main/java/ai/azure/openai/rag/workshop/backend/configuration/ChatLanguageModelAzureOpenAiProducer.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import dev.langchain4j.model.chat.ChatLanguageModel;
55
import jakarta.enterprise.inject.Produces;
66
import org.eclipse.microprofile.config.inject.ConfigProperty;
7+
8+
import com.azure.core.credential.TokenRequestContext;
9+
import com.azure.identity.DefaultAzureCredential;
710
import com.azure.identity.DefaultAzureCredentialBuilder;
811

912
import static java.time.Duration.ofSeconds;
@@ -22,27 +25,36 @@ public class ChatLanguageModelAzureOpenAiProducer {
2225

2326
@Produces
2427
public ChatLanguageModel chatLanguageModel() {
25-
28+
// initialize chat model here
2629
AzureOpenAiChatModel model;
30+
2731
try {
28-
// Instantiate the DefaultAzureCredential using managed identities
32+
// Use the current user identity to authenticate with Azure OpenAI.
33+
// (no secrets needed, just use `az login` or `azd auth login` locally, and managed identity when deployed on Azure).
34+
DefaultAzureCredential credentials = new DefaultAzureCredentialBuilder().build();
35+
36+
// Try requesting a token, so we can fallback to the other builder if it doesn't work
37+
TokenRequestContext request = new TokenRequestContext();
38+
request.addScopes("https://cognitiveservices.azure.com/.default");
39+
credentials.getTokenSync(request);
40+
2941
model = AzureOpenAiChatModel.builder()
30-
.tokenCredential(new DefaultAzureCredentialBuilder().build())
31-
.endpoint(azureOpenAiEndpoint)
32-
.deploymentName(azureOpenAiDeploymentName)
33-
.timeout(ofSeconds(60))
34-
.logRequestsAndResponses(true)
35-
.build();
42+
.tokenCredential(credentials)
43+
.endpoint(azureOpenAiEndpoint)
44+
.deploymentName(azureOpenAiDeploymentName)
45+
.timeout(ofSeconds(60))
46+
.logRequestsAndResponses(true)
47+
.build();
3648
} catch (Exception e) {
37-
// default value for local execution
49+
// Default value for local execution
3850
log.info("### Using fallback configuration for OpenAI");
3951
model = AzureOpenAiChatModel.builder()
40-
.apiKey("__dummy")
41-
.endpoint(azureOpenAiEndpoint)
42-
.deploymentName(azureOpenAiDeploymentName)
43-
.timeout(ofSeconds(60))
44-
.logRequestsAndResponses(true)
45-
.build();
52+
.apiKey("__dummy")
53+
.endpoint(azureOpenAiEndpoint)
54+
.deploymentName(azureOpenAiDeploymentName)
55+
.timeout(ofSeconds(60))
56+
.logRequestsAndResponses(true)
57+
.build();
4658
}
4759

4860
log.info("### Producing ChatLanguageModel with AzureOpenAiChatModel");

0 commit comments

Comments
 (0)