-
Notifications
You must be signed in to change notification settings - Fork 1
Adding authentication samples Java #46
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
base: main
Are you sure you want to change the base?
Changes from all commits
f40cc14
4286826
7aad3a7
f5366a4
8c4c843
89c02ea
1e058b2
00f2f53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,3 +404,6 @@ FodyWeavers.xsd | |
|
||
# JetBrains Rider | ||
*.sln.iml | ||
|
||
**/generated-samples/** | ||
**/.env |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could combine this template with the other and add a conditional on the auth type. e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Which maybe is a tradeoff of readability vs reduced surface area?) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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" |
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); | ||
} | ||
} |
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" |
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)); | ||
} | ||
} | ||
} |
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" |
Uh oh!
There was an error while loading. Please reload this page.