Skip to content

Commit 26ca9c3

Browse files
authored
deploymentId moved to be part of GetCompletions signature (Azure#33834)
* Removed custom constructors and reinstated deploymentId as part of GetCompletions's signature * Small corrections to the documentation * CodeCheck, Export-API, Update-Snippets run
1 parent 391dd0b commit 26ca9c3

File tree

7 files changed

+18
-58
lines changed

7 files changed

+18
-58
lines changed

sdk/openai/Azure.AI.OpenAI/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ dotnet add package Azure.Identity
5353

5454
```C# Snippet:CreateOpenAIClientTokenCredential
5555
string endpoint = "https://myaccount.openai.azure.com/";
56-
OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential());
56+
OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
5757
```
5858

5959
## Key concepts
@@ -102,12 +102,12 @@ The `GenerateChatbotResponse` method authenticates using a DefaultAzureCredentia
102102

103103
```C# Snippet:GenerateChatbotResponse
104104
string endpoint = "https://myaccount.openai.azure.com/";
105-
OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential());
105+
OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
106106

107107
string prompt = "What is Azure OpenAI?";
108108
Console.Write($"Input: {prompt}");
109109

110-
Response<Completions> completionsResponse = client.GetCompletions(prompt);
110+
Response<Completions> completionsResponse = client.GetCompletions("myDeploymentId", prompt);
111111
string completion = completionsResponse.Value.Choices[0].Text;
112112
Console.WriteLine($"Chatbot: {completion}");
113113
```
@@ -148,7 +148,7 @@ The `SummarizeText` method generates a summarization of the given input prompt.
148148

149149
```C# Snippet:SummarizeText
150150
string endpoint = "https://myaccount.openai.azure.com/";
151-
OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential());
151+
OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
152152

153153
string textToSummarize = @"
154154
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.

sdk/openai/Azure.AI.OpenAI/api/Azure.AI.OpenAI.netstandard2.0.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ public OpenAIClient(System.Uri endpoint, Azure.AzureKeyCredential credential) {
77
public OpenAIClient(System.Uri endpoint, Azure.AzureKeyCredential credential, Azure.AI.OpenAI.OpenAIClientOptions options) { }
88
public OpenAIClient(System.Uri endpoint, Azure.Core.TokenCredential credential) { }
99
public OpenAIClient(System.Uri endpoint, Azure.Core.TokenCredential credential, Azure.AI.OpenAI.OpenAIClientOptions options) { }
10-
public OpenAIClient(System.Uri endpoint, string deploymentId, Azure.Core.TokenCredential credential) { }
11-
public OpenAIClient(System.Uri endpoint, string deploymentId, Azure.Core.TokenCredential credential, Azure.AI.OpenAI.OpenAIClientOptions options) { }
1210
public virtual Azure.Core.Pipeline.HttpPipeline Pipeline { get { throw null; } }
1311
public virtual Azure.Response<Azure.AI.OpenAI.Models.Completions> GetCompletions(string deploymentId, Azure.AI.OpenAI.Models.CompletionsOptions completionsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
1412
public virtual Azure.Response GetCompletions(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; }
15-
public virtual Azure.Response<Azure.AI.OpenAI.Models.Completions> GetCompletions(string prompt, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
13+
public virtual Azure.Response<Azure.AI.OpenAI.Models.Completions> GetCompletions(string deploymentId, string prompt, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
1614
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.OpenAI.Models.Completions>> GetCompletionsAsync(string deploymentId, Azure.AI.OpenAI.Models.CompletionsOptions completionsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
1715
public virtual System.Threading.Tasks.Task<Azure.Response> GetCompletionsAsync(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; }
18-
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.OpenAI.Models.Completions>> GetCompletionsAsync(string prompt, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
16+
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.OpenAI.Models.Completions>> GetCompletionsAsync(string deploymentId, string prompt, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
1917
public virtual Azure.Response<Azure.AI.OpenAI.Models.Embeddings> GetEmbeddings(string deploymentId, Azure.AI.OpenAI.Models.EmbeddingsOptions embeddingsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
2018
public virtual Azure.Response GetEmbeddings(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; }
2119
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.OpenAI.Models.Embeddings>> GetEmbeddingsAsync(string deploymentId, Azure.AI.OpenAI.Models.EmbeddingsOptions embeddingsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

sdk/openai/Azure.AI.OpenAI/src/Custom/OpenAIClient.cs

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,69 +16,32 @@ namespace Azure.AI.OpenAI
1616
/// <summary> Azure OpenAI APIs for completions and search. </summary>
1717
public partial class OpenAIClient
1818
{
19-
private readonly string _completionsDeploymentId;
20-
private readonly string _embeddingsDeploymentId;
21-
22-
/// <summary> Initializes a new instance of OpenAIClient. </summary>
23-
/// <param name="endpoint">
24-
/// Supported Cognitive Services endpoints (protocol and hostname, for example:
25-
/// https://westus.api.cognitive.microsoft.com).
26-
/// </param>
27-
/// <param name="deploymentId"> default deployment id to use for operations </param>
28-
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
29-
/// <exception cref="ArgumentNullException"> <paramref name="endpoint"/> or <paramref name="credential"/> is null. </exception>
30-
public OpenAIClient(Uri endpoint, string deploymentId, TokenCredential credential) : this(endpoint, deploymentId, credential, new OpenAIClientOptions())
31-
{
32-
}
33-
34-
/// <summary> Initializes a new instance of OpenAIClient. </summary>
35-
/// <param name="endpoint">
36-
/// Supported Cognitive Services endpoints (protocol and hostname, for example:
37-
/// https://westus.api.cognitive.microsoft.com).
38-
/// </param>
39-
/// <param name="deploymentId"> default deployment id to use for operations </param>
40-
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
41-
/// <param name="options"> The options for configuring the client. </param>
42-
/// <exception cref="ArgumentNullException"> <paramref name="endpoint"/> or <paramref name="credential"/> is null. </exception>
43-
public OpenAIClient(Uri endpoint, string deploymentId, TokenCredential credential, OpenAIClientOptions options)
44-
{
45-
Argument.AssertNotNull(endpoint, nameof(endpoint));
46-
Argument.AssertNotNull(credential, nameof(credential));
47-
options ??= new OpenAIClientOptions();
48-
49-
ClientDiagnostics = new ClientDiagnostics(options, true);
50-
_tokenCredential = credential;
51-
_pipeline = HttpPipelineBuilder.Build(options, Array.Empty<HttpPipelinePolicy>(), new HttpPipelinePolicy[] { new BearerTokenAuthenticationPolicy(_tokenCredential, AuthorizationScopes) }, new ResponseClassifier());
52-
_endpoint = endpoint;
53-
_apiVersion = options.Version;
54-
_completionsDeploymentId ??= deploymentId;
55-
_embeddingsDeploymentId ??= deploymentId;
56-
}
57-
5819
/// <summary> Return the completion for a given prompt. </summary>
20+
/// <param name="deploymentId"> Deployment id (also known as model name) to use for operations </param>
5921
/// <param name="prompt"> Input string prompt to create a prompt completion from a deployment. </param>
6022
/// <param name="cancellationToken"> The cancellation token to use. </param>
61-
public virtual async Task<Response<Completions>> GetCompletionsAsync(string prompt, CancellationToken cancellationToken = default)
23+
public virtual async Task<Response<Completions>> GetCompletionsAsync(string deploymentId, string prompt, CancellationToken cancellationToken = default)
6224
{
63-
Argument.AssertNotNullOrEmpty(_completionsDeploymentId, nameof(_completionsDeploymentId));
25+
Argument.AssertNotNullOrEmpty(deploymentId, nameof(deploymentId));
6426
Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
6527

6628
CompletionsOptions completionsOptions = new CompletionsOptions();
6729
completionsOptions.Prompt.Add(prompt);
68-
return await GetCompletionsAsync(_completionsDeploymentId, completionsOptions, cancellationToken).ConfigureAwait(false);
30+
return await GetCompletionsAsync(deploymentId, completionsOptions, cancellationToken).ConfigureAwait(false);
6931
}
7032

7133
/// <summary> Return the completions for a given prompt. </summary>
34+
/// <param name="deploymentId"> Deployment id (also known as model name) to use for operations </param>
7235
/// <param name="prompt"> Input string prompt to create a prompt completion from a deployment. </param>
7336
/// <param name="cancellationToken"> The cancellation token to use. </param>
74-
public virtual Response<Completions> GetCompletions(string prompt, CancellationToken cancellationToken = default)
37+
public virtual Response<Completions> GetCompletions(string deploymentId, string prompt, CancellationToken cancellationToken = default)
7538
{
76-
Argument.AssertNotNullOrEmpty(_completionsDeploymentId, nameof(_completionsDeploymentId));
39+
Argument.AssertNotNullOrEmpty(deploymentId, nameof(deploymentId));
7740
Argument.AssertNotNullOrEmpty(prompt, nameof(prompt));
7841

7942
CompletionsOptions completionsOptions = new CompletionsOptions();
8043
completionsOptions.Prompt.Add(prompt);
81-
return GetCompletions(_completionsDeploymentId, completionsOptions, cancellationToken);
44+
return GetCompletions(deploymentId, completionsOptions, cancellationToken);
8245
}
8346
}
8447
}

sdk/openai/Azure.AI.OpenAI/tests/OpenAIInferenceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task CompletionTest()
4949
public async Task SimpleCompletionTest()
5050
{
5151
var client = GetClientWithCompletionsDeploymentId();
52-
var response = await client.GetCompletionsAsync("Hello World!");
52+
var response = await client.GetCompletionsAsync(DeploymentId, "Hello World!");
5353
Assert.That(response, Is.InstanceOf<Response<Completions>>());
5454
}
5555

sdk/openai/Azure.AI.OpenAI/tests/OpenAITestBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ protected OpenAIClient GetClientWithCredential() => InstrumentClient(
146146
protected OpenAIClient GetClientWithCompletionsDeploymentId() => InstrumentClient(
147147
new OpenAIClient(
148148
new Uri(_endpoint),
149-
DeploymentId,
150149
TestEnvironment.Credential,
151150
InstrumentClientOptions(new OpenAIClientOptions(OpenAIClientOptions.ServiceVersion.V2022_12_01))));
152151
}

sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample01_Chatbot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public void GetChatbotResponse()
1818
#region Snippet:GenerateChatbotResponse
1919
#region Snippet:CreateOpenAIClientTokenCredential
2020
string endpoint = "https://myaccount.openai.azure.com/";
21-
OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential());
21+
OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
2222
#endregion
2323

2424
string prompt = "What is Azure OpenAI?";
2525
Console.Write($"Input: {prompt}");
2626

27-
Response<Completions> completionsResponse = client.GetCompletions(prompt);
27+
Response<Completions> completionsResponse = client.GetCompletions("myDeploymentId", prompt);
2828
string completion = completionsResponse.Value.Choices[0].Text;
2929
Console.WriteLine($"Chatbot: {completion}");
3030
#endregion

sdk/openai/Azure.AI.OpenAI/tests/Samples/Sample03_SummarizeText.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void SummarizeText()
1717
{
1818
#region Snippet:SummarizeText
1919
string endpoint = "https://myaccount.openai.azure.com/";
20-
OpenAIClient client = new OpenAIClient(new Uri(endpoint), "myDeploymentId", new DefaultAzureCredential());
20+
OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
2121

2222
string textToSummarize = @"
2323
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.

0 commit comments

Comments
 (0)