Regex template filling
  • 28 Feb 2024
  • 3 Minutes to read
  • Contributors
  • Dark
    Light
  • PDF

Regex template filling

  • Dark
    Light
  • PDF

Article summary

Regex-Powered Template Filling via API

Our API now supports regex-based template filling, enabling precise formatting in generated content. Ideal for data that must conform to specific patterns like credit card numbers, phone numbers, or custom identifiers.

Introduction to Regex Template Filling

Regex, or Regular Expression, defines a search pattern for text. Using regex for template filling ensures the generated content follows a specific format, crucial for data validation and usability.

Use Case: Formatted Data Generation

This feature is very usefull when generating data that must fit a particular structure, such as for example structured data extraction, ensuring consistency and ease of parsing for tools and systems.

Example: Structured Log Message Generation

Imagine the need to extract information from a financial report. A regex template can be used to structure this information in a specific format.

# Regex pattern for structured financial report messages
financial_regex = "\\[Year: (\\d{4})\\] \\[Quarter: (Q[1-4])\\] \\[Revenue: \\$(.*?)\\] \\[Net Profit: \\$(.*?)\\] \\[Expense: \\$(.*?)\\]."

# Example of financial report and instruction
financial_report_content = """

Based on this text, you should extract the key figures : 

'''
    During the 2023 fiscal year, our company continued to demonstrate remarkable growth and resilience in a challenging market environment. The first quarter was marked by intense competition, yet thanks to our innovative strategies, we managed to increase our market share.

Revenue for the first quarter amounted to $4.7 million, up from $4.3 million in the same period last year. This increase was largely attributed to our successful expansion into new market segments.

As for expenses, the first quarter saw a slight increase to $2.5 million, compared to $2.3 million the previous year. This rise is mainly due to our investments in research and development, as well as marketing to support our growth.

The second quarter was even more promising, with revenue reaching $5.2 million, highlighting the effectiveness of our new marketing initiatives and diversified product strategy. The net profit for this quarter was impressive, amounting to $1.4 million.

Our operational expenses for the second quarter were kept at $3 million, reflecting our ongoing efforts to optimize our operations and improve efficiency.

As we move into the third quarter, the outlook remains positive, with revenue projections exceeding $5.5 million. Our commitment to innovation and excellence continues to bear fruit, positioning the company for sustained growth in the future.

It's also important to note that our corporate social responsibility initiatives have been strengthened throughout the year, contributing positively to our brand image and relationship with the communities in which we operate.

In summary, 2023 has been a year of significant growth and strategic development for our company so far. We remain focused on achieving our long-term objectives and delivering value to our shareholders.
'''

Now give me the information and key figures related to Q1 :
"""

# Generate a structured financial report message
response = client.chat.completions.create(
    model="alfred-40b-1123",
    messages=[{"role": "user", "content": financial_report_content}],
    temperature=0.5,  
    max_tokens=90,  
    extra_body={"regex": financial_regex},
)

# Print the AI-generated structured financial report message
print(response.choices[0].message.content)
Precise Content Formatting

Incorporating regex in your API requests allows you to dictate the output format, ensuring it meets specific standards. Use extra_body for regex parameters.

Sample Output

[Year: 2023] [Quarter: Q1] [Revenue: $4.7 million, an increase from $4.3 million in Q1 of the previous year]  [Expenses: $2.5 million, an increase from $2.3 million in Q1 of the previous year]  [Growth attributed to: Successful expansion into new market segments, innovative strategies]
Attention to Regex Syntax

Correctly formulating your regex pattern is crucial. Ensure you escape special characters and use quantifiers accurately to prevent errors or unexpected results.

Advantages of Regex in Data Structuring

Using regex for template filling ensures data generated by the model adheres to formats required by external systems, improving data consistency and integration.

Leverage regex within your API requests to generate precisely formatted outputs, enhancing the utility and accuracy of the content for your specific needs.


Was this article helpful?