Expression Language

LangStream uses an expression language to reference fields of a LangStream record. See Structure of a record for more on the parts of a record.

The expression language is required to evaluate the conditional step when or the compute step expression. The syntax is EL, which uses dot notation to access field properties or map keys.

For example, when the query-vector-db agent queries a Pinecone vector database, the agent uses the expression language to reference the values in "embeddings" and "query-result" with "value.embeddings" and "value.query-result".

- name: "Execute Query"
  type: "query-vector-db"
  configuration:
    datasource: "PineconeDatasource"
    query: |
      {
            "vector": ?,
            "topK": 5,
            "filter":
              {"$or": [{"genre": "comedy"}, {"year":2019}]}
       }
    fields:
      - "value.embeddings"
    output-field: "value.query-result"

Operators

The Expression Language supports the following operators:

  • Arithmetic: +, - (binary), *, / and div, % and mod, - (unary)

  • Logical: and, &&, or, ||, not, !

  • Relational: ==, eq, !=, ne, <, lt, >, gt, <=, ge, >=, le.

Functions

Utility methods available under the fn namespace. For example, to get the current timestamp, use fn:now(). The Expression Language supports the following functions:

Conditional when steps

Each step accepts an optional when configuration that is evaluated at step execution time. For example, the Dispatch agent evaluates the output from the language-detector agent and routes messages based on whether the value in the "properties.language" field is "en" or "fr". If neither, it routes to default-topic.

  - name: "Detect language"
    type: "language-detector"
    input: "input-topic"
    configuration:
      property: "language"

  - name: "Dispatch"
    type: "dispatch"
    output: default-topic
    configuration:
      routes:
        - when: properties.language == "en"
          destination: topic-english
        - when: properties.language == "fr"
          destination: topic-french
        - when: properties.language == "none"
          action: drop

Use the . operator to access top level or nested properties on a schema-full key or value. For example, properties.language or properties.language.nestedValueField.

The when condition supports the expression language syntax by providing access to the record fields below:

Last updated