Ćwiczenie — zapisywanie monitów o pliki

Ukończone

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!

  1. Otwórz projekt programu Visual Studio Code utworzony w poprzednim ćwiczeniu.

  2. W pliku Program.cs usuń prompt zmienne i input 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();
    
  3. 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!

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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!

  10. 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 monitu SuggestDestinations i rejestrujesz wyniki. Następnie zapytajmy użytkownika, gdzie chce przejść, abyśmy mogli zalecić im niektóre działania.

  11. 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.

  12. 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.