Skip to main content

Natural Language Differential Programming

Project description

๐Ÿ”ฅ Embr DSL: A Shell-Inspired Chat Abstraction for Language Programs

๐Ÿง  Overview

Embr is a domain-specific language (DSL) designed for interactive, composable natural language pipelines, inspired by the syntax and philosophy of Unix shell scripting. ๐Ÿš

It treats a conversational state (chat log) as a mutable object that can be transformed, extended, and queried using intuitive operators like <<, >>, |, and |=.

โœจ Core Design Principles

  • ๐Ÿงฑ Declarative + Composable: Like piping commands in a shell.
  • ๐Ÿง‘โ€๐ŸŽค Role-aware: Messages are typed with @ (e.g., user @ "text").
  • ๐Ÿ› ๏ธ Reducers as Functions: Language models and tools act like reducers.
  • ๐Ÿ” Stateful by Default: All operators mutate a shared conversation state (Embr).

๐Ÿงฎ Operator Table

๐Ÿ”ฃ Operator ๐Ÿ“ Description ๐Ÿ’ก Example Usage
@ Assign role to a message user @ "message"
<< Append message to chat (in-place) chat << "text"
>> Also appends in-place. updated_chat = chat >> gpt
> Send chat to model, get response chat > gpt
| Apply model and append output chat = chat << "hello" | gpt
| = Register reducer into chat's pipeline chat |= gpt | claude

๐Ÿ”„ Chat Composition and Transfer Patterns

Operator Precedence

In Embr, operators follow standard Python precedence rules. For our operators, from highest to lowest:

Precedence Operators Description
Highest @ Role annotation
High > Process through model
Medium >> Chat transfer
Low | Pipe and modify
Low << Append message

This affects how expressions are evaluated when chained together.

Chat Transfer Pattern

The chat transfer pattern allows you to process a chat through a model and transfer the results to another chat:

# Process task_chat and transfer results to global_chat
(task_chat > gpt) >> global_chat

This pattern creates a clean separation between task-specific conversations and your main conversation context.


โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ task_chat  โ”‚ >  โ”‚ gpt โ”‚ -> โ”‚ response    โ”‚ >> โ”‚ global_chatโ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚ (Spark)     โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Parenthesis-Free Alternatives

For cleaner code without parentheses, several alternatives exist:

python
# Manual two-step approach
response = task_chat > gpt
global_chat << response

# Using helper methods (proposed)
task_chat.process(gpt).append_to(global_chat)

# Using a new syntax pattern (proposed)
task_chat > gpt > global_chat

The last approach would require updating the implementation of the > operator to handle different right-hand types.


๐Ÿš€ Example Use Cases

1. ๐Ÿ› ๏ธ Manual append + model roundtrip

python
chat = Embr()
chat << user @ "hello"
response = chat > gpt
chat << response

2. ๐Ÿงช Pipe-style chaining

python
chat = Embr()
chat = chat << "what is the weather?" | gpt

3. ๐Ÿ”— Register reducers to the chat

python
chat |= gpt | claude | other_ai
chat.apply_reducers()

4. โ“ Conditional reducers

python
def gpt_if_question(chat):
if "?" in chat.last.content:
return gpt(chat)


chat |= gpt_if_question
chat << "this is a statement"
chat << "what is this?"
chat.apply_reducers()

5. ๐Ÿค– Multi-agent routing

python
chat |= on_call @ gpt | on_call @ images

๐Ÿงฉ Functional Patterns

Currying and Composition

Embr uses a functional programming pattern similar to currying, where functions like gpt are designed to:

  1. Accept an Embr object: Each reducer (like gpt, claude) takes a chat as input
  2. Return a result: Either a Spark (message) or a modified Embr object
  3. Enable operator-based application: The same function works with >, >>, and | operators

This pattern enables multiple styles of interaction:

# Direct application returning a response
response = chat > gpt

# Application with returned response
response = chat >> gpt

# Pipeline style with in-place modification
chat = chat | gpt

Embr brings the expressiveness of Unix pipelines ๐Ÿงต to AI-assisted conversation flows ๐Ÿ’ฌ, making it easy to test, iterate, and layer intelligence like shell commands.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

embr-0.0.1.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

embr-0.0.1-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file embr-0.0.1.tar.gz.

File metadata

  • Download URL: embr-0.0.1.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.9.18 Darwin/24.4.0

File hashes

Hashes for embr-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a2cc2da177e58114dd757b4ab6baa5b59487539ff9fa5b95403ad5e6093a100e
MD5 0c59604da4ddce3166ace7245b7b6a09
BLAKE2b-256 b95309a7b18ae410e0daa2ab752323bbdd0e63de51a5d056faea37eff3006042

See more details on using hashes here.

File details

Details for the file embr-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: embr-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.9.18 Darwin/24.4.0

File hashes

Hashes for embr-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e83c3654b80ef7cb64e8f35b7b26761ea5e0783449f0dcc260aeb2e07bae2969
MD5 32edc2c066a1d778bfdffb95ac299ad6
BLAKE2b-256 2ccd504afbc52859ee18ac612e40dea592536065536947e61a0a752fb12531ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page