diff --git a/.gitignore b/.gitignore index 6c1eb92..3f91753 100644 --- a/.gitignore +++ b/.gitignore @@ -404,3 +404,6 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml + +**/generated-samples/** +**/.env \ No newline at end of file diff --git a/samples/authentication-entra-id/java/sample.java.template b/samples/authentication-entra-id/java/sample.java.template new file mode 100644 index 0000000..613c4ec --- /dev/null +++ b/samples/authentication-entra-id/java/sample.java.template @@ -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)%> + + 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); + } +} \ No newline at end of file diff --git a/samples/authentication-entra-id/java/sample.yaml b/samples/authentication-entra-id/java/sample.yaml new file mode 100644 index 0000000..aa33b56 --- /dev/null +++ b/samples/authentication-entra-id/java/sample.yaml @@ -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" diff --git a/samples/authentication-key-credential/java/sample.java.template b/samples/authentication-key-credential/java/sample.java.template new file mode 100644 index 0000000..6ce9979 --- /dev/null +++ b/samples/authentication-key-credential/java/sample.java.template @@ -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); + } +} \ No newline at end of file diff --git a/samples/authentication-key-credential/java/sample.yaml b/samples/authentication-key-credential/java/sample.yaml new file mode 100644 index 0000000..6f88f24 --- /dev/null +++ b/samples/authentication-key-credential/java/sample.yaml @@ -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" diff --git a/samples/chat-completion-no-streaming/java/sample.java.template b/samples/chat-completion-no-streaming/java/sample.java.template new file mode 100644 index 0000000..9ce457a --- /dev/null +++ b/samples/chat-completion-no-streaming/java/sample.java.template @@ -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 streamResponse = + client.chat().completions().createStreaming(createParams)) { + streamResponse.stream() + .flatMap(completion -> completion.choices().stream()) + .forEach(choice -> choice.delta().content().ifPresent(System.out::print)); + } + } +} \ No newline at end of file diff --git a/samples/chat-completion-no-streaming/java/sample.yaml b/samples/chat-completion-no-streaming/java/sample.yaml new file mode 100644 index 0000000..f1902a3 --- /dev/null +++ b/samples/chat-completion-no-streaming/java/sample.yaml @@ -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" diff --git a/samples/input-data.yaml b/samples/input-data.yaml index 56502c7..5beaa50 100644 --- a/samples/input-data.yaml +++ b/samples/input-data.yaml @@ -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 @@ -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 diff --git a/scripts/resolve-sample-configs.sh b/scripts/resolve-sample-configs.sh old mode 100644 new mode 100755 diff --git a/scripts/validate-samples.sh b/scripts/validate-samples.sh old mode 100644 new mode 100755 diff --git a/scripts/validate-single-sample.sh b/scripts/validate-single-sample.sh old mode 100644 new mode 100755