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

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