Skip to main content

Providing the Compass API to langchain agents.

Project description

langchain-compass

The Compass-LangChain toolkit contains tools which enable an LLM agent to perform onchain operations on major DeFi protocols.

Setup

Installation

pip install -U langchain-compass

Environment Setup

# .env
OPENAI_API_KEY=your_openai_api_key_here

Usage:

List Tools in Toolkit:

from langchain_compass.toolkits import LangchainCompassToolkit
tools = LangchainCompassToolkit(compass_api_key=None).get_tools()
[t.name for t in tools]

Expected output:

# output
aave_supply_
aave_borrow_
aave_repay_
aave_withdraw_
aave_asset_price_get_
...

Using with an agent

from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
from langchain_compass.toolkits import LangchainCompassToolkit
from dotenv import load_dotenv
from langgraph.checkpoint.memory import MemorySaver
load_dotenv()


# Initialize LLM - replace 'gpt-4o' with a model of your choice
llm = ChatOpenAI(model='gpt-4o')

# Get the DeFi tools from LangchainCompassToolkit
tools = LangchainCompassToolkit(compass_api_key=None).get_tools()

# Setup memory for your agent
memory = MemorySaver()

# Create a ReAct agent with the specified LLM, tools, and memory
agent = create_react_agent(
    llm,
    tools=tools,
    checkpointer=memory,
    prompt="You are a helpful agent that can interact onchain using tools that you've been told how to use. If you are uncertain that you have sufficient information to call your tools then please ask the user for more information until you have sufficient information to call your tool."
)

# Example user query
from langchain_core.messages import HumanMessage
user_input = 'what is the balance of vitalic.eth.'

# Optional config data, such as thread IDs or session context
config = {"configurable": {"thread_id": "abc123"}}

# Invoke the agent with the user query
output = agent.invoke(input={"messages": [HumanMessage(content=user_input)]}, config=config)

# Display the agent's final response
print(output["messages"][-1].content)

Expected output:

$ python ./main.py 
The balance of the wallet associated with **vitalik.eth** is approximately **$486,222.54**. Here's a breakdown of the token balances:

- **1INCH**: 6.037 ($1.03)
- **AAVE**: 0.010 ($1.43)
- **BAL**: 0.932 ($1.04)
- **crvUSD**: 0.775 ($0.78)
- **DAI**: 317,203.872 ($317,242.95)
- **ENS**: 1,144.036 ($16,710.33)
- **LINK**: 1.778 ($22.52)
- **rsETH**: 0.00003 ($0.05)
- **UNI**: 0.000017 ($0.00009)
- **USDC**: 123,223.707 ($123,215.08)
- **USDT**: 170.148 ($170.12)
- **WBTC**: 0.00107 ($91.93)
- **WETH**: 16.395 ($28,765.28)

These values are subject to market fluctuations.

Run the agent interactively based on user input.

To run the agent interactively please add this snippet to the bottom of the code in the previous section.

from rich.console import Console
from rich.markdown import Markdown
console = Console()
print("Starting chat mode... Type 'exit' to end.")
while True:
    user_input = input("\nPrompt: ")
    output = agent.invoke(input = {"messages": [HumanMessage(content=user_input)]}, config=config)
    answer = output["messages"][-1].content
    console.print(Markdown(answer))

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

langchain_compass-0.2.2.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

langchain_compass-0.2.2-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file langchain_compass-0.2.2.tar.gz.

File metadata

  • Download URL: langchain_compass-0.2.2.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for langchain_compass-0.2.2.tar.gz
Algorithm Hash digest
SHA256 06b58893e1fa0b965474eca7946a7ec5269648dd7bd669e602505a605c7f43c4
MD5 7b3faf2b935d30d8b6fb3120fcf7aed7
BLAKE2b-256 773d4b86791772cace73928ba8b76aabd64e60e527517cd25577855c1c941783

See more details on using hashes here.

File details

Details for the file langchain_compass-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_compass-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1b850993474af9f6bc549502950188b5abaa0669109086daaece7c29012e16ea
MD5 7084a0497a69de34d7a026bd30361b06
BLAKE2b-256 edc7ddc5bac12c046cc51a0669495ca11653064b672c5f1d062a3fa7b17b4f1d

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