// Copyright (c) Microsoft Corporation. All rights reserved. // Function_Call_With_Gemini.cs #region Using using AutoGen.Core; using Google.Cloud.AIPlatform.V1; #endregion Using using FluentAssertions; namespace AutoGen.Gemini.Sample; #region MovieFunction public partial class MovieFunction { /// /// find movie titles currently playing in theaters based on any description, genre, title words, etc. /// /// The city and state, e.g. San Francisco, CA or a zip code e.g. 95616 /// Any kind of description including category or genre, title words, attributes, etc. /// [Function] public async Task FindMovies(string location, string description) { // dummy implementation var movies = new List { "Barbie", "Spiderman", "Batman" }; var result = $"Movies playing in {location} based on {description} are: {string.Join(", ", movies)}"; return result; } /// /// find theaters based on location and optionally movie title which is currently playing in theaters /// /// The city and state, e.g. San Francisco, CA or a zip code e.g. 95616 /// Any movie title [Function] public async Task FindTheaters(string location, string movie) { // dummy implementation var theaters = new List { "AMC", "Regal", "Cinemark" }; var result = $"Theaters playing {movie} in {location} are: {string.Join(", ", theaters)}"; return result; } /// /// Find the start times for movies playing in a specific theater /// /// The city and state, e.g. San Francisco, CA or a zip code e.g. 95616 /// Any movie title /// Name of the theater /// Date for requested showtime /// [Function] public async Task GetShowtimes(string location, string movie, string theater, string date) { // dummy implementation var showtimes = new List { "10:00 AM", "12:00 PM", "2:00 PM", "4:00 PM", "6:00 PM", "8:00 PM" }; var result = $"Showtimes for {movie} at {theater} in {location} are: {string.Join(", ", showtimes)}"; return result; } } #endregion MovieFunction /// /// Modified from https://ai.google.dev/gemini-api/docs/function-calling /// public partial class Function_Call_With_Gemini { public static async Task RunAsync() { #region Create_Gemini_Agent var projectID = Environment.GetEnvironmentVariable("GCP_VERTEX_PROJECT_ID"); if (projectID is null) { Console.WriteLine("Please set GCP_VERTEX_PROJECT_ID environment variable."); return; } var movieFunction = new MovieFunction(); var functionMiddleware = new FunctionCallMiddleware( functions: [ movieFunction.FindMoviesFunctionContract, movieFunction.FindTheatersFunctionContract, movieFunction.GetShowtimesFunctionContract ], functionMap: new Dictionary>> { { movieFunction.FindMoviesFunctionContract.Name!, movieFunction.FindMoviesWrapper }, { movieFunction.FindTheatersFunctionContract.Name!, movieFunction.FindTheatersWrapper }, { movieFunction.GetShowtimesFunctionContract.Name!, movieFunction.GetShowtimesWrapper }, }); var geminiAgent = new GeminiChatAgent( name: "gemini", model: "gemini-1.5-flash-001", location: "us-central1", project: projectID, systemMessage: "You are a helpful AI assistant", toolConfig: new ToolConfig() { FunctionCallingConfig = new FunctionCallingConfig() { Mode = FunctionCallingConfig.Types.Mode.Auto, } }) .RegisterMessageConnector() .RegisterPrintMessage() .RegisterStreamingMiddleware(functionMiddleware); #endregion Create_Gemini_Agent #region Single_turn var question = new TextMessage(Role.User, "What movies are showing in North Seattle tonight?"); var functionCallReply = await geminiAgent.SendAsync(question); #endregion Single_turn #region Single_turn_verify_reply functionCallReply.Should().BeOfType(); #endregion Single_turn_verify_reply #region Multi_turn var finalReply = await geminiAgent.SendAsync(chatHistory: [question, functionCallReply]); #endregion Multi_turn #region Multi_turn_verify_reply finalReply.Should().BeOfType(); #endregion Multi_turn_verify_reply } }