AI Prompting 101

Last week, on 2024.05.14, Google updated their Gemini AI models and conducted a demo. They also released a short document titled “Prompting Guide 101”. While the document covers a broad range of topics and is not specifically focused on software development, it provides useful information on how to use prompts effectively, with examples included.

This prompting guide was initially created for Gemini, but its principles are applicable to all GPTs, such as ChatGPT.

Quoting directly from their freely available PDF: Taking it directly from their free PDF

Writing effective prompts

There are four main areas to consider when writing an effective prompt. You don’t need to use all four, but using a few will help!

  • Persona
  • Task
  • Context
  • Format

Here is an example of a prompt using all four areas that could work well in Gmail and Google Docs:

You are a Google Cloud program manager. Draft an executive summary email to [persona] based on
[details about relevant program docs]. Limit to bullet points.

Here are quick tips to get you started with Gemini for Workspace:

  1. Use natural language. Write as if you’re speaking to another person. Express complete thoughts in full sentences.
  2. Be specific and iterate. Tell Gemini for Workspace what you need it to do (summarize, write, change the tone, create). Provide as much context as possible.
  3. Be concise and avoid complexity. State your request in brief — but specific — language. Avoid jargon.
  4. Make it a conversation. Fine-tune your prompts if the results don’t meet your expectations or if you believe there’s room for improvement. Use follow-up prompts and an iterative process of review and refinement to yield better results.

Other important part of the document:

Leveling up your prompt writing

This guide is meant to serve as inspiration, but the possibilities are nearly endless with Gemini for Google Workspace. Try these additional tips to build on your prompt-writing skills.

  • Break it up. If you want Gemini for Workspace to perform several related tasks, break them into separate prompts.
  • Give constraints. To generate specific results, include details in your prompt such as character count limits or the number of options you’d like to generate.
  • Assign a role. To encourage creativity, assign a role. You can do this by starting your prompt with language like: “You are the head of a creative department for a leading advertising agency …”
  • Ask for feedback. In your conversation with Gemini at gemini.google.com, tell it that you’re giving it a project, include all the details you have and everything you know, and then describe the output you want. Continue the conversation by asking questions like, “What questions do you have for me that would help you provide the best output?”
  • Consider tone. Tailor your prompts to suit your intended audience and desired tone of the content. Ask for a specific tone such as formal, informal, technical, creative, or casual in the output.
  • Say it another way. Fine-tune your prompts if the results don’t meet your expectations or if you believe

Be sure to check their guide, because they have a lot of examples!

Sources

Calling Azure Service Bus from Azure API Management Service

Azure API Management Service (APIM) can seamlessly integrate with Azure Service Bus, allowing you to send messages to both queues and topics, including their respective subscriptions. To achieve this integration, a few configurations and authentication steps are necessary, ensuring a secure and efficient communication channel between your API Gateway and Service Bus resources.

Authentication

For secure message transmission between APIM and Service Bus, proper authentication is crucial. Here’s how you can establish the connection:

  1. Activate Managed Identity: Start by activating a system-assigned Managed Identity from the API Management Service. This identity will serve as the authentication mechanism.

  1. Assign Roles in Service Bus

    2a. In the Azure Service Bus resource, navigate to the Access Control menu, then select Role Assignments and click Add.

    2b. From the Role tab, assign roles such as Azure Service Bus Data Receiver and/or Azure Service Bus Data Sender.

    2c. In the Assign access to group, specify the Managed identity from the available options and select the relevant API Management service.

Api Management Configuration

To enable APIM to send messages to Service Bus, you must define a new operation and configure the inbound rules appropriately. The following inbound policy should be set for the operation:

<policies>
    <inbound>
        <base />
            <authentication-managed-identity resource="https://servicebus.azure.net" output-token-variable-name="msi-access-token" ignore-error="false" />
            <set-header name="Authorization" exists-action="override">
                    <value>@((string)context.Variables["msi-access-token"])</value>
            </set-header>
            <set-header name="BrokerProperties" exists-action="override">
                <value>@("{ \"CorrelationId\": \"TestCorelationId\" }")</value>
            </set-header>
            <set-backend-service base-url="https://YOUR-SERVICE-BUS-URL.servicebus.windows.net" />
            <rewrite-uri template="YOUR-TOPIC-OR-QUEUE/messages" />
    </inbound>
