Using patterns in Fabric to solve problems

Using patterns in Fabric to solve problems

On my first post, I discussed Fabric AI, guided you through the setup process, and provided a brief demonstration on how to derive value from it. I briefly mentioned how you can create patterns in Fabric. In this post, I want to dive deeper into what patterns are and how you can design your own to solve specific problems.

What are patterns?

Patterns are a set of directives you give an LLM model to solve a specific problem. One pattern addresses exactly one task or problem. Within the pattern, you always define the following three points:

  1. Identity and Purpose: In this section, the capabilities of the AI are defined. The AI is informed about its strengths and the objectives in the context of this specific pattern, including its persona and purpose.
  2. Steps: This section outlines the individual steps the AI should follow to complete the task. Here, you can specify what information you want it to generate and how you want to structure the sections of the document it outputs.
  3. Output Instructions: This section provides instructions on the constraints of the output. You may define how many words it should use per section, specify style, and give instructions on what you do not want included in the output, etc.

How to create a pattern?

I have been interacting with LLMs daily for quite some time now. My approach has always been very casual. I would describe my method as “talking out” the task without heavily relying on prompt engineering to maximize the use of LLMs like ChatGPT.

This is one of the advantages you gain when using Fabric. Fabric already has patterns to enhance your “natural” prompts significantly without requiring expertise in prompt engineering. It leverages the power of the LLM to improve the quality of a given prompt and optimize results.

How do you do that in Fabric?

echo “I want you to analyse some CSV files that contain information about my financial activities and help create a budget based on how much I have been spending in a given category. Please, generate a table template where you can fill the category, the allocated budget and leave an empty column for me to input what I have spent at the end of the month in that category.” | fabric -s -p improve_prompt

The output I get from Fabric looks like this:

**Task:** Analyze CSV files containing financial activity data to help create a budget.

**Instructions:**

1. **CSV Analysis:**
   - You will be provided with CSV files containing information about financial activities.
   - Analyze the data to determine spending patterns in various categories.

2. **Budget Creation:**
   - Based on the analysis, assist in creating a budget by identifying how much has been spent in each category.

3. **Table Template Generation:**
   - Generate a table template with the following columns:
     - **Category:** List of spending categories.
     - **Allocated Budget:** Suggested budget allocation for each category based on past spending.
     - **Actual Spending:** Leave this column empty for manual input at the end of the month.

**Output Format:**

- Provide the table template in a clear and organized format.
- Ensure that the table is easy to understand and fill out.

**Additional Notes:**

- If possible, provide insights or suggestions on how to optimize spending based on the analysis.
- Ensure that the instructions are clear and concise for ease of use.

Fabric took my simple prompt and converted it into a much better-structured set of definitions and instructions. It even added additional notes.

This is already very powerful and can be used for any type of task you would like an LLM to assist you with. But we can take it one step further. Let's combine the patterns improve_prompt and create_pattern.

