Skip to main content

A minimal, async-native, and unopinionated toolkit for modern LLM applications.

Project description

lingo library logo

A minimal, async-native, and unopinionated toolkit for modern LLM applications.


PyPI - Version PyPi - Python Version Github - Open Issues PyPi - Downloads (Monthly) Github - Commits


lingo provides a powerful, two-layered API for building, testing, and deploying complex LLM workflows with precision and clarity.

The Philosophy: A Dual-Layer API

lingo is built on the idea that developers need both a high-level, declarative way to build workflows and a low-level, imperative way to control them.

  1. The High-Level Flow API: For most applications, you'll use the fluent, chainable Flow API. This allows you to define complex, reusable conversation logic with branching, tool use, and subroutines in a declarative, readable way.

  2. The Low-Level Context API: For full, fine-grained control, lingo provides the LLM and Context classes. This imperative layer is perfect for building custom chatbot loops or implementing your own conversational state logic from scratch.

Installation

pip install lingo-ai

Quickstart: Building a Declarative Flow

Let's build a simple assistant that can check the weather, but will first decide if the user is being polite.

1. Define Tools & LLM

import asyncio
from lingo.llm import LLM, Message
from lingo.tools import tool
from lingo.flow import Flow, NoOp

llm = LLM(model="gpt-4o")

@tool
async def get_weather(location: str) -> str:
    """Gets the current weather for a specified location."""
    if "boston" in location.lower():
        return "It's 75°F and sunny in Boston."
    else:
        return f"Weather for {location} is unknown."

2. Define Reusable Sub-Flows

# A sub-flow is just another 'Flow' instance.
polite_flow = Flow().system(
    "Acknowledge the user's politeness before proceeding."
)

3. Define the Main Workflow

main_flow = (
    Flow()
    # Step 1: Add a system message to the context
    .system("You are a helpful assistant. You can check the weather.")

    # Step 2: Use an LLM to make a True/False decision
    .decide(
        prompt="Is the user being polite (e.g., asking 'please')?",
        on_true=polite_flow,  # Run the sub-flow if True
        on_false=NoOp()       # Do nothing if False
    )

    # Step 3: Equip and invoke a tool from the available list
    .invoke(get_weather)

    # Step 4: Generate a final reply using the tool's output
    .reply()
)

4. Execute the Flow

async def main():
    # Example 1: The "polite" branch
    user_query_1 = "Could you please tell me the weather in Boston?"

    # .execute() runs the full pipeline on the initial messages
    final_context = await main_flow.run(llm, [Message.user(user_query_1)])

    print(f"User: {user_query_1}")
    print(f"Assistant: {final_context.messages[-1].content}")
    # Assistant: It's 75°F and sunny in Boston. I'm happy to help!

    print("\n" + "-"*20 + "\n")

    # Example 2: The "impolite" branch
    user_query_2 = "boston weather now"
    final_context_2 = await main_flow.run(llm, [Message.user(user_query_2)])

    print(f"User: {user_query_2}")
    print(f"Assistant: {final_context_2.messages[-1].content}")
    # Assistant: It's 75°F and sunny in Boston.

if __name__ == "__main__":
    asyncio.run(main())

The Two APIs

lingo gives you the flexibility to choose the right level of abstraction.

1. High-Level API: Flow

The Flow class provides a fluent, chainable interface for declaratively building reusable workflows. You define the steps of the conversation, and lingo handles the execution.

# A flow is a composable, reusable blueprint
weather_flow = (
    Flow()
    .system("You only answer with the weather.")
    .invoke(get_weather)
    .reply()
)

# You can nest flows inside other flows
main_flow = (
    Flow()
    .choose(
        prompt="Is the user asking about weather or stocks?",
        choices={
            "weather": weather_flow,
            "stocks": stock_flow,
        }
    )
)

2. Low-Level API: LLM & Context

For maximum control, you can use the imperative API. The Context object holds the message history, and you call its methods (.reply(), .decide(), .invoke()) directly. This is ideal for building custom chatbot loops.

# Manually building a conversation turn by turn
llm = LLM(model="gpt-4o")
context = Context(llm, [Message.system("You are a chatbot.")])

while True:
    user_input = input("You: ")
    context.add(Message.user(user_input))

    # Call the LLM with the current context
    response = await context.reply()

    context.add(response)
    print(f"Bot: {response.content}")

Contributing

Contributions are welcome! lingo is an open-source project, and we'd love your help in making it better. Please feel free to open an issue or submit a pull request.

License

lingo is licensed under the MIT License. See the LICENSE file for details.

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

lingo_ai-0.2.1.tar.gz (50.0 kB view details)

Uploaded Source

Built Distribution

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

lingo_ai-0.2.1-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file lingo_ai-0.2.1.tar.gz.

File metadata

  • Download URL: lingo_ai-0.2.1.tar.gz
  • Upload date:
  • Size: 50.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lingo_ai-0.2.1.tar.gz
Algorithm Hash digest
SHA256 da25e73d2cea86e017e669fda86d4dffc0908c17034607724b76de589e1735e0
MD5 e2cdf766d22ac4c5bea150bafabfaea3
BLAKE2b-256 ad614ae4d56f2e5a8437cb2721868786e8892d66fd9a6a3e73c4dccad0dc30f5

See more details on using hashes here.

File details

Details for the file lingo_ai-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: lingo_ai-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for lingo_ai-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 940b005272fd0f34207ae4d46d53be7e6da9b154eb290115ca71c983e7ab8f7e
MD5 bff4eecc1c9dcd82fa12bc40b159038a
BLAKE2b-256 7b373d258c1d3b7aecf36af753cad29e1d49008c157b012257e0fdb3541edd38

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