...
</policies>

Explanation of Inbound Policy:

This inbound policy performs authentication using managed identity, sets custom headers (“Authorization” and “BrokerProperties”), specifies the backend service URL, and rewrites the request URI before forwarding the request to the specified Service Bus endpoint.

  • Authentication (authentication-managed-identity): Requests a managed identity (MSI) against the specified Azure Service Bus resource, storing the resulting access token in the variable msi-access-token.

  • Setting Headers (set-header): Custom headers like Authorization and BrokerProperties are set for the request. The Authorization header contains the access token, and the BrokerProperties header includes additional message properties like correlation IDs.
    The full list properties that can be set looks like this:

BrokerProperties:  { "SessionId": "{27729E1-B37B-4D29-AA0A-E367906C206E}", "MessageId": "{701332E1-B37B-4D29-AA0A-E367906C206E}", "TimeToLive" : 90, "CorrelationId": "{701332F3-B37B-4D29-AA0A-E367906C206E}", "SequenceNumber" : 12345, "DeliveryCount" : 2, "To" : "http://contoso.com", "ReplyTo" : "http://fabrikam.com",  "EnqueuedTimeUtc" : " Sun, 06 Nov 1994 08:49:37 GMT", "ScheduledEnqueueTimeUtc" : " Sun, 06 Nov 1994 08:49:37 GMT"}
  • Backend Service Configuration (set-backend-service): Specifies the base URL of the Service Bus endpoint where requests will be forwarded.

  • URI Rewriting (rewrite-uri): Directs the request to a specific topic or queue by rewriting the URI. The template should be the topic/queue name followed by “/messages”.

Sources

Remove subtitles automatically from video files

Shell scripts still rule!

I have my personal PLEX setup at home running from a Raspberry Pi. It is easy to manage and is useful in many occasions.

My Samsung TV has issues when loading a video file that has over 30 tracks (video/audio/subtitles). This was not a general problem in the past, but I am glad that the PLEX documentation mentions it. (It was hard to find)

Removing subtitles

Trying to fix the issue I came across mkvtoolnix tool that helps re-muxing the video files and remove all the tracks that I do not need.

The command to do so is quite simple. First one selects 2 & 3 subtitle tracks (Subtitle Id can differ). The second command just removes all subtitles from the video file.

mkvmerge -o output.mkv -s 2,3 input.mkv
or
mkvmerge -o output.mkv --no-subtitles input.mkv

I quickly created a small shell script that iterates through my video files and creates new versions for me.

for file in *mkv; do
 sudo mkvmerge -o "${file%.mkv}".PLEX.mkv -s 2,3 "$file"
done

Super! My issue is fixed, but I don’t want to manually log on to my Pi every time and run the command. This lead me to automate this task even further.

Automating subtitles removal

Then I found inotifywait from inotify-tools. This monitors directory tree for changes and can call an action on events like file created, modified, etc.

This prompted me to write a script combining the two tools and also adding some logic to the script. Ended up with the following script:

This script activates on new files. Filters out files created after the subtitles have been removed and also files that do not meet a certain criteria. I also automated the removal of these files. If the original file is not present anymore then this script also removes the altered version.

Making it a service

The next step is to create a service out of this script that is started at system boot.

For this a file must be created at /etc/systemd/system/sub-remover.service

The contents should be:

[Unit]
Description=Subtitle Remover Service
After=network.target

[Service]
ExecStart=/mnt/subremover/subremover.sh /mnt
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

The important part is the ExecStart this should point to the shell script file and should also have the folder it will be looking at. In this case the subremover.sh path with /mnt as the folder it should look at.

To enable the service run:

sudo systemctl daemon-reload
sudo systemctl enable sub-remover.service

For managing the service you can use:

sudo systemctl start sub-remover.service
sudo systemctl stop sub-remover.service
sudo systemctl status sub-remover.service

For checking the logs of the service:

sudo journalctl -u sub-remover.service -f 

Use -f to follow the logs live.