Skip to content

Commit

Permalink
Initial checkin for openai (Azure#33457)
Browse files Browse the repository at this point in the history
* initial wip

* update to new location

* Add netframework 4.7 to platforms in Inference, add test for client creation and simple completion

* add example test secrets file

* updated secrets instructions

* Remove custom code for CompletionsRequest, update test

* fix an edge case around calculating resourcetype from resourceid

* Don't use CSharp package, remove VersionOverrides

* update test case to use testframework patterns

* drop net7 from frameworks and add api file

* remove shared source since the temp authoring client isn't needed

* remove unused fields

* remove old file

* add template changelog and readme

* create real snippet example

* remove python reference

* Add hero scenario sample with updated README

* Add service description to README

* Pull in latest generated Embeddings classes from https://github.com/Azure/azure-sdk-for-net/blob/feature/codegened-AOAIinference-models, add embeddings test with associated deployment model variables

* Update OpenAIInferenceTests.cs

Make default test run mode Recorded

* Added sample and updated test to use DefaultAzureCredential authentication path as well

* update to latest spec

* update to point at main feature branch

* update ci.yml based on comments

* update spelling

* update link

* update api

* remove samples link until we have them written

* revert uneeded change

* added examples header back into readme

* rename package to Azure.AI.OpenAI

* update ci triggers

* update api fle

* fix embedding recordings

* update cadl location to latest feature/cognitiveservices/openai-cadl sha

Co-authored-by: Glenn Harper <glharper@microsoft.com>
Co-authored-by: Glenn Harper <64209257+glharper@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 25, 2023
1 parent ab6c38d commit 18478a8
Show file tree
Hide file tree
Showing 44 changed files with 27,337 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ artifacts
.assets

# Temporary cadl folders for cadl generation
TempCadlFiles/
TempCadlFiles/
8 changes: 8 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,14 @@
"Ackable",
"awps"
]
},
{
"filename": "**/sdk/openai/**/*.cs",
"words": [
"Probs",
"Logprobs",
"Logit"
]
}
],
"allowCompoundWords": true
Expand Down
1 change: 1 addition & 0 deletions eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
<PackageReference Update="Azure.Messaging.EventGrid" Version="4.0.0" />
<PackageReference Update="Azure.Messaging.ServiceBus" Version="7.11.1" />
<PackageReference Update="Azure.ResourceManager.Compute" Version="1.0.0" />
<PackageReference Update="Azure.ResourceManager.CognitiveServices" Version="1.1.0" />
<PackageReference Update="Azure.ResourceManager.Network" Version="1.1.0" />
<PackageReference Update="Azure.ResourceManager.OperationalInsights" Version="1.0.0" />
<PackageReference Update="Azure.ResourceManager.Resources" Version="1.3.1" />
Expand Down
41 changes: 41 additions & 0 deletions sdk/openai/Azure.AI.OpenAI/Azure.AI.OpenAI.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29806.167
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.AI.OpenAI", "src\Azure.AI.OpenAI.csproj", "{E33D09D9-D809-472C-82E6-6A26BDB86FC2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.AI.OpenAI.Tests", "tests\Azure.AI.OpenAI.Tests.csproj", "{4F476D56-DDE7-43D3-8CB4-BA1E77F5A300}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core.TestFramework", "..\..\core\Azure.Core.TestFramework\src\Azure.Core.TestFramework.csproj", "{439F1494-8E96-4931-AB0A-5BBA7EBA15D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E33D09D9-D809-472C-82E6-6A26BDB86FC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E33D09D9-D809-472C-82E6-6A26BDB86FC2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E33D09D9-D809-472C-82E6-6A26BDB86FC2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E33D09D9-D809-472C-82E6-6A26BDB86FC2}.Release|Any CPU.Build.0 = Release|Any CPU
{4F476D56-DDE7-43D3-8CB4-BA1E77F5A300}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F476D56-DDE7-43D3-8CB4-BA1E77F5A300}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F476D56-DDE7-43D3-8CB4-BA1E77F5A300}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F476D56-DDE7-43D3-8CB4-BA1E77F5A300}.Release|Any CPU.Build.0 = Release|Any CPU
{439F1494-8E96-4931-AB0A-5BBA7EBA15D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{439F1494-8E96-4931-AB0A-5BBA7EBA15D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{439F1494-8E96-4931-AB0A-5BBA7EBA15D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{439F1494-8E96-4931-AB0A-5BBA7EBA15D2}.Release|Any CPU.Build.0 = Release|Any CPU
{71C00248-3DEE-4C44-A2BB-4CCAF994DB32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71C00248-3DEE-4C44-A2BB-4CCAF994DB32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71C00248-3DEE-4C44-A2BB-4CCAF994DB32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71C00248-3DEE-4C44-A2BB-4CCAF994DB32}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A97F4B90-2591-4689-B1F8-5F21FE6D6CAE}
EndGlobalSection
EndGlobal
11 changes: 11 additions & 0 deletions sdk/openai/Azure.AI.OpenAI/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Release History

## 1.0.0-beta.1 (Unreleased)

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes
6 changes: 6 additions & 0 deletions sdk/openai/Azure.AI.OpenAI/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
Add any shared properties you want for the projects under this package directory that need to be set before the auto imported Directory.Build.props
-->
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory).., Directory.Build.props))\Directory.Build.props" />
</Project>
159 changes: 159 additions & 0 deletions sdk/openai/Azure.AI.OpenAI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Azure OpenAI client library for .NET

