A minimal, async-native, and unopinionated toolkit for modern LLM applications.
Project description
A minimal, async-native, and unopinionated toolkit for modern LLM applications.
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.
-
The High-Level
FlowAPI: For most applications, you'll use the fluent, chainableFlowAPI. This allows you to define complex, reusable conversation logic with branching, tool use, and subroutines in a declarative, readable way. -
The Low-Level
ContextAPI: For full, fine-grained control,lingoprovides theLLMandContextclasses. 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lingo_ai-0.2.2.tar.gz.
File metadata
- Download URL: lingo_ai-0.2.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1efeabfeebe410b81530745b9f71c111ab694444fc201ba36cb83c39aab1804b
|
|
| MD5 |
029921bd7a85b76c29476eb33c066a3b
|
|
| BLAKE2b-256 |
8c587905660ba151fd61691573b226e5aed5a4f94fa99c88f38f8ba856d824de
|
File details
Details for the file lingo_ai-0.2.2-py3-none-any.whl.
File metadata
- Download URL: lingo_ai-0.2.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a755670ff79d08b26907c2bdb496e896ec7205b615044f0c7766c599d7bdf29
|
|
| MD5 |
3861bef4581eef7f0449bf55c1047e34
|
|
| BLAKE2b-256 |
d037538bb9a683b3925136ab2b700fd05c0a27e5b9a024c89a16a2ce1c38ca1c
|