A simplified wrapper around LangChain for easy tool usage.
Project description
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😊.
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 easylangchain-0.2.3.tar.gz.
File metadata
- Download URL: easylangchain-0.2.3.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5381e06cc9d9a7247b7a57cf800fce2ad4adac0e775d2b8aee9b568ddaac656b
|
|
| MD5 |
381e885409d0ec0393bc0e9224ad9aa0
|
|
| BLAKE2b-256 |
2956397d1c0da613fe3836c29d4505758f9442a0a3ef9fc3bc774dab340aba14
|
Provenance
The following attestation bundles were made for easylangchain-0.2.3.tar.gz:
Publisher:
publish.yml on zhiyiZeng/easylangchain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
easylangchain-0.2.3.tar.gz -
Subject digest:
5381e06cc9d9a7247b7a57cf800fce2ad4adac0e775d2b8aee9b568ddaac656b - Sigstore transparency entry: 148222610
- Sigstore integration time:
-
Permalink:
zhiyiZeng/easylangchain@cc08c9c6122e60e297be6724c5da235f949d0071 -
Branch / Tag:
refs/tags/v0.2.3 - Owner: https://github.com/zhiyiZeng
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@cc08c9c6122e60e297be6724c5da235f949d0071 -
Trigger Event:
push
-
Statement type:
File details
Details for the file easylangchain-0.2.3-py3-none-any.whl.
File metadata
- Download URL: easylangchain-0.2.3-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
444cb1d65a8beaaf5024fb2484cf6fedb5904f8ecbc37bfd9e298717e4c8c46e
|
|
| MD5 |
70c5780e4d9e3859262c624226c7b185
|
|
| BLAKE2b-256 |
095483be856516c3f1b372cf2f8b19caa7e00c70464c59bb347d3038775972cd
|
Provenance
The following attestation bundles were made for easylangchain-0.2.3-py3-none-any.whl:
Publisher:
publish.yml on zhiyiZeng/easylangchain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
easylangchain-0.2.3-py3-none-any.whl -
Subject digest:
444cb1d65a8beaaf5024fb2484cf6fedb5904f8ecbc37bfd9e298717e4c8c46e - Sigstore transparency entry: 148222613
- Sigstore integration time:
-
Permalink:
zhiyiZeng/easylangchain@cc08c9c6122e60e297be6724c5da235f949d0071 -
Branch / Tag:
refs/tags/v0.2.3 - Owner: https://github.com/zhiyiZeng
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@cc08c9c6122e60e297be6724c5da235f949d0071 -
Trigger Event:
push
-
Statement type: