microsoft / teams-ai

SDK focused on building AI based applications and extensions for Microsoft Teams and other Bot Framework channels
MIT License
419 stars 184 forks source link

[Dev support]: Selecting a Default Prompt Based on User Intent Using OpenAI API in ActionPlanner #2042

Closed asith-w closed 1 month ago

asith-w commented 1 month ago

Question

I have 2 prompts and I'm trying to select a default prompt based on user intent. I decided to make a custom OpenAI API call to detect the default prompt in ActionPlanner. Is this approach a bad idea?

Code snippets

ActionPlanner<AppState> planner = new(
    options: new(
        model: sp.GetService<OpenAIModel>(),
        prompts: prompts,
        defaultPrompt: async (context, state, planner) =>
        {
            string? userInput = context.Activity.Text?.Trim();
            try
            {
                var azureClient = new AzureOpenAIClient(new Uri(openAIEndpoint), new AzureKeyCredential(openAIApiKey));
                var chatClient = azureClient.GetChatClient("Live_GPT4o");
                string prompt = @"You are an AI assistant that identifies user intent. 
Analyze the input and determine if the user is asking to create or generate a mind map.
 If so, respond with:  {""userIntent"": ""CREATION_PROMPT""}
                                Otherwise, respond with:  {  ""userIntent"": """"  } ";
                ChatCompletion completion = chatClient.CompleteChat(
                    new OpenAI.Chat.ChatMessage[] {
                new SystemChatMessage(prompt),
                new UserChatMessage(userInput)
                        },
                        new ChatCompletionOptions
                        {
                            MaxTokens = 30,  // Keep the response concise
                            Temperature = 0.0f,  // Make it deterministic
                        });

                var completionContent = completion.Content[0].Text;

                isCreatePrompt= completionContent.Contains("CREATION_PROMPT");

            }
            catch (Exception ex)
            {

            }

            PromptTemplate template = prompts.GetPrompt(isCreatePrompt? "creation" : "Chat" );
            return await Task.FromResult(template);
        }

    )
    { LogRepairs = true },
    loggerFactory: loggerFactory
);

.

SubbaReddi commented 1 month ago

If your prompts cover a wide range of scenarios, a custom API call can help in accurately matching user intent to the appropriate prompt. However, if the intents are relatively straightforward, simpler methods like keyword matching or rule-based approaches might suffice.