Tuesday, August 20, 2024

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate

graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages

Channel:
----------
{
  "body": {
    "contentType": "html",
    "content": "<at id=\"0\">General</at> This is a channel alert! 🔔"
  },
  "mentions": [
    {
      "id": 0,
      "mentionText": "General",
      "mentioned": {
        "conversation": {
          "id": "@{outputs('ChannelID')}",
          "displayName": "General",
          "@odata.type": "microsoft.graph.teamworkConversationIdentity",
          "conversationIdentityType": "channel"
        }
      }
    }
  ]
}


Team:
----------
{
  "body": {
    "contentType": "html",
    "content": "<at id=\"0\">TheWholeTeam</at> Hello Team! 😉"
  },
  "mentions": [
    {
      "id": 0,
      "mentionText": "TheWholeTeam",
      "mentioned": {
        "conversation": {
          "id": "@{outputs('GroupTeamID')}",
          "displayName": "TheWholeTeam",
          "@odata.type": "microsoft.graph.teamworkConversationIdentity",
          "conversationIdentityType": "team"
        }
      }
    }
  ]
}


https://damobird365.com/mention-a-channel-or-team-power-automate/#:~:text=Via%20Microsoft%20Teams%2C%20you%20can,you%20are%20familiar%20with%20Graph.
https://manish-solanki.com/how-to-mention-a-teams-or-channel-in-channel-message-using-power-automate/

==============================================
Send a Microsoft Graph HTTP request: Adaptive card and MS Teams Mention in same HTTP request.

URL: https://graph.microsoft.com/v1.0/teams/ad538402-ad01-4a18-a6b3-78b6893e40a5/channels/19:9PcW21v4L5Z5IHj60yJ74mS5-Ov50zoWo3sEszQ3R7w1@thread.tacv2/messages

Method: POST

