(function() { var utmInheritingDomain = "appstore.com", utmRegExp = /(&|\?)utm_[A-Za-z]+=[A-Za-z0-9]+/gi, links = document.getElementsByTagName("a"), utms = [ "utm_medium={{URL - utm_medium}}", "utm_source={{URL - utm_source}}", "utm_campaign={{URL - utm_campaign}}" ]; for (var index = 0; index < links.length; index += 1) { var tempLink = links[index].href, tempParts; if (tempLink.indexOf(utmInheritingDomain) > 0) { tempLink = tempLink.replace(utmRegExp, ""); tempParts = tempLink.split("#"); if (tempParts[0].indexOf("?") < 0 ) { tempParts[0] += "?" + utms.join("&"); } else { tempParts[0] += "&" + utms.join("&"); } tempLink = tempParts.join("#"); } links[index].href = tempLink; } }());
  • June 10, 2024
  • 3 min read

Introducing Structured Output on Friendli Engine for Building LLM Agents

Introducing Structured Output on Friendli Engine for Building LLM Agents thumbnail

Large language models (LLMs) excel at creative text generation, but we often face a case where we need LLM outputs to be more structured. This is where our exciting new "structured output" feature comes in.

Why Structured Generation Matters

Imagine you're building a data pipeline. You want an LLM to analyze text and output the sentiment in a machine-readable format, like JSON, which can then be fed into other system components (e.g., for building LLM agents) that require data inputs to follow accurate syntaxes. Free-flowing text will probably generate parsing errors if naively pasted in programming languages, so you would probably need a specific structure for seamless integration with other tools. Structured output lets you achieve this by:

  • Enforcing Patterns (Regex): Specify a specific pattern (e.g., CSV), character sets, or specific language characters (e.g., Korean Hangul characters) for your LLM's output.
  • Enforcing Format (JSON): Specify a particular format, like JSON, to easily import them into your code. Structured output makes this happen seamlessly within your query.

The Challenge: Dealing with the Probability on LLMs

LLMs are probabilistic - they generate creative text, but following strict rules can be tricky. Even with careful prompt engineering, enforcing specific formats and patterns isn't always straightforward.

Structured Output: Providing Strict Syntaxes for the LLMs

Here's how we ensure structured output:

  • Token Filtering: We create a filter that allows only specific "tokens" to be generated by the LLM. Structured output lets you define this filter, ensuring the LLM's creations adhere to your format.
  • The response_format Query Parameter: Concretely, the Friendli Engine receives this option within your LLM queries to allow you to specify the desired output structure. This supports JSON schemas and regular expressions, which can be extended to many use cases including character restrictions or CSV-file generations!
  • Integrated in the Friendli Engine: The Friendli Engine powers all the Friendli products, including Friendli Container, Dedicated Endpoints, and Serverless Endpoints. Therefore, this feature is readily available in all of the different products.

Real-World Examples

Let's see structured output in action:

  • Example 1: Structured Sentiment Analysis (JSON Schema): Imagine analyzing customer reviews and needing sentiment scores in a specific JSON format for further analysis. Structured output allows you to define the exact JSON schema, ensuring the LLM outputs data perfectly formatted for your needs. Let’s check out the example code below, where the comments describe each of the components:
python
import json
import requests

