EasyLangChain
A simplified wrapper around LangChain for easy tool usage.
While LangChain is a powerful framework, I found it over-engineered and highly abstracted, which can make it challenging to use effectively. For example, when implementing LLM + Prompt + Tools + Agents + Memory, I encountered numerous issues where both the official documentation examples and ChatGPT-provided solutions would fail. One specific pain point is the complexity around agent creation - it requires using a separate create_tool_agents function and an AgentExecutor class (which lacks compatible memory APIs), when a single Agent class could potentially handle both responsibilities more elegantly.
However, I do appreciate some of LangChain's standardized design choices, such as the .invoke naming convention. I want to use LangChain painlessly. If you've faced similar frustrations, you might want to try this library. EasyLangChain's goal is simple: implement most common functionalities in the simplest way possible.
Installation
- from pypi
pip install easylangchain
- from github
git clone https://github.com/zhiyiZeng/easylangchain.git
cd easylangchain
pip install -r requirements.txt
pip install -e .
A toy example
Example codes are in easylangchain/examples/tryit.py, just run
python tryit.py
or copy codes below.
from easylangchain.llms import ChatTongyi, ChatOpenAI
from easylangchain.tools import tool
weathers = {
"Beijing": {"2024-11-10": "Sunny", "2024-11-11": "Cloudy"},
"Shanghai": {"2024-11-10": "Light Rain", "2024-11-11": "Cloudy"},
}
@tool
def get_weather(location: str, date: str = "2024-11-10") -> int:
"""Get the weather of a location on a specific date.
Args:
location: The location to get the weather of.
date: The date to get the weather of. defaults to 2024-11-10.
"""
print("get_weather is called")
return weathers[location][date]
# OpenAI model
llm = ChatOpenAI(
api_key = "YOUR_OPENAI_TOKEN",
model ="gpt-4o",
has_memory = True, # Enable multi-rounds conversation(with memory)
)
# Tongyi model
# llm = ChatTongyi(
# api_key = "YOUR_QW_TOKEN",
# model = "qwen-plus",
# has_memory = True,
# )
# bind tools
llm.bind_tools([get_weather,])
print("The first round:")
query = "How's the weather in Beijing"
print(f"You: {query}")
# Method 1: Pass a string directly
response = llm.invoke(query)
# Method 2: Pass an OpenAI-style dictionary
# response = llm.invoke({"role": "user", "content": query})
# Method 3: Use LangChain-style message object
# from easylangchain.messages import HumanMessage
# response = llm.invoke(HumanMessage(content=query))
# parse response into string, default format_type is 'str'
print(f"Assistant: {llm.parse_response(response)}")
# The second round
print("\nThe second round:")
response = llm.invoke("What was my last question?")
print(f"Assistant: {llm.parse_response(response)}")
As you can see, only minimal abstracts and classes you are exposed to. All you need to do is to
- instantiate the model class with
ChatOpenAI(), - bind tools if necessary with
.bind(), - start your query with
.invoke(), - format the result with
.parse_response().
In the meanwhile,
- Don't need to bother what are the differences between
OpenAIandChatOpenAI. - Don't need to bother what are the differences between different Message classes, such as
HumanMessage,SystemMessage,AssistantMessage,ToolMessage... - Don't need to bother what are the differences between different Memory classes.
- Don't need to bother what are the differences between different Agent classes.
- Don't need to bother what are the differences between different AgentExecutor classes.
With simply wrapping langchain and relevant packages, you can use almost pure python to construct a multi-rounds conversation with tools.
How about more?
If you want more features, just inherit the base classes of EasyLangChain and rewrite them. It’s easier than you think.
For example, if you want to more output formats, just copy BaseLLM from /easylangchain/llms/models.py and rewrite .parse_response().
before:
class BaseLLM
RESPONSE_FORMAT_TYPES = {
"str": StrOutputParser(),
}
...
def parse_response(self, response, format_type: str = "str"):
"""Parse the response"""
format_parser = self.RESPONSE_FORMAT_TYPES.get(format_type)
if format_parser is None:
raise ValueError("not implemented, you can implement it by updating self.RESPONSE_FORMAT_TYPES using pure python dict grammar.")
return format_parser.invoke(response)
class ChatOpenAI(BaseLLM):
...
after:
class CustomBaseLLM(BaseLLM)
self.RESPONSE_FORMAT_TYPES = {
"str": StrOutputParser(),
"json": JsonOutputParser()
}
...
class ChatOpenAI(CustomBaseLLM):
...
Notes
To be clear, I think langchain is powerful, but I think it's over-engineered. I appreciate langchain commmuity very much😊.
Metadata
Release files for easylangchain 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| easylangchain-0.2.3.tar.gz | 12.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| easylangchain-0.2.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.9 kB
Release files / easylangchain-0.2.3.tar.gz
| Download URL | easylangchain-0.2.3.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5381e06cc9d9a7247b7a57cf800fce2ad4adac0e775d2b8aee9b568ddaac656b
|
|
BLAKE2b-256 checksum How to use checksums |
2956397d1c0da613fe3836c29d4505758f9442a0a3ef9fc3bc774dab340aba14
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2024.
Transparency logRelease files / easylangchain-0.2.3-py3-none-any.whl
| Download URL | easylangchain-0.2.3-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
444cb1d65a8beaaf5024fb2484cf6fedb5904f8ecbc37bfd9e298717e4c8c46e
|
|
BLAKE2b-256 checksum How to use checksums |
095483be856516c3f1b372cf2f8b19caa7e00c70464c59bb347d3038775972cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2024.
Transparency log