Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,6 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

**/generated-samples/**
**/.env
33 changes: 33 additions & 0 deletions samples/authentication-entra-id/java/sample.java.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import com.azure.identity.AuthenticationUtil;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.credential.BearerTokenCredential;
import com.openai.models.ChatModel;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

public class Main {
public static void main(String[] args) {

<%= java.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint)%>
<%= java.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>
Comment on lines +12 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

(Which maybe is a tradeoff of readability vs reduced surface area?)

Copy link
Collaborator Author

@jpalvarezl jpalvarezl Jul 30, 2025

Choose a reason for hiding this comment

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

That's what felt kind of weird about adding these samples, but rather than exercising ChatCompletions I want to draw attention to the authentication type. But they are totally redundant and in fact could be compacted into a single sample, like the one you shared.

Not entirely sure how this will be surfaced in the Foundry website, we might not even need samples showcasing authentication specifically 🤔 Happy to close the PR without merging.

Copy link
Collaborator

Choose a reason for hiding this comment

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

my mistake! Even though the PR title says as much, I didn't pick up on the intention to focus on auth. I think it's totally founded to have a sample that targets auth.

These are firstly to provide SDK coverage, then hopefully secondarily provide useful to partners (like Foundry or Docs teams). So no worries on adding things that Foundry or other doesn't use.


OpenAIClient client = OpenAIOkHttpClient.builder()
.baseUrl(endpoint)
// Set the Azure Entra ID
.credential(BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier(
new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")))
.build();

ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
.model(ChatModel.of(deploymentName))
.addSystemMessage("You are a helpful assistant. You will talk like a pirate.")
.addUserMessage("Can you help me?")
.addUserMessage("What's the best way to train a parrot?")
.build();

client.chat().completions().create(createParams).choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println);
}
}
25 changes: 25 additions & 0 deletions samples/authentication-entra-id/java/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
template: sample.java.template
type: java
dependencies:
- name: com.openai:openai-java
version: 2.3.0
- name: com.azure:azure-identity
version: 1.16.1
input:
- name: endpoint
type: string
required: false
description: "Endpoint URL for the OpenAI API"
- name: deploymentName
type: string
required: true
description: "Model to use for chat completion"
- name: useEnvVars
type: boolean
required: false
default: false
description: "Use environment variables for configuration"
- name: extraParams
type: object
required: false
description: "Additional parameters for the request"
30 changes: 30 additions & 0 deletions samples/authentication-key-credential/java/sample.java.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import com.openai.azure.credential.AzureApiKeyCredential;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ChatModel;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

public class Main {
public static void main(String[] args) {

<%= java.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint)%>
<%= java.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey)%>
<%= java.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>

OpenAIClient client = OpenAIOkHttpClient.builder()
.baseUrl(endpoint)
.credential(AzureApiKeyCredential.create(apiKey))
.build();

ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
.model(ChatModel.of(deploymentName))
.addSystemMessage("You are a helpful assistant. You will talk like a pirate.")
.addUserMessage("Can you help me?")
.addUserMessage("What's the best way to train a parrot?")
.build();

client.chat().completions().create(createParams).choices().stream()
.flatMap(choice -> choice.message().content().stream())
.forEach(System.out::println);
}
}
29 changes: 29 additions & 0 deletions samples/authentication-key-credential/java/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
template: sample.java.template
type: java
dependencies:
- name: com.openai:openai-java
version: 2.3.0
- name: com.azure:azure-identity
version: 1.16.1
input:
- name: endpoint
type: string
required: false
description: "Endpoint URL for the OpenAI API"
- name: apiKey
type: string
required: false
description: "API key for authentication"
- name: deploymentName
type: string
required: false
description: "Model to use for chat completion"
- name: useEnvVars
type: boolean
required: false
default: false
description: "Use environment variables for configuration"
- name: extraParams
type: object
required: false
description: "Additional parameters for the request"
53 changes: 53 additions & 0 deletions samples/chat-completion-no-streaming/java/sample.java.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.http.StreamResponse;
import com.openai.models.ChatModel;
import com.openai.models.chat.completions.ChatCompletionChunk;
import com.openai.models.chat.completions.ChatCompletionCreateParams;

<% if (useTokenCredentials) { %>
import com.azure.identity.AuthenticationUtil;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.openai.credential.BearerTokenCredential; <%
} else { %>import com.openai.azure.credential.AzureApiKeyCredential;
<% } %>


public class Main {
public static void main(String[] args) {

<%= java.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint)%>
<%if (!useTokenCredentials) { %>
<%= java.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey)%>
<%}%>
<%= java.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>

OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder();
<% if (useTokenCredentials) { %>
clientBuilder
.baseUrl(endpoint)
.credential(BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier(
new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")));
<%
} else { %>
clientBuilder
.baseUrl(endpoint)
.credential(AzureApiKeyCredential.create(apiKey));
<%} %>
OpenAIClient client = clientBuilder.build();

ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
.model(ChatModel.of(deploymentName))
.addSystemMessage("You are a helpful assistant. You will talk like a pirate.")
.addUserMessage("Can you help me?")
.addUserMessage("What's the best way to train a parrot?")
.build();

try (StreamResponse<ChatCompletionChunk> streamResponse =
client.chat().completions().createStreaming(createParams)) {
streamResponse.stream()
.flatMap(completion -> completion.choices().stream())
.forEach(choice -> choice.delta().content().ifPresent(System.out::print));
}
}
}
33 changes: 33 additions & 0 deletions samples/chat-completion-no-streaming/java/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
template: sample.java.template
type: java
dependencies:
- name: com.openai:openai-java
version: 2.3.0
- name: com.azure:azure-identity
version: 1.16.1
input:
- name: useTokenCredentials
type: boolean
default: true
description: "Use token credentials for authentication"
- name: endpoint
type: string
required: false
description: "Endpoint URL for the OpenAI API"
- name: apiKey
type: string
required: false
description: "API key for authentication"
- name: deploymentName
type: string
required: true
description: "Model to use for chat completion"
- name: useEnvVars
type: boolean
required: false
default: false
description: "Use environment variables for configuration"
- name: extraParams
type: object
required: false
description: "Additional parameters for the request"
21 changes: 21 additions & 0 deletions samples/input-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ samples:
# input:
# type: reference
# value: defaultConfiguration
- templatePath: ./authentication-entra-id/java
variants:
- output: ../generated-samples/java/authentication-entra-id
input:
type: reference
value: defaultConfiguration
- templatePath: ./authentication-key-credential/java
variants:
- output: ../generated-samples/java/authentication-key-credential
input:
type: object
properties:
apiKey: fake-api-key
endpoint: fake-endpoint
deploymentName: fake-deployment-name
- templatePath: ./chat-completion-async/csharp
variants:
- output: ../generated-samples/csharp/chat-completion-async
Expand Down Expand Up @@ -139,6 +154,12 @@ samples:
input:
type: reference
value: defaultConfiguration
- templatePath: ./chat-completion-no-streaming/java
variants:
- output: ../generated-samples/java/chat-completion-no-streaming
input:
type: reference
value: defaultConfiguration
- templatePath: ./chat-completion-streaming-async/csharp
variants:
- output: ../generated-samples/csharp/chat-completion-streaming-async
Expand Down
Empty file modified scripts/resolve-sample-configs.sh
100644 → 100755
Empty file.
Empty file modified scripts/validate-samples.sh
100644 → 100755
Empty file.
Empty file modified scripts/validate-single-sample.sh
100644 → 100755
Empty file.