Skip to main content

LangChain integration layer for the Ape AI-first programming language

Project description

ape-langchain

LangChain integration layer for the Ape AI-first programming language

License: MIT Python 3.11+

What is this?

ape-langchain bridges the Ape programming language with LangChain, enabling you to:

  • Use Ape tasks as LangChain tools with deterministic execution
  • Eliminate AI hallucination in tool invocation through strict validation
  • Build reliable AI agents with formally defined, constraint-validated workflows

Ape is a deterministic AI-first language designed to remove ambiguity from AI-generated code. ape-langchain makes Ape tasks seamlessly callable from LangChain agents.

Installation

pip install ape-langchain

This will automatically install ape-lang and langchain as dependencies.

Quick Start

1. Write an Ape Task

Create pricing.ape:

task calculate_total_price
  inputs:
    base_price: String
    tax_rate: String
  outputs:
    total: String
  constraints:
    - base_price must be numeric
    - tax_rate must be between 0 and 1
  steps:
    - Parse base_price as decimal
    - Multiply by (1 + tax_rate)
    - Return as string

2. Use it in LangChain

from ape_langchain import ApeTool
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI

# Wrap Ape task as LangChain tool
pricing_tool = ApeTool(
    ape_file="pricing.ape",
    function="calculate_total_price",
    description="Calculate total price with tax"
)

# Create agent with deterministic tool
llm = OpenAI(temperature=0)
agent = initialize_agent(
    tools=[pricing_tool],
    llm=llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

# Execute - AI decides when to call, but execution is deterministic
result = agent.run("What's the total for $99.99 with 21% tax?")
print(result)

3. Or Use ApeChain Directly

For simpler workflows without full agent orchestration:

from ape_langchain import ApeChain

chain = ApeChain.from_ape_file("pricing.ape", function="calculate_total_price")

result = chain.run(base_price="99.99", tax_rate="0.21")
print(result)  # Deterministic, validated output

Why Use This?

Problem: AI Tool Invocation is Unreliable

# Traditional approach - AI can hallucinate parameters, miss constraints
def calculate_price(base, tax):
    return base * (1 + tax)  # What if base is negative? What if tax > 1?

Solution: Ape + LangChain

# Ape approach - constraints enforced, no ambiguity
pricing_tool = ApeTool("pricing.ape", "calculate_total_price")
# - Input validation automatic
# - Constraints checked before execution
# - No hallucination possible

API Overview

ApeTool

LangChain BaseTool adapter for Ape tasks.

from ape_langchain import ApeTool

tool = ApeTool(
    ape_file="path/to/module.ape",
    function="task_name",
    description="Optional description for LLM"
)

# Use in LangChain agents
agent = initialize_agent(tools=[tool], llm=llm, ...)

ApeChain

High-level wrapper for direct execution.

from ape_langchain import ApeChain

chain = ApeChain.from_ape_file("module.ape", function="task_name")
result = chain.run(param1="value", param2="value")

ApeTask

Low-level task representation with JSON Schema export.

from ape_langchain import ApeTask
from ape import compile

module = compile("module.ape")
task = ApeTask.from_module(module, "task_name")

# Get JSON Schema for OpenAI/Anthropic function calling
schema = task.to_json_schema()

Features

  • Deterministic execution - Ape constraints prevent invalid inputs
  • Type-safe - Full type checking before execution
  • LangChain native - Works with all LangChain agent types
  • JSON Schema export - Compatible with OpenAI/Anthropic function calling
  • Error handling - Clear exception types for debugging

Requirements

  • Python 3.11+
  • ape-lang >=0.2.0
  • langchain >=0.1.0

Development Status

v0.1.0 - Initial release

Currently supports:

  • Basic Ape task wrapping
  • LangChain tool integration
  • Simple type mapping (all parameters as strings for now)

Roadmap:

  • Advanced type system (int, float, list, dict)
  • Async execution support
  • Multi-task orchestration
  • Native Ape control flow support

Contributing

Contributions welcome! See the main Ape repository for guidelines.

License

MIT License - see LICENSE file for details.

Links


Built with Ape 🦍 - Because AI should execute deterministically, not approximately.

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

ape_langchain-0.1.2.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

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

ape_langchain-0.1.2-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file ape_langchain-0.1.2.tar.gz.

File metadata

  • Download URL: ape_langchain-0.1.2.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ape_langchain-0.1.2.tar.gz
Algorithm Hash digest
SHA256 23f6665c3739bc3eac062e60f74d1fa176d2af9519af8e6ef2bc7a61ccf1191e
MD5 53ecad987ab8be1dbe77e83dd46edad1
BLAKE2b-256 584d4f8bc6ef01f703b1365e5a1e4515aa92c428110a0b68c896570aa96a759a

See more details on using hashes here.

File details

Details for the file ape_langchain-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ape_langchain-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for ape_langchain-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 409eeb251f3e6aa1dbc2b0d2c12a6a82d54eaced3f52ff275cb35bd24f38a806
MD5 7dc5c4cb397ca029036a34b265f65629
BLAKE2b-256 59feb1e534d12f02267dd490b28de26fdfe3b3c71daee173514631867ff1ea64

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