Created
May 2, 2024 16:02
-
-
Save trrwilson/432522fd2540146184439d5deb61828d to your computer and use it in GitHub Desktop.
Azure.AI.OpenAI beta.16 localhost server use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Azure.Core; | |
using Azure.Core.Pipeline; | |
using Azure.AI.OpenAI; | |
#nullable disable | |
namespace AzureOpenAILocalhostExample; | |
public static class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
Uri localhostUri = new("http://localhost:1234/v1/chat/completions"); | |
OpenAIClientOptions clientOptions = new(); | |
clientOptions.AddPolicy( | |
new OverrideRequestUriPolicy(localhostUri), | |
HttpPipelinePosition.BeforeTransport); | |
OpenAIClient client = new(openAIApiKey: "unused", clientOptions); | |
ChatCompletionsOptions options = new() | |
{ | |
DeploymentName = "unused", | |
Messages = | |
{ | |
new ChatRequestSystemMessage("You are a helpful assistant. Be brief and succinct."), | |
new ChatRequestUserMessage("Hello, localhost chat completions!"), | |
} | |
}; | |
StreamingResponse<StreamingChatCompletionsUpdate> streamingChatResponse | |
= await client.GetChatCompletionsStreamingAsync(options); | |
await foreach (StreamingChatCompletionsUpdate chatChunk in streamingChatResponse) | |
{ | |
Console.Write(chatChunk.ContentUpdate); | |
} | |
} | |
} | |
internal partial class OverrideRequestUriPolicy(Uri overrideUri) | |
: HttpPipelineSynchronousPolicy | |
{ | |
private readonly Uri _overrideUri = overrideUri; | |
public override void OnSendingRequest(HttpMessage message) | |
{ | |
message.Request.Uri.Reset(_overrideUri); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment