diff --git a/samples/embeddings-async/csharp/sample.cs.template b/samples/embeddings-async/csharp/sample.cs.template index 91f0779..3e78a96 100644 --- a/samples/embeddings-async/csharp/sample.cs.template +++ b/samples/embeddings-async/csharp/sample.cs.template @@ -4,6 +4,7 @@ using Azure.Identity; <% } %> using Azure.AI.OpenAI; using OpenAI.Embeddings; +using System.ClientModel; <%= csharp.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint) %>; <%if (useTokenCredentials) { %> diff --git a/samples/embeddings/csharp/sample.cs.template b/samples/embeddings/csharp/sample.cs.template new file mode 100644 index 0000000..cc39993 --- /dev/null +++ b/samples/embeddings/csharp/sample.cs.template @@ -0,0 +1,37 @@ +<% if (useTokenCredentials) { %> +using Azure.Identity; +<%} else { %>using Azure; +<% } %> +using Azure.AI.OpenAI; +using OpenAI.Embeddings; +using System.ClientModel; + +<%= csharp.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint) %>; +<%if (useTokenCredentials) { %> +<% } else { %> +<%= csharp.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey) %>; +<% } %> +<%= csharp.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName) %>; +<% if (useTokenCredentials) { %> +AzureOpenAIClient azureClient = new(new Uri(endpoint), new DefaultAzureCredential()); +<% } else { %> +AzureOpenAIClient azureClient = new(new Uri(endpoint), new AzureKeyCredential(apiKey)); +<% } %>EmbeddingClient embeddingClient = azureClient.GetEmbeddingClient(deploymentName); + +ClientResult embeddingResult = embeddingClient.GenerateEmbedding("The quick brown fox jumped over the lazy dog"); + +if (embeddingResult?.Value != null) +{ + float[] embedding = embeddingResult.Value.ToFloats().ToArray(); + + Console.WriteLine($"Embedding Length: {embedding.Length}"); + Console.WriteLine("Embedding Values:"); + foreach (float value in embedding) + { + Console.Write($"{value}, "); + } +} +else +{ + Console.WriteLine("Failed to generate embedding or received null value."); +} diff --git a/samples/embeddings/csharp/sample.yaml b/samples/embeddings/csharp/sample.yaml new file mode 100644 index 0000000..05ae394 --- /dev/null +++ b/samples/embeddings/csharp/sample.yaml @@ -0,0 +1,35 @@ +template: sample.cs.template +type: csharp +dependencies: + - name: OpenAI + version: 2.1.0 + - name: Azure.AI.OpenAI + version: 2.1.0 + - name: Azure.Identity + version: 1.14.0 +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" \ No newline at end of file diff --git a/samples/embeddings/go/sample.go.template b/samples/embeddings/go/sample.go.template new file mode 100644 index 0000000..1126409 --- /dev/null +++ b/samples/embeddings/go/sample.go.template @@ -0,0 +1,64 @@ +package main + +<%= go.includes( + "context", + "fmt", + "log", + "os", + { module: "github.com/Azure/azure-sdk-for-go/sdk/azidentity", condition: useTokenCredentials }, + "github.com/openai/openai-go", + "github.com/openai/openai-go/azure" +) %> + +func main() { + <%= go.valueOrEnvironment(useEnvVars, "endpoint", "AZURE_OPENAI_ENDPOINT", endpoint) %> + <%= go.valueOrEnvironment(useEnvVars, "api_version", "AZURE_OPENAI_API_VERSION", apiVersion) %> + <% if (useTokenCredentials) { %> + <% } else { %> + <%= go.valueOrEnvironment(useEnvVars, "apiKey", "AZURE_OPENAI_API_KEY", apiKey) %> + <% } %> + <%= go.valueOrEnvironment(useEnvVars, "deploymentName", "AZURE_OPENAI_DEPLOYMENT", deploymentName) %> + + <% if (useTokenCredentials) { %> + token_credential, err := azidentity.NewDefaultAzureCredential(nil) + if err != nil { + fmt.Println("Error creating credential:", err) + os.Exit(1) + } + client := openai.NewClient( + azure.WithEndpoint(endpoint, api_version), + azure.WithTokenCredential(token_credential), + ) + <% } else { %> + client := openai.NewClient( + azure.WithEndpoint(endpoint, api_version), + azure.WithAPIKey(apiKey), + ) + <% } %> + + inputText := "The quick brown fox jumped over the lazy dog" + + // Make the embedding request synchronously + resp, err := client.Embeddings.New(context.Background(), openai.EmbeddingNewParams{ + Model: openai.EmbeddingModel(deploymentName), + Input: openai.EmbeddingNewParamsInputUnion{ + OfArrayOfStrings: []string{inputText}, + }, + }) + if err != nil { + log.Fatalf("Failed to get embedding: %v", err) + } + + if len(resp.Data) == 0 { + fmt.Println("No embedding data returned.") + return + } + + embedding := resp.Data[0].Embedding + fmt.Printf("Embedding Length: %d\n", len(embedding)) + fmt.Println("Embedding Values:") + for _, value := range embedding { + fmt.Printf("%f, ", value) + } + fmt.Println() +} diff --git a/samples/embeddings/go/sample.yaml b/samples/embeddings/go/sample.yaml new file mode 100644 index 0000000..836ab00 --- /dev/null +++ b/samples/embeddings/go/sample.yaml @@ -0,0 +1,38 @@ +template: sample.go.template +type: go +dependencies: + - name: github.com/Azure/azure-sdk-for-go/sdk/azidentity + version: v1.10.0 + - name: github.com/openai/openai-go + version: v1.1.0 +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: apiVersion + type: string + required: false + default: "2025-03-01-preview" + description: "API version to use for the request" + - 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" \ No newline at end of file diff --git a/samples/embeddings/java/sample.java.template b/samples/embeddings/java/sample.java.template new file mode 100644 index 0000000..690ebf6 --- /dev/null +++ b/samples/embeddings/java/sample.java.template @@ -0,0 +1,42 @@ +import com.openai.client.OpenAIClient; +import com.openai.client.okhttp.OpenAIOkHttpClient; + +<% if (useTokenCredentials) { %>import com.azure.identity.AuthenticationUtil; +import com.azure.identity.DefaultAzureCredentialBuilder; +import com.openai.credential.BearerTokenCredential; <% +} else { %>import com.openai.azure.credential.AzureApiKeyCredential; +<% } %> + +import com.openai.models.embeddings.EmbeddingCreateParams; +import com.openai.models.embeddings.EmbeddingModel; + +public class Main { + public static void main(String[] args) { + String endpoint = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_ENDPOINT", endpoint)%>; + <%if (!useTokenCredentials) { %> + String azureOpenaiKey = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_API_KEY", apiKey)%>; + <%}%> + String deploymentName = <%= java.valueOrEnvironment(useEnvVars, "AZURE_OPENAI_DEPLOYMENT", deploymentName)%>; + + OpenAIOkHttpClient.Builder clientBuilder = OpenAIOkHttpClient.builder(); + <% if (!useTokenCredentials) { %> + clientBuilder + .baseUrl(endpoint) + .credential(AzureApiKeyCredential.create(azureOpenaiKey)); + <% + } else { %> + clientBuilder + .baseUrl(endpoint) + .credential(BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( + new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default"))); + <%} %> + OpenAIClient client = clientBuilder.build(); + + EmbeddingCreateParams createParams = EmbeddingCreateParams.builder() + .input("The quick brown fox jumped over the lazy dog") + .model(EmbeddingModel.of(deploymentName)) + .build(); + + System.out.println(client.embeddings().create(createParams)); + } +} \ No newline at end of file diff --git a/samples/embeddings/java/sample.yaml b/samples/embeddings/java/sample.yaml new file mode 100644 index 0000000..f1902a3 --- /dev/null +++ b/samples/embeddings/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"