BODY:
{  
    "subject": null,  
    "body": {  
        "contentType": "html",  
        "content": "<attachment id=\"1\"></attachment><at id=\"2\">TestSite</at>"  
    },  
    "attachments": [          {  
            "id": "1",  
            "contentType": "application/vnd.microsoft.card.thumbnail",  
            "contentUrl": null,  
            "content": "{\r\n  \"title\": \" Title @{outputs('Get_user_profile_(V2)_-_User')?['body/displayName']}    !\",\r\n  \"subtitle\": \"<h3>sub title </h3>\",\r\n  \"text\": \" sample text  ; ; ; \r\\n \r\\n   <img src=\\\"@{variables('randomImages')[rand(0, length(variables('randomImages')))]}\\\">   \"}",  
            "name": null,  
            "thumbnailUrl": null  
        }    ],
 "mentions": [      {
      "id": 2,
      "mentionText": "TestSite",
      "mentioned": {
        "conversation": {
          "id": "ad344409-a404-4a58-a673-7286850367a5",
          "displayName": "TestSite",
          "@odata.type": "microsoft.graph.teamworkConversationIdentity",
          "conversationIdentityType": "team"
        }      }    }  ]






















































==============================================




Top 6 Most Popular API Architecture Styles

Top 6 Most Popular API Architecture Styles
Source: ByteByteGo



Wednesday, June 19, 2024

Azure Scripts

 Azure Scripts:
Azure > Storage account > File shares > SMB File share
To connect to this Azure file share from Windows, run the PowerShell commands from a normal (not elevated) PowerShell terminal:
$connectTestResult = Test-NetConnection -ComputerName mystorageaccsree.file.core.windows.net -Port 445
if ($connectTestResult.TcpTestSucceeded) {
   
# Save the password so the drive will persist on reboot
 cmd.exe /C "cmdkey /add:`"mystorageaccsree.file.core.windows.net`" /user:`"localhost\mystorageaccsree`" /pass:`"zmKoauV41A9Lt9PUwt+xTSDeQaySe2yfldlhJPVU2gYfY0KWL7VB+OXvtO73WQTUkm7GRXmwC90j+ASt7Vdj/Q==`""
    
# Mount the drive
 New-PSDrive -Name Z -PSProvider FileSystem -Root "\\mystorageaccsree.file.core.windows.net\myfile123" -Persist
else {  
Write-Error -Message "Unable to reach the Azure storage account via port 445. Check to make sure your organization or ISP is not blocking port 445, or use Azure P2S VPN, Azure S2S VPN, or Express Route to tunnel SMB traffic over a different port."
}
###


###
#AZURE BASH SCRIPT:
#Create a blob storage using CLI:
#az storage account create -n <storage account name> -g <resource group name> -l westus --sku Standard_LRS
az storage account create -n storageaccountsree123 -g rg_eastus_52415_1_171886025677 -l westus --sku Standard_LRS
 
#Create a Container using CLI:
#az storage container create --account-name <storage account name> --name <container name>
az storage container create --account-name storageaccountsree123 --name containersree123
 
#Upload a blob on to storage account:
echo This is a sample html file > sample.html
#az storage blob upload --account-name <storage account name> --container-name <container name> --name sample.html --file sample.html
az storage blob upload --account-name storageaccountsree123 --container-name containersree123 --name sample.html --file sample.html
 
#Give public access to the blob file:
#az storage container set-permission --account-name <storage account name> --name <container name> --public-access blob
az storage container set-permission --account-name storageaccountsree123 --name containersree123 --public-access blob
 
#List and download the blob using CLI:
#az storage blob list --account-name <storage account name> --container-name <container name> --output table
az storage blob list --account-name storageaccountsree123 --container-name containersree123 --output table
 
#az storage blob download --account-name <storage account name> --container-name <container name> --name sample.html --file sample2.html
az storage blob download --account-name storageaccountsree123 --container-name containersree123 --name sample.html --file sample2.html

###

 Azure AZ Copy:
#Download AzCopy and upload your data to Blob storage:
#https://aka.ms/downloadazcopy-v10-windows
azcopy login
 
#azcopy copy "<local-folder-path>" "https://<storage-account-name>.blob.core.windows.net/<container-name>" --recursive=true
azcopy copy "C:\testfolder1" "https://whizstoragesree123.blob.core.windows.net/democontainer" --recursive=true
 
#azcopy sync "<local-folder-path>" "https://<storage-account-name>.blob.core.windows.net/<container-name>" --recursive=true
azcopy sync "C:\testfolder1" "https://whizstoragesree123.blob.core.windows.net/democontainer" --recursive=true
 
#Create a scheduled task:
schtasks /CREATE /SC minute /MO 5 /TN "AzCopy Script" /TR C:\testfolder1\script.bat

###


###
###
###
###
###
###
###
 
 
 

Friday, May 3, 2024

Learn Azure OpenAI

 Azure OpenAI includes several types of model:

  • GPT-4 models are the latest generation of generative pretrained (GPT) models that can generate natural language and code completions based on natural language prompts.
  • GPT 3.5 models can generate natural language and code completions based on natural language prompts. In particular, GPT-35-turbo models are optimized for chat-based interactions and work well in most generative AI scenarios.
  • Embeddings models convert text into numeric vectors, and are useful in language analytics scenarios such as comparing text sources for similarities.
  • DALL-E models are used to generate images based on natural language prompts. Currently, DALL-E models are in preview. DALL-E models aren't listed in the Azure OpenAI Studio interface and don't need to be explicitly deployed.

Create an Azure OpenAI Service resource in Azure CLI:
az cognitiveservices account create \
-n MyOpenAIResource \
-g OAIResourceGroup \
-l eastus \
--kind OpenAI \
--sku s0 \
--subscription subscriptionID


Deploy using Azure CLI:
az cognitiveservices account deployment create \
   -g OAIResourceGroup \
   -n MyOpenAIResource \
   --deployment-name MyModel \
   --model-name gpt-35-turbo \
   --model-version "0301"  \
   --model-format OpenAI \
   --sku-name "Standard" \
   --sku-capacity 1


Prompt types:

Prompts can be grouped into types of requests based on task.

Task typePrompt exampleCompletion example
Classifying contentTweet: I enjoyed the trip.
Sentiment:
Positive
Generating new contentList ways of traveling1. Bike
2. Car ...
Holding a conversationA friendly AI assistantSee examples
Transformation (translation and symbol conversion)English: Hello
French:
bonjour
Summarizing contentProvide a summary of the content
{text}
The content shares methods of machine learning.
Picking up where you left offOne way to grow tomatoesis to plant seeds.
Giving factual responsesHow many moons does Earth have?One
Completions Playground parameters:

here are many parameters that you can adjust to change the performance of your model:

  • Temperature: Controls randomness. Lowering the temperature means that the model produces more repetitive and deterministic responses. Increasing the temperature results in more unexpected or creative responses. Try adjusting temperature or Top P but not both.
  • Max length (tokens): Set a limit on the number of tokens per model response. The API supports a maximum of 4000 tokens shared between the prompt (including system message, examples, message history, and user query) and the model's response. One token is roughly four characters for typical English text.
  • Stop sequences: Make responses stop at a desired point, such as the end of a sentence or list. Specify up to four sequences where the model will stop generating further tokens in a response. The returned text won't contain the stop sequence.
  • Top probabilities (Top P): Similar to temperature, this controls randomness but uses a different method. Lowering Top P narrows the model’s token selection to likelier tokens. Increasing Top P lets the model choose from tokens with both high and low likelihood. Try adjusting temperature or Top P but not both.
  • Frequency penalty: Reduce the chance of repeating a token proportionally based on how often it has appeared in the text so far. This decreases the likelihood of repeating the exact same text in a response.
  • Presence penalty: Reduce the chance of repeating any token that has appeared in the text at all so far. This increases the likelihood of introducing new topics in a response.
  • Pre-response text: Insert text after the user’s input and before the model’s response. This can help prepare the model for a response.
  • Post-response text: Insert text after the model’s generated response to encourage further user input, as when modeling a conversation.


Available endpoints:
  • Completion - model takes an input prompt, and generates one or more predicted completions. You'll see this playground in the studio, but won't be covered in depth in this module.
  • ChatCompletion - model takes input in the form of a chat conversation (where roles are specified with the message they send), and the next chat completion is generated.
  • Embeddings - model takes input and returns a vector representation of that input.

Use Azure OpenAI REST API:
curl "https://eastus.api.cognitive.microsoft.com/openai/deployments/MyModel/chat/completions?api-version=2024-02-15-preview"   -H "Content-Type: application/json"   -H "api-key: 240ac7db42fb486686ef1e5088af77ae"   -d '{   "messages": [{"role":"system","content":"You are an AI assistant that helps people find information."},{"role":"user","content":"hi what is today"}],  "max_tokens": 800,   "temperature": 0.7,   "frequency_penalty": 0,   "presence_penalty": 0,   "top_p": 0.95,   "stop": null }'


Use Azure OpenAI SDK c#:

//install-package Azure.AI.OpenAI -prerelease
//dotnet add package Azure.AI.OpenAI --prerelease
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure;
using Azure.AI.OpenAI;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string endpoint = "https://eastus.api.cognitive.microsoft.com/";
//"<YOUR_ENDPOINT_NAME>";
            string key = "240ac7db42fb486686ef2e5088af87de";
//"<YOUR_API_KEY>";
            string deploymentName = "MyModel"; //"<YOUR_DEPLOYMENT_NAME>";

            OpenAIClient client = new OpenAIClient(new Uri(endpoint),
new AzureKeyCredential(key));
            // Build completion options object
            ChatCompletionsOptions chatCompletionsOptions =
new ChatCompletionsOptions()
            {
                Messages =    {
                    new ChatRequestSystemMessage("You are a helpful AI bot."),
                    new ChatRequestUserMessage("What is Azure OpenAI?"),
                },
                DeploymentName = deploymentName
            };

            // Send request to Azure OpenAI model
            ChatCompletions response =
client.GetChatCompletions(chatCompletionsOptions);

            // Print the response
            string completion = response.Choices[0].Message.Content;
            Console.WriteLine("Response: " + completion + "\n");
            Console.ReadKey();
        }
    }
}