# The json schema that we want our outputs to be. The "properties" describe each item to be included in our results and "descriptions" describe the role of each property.
json_schema = {
    "type": "object",
    "properties": {
        "rationale": {
            "type": "string",
            "description": "The rationale behind the sentiment classification, explaining why the text was classified in a certain way."
        },
        "sentiment": {
            "enum": ["positive", "neutral", "negative"],
            "description": "The sentiment classification of the text."
        },
        "confidence": {
            "type": "integer",
            "minimum": 1,
            "maximum": 5,
            "description": "The confidence level of the sentiment classification, ranging from 1 to 5. A value of 1 indicates low confidence or vagueness, while 5 indicates high confidence or obvious sentiment."

As you can see from the example, the output message contains a json-formatted LLM output, containing the three properties that we have specified in our json_schema.

python
{
  'choices': [
    {
      'finish_reason': 'stop',
      'index': 0,
      'logprobs': None,
      'message': {
        'content': '{"rationale":"The text expresses a positive sentiment due to the use of the word\'very happy\' and the description of the picture quality as \'amazing\'. The phrase \'easy to set up\' also contributes to the positive tone, as it implies a hassle-free experience. The text lacks any negative language or criticisms, further supporting the positive sentiment.","sentiment":"positive","confidence":5}',
        'role': 'assistant'
      }
    }
  ],
  'created': 1717568979,
  'usage': {
    'completion_tokens': 82,
    'prompt_tokens': 302,
    'total_tokens': 384
  }
}
  • Example 2: Language-Specific Results (Regex): If you need search results consisting of only a specific set of characters (e.g., Korean letters), structured output lets you define a regular expression that restricts the LLM's output to those characters (i.e., ensuring only Korean text is generated). Let’s check out the example code below:
python
import requests

url = 'https://api.friendli.ai/serverless/v1/chat/completions'
PAT = "flp_FILL_IN_YOUR_PERSONAL_ACCESS_TOKEN"
# By specifying the response_format, we can restrict our output to contain Korean characters only. Otherwise, it would produce results mixed with alphabets.
# Note that \uac00-\ud7af is the unicode range of Korean hangul characters.
data = {
    "model": "meta-llama-3.1-8b-instruct",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant that generates historical facts.",
      },
      {
        "role": "user",
        "content": "조선 왕조의 첫번째 왕은 누구입니까 (who is the first king of the Joseon Dynasty)?",
      }
    ],
    "response_format": {"type": "regex", "schema": r"[\n ,.?!0-9\uac00-\ud7af]*"},
    "frequency_penalty": 0,

You can see that the content of the generated output message only contains the characters restricted by our regex.

json
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "message": {
        "content": "조선 왕조의 첫번째 왕은 태조 이성계입니다.",
        "role": "assistant"
      }
    }
  ],
  "created": 1717567041,
  "usage": {
    "completion_tokens": 17,
    "prompt_tokens": 49,
    "total_tokens": 66
  }
}
  • Example 3: Data Wrangling with CSVs (Regex): One can easily scrape product information from websites and import it into a spreadsheet with AI. Structured output lets you define a CSV format, allowing the LLM to directly generate comma-separated data, ready for Excel's powerful functionalities. Let’s check out the example code:
python
import json

system_prompt = """You are a highly advanced data analyst API specializing in parsing a html from a website and summarizing it in CSV.
You must respond with your analysis only in CSV. You just print a clear, concise, minimal CSV.
"""
# We are going to parse this HTML file.
HTML = """
<div class="ant-row ant-row-start products-row" style="margin: -8px -20px 8px;">
  <div class="ant-col ant-col-md-8" style="padding: 8px 20px;">
    <div class="ant-card ant-card-bordered ant-card-hoverable" style="padding: 10px;">
      <div class="ant-card-cover">
        <img height="320px" width="280px" alt="example" src="https://static.nike.com/a/images/c_limit,w_318,f_auto/t_product_v1/i1-f4a2691f-131f-4d2a-855b-d5c62fe68ccd/react-miler-mens-running-shoe-DgF6nr.jpg">
      </div>
      <div class="ant-card-body">
        <div class="ant-card-meta">
          <div class="ant-card-meta-detail">
            <div class="ant-card-meta-title">
              <h2>Nike React Miler</h2>
            </div>
            <div class="ant-card-meta-description">The Nike React Miler is a running shoe designed for long-distance running and provides a balance of comfort, stability, and durability.</div>

We can see that the result is ready to be imported to a spreadsheet in a csv format.

Title,Price,Description
Nike React Miler,$130,The Nike React Miler is a running shoe designed for long-distance running and provides a balance of comfort.
Nike React Infinity Run Flyknit,$160,The Nike React Infinity Run Flyknit is a running shoe designed to reduce the risk of running-related injuries.
Nike Air Force 1 '07,$90,The Nike Air Force 1 '07 is a modern version of the classic Nike Air Force 1 sneaker. The '07 version retains the iconic silhouette and design elements of the original while incorporating some updated materials and construction techniques.

Beyond Overcoming Probabilistic Errors

Structured output empowers you to create LLMs that are (almost) free from probabilistic errors when it comes to format and pattern adherence. This opens doors for building robust and reliable pipelines and LLM agents that leverage the power of LLMs with the control you crave.

Try it out today and unlock a whole new level of control over your LLM's creations! We offer three options to suit your preferences:

Visit https://friendli.ai/try-friendli/ to begin your journey into the world of high-performance LLM serving with the Friendli Engine!


Written by

FriendliAI logo

FriendliAI Tech & Research


Share