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