diff --git a/sdk/openai/Azure.AI.OpenAI/README.md b/sdk/openai/Azure.AI.OpenAI/README.md index 2df0b06dbd96a..fa17d9567d9d3 100644 --- a/sdk/openai/Azure.AI.OpenAI/README.md +++ b/sdk/openai/Azure.AI.OpenAI/README.md @@ -53,7 +53,7 @@ dotnet add package Azure.Identity ```C# Snippet:CreateOpenAIClientTokenCredential string endpoint = "https://myaccount.openai.azure.com/"; -OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential()); +OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential()); ``` ## Key concepts @@ -102,12 +102,12 @@ The `GenerateChatbotResponse` method authenticates using a DefaultAzureCredentia ```C# Snippet:GenerateChatbotResponse string endpoint = "https://myaccount.openai.azure.com/"; -OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential()); +OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential()); string prompt = "What is Azure OpenAI?"; Console.Write($"Input: {prompt}"); -Response completionsResponse = client.GetCompletions(prompt); +Response completionsResponse = client.GetCompletions("myDeploymentId", prompt); string completion = completionsResponse.Value.Choices[0].Text; Console.WriteLine($"Chatbot: {completion}"); ``` @@ -148,7 +148,7 @@ The `SummarizeText` method generates a summarization of the given input prompt. ```C# Snippet:SummarizeText string endpoint = "https://myaccount.openai.azure.com/"; -OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential()); +OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential()); string textToSummarize = @" Two independent experiments reported their results this morning at CERN, Europe's high-energy physics laboratory near Geneva in Switzerland. Both show convincing evidence of a new boson particle weighing around 125 gigaelectronvolts, which so far fits predictions of the Higgs previously made by theoretical physicists. diff --git a/sdk/openai/Azure.AI.OpenAI/api/Azure.AI.OpenAI.netstandard2.0.cs b/sdk/openai/Azure.AI.OpenAI/api/Azure.AI.OpenAI.netstandard2.0.cs index e3407dd8879ce..11610058360d0 100644 --- a/sdk/openai/Azure.AI.OpenAI/api/Azure.AI.OpenAI.netstandard2.0.cs +++ b/sdk/openai/Azure.AI.OpenAI/api/Azure.AI.OpenAI.netstandard2.0.cs @@ -7,15 +7,13 @@ public OpenAIClient(System.Uri endpoint, Azure.AzureKeyCredential credential) { public OpenAIClient(System.Uri endpoint, Azure.AzureKeyCredential credential, Azure.AI.OpenAI.OpenAIClientOptions options) { } public OpenAIClient(System.Uri endpoint, Azure.Core.TokenCredential credential) { } public OpenAIClient(System.Uri endpoint, Azure.Core.TokenCredential credential, Azure.AI.OpenAI.OpenAIClientOptions options) { } - public OpenAIClient(System.Uri endpoint, string deploymentId, Azure.Core.TokenCredential credential) { } - public OpenAIClient(System.Uri endpoint, string deploymentId, Azure.Core.TokenCredential credential, Azure.AI.OpenAI.OpenAIClientOptions options) { } public virtual Azure.Core.Pipeline.HttpPipeline Pipeline { get { throw null; } } public virtual Azure.Response GetCompletions(string deploymentId, Azure.AI.OpenAI.Models.CompletionsOptions completionsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual Azure.Response GetCompletions(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; } - public virtual Azure.Response GetCompletions(string prompt, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual Azure.Response GetCompletions(string deploymentId, string prompt, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task> GetCompletionsAsync(string deploymentId, Azure.AI.OpenAI.Models.CompletionsOptions completionsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual System.Threading.Tasks.Task GetCompletionsAsync(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; } - public virtual System.Threading.Tasks.Task> GetCompletionsAsync(string prompt, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public virtual System.Threading.Tasks.Task> GetCompletionsAsync(string deploymentId, string prompt, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual Azure.Response GetEmbeddings(string deploymentId, Azure.AI.OpenAI.Models.EmbeddingsOptions embeddingsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public virtual Azure.Response GetEmbeddings(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; } public virtual System.Threading.Tasks.Task> GetEmbeddingsAsync(string deploymentId, Azure.AI.OpenAI.Models.EmbeddingsOptions embeddingsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } diff --git a/sdk/openai/Azure.AI.OpenAI/src/Custom/OpenAIClient.cs b/sdk/openai/Azure.AI.OpenAI/src/Custom/OpenAIClient.cs index 29c8b5a9b18a0..58ff39aba3353 100644 --- a/sdk/openai/Azure.AI.OpenAI/src/Custom/OpenAIClient.cs +++ b/sdk/openai/Azure.AI.OpenAI/src/Custom/OpenAIClient.cs @@ -16,69 +16,32 @@ namespace Azure.AI.OpenAI /// Azure OpenAI APIs for completions and search. public partial class OpenAIClient { - private readonly string _completionsDeploymentId; - private readonly string _embeddingsDeploymentId; - - /// Initializes a new instance of OpenAIClient. - /// - /// Supported Cognitive Services endpoints (protocol and hostname, for example: - /// https://westus.api.cognitive.microsoft.com). - /// - /// default deployment id to use for operations - /// A credential used to authenticate to an Azure Service. - /// or is null. - public OpenAIClient(Uri endpoint, string deploymentId, TokenCredential credential) : this(endpoint, deploymentId, credential, new OpenAIClientOptions()) - { - } - - /// Initializes a new instance of OpenAIClient. - /// - /// Supported Cognitive Services endpoints (protocol and hostname, for example: - /// https://westus.api.cognitive.microsoft.com). - /// - /// default deployment id to use for operations - /// A credential used to authenticate to an Azure Service. - /// The options for configuring the client. - /// or is null. - public OpenAIClient(Uri endpoint, string deploymentId, TokenCredential credential, OpenAIClientOptions options) - { - Argument.AssertNotNull(endpoint, nameof(endpoint)); - Argument.AssertNotNull(credential, nameof(credential)); - options ??= new OpenAIClientOptions(); - - ClientDiagnostics = new ClientDiagnostics(options, true); - _tokenCredential = credential; - _pipeline = HttpPipelineBuilder.Build(options, Array.Empty(), new HttpPipelinePolicy[] { new BearerTokenAuthenticationPolicy(_tokenCredential, AuthorizationScopes) }, new ResponseClassifier()); - _endpoint = endpoint; - _apiVersion = options.Version; - _completionsDeploymentId ??= deploymentId; - _embeddingsDeploymentId ??= deploymentId; - } - /// Return the completion for a given prompt. + /// Deployment id (also known as model name) to use for operations /// Input string prompt to create a prompt completion from a deployment. /// The cancellation token to use. - public virtual async Task> GetCompletionsAsync(string prompt, CancellationToken cancellationToken = default) + public virtual async Task> GetCompletionsAsync(string deploymentId, string prompt, CancellationToken cancellationToken = default) { - Argument.AssertNotNullOrEmpty(_completionsDeploymentId, nameof(_completionsDeploymentId)); + Argument.AssertNotNullOrEmpty(deploymentId, nameof(deploymentId)); Argument.AssertNotNullOrEmpty(prompt, nameof(prompt)); CompletionsOptions completionsOptions = new CompletionsOptions(); completionsOptions.Prompt.Add(prompt); - return await GetCompletionsAsync(_completionsDeploymentId, completionsOptions, cancellationToken).ConfigureAwait(false); + return await GetCompletionsAsync(deploymentId, completionsOptions, cancellationToken).ConfigureAwait(false); } /// Return the completions for a given prompt. + /// Deployment id (also known as model name) to use for operations /// Input string prompt to create a prompt completion from a deployment. /// The cancellation token to use. - public virtual Response GetCompletions(string prompt, CancellationToken cancellationToken = default) + public virtual Response GetCompletions(string deploymentId, string prompt, CancellationToken cancellationToken = default) { - Argument.AssertNotNullOrEmpty(_completionsDeploymentId, nameof(_completionsDeploymentId)); + Argument.AssertNotNullOrEmpty(deploymentId, nameof(deploymentId)); Argument.AssertNotNullOrEmpty(prompt, nameof(prompt)); CompletionsOptions completionsOptions = new CompletionsOptions(); completionsOptions.Prompt.Add(prompt); - return GetCompletions(_completionsDeploymentId, completionsOptions, cancellationToken); + return GetCompletions(deploymentId, completionsOptions, cancellationToken); } } } diff --git a/sdk/openai/Azure.AI.OpenAI/tests/OpenAIInferenceTests.cs b/sdk/openai/Azure.AI.OpenAI/tests/OpenAIInferenceTests.cs index 2d56b99b33c31..c39deceeaa15d 100644 --- a/sdk/openai/Azure.AI.OpenAI/tests/OpenAIInferenceTests.cs +++ b/sdk/openai/Azure.AI.OpenAI/tests/OpenAIInferenceTests.cs @@ -49,7 +49,7 @@ public async Task CompletionTest() public async Task SimpleCompletionTest() { var client = GetClientWithCompletionsDeploymentId(); - var response = await client.GetCompletionsAsync("Hello World!"); + var response = await client.GetCompletionsAsync(DeploymentId, "Hello World!"); Assert.That(response, Is.InstanceOf>()); } diff --git a/sdk/openai/Azure.AI.OpenAI/tests/OpenAITestBase.cs b/sdk/openai/Azure.AI.OpenAI/tests/OpenAITestBase.cs index c857a2ac963c6..e7d0f91931bc3 100644 --- a/sdk/openai/Azure.AI.OpenAI/tests/OpenAITestBase.cs +++ b/sdk/openai/Azure.AI.OpenAI/tests/OpenAITestBase.cs @@ -146,7 +146,6 @@ protected OpenAIClient GetClientWithCredential() => InstrumentClient( protected OpenAIClient GetClientWithCompletionsDeploymentId() => InstrumentClient( new OpenAIClient( new Uri(_endpoint), - DeploymentId, TestEnvironment.Credential, InstrumentClientOptions(new OpenAIClientOptions(OpenAIClientOptions.ServiceVersion.V2022_12_01)))); } diff --git a/sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample01_Chatbot.cs b/sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample01_Chatbot.cs index d565242628787..debe156c66f61 100644 --- a/sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample01_Chatbot.cs +++ b/sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample01_Chatbot.cs @@ -18,13 +18,13 @@ public void GetChatbotResponse() #region Snippet:GenerateChatbotResponse #region Snippet:CreateOpenAIClientTokenCredential string endpoint = "https://myaccount.openai.azure.com/"; - OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential()); + OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential()); #endregion string prompt = "What is Azure OpenAI?"; Console.Write($"Input: {prompt}"); - Response completionsResponse = client.GetCompletions(prompt); + Response completionsResponse = client.GetCompletions("myDeploymentId", prompt); string completion = completionsResponse.Value.Choices[0].Text; Console.WriteLine($"Chatbot: {completion}"); #endregion diff --git a/sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample03_SummarizeText.cs b/sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample03_SummarizeText.cs index fef49280b8816..fbc41a882a455 100644 --- a/sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample03_SummarizeText.cs +++ b/sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample03_SummarizeText.cs @@ -17,7 +17,7 @@ public void SummarizeText() { #region Snippet:SummarizeText string endpoint = "https://myaccount.openai.azure.com/"; - OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential()); + OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential()); string textToSummarize = @" Two independent experiments reported their results this morning at CERN, Europe's high-energy physics laboratory near Geneva in Switzerland. Both show convincing evidence of a new boson particle weighing around 125 gigaelectronvolts, which so far fits predictions of the Higgs previously made by theoretical physicists.