79264170

Date: 2024-12-09 07:43:59
Score: 3
Natty:
Report link

Below is the code to send a request to a custom PromptFlow endpoint hosted in Azure, allowing interaction with an AI model deployment.

 var chatHistory = new[]
        {
            new { role = "system", content = "You are a helpful customer support chatbot." },
            new { role = "user", content = "Hi, I need help with my subscription." },
            new { role = "assistant", content = "Sure, can you provide more details about the issue?" }
        };

        var prompt = "What are the steps to cancel my subscription?";
        var requestBody = new
        {
            chat_history = chatHistory,
            question = prompt
        };

        string jsonRequestBody = JsonConvert.SerializeObject(requestBody);

        try
        {
            var response = await CallPromptFlowEndpoint(jsonRequestBody);
            Console.WriteLine("API Response:");
            Console.WriteLine(response);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }

    private static async Task<string> CallPromptFlowEndpoint(string jsonRequestBody)
    {
        using (HttpClient client = new HttpClient())
        {
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {ApiKey}");
            client.DefaultRequestHeaders.Add("azureml-model-deployment", ModelDeploymentName);
            var response = await client.PostAsync(
                EndpointUrl,
                new StringContent(jsonRequestBody, Encoding.UTF8, "application/json")
            );

            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }
            else
            {
                string errorMessage = await response.Content.ReadAsStringAsync();
                throw new Exception($"Error: {response.StatusCode}, Details: {errorMessage}");
            }
        }
    }
}

Output

I referred to the Azure OpenAI Service REST API documentation for this implementation and the How-to Guide on Creating and Managing Compute Sessions in Azure.

Additionally, I referred to the How to Manage Compute Sessions for PromptFlow and How to Customize Session Base Image for PromptFlow.

Below is the code to create images given a prompt.

POST https://{endpoint}/openai/deployments/{deployment-id}/images/generations?api-version=2024-10-21

{
 "prompt": "In the style of WordArt, Microsoft Clippy wearing a cowboy hat.",
 "n": 1,
 "style": "natural",
 "quality": "standard"
}

Output of response

Reasons:
  • Blacklisted phrase (0.5): I need
  • Blacklisted phrase (2.5): I need help
  • RegEx Blacklisted phrase (2.5): can you provide
  • Long answer (-1):
  • Has code block (-0.5):
  • High reputation (-1):
Posted by: Sampath