As I already mentioned, patterns solve specific problems. For me, it would save a lot of time if I could distill YouTube video guides (how-to's) into text form. So let's create a pattern for that. I will simply stitch the two patterns together. I will give it my prompt and pass it to Fabric, running it through improve_prompt, and then directly piping the output to the next pattern, create_pattern.

echo "You are an IT expert. You are able to extract step-by-step guides from long transcripts or explanations. The output should include a guide section and concept sections, in which the details are explained. If there are any relevant prerequisites, these should be clearly noted in their own section. The first section should be a summary and rating of the content (simple rating from 1-5). The rating should evaluate how complete the guide is and how clearly the concepts are communicated." | fabric -p improve_prompt | fabric -s -p create_pattern

Running that returns the following result:

# IDENTITY and PURPOSE                                                                                                                                      
You are an IT expert whose primary responsibility is to extract step-by-step guides from long transcripts or explanations. Your role involves analyzing detailed content to distill it into a structured format that includes a summary, rating, prerequisites, a detailed guide, and concept explanations. You must ensure that each section is clearly labeled and presented in a professional and concise manner. Your expertise allows you to evaluate the completeness and clarity of the content, providing valuable insights through your ratings.

Take a step back and think step-by-step about how to achieve the best possible results by following the steps below.

# STEPS

- Extract a brief summary of the content.

- Rate the content on a scale from 1 to 5, evaluating how complete the guide is and how clearly the concepts are communicated.

- Clearly list any relevant prerequisites needed to understand or implement the guide.

- Present a detailed step-by-step guide extracted from the content.

- Explain the details of each concept mentioned in the guide, ensuring clarity and comprehensiveness.

# OUTPUT INSTRUCTIONS

- Only output Markdown.

- Ensure each section is clearly labeled.

- Use bullet points or numbered lists for clarity where applicable.

- Maintain a professional and concise tone throughout.

- Ensure you follow ALL these instructions when creating your output.

And that's it! We have generated our pattern capable of creating written step-by-step guides from video transcripts.

Saving a custom pattern

How can we use this newly created pattern in our running Fabric installation? Let me walk you through it.

  1. Create a directory with the name you wish to give your pattern. In my case, I'll create a directory called extract_guide.
cd /path/to/your/custom/pattern
mkdir extract_guide
  1. Save the content of your pattern to a file inside that newly created directory and call it system.md. For example, open a new file with your favorite text editor,
nano extract_guide/system.md

and save the content of the pattern to that file.

  1. Copy the whole directory to your patterns directory.
cp -r extract_guide ~/.config/fabric/patterns/
  1. Restart your terminal session and check that your pattern is listed properly.
fabric -l | grep extract_guide

Now you should be able to use your custom pattern by calling it through Fabric like you do any other pattern.

Demonstration

Let's take our newly created pattern out for a spin and see how it can help extract guides from YouTube videos. I've been wanting to try out the open-source self-hosted application Stirling-PDF, but I haven't found time to watch videos or read documentation. Let's see how my new pattern can help with that. I found this video on YouTube. I can get the transcription of the video with Fabric's yt command and pass it to my pattern like this:

yt "https://www.youtube.com/watch?v=o8N-njA57iQ" | fabric -s -p extract_guide

The output looks like this:

# Summary

The video provides a comprehensive guide on using Stirling PDF, an open-source tool for managing and editing PDF documents. It covers various functionalities such as viewing, annotating, merging, converting, and signing PDFs. The video also includes a detailed tutorial on setting up Stirling PDF using Docker and Docker Compose. The content is rated 4 out of 5 for its thoroughness and clarity, though it could benefit from a more concise presentation.

# Prerequisites

Before following the guide, ensure you have the following:

- Basic understanding of PDF document handling.
- Familiarity with Docker and Docker Compose.
- Access to a server or local machine where Docker is installed.
- Basic knowledge of using command-line interfaces.

# Step-by-Step Guide

## Setting Up Stirling PDF

1. **Install Docker and Docker Compose:**
   - Ensure Docker and Docker Compose are installed on your system. If not, refer to the provided video link for installation instructions.

2. **Access Your Server:**
   - SSH into your server where you plan to run Stirling PDF.

3. **Create a Directory for Stirling PDF:**
   - Run `mkdir Stirling_pdf` to create a new directory.
   - Navigate into the directory using `cd Stirling_pdf`.

4. **Create a Docker Compose File:**
   - Use a text editor like Nano to create a file named `docker-compose.yml`.
   - Add the following content to the file:
     ```yaml
     services:
       Stirling-pdf:
         image: s-pdf:latest
         ports:
           - "8092:8080"
         volumes:
           - ./training_data:/app/training_data
           - ./extra_configs:/app/extra_configs
     ```

5. **Download Language Packs (Optional):**
   - Visit the Tesseract language data repository.
   - Download necessary language packs and place them in the `training_data` directory.

6. **Start the Docker Container:**
   - Verify setup by checking logs with `docker-compose logs -f`.

7. **Access Stirling PDF:**
   - Open a web browser and navigate to `http://<your-server-ip>:8092`.
   - You should see Stirling PDF interface ready for use.

## Using Stirling PDF

### Viewing and Annotating PDFs

- **Open a PDF:** Click on "View PDF" and upload your document.
- **Annotate:** Use tools to highlight text, add comments, or insert images.

### Converting PDFs

- **Convert to Word/ODT:** Select "Convert" and choose your desired format (e.g., ODT for LibreOffice).
- **Edit in LibreOffice:** Open converted document in LibreOffice for further editing.

### Signing PDFs

- **Add Signature:** Use "Sign" feature to draw or type your signature.
### Additional Features

- **Merge PDFs:** Combine multiple PDFs into one document.
- **Add Watermarks:** Insert watermarks for confidentiality or branding.
- **OCR Processing:** Use OCR to extract text from scanned documents.

# Key Concepts Explained
## Docker and Docker Compose

**Docker** is a platform that allows you to automate deployment of applications inside lightweight, portable containers. **Docker Compose** is a tool for defining and running multi-container Docker applications using YAML files to configure application services.
## OCR (Optical Character Recognition)

OCR is technology used to convert different types of documents, such as scanned paper documents or PDFs, into editable and searchable data. It is particularly useful for digitizing printed texts so they can be electronically edited, searched, stored more compactly, displayed online, and used in machine processes such as cognitive computing, machine translation, (extracted) text-to-speech, key data extraction, and text mining.

## PDF Annotations

Annotations in PDFs allow users to add notes, comments, highlights, or other markings directly onto document. This feature is useful for collaborative work or personal note-taking.

By following this guide, you can effectively set up and utilize Stirling PDF for various document management tasks without relying on proprietary software like Adobe Acrobat.

This gives me a quick guide that I can follow (pending some fact-checking) to quickly get started with Stirling-PDF. Not bad for a first iteration of my new pattern.

Final Recommendations

  • Be specific: Try to be very specific about what problem you are solving exactly.
  • Find a good procedure: Tweak your pattern's steps to find procedure that gives the best results.
  • It is still only an AI: Don't rely solely on the output of Fabric. It is still up to you to check that any claims made by AI are true.

Conclusion

We have discussed what patterns are and how they can be leveraged to help solve specific problems. We have utilized Fabric's built-in patterns and capabilities to quickly develop new patterns. Throw some problems at them and see what modifications you can make for even better results.

Now find a problem or task you'd like assistance with from Fabric, design a pattern to solve it, and enjoy!

References

  1. Stirling-PDF installation video: https://www.youtube.com/watch?v=o8N-njA57iQ
  2. Fabirc: https://github.com/danielmiessler/fabric