Semantic Kernel SDK in C#:
Semantic Kernel integrates LLMs like OpenAI, Azure OpenAI, and Hugging Face with conventional programming languages like C#, Python, and Java. Developers can create "plugins" to interface with the LLMs and perform all sorts of tasks. The Semantic Kernel SDK also provides built-in plugins that quickly enhance an application. Developers can easily apply AI models in their own applications without having to learn the intricacies of each model's API.


//dotnet add package Azure.AI.OpenAI --prerelease
//dotnet add package Microsoft.SemanticKernel --version 1.2.0
//install-package Azure.AI.OpenAI -prerelease
//install-package Microsoft.SemanticKernel -version 1.2.0

using Microsoft.SemanticKernel;
using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var builder = Kernel.CreateBuilder();

            builder.Services.AddAzureOpenAIChatCompletion(
                "gpt35turbo16kdemo",
                "https://rg1openailab.openai.azure.com/",
                "e9742dc35fd441a38492fecb1f3c8e3d",
                "gpt-35-turbo-16k");
            var kernel = builder.Build();

            System.Net.ServicePointManager
.ServerCertificateValidationCallback = (senderX, certificate, chain,
sslPolicyErrors) => { return true; };

            var result = await kernel.InvokePromptAsync(
"Give me a list of breakfast foods");
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}





Semantic Kernel SDK in C# - Built-in plugins:
The Semantic Kernel SDK offers an extra package with predefined plugins for common tasks. These are available in the Plugins.Core package:
  • ConversationSummaryPlugin - Summarizes conversation
  • FileIOPlugin - Reads and writes to the filesystem
  • HttpPlugin - Makes requests to HTTP endpoints
  • MathPlugin - Performs mathematical operations
  • TextPlugin - Performs text manipulation
  • TimePlugin - Gets time and date information
  • WaitPlugin - Pauses execution for a specified amount of time

//dotnet add package Azure.AI.OpenAI --prerelease
//install-package Azure.AI.OpenAI -prerelease

//dotnet add package Microsoft.SemanticKernel --version 1.2.0
//install-package Microsoft.SemanticKernel -version 1.2.0

//dotnet add package Microsoft.SemanticKernel.Plugins.Core --version 1.2.0-alpha
//install-package Microsoft.SemanticKernel.Plugins.Core -version 1.2.0-alpha

using Microsoft.SemanticKernel;
using System;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Plugins.Core;

namespace ConsoleApp1
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var builder = Kernel.CreateBuilder();
            builder.Services.AddAzureOpenAIChatCompletion(
                "gpt35turbo16kdemo",
                "https://rg1openailab.openai.azure.com/",
                "f9742dc25fd441a38492fecb1f3c8e3d",
                "gpt-35-turbo-16k");

            builder.Plugins.AddFromType<TimePlugin>();
            var kernel = builder.Build();
            var currentDay = await kernel.InvokeAsync("TimePlugin",
"DayOfWeek");
            Console.WriteLine(currentDay);

            System.Net.ServicePointManager
.ServerCertificateValidationCallback = (senderX, certificate, chain,
sslPolicyErrors) => { return true; };

            var result = await kernel.InvokePromptAsync(
"Give me a list of breakfast foods");
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}









Saturday, April 13, 2024

Azure OpenAI Chat and DALL-E in C# and Python

Azure OpenAI Chat in C#:
// Install the .NET library via NuGet: dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.5
 
using System;
using System.Threading.Tasks;
using Azure;
using Azure.AI.OpenAI;
namespace ConsoleApp
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            OpenAIClient client = new OpenAIClient(new Uri("https://xxx-xxxxxxxxx.openai.azure.com/"),
                new AzureKeyCredential("123456895a5bcef0hijklmnopqrstuvwx"));
 
            Response<ChatCompletions> responseWithoutStream = await client.GetChatCompletionsAsync("model_deployment_name",
                new ChatCompletionsOptions()
                {
                    Messages = {
                        new ChatMessage(ChatRole.System, @"You are an AI assistant that helps people find information."),
                        new ChatMessage(ChatRole.User, @"Summarize:
                        Full-stack developer with Azure Development")
                    },
                    Temperature = (float)0.7,
                    MaxTokens = 800,
                    NucleusSamplingFactor = (float)0.95,
                    FrequencyPenalty = 0,
                    PresencePenalty = 0,
                });
            ChatCompletions response = responseWithoutStream.Value;
            Console.WriteLine(response.Choices[0].Message.Content);
            Console.ReadKey();
        }
    }
}

Azure OpenAI Chat in Python:

from openai import AzureOpenAI

client = AzureOpenAI(
    azure_endpoint = "https://xxx-xxxxxxxxx.openai.azure.com/",
    api_key = "123456895a5bcef0hijklmnopqrstuvwx",  
    api_version="2024-02-15-preview"
    )

message_text = [
    {"role":"system",
     "content":"You are an AI assistant that helps people find information."},
    {"role":"user","content":"Summary  
\nFull-stack developer with Azure Development"}
    ]

completion = client.chat.completions.create(
    model="model_deployment_name",
    messages = message_text,
    temperature=0.7,
    max_tokens=800,
    top_p=0.95,
    frequency_penalty=0,
    presence_penalty=0,
    stop=None
    )

print(completion.choices[0].message.content)



Azure OpenAI DALL-E 3 in Python:
# Note: DALL-E 3 requires version 1.0.0 of the openai-python library or later
# !pip install openai
import os
from openai import AzureOpenAI
import json

client = AzureOpenAI(
    api_version="2024-02-01",
    azure_endpoint="https://mydemo.openai.azure.com/",
    api_key='key'  # os.environ["AZURE_OPENAI_API_KEY"],
)

result = client.images.generate(
    model="Dalle3", # the name of your DALL-E 3 deployment
    prompt="A relisting image of a futuristic skyline consisting of the world's
tallest building ",
    n=1
)

image_url = json.loads(result.model_dump_json())['data'][0]['url']
print(image_url)









































Use Azure OpenAI APIs in your app c#:

// {
//     "AzureOAIEndpoint": "https://rg1openailab.openai.azure.com/",
//     "AzureOAIKey": "e9742dc25fd446a38492fecb6f3c8e3d",
//     "AzureOAIDeploymentName": "gpt35turbo16kdemo"
// }

//  dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.14
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Azure;
using Azure.AI.OpenAI;

IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json").Build();
string? oaiEndpoint = config["AzureOAIEndpoint"];
string? oaiKey = config["AzureOAIKey"];
string? oaiDeploymentName = config["AzureOAIDeploymentName"];

if (string.IsNullOrEmpty(oaiEndpoint) || string.IsNullOrEmpty(oaiKey) ||
string.IsNullOrEmpty(oaiDeploymentName))
{
    Console.WriteLine("Please check your appsettings.json file for missing
or incorrect values.");
    return;
}

OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint),
new AzureKeyCredential(oaiKey));
string systemMessage = "I am a hiking enthusiast named Forest who helps
people discover hikes in their area. If no area is specified, I will
default to near Rainier National Park. I will then provide three
suggestions for nearby hikes that vary in length. I will also share an
interesting fact about the local nature on the hikes when making a
recommendation.";
var messagesList = new List<ChatRequestMessage>()
{
    new ChatRequestSystemMessage(systemMessage),
};

do
{
    Console.WriteLine("Enter your prompt text (or type 'quit' to exit): ");
    string? inputText = Console.ReadLine();
    if (inputText == "quit") break;

    if (inputText == null)
    {
        Console.WriteLine("Please enter a prompt.");
        continue;
    }

    Console.WriteLine("\nSending request for summary to Azure OpenAI
endpoint...\n\n");
    messagesList.Add(new ChatRequestUserMessage(inputText));

    ChatCompletionsOptions chatCompletionsOptions = new
ChatCompletionsOptions()
    {
        MaxTokens = 1200,
        Temperature = 0.7f,
        DeploymentName = oaiDeploymentName
    };

    foreach (ChatRequestMessage chatMessage in messagesList)
    {
        chatCompletionsOptions.Messages.Add(chatMessage);
    }

    ChatCompletions response =
client.GetChatCompletions(chatCompletionsOptions);
    string completion = response.Choices[0].Message.Content;
    messagesList.Add(new ChatRequestAssistantMessage(completion));
    Console.WriteLine("Response: " + completion + "\n");
} while (true);





Utilize prompt engineering in your app in c#:

// {
//     "AzureOAIEndpoint": "https://openailab.openai.azure.com/",
//     "AzureOAIKey": "e8762dc15fd441a68495fecb7f3c8e3d",
//     "AzureOAIDeploymentName": "gpt35turbo16kdemo"
// }
//  dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.14
//dotnet run - to run app
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Azure;
using Azure.AI.OpenAI;

IConfiguration config = new ConfigurationBuilder().
AddJsonFile("appsettings.json").Build();
string? oaiEndpoint = config["AzureOAIEndpoint"];
string? oaiKey = config["AzureOAIKey"];
string? oaiDeploymentName = config["AzureOAIDeploymentName"];
bool printFullResponse = true;

do
{
    Console.WriteLine("-----------\nPausing the app to allow you to change
the system prompt.\nPress any key to continue...");
    Console.ReadKey();
    Console.WriteLine("\nUsing system message from system.txt");
    string systemMessage = System.IO.File.ReadAllText("system.txt");
    systemMessage = systemMessage.Trim();
    Console.WriteLine("\nEnter user message or type 'quit' to exit:");
    string userMessage = Console.ReadLine() ?? "";
    userMessage = userMessage.Trim();
    if (systemMessage.ToLower() == "quit" ||
userMessage.ToLower() == "quit")
    {
        break;
    }
    else if (string.IsNullOrEmpty(systemMessage) ||
string.IsNullOrEmpty(userMessage))
    {
        Console.WriteLine("Please enter a system and user message.");
        continue;
    }
    else
    {
        await GetResponseFromOpenAI(systemMessage, userMessage);
    }
} while (true);

async Task GetResponseFromOpenAI(string systemMessage, string userMessage)
{
    Console.WriteLine("\nSending prompt to Azure OpenAI endpoint...\n\n");
    if (string.IsNullOrEmpty(oaiEndpoint) || string.IsNullOrEmpty(oaiKey)
|| string.IsNullOrEmpty(oaiDeploymentName))
    {
        Console.WriteLine("Please check your appsettings.json file for
missing or incorrect values.");
        return;
    }
    OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint),
new AzureKeyCredential(oaiKey));
    Console.WriteLine("\nAdding grounding context from grounding.txt");
    string groundingText = System.IO.File.ReadAllText("grounding.txt");
    userMessage = groundingText + userMessage;
    var chatCompletionsOptions = new ChatCompletionsOptions()
    {
        Messages =
     {
         new ChatRequestSystemMessage(systemMessage),
         new ChatRequestUserMessage(userMessage)
     },
        Temperature = 0.7f,
        MaxTokens = 800,
        DeploymentName = oaiDeploymentName
    };
    Response<ChatCompletions> response =
await client.GetChatCompletionsAsync(chatCompletionsOptions);
    ChatCompletions completions = response.Value;
    string completion = completions.Choices[0].Message.Content;
    if (printFullResponse)
    {
        Console.WriteLine($"\nFull response:
{JsonSerializer.Serialize(completions, new JsonSerializerOptions
{ WriteIndented = true })}\n\n");
    }
    Console.WriteLine($"\nResponse:\n{completion}\n\n");
}


Generate and improve code with Azure OpenAI Service in c#:
// {
//     "AzureOAIEndpoint": "https://rg1openailab.openai.azure.com/",
//     "AzureOAIKey": "e9712dc11fd141a31491fecb1f3c8e3d",
//     "AzureOAIDeploymentName": "gpt35turbo16kdemo"
// }

// dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.14
//dotnet run

using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Azure;
using Azure.AI.OpenAI;

IConfiguration config = new ConfigurationBuilder().
AddJsonFile("appsettings.json").Build();
string? oaiEndpoint = config["AzureOAIEndpoint"];
string? oaiKey = config["AzureOAIKey"];
string? oaiDeploymentName = config["AzureOAIDeploymentName"];
string command;
bool printFullResponse = false;
do
{
    Console.WriteLine("\n1: Add comments to my function\n" +
    "2: Write unit tests for my function\n" +
    "3: Fix my Go Fish game\n" +
    "\"quit\" to exit the program\n\n" +
    "Enter a number to select a task:");

    command = Console.ReadLine() ?? "";
    if (command == "quit")
    {
        Console.WriteLine("Exiting program...");
        break;
    }
    Console.WriteLine("\nEnter a prompt: ");
    string userPrompt = Console.ReadLine() ?? "";
    string codeFile = "";
    if (command == "1" || command == "2")
        codeFile = System.IO.File
.ReadAllText("../sample-code/function/function.cs");
    else if (command == "3")
        codeFile = System.IO.File
.ReadAllText("../sample-code/go-fish/go-fish.cs");
    else
    {
        Console.WriteLine("Invalid input. Please try again.");
        continue;
    }
    userPrompt += codeFile;
    await GetResponseFromOpenAI(userPrompt);
} while (true);

async Task GetResponseFromOpenAI(string prompt)
{
    Console.WriteLine("\nCalling Azure OpenAI to generate code...\n\n");
    if (string.IsNullOrEmpty(oaiEndpoint) ||
string.IsNullOrEmpty(oaiKey) || string.IsNullOrEmpty(oaiDeploymentName))
    {
        Console.WriteLine("Please check your appsettings.json
file for missing or incorrect values.");
        return;
    }
    OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint),
new AzureKeyCredential(oaiKey));
    string systemPrompt = "You are a helpful AI assistant that helps
programmers write code.";
    string userPrompt = prompt;
    var chatCompletionsOptions = new ChatCompletionsOptions()
    {
        Messages =
     {
         new ChatRequestSystemMessage(systemPrompt),
         new ChatRequestUserMessage(userPrompt)
     },
        Temperature = 0.7f,
        MaxTokens = 1000,
        DeploymentName = oaiDeploymentName
    };
    Response<ChatCompletions> response =
await client.GetChatCompletionsAsync(chatCompletionsOptions);
    ChatCompletions completions = response.Value;
    string completion = completions.Choices[0].Message.Content;
    if (printFullResponse)
    {
        Console.WriteLine($"\nFull response:
{JsonSerializer.Serialize(completions, new JsonSerializerOptions
{ WriteIndented = true })}\n\n");
    }
    System.IO.File.WriteAllText("result/app.txt", completion);
    Console.WriteLine($"\nResponse written to result/app.txt\n\n");
}



Generate images with a DALL-E model - Use the REST API to generate images in c#:
// {
//     "AzureOAIEndpoint": "https://rg1openailab.openai.azure.com/",
//     "AzureOAIKey": "e1742dc35fd341a48592fecb6f7c8e3d"
// }
using System;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;

namespace generate_image
{
    class Program
    {
        private static string? aoaiEndpoint;
        private static string? aoaiKey;
        static async Task Main(string[] args)
        {
            try
            {
                IConfigurationBuilder builder =
new ConfigurationBuilder().AddJsonFile("appsettings.json");
                IConfigurationRoot configuration = builder.Build();
                aoaiEndpoint = configuration["AzureOAIEndpoint"] ?? "";
                aoaiKey = configuration["AzureOAIKey"] ?? "";
                Console.Clear();
                Console.WriteLine("Enter a prompt to request an image:");
                string prompt = Console.ReadLine() ?? "";

                using (var client = new HttpClient())
                {
                    var contentType =
new MediaTypeWithQualityHeaderValue("application/json");
                    var api = "openai/deployments/dalle3/images/generations
?api-version=2024-02-15-preview";
                    client.BaseAddress = new Uri(aoaiEndpoint);
                    client.DefaultRequestHeaders.Accept.Add(contentType);
                    client.DefaultRequestHeaders.Add("api-key", aoaiKey);
                    var data = new
                    {
                        prompt = prompt,
                        n = 1,
                        size = "1024x1024"
                    };
                    var jsonData = JsonSerializer.Serialize(data);
                    var contentData = new StringContent(jsonData,
Encoding.UTF8, "application/json");
                    var response = await client.PostAsync(api, contentData);
                    var stringResponse =
await response.Content.ReadAsStringAsync();
                    JsonNode contentNode = JsonNode.Parse(stringResponse)!;
                    JsonNode dataCollectionNode = contentNode!["data"];
                    JsonNode dataNode = dataCollectionNode[0]!;
                    JsonNode revisedPrompt = dataNode!["revised_prompt"];
                    JsonNode url = dataNode!["url"];
                    Console.WriteLine(revisedPrompt.ToJsonString());
                    Console.WriteLine(url.ToJsonString()
.Replace(@"\u0026", "&"));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}




Implement Retrieval Augmented Generation (RAG) with Azure OpenAI Service c#:
// {
//"AzureOAIEndpoint": "https://rg1openailab.openai.azure.com/",
//"AzureOAIKey": "e9742dc35fd441a38492fecb4f3c8e4d",
//"AzureOAIDeploymentName": "gpt35turbo16kdemo",
//"AzureSearchEndpoint": "https://rg1searchservice.search.windows.net",
//"AzureSearchKey": "vMsdF7CosssQ0LtzqGhpyi4Z6NFes4FGMj5UQ4dXQO0DtAzSeBh3Dg0",
//"AzureSearchIndex": "margiestravel"
// }
//  dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.14

using System;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Azure;
using Azure.AI.OpenAI;

bool showCitations = true;
IConfiguration config =
new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
string oaiEndpoint = config["AzureOAIEndpoint"] ?? "";
string oaiKey = config["AzureOAIKey"] ?? "";
string oaiDeploymentName = config["AzureOAIDeploymentName"] ?? "";
string azureSearchEndpoint = config["AzureSearchEndpoint"] ?? "";
string azureSearchKey = config["AzureSearchKey"] ?? "";
string azureSearchIndex = config["AzureSearchIndex"] ?? "";

OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint),
new AzureKeyCredential(oaiKey));
Console.WriteLine("Enter a question:");
string text = Console.ReadLine() ?? "";
AzureSearchChatExtensionConfiguration ownDataConfig = new()
{
    SearchEndpoint = new Uri(azureSearchEndpoint),
    Authentication =
new OnYourDataApiKeyAuthenticationOptions(azureSearchKey),
    IndexName = azureSearchIndex
};
Console.WriteLine("...Sending the following request to Azure OpenAI
endpoint...");
Console.WriteLine("Request: " + text + "\n");
ChatCompletionsOptions chatCompletionsOptions =
new ChatCompletionsOptions()
{
    Messages =
{
new ChatRequestUserMessage(text)
},
    MaxTokens = 600,
    Temperature = 0.9f,
    DeploymentName = oaiDeploymentName,
    AzureExtensionsOptions = new AzureChatExtensionsOptions()
    {
        Extensions = { ownDataConfig }
    }
};
ChatCompletions response=client.GetChatCompletions(chatCompletionsOptions);
ChatResponseMessage responseMessage = response.Choices[0].Message;
Console.WriteLine("Response: " + responseMessage.Content + "\n");
Console.WriteLine("  Intent: " +
responseMessage.AzureExtensionsContext.Intent);
if (showCitations)
{
    Console.WriteLine($"\n  Citations of data used:");
    foreach (AzureChatExtensionDataSourceResponseCitation citation in
responseMessage.AzureExtensionsContext.Citations)
    {
        Console.WriteLine($"    Citation: {citation.Title} -
{citation.Url}");
    }
}







Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   &qu...

Popular posts