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.

Function_Call_With_Gemini.cs 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Function_Call_With_Gemini.cs
  3. #region Using
  4. using AutoGen.Core;
  5. using Google.Cloud.AIPlatform.V1;
  6. #endregion Using
  7. using FluentAssertions;
  8. namespace AutoGen.Gemini.Sample;
  9. #region MovieFunction
  10. public partial class MovieFunction
  11. {
  12. /// <summary>
  13. /// find movie titles currently playing in theaters based on any description, genre, title words, etc.
  14. /// </summary>
  15. /// <param name="location">The city and state, e.g. San Francisco, CA or a zip code e.g. 95616</param>
  16. /// <param name="description">Any kind of description including category or genre, title words, attributes, etc.</param>
  17. /// <returns></returns>
  18. [Function]
  19. public async Task<string> FindMovies(string location, string description)
  20. {
  21. // dummy implementation
  22. var movies = new List<string> { "Barbie", "Spiderman", "Batman" };
  23. var result = $"Movies playing in {location} based on {description} are: {string.Join(", ", movies)}";
  24. return result;
  25. }
  26. /// <summary>
  27. /// find theaters based on location and optionally movie title which is currently playing in theaters
  28. /// </summary>
  29. /// <param name="location">The city and state, e.g. San Francisco, CA or a zip code e.g. 95616</param>
  30. /// <param name="movie">Any movie title</param>
  31. [Function]
  32. public async Task<string> FindTheaters(string location, string movie)
  33. {
  34. // dummy implementation
  35. var theaters = new List<string> { "AMC", "Regal", "Cinemark" };
  36. var result = $"Theaters playing {movie} in {location} are: {string.Join(", ", theaters)}";
  37. return result;
  38. }
  39. /// <summary>
  40. /// Find the start times for movies playing in a specific theater
  41. /// </summary>
  42. /// <param name="location">The city and state, e.g. San Francisco, CA or a zip code e.g. 95616</param>
  43. /// <param name="movie">Any movie title</param>
  44. /// <param name="theater">Name of the theater</param>
  45. /// <param name="date">Date for requested showtime</param>
  46. /// <returns></returns>
  47. [Function]
  48. public async Task<string> GetShowtimes(string location, string movie, string theater, string date)
  49. {
  50. // dummy implementation
  51. var showtimes = new List<string> { "10:00 AM", "12:00 PM", "2:00 PM", "4:00 PM", "6:00 PM", "8:00 PM" };
  52. var result = $"Showtimes for {movie} at {theater} in {location} are: {string.Join(", ", showtimes)}";
  53. return result;
  54. }
  55. }
  56. #endregion MovieFunction
  57. /// <summary>
  58. /// Modified from https://ai.google.dev/gemini-api/docs/function-calling
  59. /// </summary>
  60. public partial class Function_Call_With_Gemini
  61. {
  62. public static async Task RunAsync()
  63. {
  64. #region Create_Gemini_Agent
  65. var projectID = Environment.GetEnvironmentVariable("GCP_VERTEX_PROJECT_ID");
  66. if (projectID is null)
  67. {
  68. Console.WriteLine("Please set GCP_VERTEX_PROJECT_ID environment variable.");
  69. return;
  70. }
  71. var movieFunction = new MovieFunction();
  72. var functionMiddleware = new FunctionCallMiddleware(
  73. functions: [
  74. movieFunction.FindMoviesFunctionContract,
  75. movieFunction.FindTheatersFunctionContract,
  76. movieFunction.GetShowtimesFunctionContract
  77. ],
  78. functionMap: new Dictionary<string, Func<string, Task<string>>>
  79. {
  80. { movieFunction.FindMoviesFunctionContract.Name!, movieFunction.FindMoviesWrapper },
  81. { movieFunction.FindTheatersFunctionContract.Name!, movieFunction.FindTheatersWrapper },
  82. { movieFunction.GetShowtimesFunctionContract.Name!, movieFunction.GetShowtimesWrapper },
  83. });
  84. var geminiAgent = new GeminiChatAgent(
  85. name: "gemini",
  86. model: "gemini-1.5-flash-001",
  87. location: "us-central1",
  88. project: projectID,
  89. systemMessage: "You are a helpful AI assistant",
  90. toolConfig: new ToolConfig()
  91. {
  92. FunctionCallingConfig = new FunctionCallingConfig()
  93. {
  94. Mode = FunctionCallingConfig.Types.Mode.Auto,
  95. }
  96. })
  97. .RegisterMessageConnector()
  98. .RegisterPrintMessage()
  99. .RegisterStreamingMiddleware(functionMiddleware);
  100. #endregion Create_Gemini_Agent
  101. #region Single_turn
  102. var question = new TextMessage(Role.User, "What movies are showing in North Seattle tonight?");
  103. var functionCallReply = await geminiAgent.SendAsync(question);
  104. #endregion Single_turn
  105. #region Single_turn_verify_reply
  106. functionCallReply.Should().BeOfType<ToolCallAggregateMessage>();
  107. #endregion Single_turn_verify_reply
  108. #region Multi_turn
  109. var finalReply = await geminiAgent.SendAsync(chatHistory: [question, functionCallReply]);
  110. #endregion Multi_turn
  111. #region Multi_turn_verify_reply
  112. finalReply.Should().BeOfType<TextMessage>();
  113. #endregion Multi_turn_verify_reply
  114. }
  115. }