You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Connect_To_Azure_OpenAI.cs 1.6 kB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Connect_To_Azure_OpenAI.cs
  3. #region using_statement
  4. using System.ClientModel;
  5. using AutoGen.Core;
  6. using AutoGen.OpenAI.Extension;
  7. using Azure.AI.OpenAI;
  8. #endregion using_statement
  9. namespace AutoGen.OpenAI.Sample;
  10. public class Connect_To_Azure_OpenAI
  11. {
  12. public static async Task RunAsync()
  13. {
  14. #region create_agent
  15. var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new InvalidOperationException("Please set environment variable AZURE_OPENAI_API_KEY");
  16. var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("Please set environment variable AZURE_OPENAI_ENDPOINT");
  17. var model = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOY_NAME") ?? "gpt-4o-mini";
  18. // Use AzureOpenAIClient to connect to openai model deployed on azure.
  19. // The AzureOpenAIClient comes from Azure.AI.OpenAI package
  20. var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey));
  21. var agent = new OpenAIChatAgent(
  22. chatClient: openAIClient.GetChatClient(model),
  23. name: "assistant",
  24. systemMessage: "You are a helpful assistant designed to output JSON.",
  25. seed: 0)
  26. .RegisterMessageConnector()
  27. .RegisterPrintMessage();
  28. #endregion create_agent
  29. #region send_message
  30. await agent.SendAsync("Can you write a piece of C# code to calculate 100th of fibonacci?");
  31. #endregion send_message
  32. }
  33. }