Azure OpenAI is a managed service that allows developers to deploy, tune, and generate content from OpenAI models on Azure resouces.

Use the client library for to:

* [Get secret](https://docs.microsoft.com/azure)

[Source code][source_root] | [Package (NuGet)][package] | [API reference documentation][reference_docs] | [Product documentation][azconfig_docs] | [Samples][source_samples]

[Source code](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/src) | [Package (NuGet)](https://www.nuget.org/packages) | [API reference documentation](https://azure.github.io/azure-sdk-for-net) | [Product documentation](https://docs.microsoft.com/azure)

## Getting started

This section should include everything a developer needs to do to install and create their first client connection *very quickly*.

### Install the package

First, provide instruction for obtaining and installing the package or library. This section might include only a single line of code, like `dotnet add package package-name`, but should enable a developer to successfully install the package from NuGet, npm, or even cloning a GitHub repository.

Install the client library for .NET with [NuGet](https://www.nuget.org/ ):

```dotnetcli
dotnet add package Azure.AI.OpenAI --prerelease
```

### Prerequisites

Include a section after the install command that details any requirements that must be satisfied before a developer can [authenticate](#authenticate-the-client) and test all of the snippets in the [Examples](#examples) section. For example, for Cosmos DB:

> You must have an [Azure subscription](https://azure.microsoft.com/free/dotnet/) and [Cosmos DB account](https://docs.microsoft.com/azure/cosmos-db/account-overview) (SQL API). In order to take advantage of the C# 8.0 syntax, it is recommended that you compile using the [.NET Core SDK](https://dotnet.microsoft.com/download) 3.0 or higher with a [language version](https://docs.microsoft.com/dotnet/csharp/language-reference/configure-language-version#override-a-default) of `latest`. It is also possible to compile with the .NET Core SDK 2.1.x using a language version of `preview`.
### Authenticate the client

If your library requires authentication for use, such as for Azure services, include instructions and example code needed for initializing and authenticating.

For example, include details on obtaining an account key and endpoint URI, setting environment variables for each, and initializing the client object.

## Key concepts

The *Key concepts* section should describe the functionality of the main classes. Point out the most important and useful classes in the package (with links to their reference pages) and explain how those classes work together. Feel free to use bulleted lists, tables, code blocks, or even diagrams for clarity.

Include the *Thread safety* and *Additional concepts* sections below at the end of your *Key concepts* section. You may remove or add links depending on what your library makes use of:

### Thread safety

We guarantee that all client instance methods are thread-safe and independent of each other ([guideline](https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-service-methods-thread-safety)). This ensures that the recommendation of reusing client instances is always safe, even across threads.

### Additional concepts
<!-- CLIENT COMMON BAR -->
[Client options](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#configuring-service-clients-using-clientoptions) |
[Accessing the response](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#accessing-http-response-details-using-responset) |
[Long-running operations](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#consuming-long-running-operations-using-operationt) |
[Handling failures](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#reporting-errors-requestfailedexception) |
[Diagnostics](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md) |
[Mocking](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/README.md#mocking) |
[Client lifetime](https://devblogs.microsoft.com/azure-sdk/lifetime-management-and-thread-safety-guarantees-of-azure-sdk-net-clients/)
<!-- CLIENT COMMON BAR -->

## Examples

You can familiarize yourself with different APIs using Samples.

### Get secret

The `GetSecret` method retrieves a secret from the service.

```C# Snippet:Azure_OpenAI_GetSecret
string endpoint = "http://myaccount.openai.azure.com/";
string key = "myKey";

OpenAIClient client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
CompletionsRequest completionsRequest = new CompletionsRequest();
completionsRequest.Prompt.Add("Hello world");
completionsRequest.Prompt.Add("running over the same old ground");
Completion response = client.Completions("myModelDeployment", completionsRequest);

foreach (Choice choice in response.Choices)
{
Console.WriteLine(choice.Text);
}
```

### Generate Chatbot Responses

The `GenerateChatbotResponses` method gives an example of generating text responses to input prompts.

```C# Snippet:GenerateChatbotResponses
List<string> examplePrompts = new(){
"How are you today?",
"What is Azure OpenAI?",
"Why do children love dinosaurs?",
"Generate a proof of Euler's identity",
"Describe in single words only the good things that come into your mind about your mother.",
};

foreach (var prompt in examplePrompts)
{
Console.Write($"Input: {prompt}");
var request = new CompletionsRequest();
request.Prompt.Add(prompt);

Completion completion = client.Completions("myModelDeployment", request);
var response = completion.Choices[0].Text;
Console.WriteLine($"Chatbot: {response}");
}
```

### Generate Chatbot Responses With Token

The `GenerateChatbotResponsesWithToken` method authenticates using a DefaultAzureCredential, then generates text responses to input prompts.

```C# Snippet:GenerateChatbotResponsesWithToken
string endpoint = "http://myaccount.openai.azure.com/";
OpenAIClient client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential());

List<string> examplePrompts = new(){
"How are you today?",
"What is Azure OpenAI?",
"Why do children love dinosaurs?",
"Generate a proof of Euler's identity",
"Describe in single words only the good things that come into your mind about your mother.",
};

foreach (var prompt in examplePrompts)
{
Console.Write($"Input: {prompt}");
var request = new CompletionsRequest();
request.Prompt.Add(prompt);

Completion completion = client.Completions("myModelDeployment", request);
var response = completion.Choices[0].Text;
Console.WriteLine($"Chatbot: {response}");
}
```

## Troubleshooting

Describe common errors and exceptions, how to "unpack" them if necessary, and include guidance for graceful handling and recovery.

Provide information to help developers avoid throttling or other service-enforced errors they might encounter. For example, provide guidance and examples for using retry or connection policies in the API.

If the package or a related package supports it, include tips for logging or enabling instrumentation to help them debug their code.

## Next steps

* Provide a link to additional code examples, ideally to those sitting alongside the README in the package's `/samples` directory.
* If appropriate, point users to other packages that might be useful.
* If you think there's a good chance that developers might stumble across your package in error (because they're searching for specific functionality and mistakenly think the package provides that functionality), point them to the packages they might be looking for.

## Contributing

This is a template, but your SDK readme should include details on how to contribute code to the repo/package.

<!-- LINKS -->
[style-guide-msft]: https://docs.microsoft.com/style-guide/capitalization
[style-guide-cloud]: https://aka.ms/azsdk/cloud-style-guide

![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-net/sdk/openai/Azure.AI.OpenAI/README.png)
98 changes: 98 additions & 0 deletions sdk/openai/Azure.AI.OpenAI/api/Azure.AI.OpenAI.netstandard2.0.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace Azure.AI.OpenAI
{
public partial class OpenAIClient
{
protected OpenAIClient() { }
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 virtual Azure.Core.Pipeline.HttpPipeline Pipeline { get { throw null; } }
public virtual Azure.Response<Azure.AI.OpenAI.Models.Completion> Completions(string deploymentId, Azure.AI.OpenAI.Models.CompletionsRequest completionsRequest, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response Completions(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.OpenAI.Models.Completion>> CompletionsAsync(string deploymentId, Azure.AI.OpenAI.Models.CompletionsRequest completionsRequest, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> CompletionsAsync(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; }
public virtual Azure.Response<Azure.AI.OpenAI.Models.Embeddings> Embeddings(string deploymentId, Azure.AI.OpenAI.Models.EmbeddingsRequest embeddingsRequest, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response Embeddings(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.OpenAI.Models.Embeddings>> EmbeddingsAsync(string deploymentId, Azure.AI.OpenAI.Models.EmbeddingsRequest embeddingsRequest, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> EmbeddingsAsync(string deploymentId, Azure.Core.RequestContent content, Azure.RequestContext context = null) { throw null; }
}
public partial class OpenAIClientOptions : Azure.Core.ClientOptions
{
public OpenAIClientOptions(Azure.AI.OpenAI.OpenAIClientOptions.ServiceVersion version = Azure.AI.OpenAI.OpenAIClientOptions.ServiceVersion.V2022_06_01_Preview) { }
public enum ServiceVersion
{
V2022_06_01_Preview = 1,
}
}
}
namespace Azure.AI.OpenAI.Models
{
public partial class Choice
{
internal Choice() { }
public string FinishReason { get { throw null; } }
public int? Index { get { throw null; } }
public Azure.AI.OpenAI.Models.CompletionsLogProbsModel Logprobs { get { throw null; } }
public string Text { get { throw null; } }
}
public partial class Completion
{
internal Completion() { }
public System.Collections.Generic.IReadOnlyList<Azure.AI.OpenAI.Models.Choice> Choices { get { throw null; } }
public int? Created { get { throw null; } }
public string Id { get { throw null; } }
public string Model { get { throw null; } }
public string Object { get { throw null; } }
}
public partial class CompletionsLogProbsModel
{
internal CompletionsLogProbsModel() { }
public System.Collections.Generic.IReadOnlyList<int> TextOffset { get { throw null; } }
public System.Collections.Generic.IReadOnlyList<float> TokenLogprobs { get { throw null; } }
public System.Collections.Generic.IReadOnlyList<string> Tokens { get { throw null; } }
public System.Collections.Generic.IReadOnlyList<System.Collections.Generic.IDictionary<string, float>> TopLogprobs { get { throw null; } }
}
public partial class CompletionsRequest
{
public CompletionsRequest() { }
public int? BestOf { get { throw null; } set { } }
public int? CacheLevel { get { throw null; } set { } }
public string CompletionConfig { get { throw null; } set { } }
public bool? Echo { get { throw null; } set { } }
public float? FrequencyPenalty { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, int> LogitBias { get { throw null; } }
public int? Logprobs { get { throw null; } set { } }
public int? MaxTokens { get { throw null; } set { } }
public string Model { get { throw null; } set { } }
public int? N { get { throw null; } set { } }
public float? PresencePenalty { get { throw null; } set { } }
public System.Collections.Generic.IList<string> Prompt { get { throw null; } }
public System.Collections.Generic.IList<string> Stop { get { throw null; } }
public bool? Stream { get { throw null; } set { } }
public float? Temperature { get { throw null; } set { } }
public float? TopP { get { throw null; } set { } }
public string User { get { throw null; } set { } }
}
public partial class EmbeddingItem
{
internal EmbeddingItem() { }
public System.Collections.Generic.IReadOnlyList<float> Embedding { get { throw null; } }
public int Index { get { throw null; } }
public string Object { get { throw null; } }
}
public partial class Embeddings
{
internal Embeddings() { }
public System.Collections.Generic.IReadOnlyList<Azure.AI.OpenAI.Models.EmbeddingItem> Data { get { throw null; } }
public string Object { get { throw null; } }
}
public partial class EmbeddingsRequest
{
public EmbeddingsRequest(string input) { }
public string Input { get { throw null; } }
public string InputType { get { throw null; } set { } }
public string Model { get { throw null; } set { } }
public string User { get { throw null; } set { } }
}
}
25 changes: 25 additions & 0 deletions sdk/openai/Azure.AI.OpenAI/src/Azure.AI.OpenAI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>This is the Microsoft Azure Cognitive Services OpenAI client library</Description>
<AssemblyTitle>Microsoft Azure.AI.OpenAI client library</AssemblyTitle>
<Version>1.0.0-beta.1</Version>
<PackageTags>Microsoft Azure OpenAI</PackageTags>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
<GenerateAPIListing>true</GenerateAPIListing>
<NoWarn>$(NoWarn);CS1591;AZC0012;AZC0001</NoWarn>
<IncludeOperationsSharedSource>true</IncludeOperationsSharedSource>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Core" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>

<!-- Shared source from Azure.Core -->
<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)ArrayBufferWriter.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)AzureKeyCredentialPolicy.cs" LinkBase="Shared" />
<Compile Include="$(AzureCoreSharedSources)AzureResourceProviderNamespaceAttribute.cs" LinkBase="Shared" />
</ItemGroup>

</Project>
Loading

0 comments on commit 18478a8

Please sign in to comment.