# Azure OpenAI client library for .NET
The Azure OpenAI client library for .NET is an adaptation of OpenAI's REST APIs that provides an idiomatic interface
and rich integration with the rest of the Azure SDK ecosystem. It can connect to Azure OpenAI resources *or* to the
non-Azure OpenAI inference endpoint, making it a great choice for even non-Azure OpenAI development.
Use the client library for Azure OpenAI to:
* [Create a completion for text][msdocs_openai_completion]
* [Create a text embedding for comparisons][msdocs_openai_embedding]
Azure OpenAI is a managed service that allows developers to deploy, tune, and generate content from OpenAI models on Azure resources.
[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/Azure.AI.OpenAI) | [API reference documentation](https://learn.microsoft.com/azure/cognitive-services/openai/reference) | [Product documentation](https://learn.microsoft.com/azure/cognitive-services/openai/) | [Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/tests/Samples)
## Getting started
### Prerequisites
If you'd like to use an Azure OpenAI resource, you must have an [Azure subscription](https://azure.microsoft.com/free/dotnet/)
and [Azure OpenAI access](https://learn.microsoft.com/azure/cognitive-services/openai/overview#how-do-i-get-access-to-azure-openai).
This will allow you to create an Azure OpenAI resource and get both a connection URL as well as API keys. For more
information, see [Quickstart: Get started generating text using Azure OpenAI Service](https://learn.microsoft.com/azure/cognitive-services/openai/quickstart).
If you'd like to use the Azure OpenAI .NET client library to connect to non-Azure OpenAI, you'll need an API key
from a developer account at https://platform.openai.com/.
### Install the package
Install the client library for .NET with [NuGet](https://www.nuget.org/ ):
```dotnetcli
dotnet add package Azure.AI.OpenAI --prerelease
```
### Authenticate the client
In order to interact with Azure OpenAI or OpenAI, you'll need to create an instance of the [OpenAIClient][openai_client_class]
class. To configure a client for use with Azure OpenAI, provide a valid endpoint URI to an Azure OpenAI resource
along with a corresponding key credential, token credential, or Azure identity credential that's authorized to use the
Azure OpenAI resource. To instead configure the client to connect to OpenAI's service, provide an API key from OpenAI's
developer portal.
```C# Snippet:MakeClientWithAzureOrNonAzureOpenAI
OpenAIClient client = useAzureOpenAI
? new OpenAIClient(
new Uri("https://your-azure-openai-resource.com/"),
new AzureKeyCredential("your-azure-openai-resource-api-key"))
: new OpenAIClient("your-api-key-from-platform.openai.com");
```
#### Create OpenAIClient with an Azure Active Directory Credential
Client subscription key authentication is used in most of the examples in this getting started guide, but you can also authenticate with Azure Active Directory using the [Azure Identity library][azure_identity]. To use the [DefaultAzureCredential][azure_identity_dac] provider shown below,
or other credential providers provided with the Azure SDK, please install the Azure.Identity package:
```dotnetcli
dotnet add package Azure.Identity
```
```C# Snippet:CreateOpenAIClientTokenCredential
string endpoint = "https://myaccount.openai.azure.com/";
var client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
```
## Key concepts
The main concept to understand is [Completions][azure_openai_completions_docs]. Briefly explained, completions provides its functionality in the form of a text prompt, which by using a specific [model](https://learn.microsoft.com/azure/cognitive-services/openai/concepts/models), will then attempt to match the context and patterns, providing an output text. The following code snippet provides a rough overview (more details can be found in the `GenerateChatbotResponsesWithToken` sample code):
```C# Snippet:UseAzureOrNonAzureOpenAI
OpenAIClient client = useAzureOpenAI
? new OpenAIClient(
new Uri("https://your-azure-openai-resource.com/"),
new AzureKeyCredential("your-azure-openai-resource-api-key"))
: new OpenAIClient("your-api-key-from-platform.openai.com");
Response<Completions> response = await client.GetCompletionsAsync(new CompletionsOptions()
{
DeploymentName = "text-davinci-003", // assumes a matching model deployment or model name
Prompts = { "Hello, world!" },
});
foreach (Choice choice in response.Value.Choices)
{
Console.WriteLine(choice.Text);
}
```
### 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 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://learn.microsoft.com/dotnet/azure/sdk/unit-testing-mocking) |
[Client lifetime](https://devblogs.microsoft.com/azure-sdk/lifetime-management-and-thread-safety-guarantees-of-azure-sdk-net-clients/)
## Examples
You can familiarize yourself with different APIs using [Samples](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/openai/Azure.AI.OpenAI/tests/Samples).
### Generate chatbot response
The `GenerateChatbotResponse` method authenticates using a DefaultAzureCredential, then generates text responses to input prompts.
```C# Snippet:GenerateChatbotResponse
string endpoint = "https://myaccount.openai.azure.com/";
var client = new OpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
CompletionsOptions completionsOptions = new()
{
DeploymentName = "text-davinci-003",
Prompts = { "What is Azure OpenAI?" },
};
Response<Completions> completionsResponse = client.GetCompletions(completionsOptions);
string completion = completionsResponse.Value.Choices[0].Text;
Console.WriteLine($"Chatbot: {completion}");
```
### Generate multiple chatbot responses with subscription key
The `GenerateMultipleChatbotResponsesWithSubscriptionKey` method gives an example of generating text responses to input prompts using an Azure subscription key
```C# Snippet:GenerateMultipleChatbotResponsesWithSubscriptionKey
// Replace with your Azure OpenAI key
string key = "YOUR_AZURE_OPENAI_KEY";
string endpoint = "https://myaccount.openai.azure.com/";
var client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
CompletionsOptions completionsOptions = new()
{
DeploymentName = "text-davinci-003",
Prompts =
{
"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."
},
};
Response<Completions> completionsResponse = client.GetCompletions(completionsOptions);
没有合适的资源?快使用搜索试试~ 我知道了~
通过C#代码创建IIS应用程序池和网站
共566个文件
dll:180个
xml:82个
_:56个
需积分: 5 0 下载量 118 浏览量
2023-11-28
17:14:40
上传
评论
收藏 45.68MB ZIP 举报
温馨提示
創建的站點 在 Sites下面的 Default Web Site 下面 創建 站點
资源推荐
资源详情
资源评论
收起资源包目录
通过C#代码创建IIS应用程序池和网站 (566个子文件)
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
_._ 0B
Global.asax 100B
MainHandler.ashx 106B
Main.aspx 8KB
ConsoleApp2.csproj.AssemblyReference.cache 43KB
LiteonAIGCWebApp.csproj.AssemblyReference.cache 33KB
DesignTimeResolveAssemblyReferencesInput.cache 13KB
WindowsFormsOpenai.csproj.AssemblyReference.cache 10KB
DesignTimeResolveAssemblyReferencesInput.cache 9KB
DesignTimeResolveAssemblyReferencesInput.cache 7KB
DesignTimeResolveAssemblyReferences.cache 2KB
DesignTimeResolveAssemblyReferences.cache 342B
DesignTimeResolveAssemblyReferences.cache 134B
WindowsFormsOpenai.csproj.GenerateResource.cache 126B
LiteonAIGCWebApp.csproj.CoreCompileInputs.cache 42B
WindowsFormsOpenai.csproj.CoreCompileInputs.cache 42B
ConsoleApp2.csproj.CoreCompileInputs.cache 42B
WindowsFormsOpenai.csproj.SuggestedBindingRedirects.cache 0B
ConsoleApp2.csproj.SuggestedBindingRedirects.cache 0B
applicationhost.config 85KB
csi.exe.config 8KB
csi.exe.config 8KB
VBCSCompiler.exe.config 8KB
VBCSCompiler.exe.config 8KB
vbc.exe.config 8KB
csc.exe.config 8KB
vbc.exe.config 8KB
csc.exe.config 8KB
LiteonAIGCWebApp.dll.config 3KB
Web.config 3KB
App.config 2KB
ConsoleApp2.exe.config 2KB
VBCSCompiler.exe.config 2KB
vbc.exe.config 2KB
csc.exe.config 2KB
Web.Release.config 1KB
Web.Debug.config 1KB
packages.config 1KB
packages.config 1KB
App.config 189B
WindowsFormsOpenai.exe.config 189B
LiteonAIGCWebApp.csproj.CopyComplete 0B
ConsoleApp2.csproj.CopyComplete 0B
Program.cs 24KB
Form1.Designer.cs 7KB
共 566 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
凭萍
- 粉丝: 1
- 资源: 43
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- docker一键安装包
- Screenshot_20240430_144340_com.ss.android.ugc.live.jpg
- 回到山沟沟.mp3
- 基于matlab实现自适应波束形成RLS及LMS算法仿真源程序1.rar
- 基于matlab实现自己编写的基于卡尔曼滤波的利用加速度传感器的计步器,测试数据是传感器放在腰部和手臂 .rar
- 基于matlab实现阵列信号处理,波束形成.rar
- 111111111111111111
- 基于matlab实现计步器编程;对当前的计步器装置的数值算法模拟 .rar
- Mdb学习查看PW;access;mdb;pw;password;patch
- 基于matlab实现关于语音信号声源定位DOA估计所用的一些传统算法.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功