Ćwiczenie — zapisywanie monitów o pliki
Załóżmy, że chcesz zasugerować miejsca docelowe podróży i zalecić działania dla użytkownika. W tym ćwiczeniu przećwiczysz tworzenie monitów i zapisywanie ich w plikach. Zaczynamy!
Otwórz projekt programu Visual Studio Code utworzony w poprzednim ćwiczeniu.
W pliku Program.cs usuń
prompt
zmienne iinput
utworzone w poprzednim ćwiczeniu, aby pozostały z następującym kodem:using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Plugins.Core; var builder = Kernel.CreateBuilder(); builder.AddAzureOpenAIChatCompletion( "your-deployment-name", "your-endpoint", "your-api-key", "deployment-model"); var kernel = builder.Build();
Sprawdź, czy w projekcie istnieją następujące foldery:
- "Monity"
- "Monity/TravelPlugins"
- "Monity/TravelPlugins/SuggestDestinations"
- "Monity/TravelPlugins/GetDestination"
- "Monity/TravelPlugins/SuggestActivities"
Te katalogi ułatwiają organizowanie monitów. Najpierw należy utworzyć monit, który identyfikuje miejsce docelowe, do którego użytkownik chce podróżować. Aby utworzyć monit, należy utworzyć config.json i pliki skprompt.txt. Zaczynamy!
W folderze GetDestination otwórz plik config.json i wprowadź następujący kod:
{ "schema": 1, "type": "completion", "description": "Identify the destination of the user's travel plans", "execution_settings": { "default": { "max_tokens": 1200, "temperature": 0 } }, "input_variables": [ { "name": "input", "description": "Text from the user that contains their travel destination", "required": true } ] }
Ta konfiguracja informuje jądro o tym, co robi monit i jakie zmienne wejściowe mają być akceptowane. Następnie należy podać tekst monitu w pliku skprompt.txt.
W folderze GetDestination otwórz plik skprompt.txt i wprowadź następujący tekst:
<message role="system"> Instructions: Identify the destination the user wants to travel to. </message> <message role="user"> I am so excited to take time off work! My partner and I are thinking about going to Santorini in Greece! I absolutely LOVE Greek food, I can't wait to be some place warm. </message> <message role="assistant">Santorini, Greece</message> <message role="user">{{$input}}</message>
Ten monit pomaga dużemu modelowi językowemu (LLM) filtrować dane wejściowe użytkownika i pobierać tylko miejsce docelowe z tekstu.
W folderze SuggestDestinations otwórz plik config.json i wprowadź następujący tekst:
{ "schema": 1, "type": "completion", "description": "Recommend travel destinations to the user", "execution_settings": { "default": { "max_tokens": 1200, "temperature": 0.3 } }, "input_variables": [ { "name": "input", "description": "Details about the user's travel plans", "required": true } ] }
W tej konfiguracji możesz nieco podnieść temperaturę, aby dane wyjściowe bardziej kreatywne.
W folderze SuggestDestinations otwórz plik skprompt.txt i wprowadź następujący tekst:
The following is a conversation with an AI travel assistant. The assistant is helpful, creative, and very friendly. <message role="user">Can you give me some travel destination suggestions?</message> <message role="assistant">Of course! Do you have a budget or any specific activities in mind?</message> <message role="user">${input}</message>
Ten monit sugeruje podróż do użytkownika na podstawie danych wejściowych. Teraz utwórzmy wtyczkę, aby zalecić działania w ich miejscu docelowym.
W folderze SuggestActivities otwórz plik config.json i wprowadź następujący tekst:
{ "schema": 1, "type": "completion", "description": "Recommend activities at a travel destination to the user", "execution_settings": { "default": { "max_tokens": 4000, "temperature": 0.3 } }, "input_variables": [ { "name": "history", "description": "Background information about the user", "required": true }, { "name": "destination", "description": "The user's travel destination", "required": true } ] }
W tej konfiguracji zwiększysz
max_tokens
wartość , aby zezwolić na więcej tekstu dla historii i wygenerowany tekst.W folderze SuggestActivities otwórz plik skprompt.txt i wprowadź następujący tekst:
You are a travel assistant. You are helpful, creative, and very friendly. Consider your previous conversation with the traveler: {{$history}} The traveler would like some activity recommendations, things to do, and points of interest for their trip. They want to go to {{$destination}}. Please provide them with a list of things they might like to do at their chosen destination.
Teraz zaimportujmy i przetestujmy nowe monity!
Zaktualizuj plik Program.cs przy użyciu następującego kodu:
using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Plugins.Core; using Microsoft.SemanticKernel.ChatCompletion; var builder = Kernel.CreateBuilder(); builder.AddAzureOpenAIChatCompletion( "your-deployment-name", "your-endpoint", "your-api-key", "deployment-model"); var kernel = builder.Build(); kernel.ImportPluginFromType<ConversationSummaryPlugin>(); var prompts = kernel.ImportPluginFromPromptDirectory("Prompts/TravelPlugins"); ChatHistory history = []; string input = @"I'm planning an anniversary trip with my spouse. We like hiking, mountains, and beaches. Our travel budget is $15000"; var result = await kernel.InvokeAsync<string>(prompts["SuggestDestinations"], new() {{ "input", input }}); Console.WriteLine(result); history.AddUserMessage(input); history.AddAssistantMessage(result);
W tym kodzie zaimportujesz utworzone wtyczki. Obiekt służy
ChatHistory
również do przechowywania konwersacji użytkownika. Na koniec przekazujesz kilka informacji do monituSuggestDestinations
i rejestrujesz wyniki. Następnie zapytajmy użytkownika, gdzie chce przejść, abyśmy mogli zalecić im niektóre działania.Dodaj następujący kod do pliku Program.cs :
Console.WriteLine("Where would you like to go?"); input = Console.ReadLine(); result = await kernel.InvokeAsync<string>(prompts["SuggestActivities"], new() { { "history", history }, { "destination", input }, } ); Console.WriteLine(result);
W tym kodzie uzyskasz pewne dane wejściowe od użytkownika, aby dowiedzieć się, gdzie chcesz przejść. Następnie wywołasz
SuggestActivities
monit z miejscem docelowym i historią konwersacji.Aby przetestować kod, wprowadź
dotnet run
w terminalu.Ostateczne dane wyjściowe mogą wyglądać podobnie do następujących:
Absolutely! Japan is a wonderful destination with so much to see and do. Here are some recommendations for activities and points of interest: 1. Visit Tokyo Tower: This iconic tower offers stunning views of the city and is a must-visit attraction. 2. Explore the temples of Kyoto: Kyoto is home to many beautiful temples, including the famous Kiyomizu-dera and Fushimi Inari-taisha. 3. Experience traditional Japanese culture: Attend a tea ceremony, try on a kimono, or take a calligraphy class to immerse yourself in Japanese culture.
Teraz utworzono początki asystenta podróży sztucznej inteligencji! Spróbuj zmienić dane wejściowe, aby zobaczyć, jak reaguje usługa LLM.