An integration package connecting OpenGradient and LangChain
Project description
langchain-opengradient
This package contains the LangChain integration with OpenGradient.
More information about OpenGradient can be found here.
Installation
pip install -U langchain-opengradient
And you should configure credentials by setting the following environment variables:
OPENGRADIENT_PRIVATE_KEY - Your OpenGradient private API key
If you do not have an OpenGradient private key configured you can get one by running
pip install opengradient
opengradient config init
Toolkits
OpenGradientToolkit
class provides a set of functions for creating tools that integrate OpenGradient models and workflows into LangChain agents.
from langchain_opengradient import OpenGradientToolkit
import opengradient as og
from pydantic import BaseModel, Field
from typing import List
# Initialize the toolkit
# Either set the environment variable "OPENGRADIENT_PRIVATE_KEY"
# or directly pass in private key.
toolkit = OpenGradientToolkit(private_key="MY_PRIVATE_KEY")
# Example 1: Create a volatility prediction tool with no input schema
def model_input_provider():
return {
"open_high_low_close": [
[2535.79, 2535.79, 2505.37, 2515.36],
[2515.37, 2516.37, 2497.27, 2506.94],
# ... more price data
]
}
def output_formatter(inference_result):
return format(float(inference_result.model_output["Y"].item()), ".3%")
volatility_tool = toolkit.create_run_model_tool(
model_cid="QmRhcpDXfYCKsimTmJYrAVM4Bbvck59Zb2onj3MHv9Kw5N",
tool_name="eth_usdt_volatility",
model_input_provider=model_input_provider,
model_output_formatter=output_formatter,
tool_description="Generates volatility measurement for ETH/USDT",
inference_mode=og.InferenceMode.VANILLA,
)
# Example 2: Create a tool with an input schema
class VolatilityInputSchema(BaseModel):
token: str = Field(description="Token name (e.g., 'ethereum' or 'bitcoin')")
def model_input_provider_with_schema(**llm_input):
token = llm_input.get("token")
# Fetch appropriate data based on token
if token == "bitcoin":
return {"price_series": [100001.1, 100013.2, 100149.2, 99998.1]} # Replace with live data
elif token == "ethereum":
return {"price_series": [2010.1, 2012.3, 2020.1, 2019.2]} # Replace with live data
else: # ethereum
raise ValueError("Received unexpected token")
token_volatility_tool = toolkit.create_run_model_tool(
model_cid="QmZdSfHWGJyzBiB2K98egzu3MypPcv4R1ASypUxwZ1MFUG",
tool_name="token_volatility",
model_input_provider=model_input_provider_with_schema,
model_output_formatter=lambda x: format(float(x.model_output["std"].item()), ".3%"),
tool_input_schema=VolatilityInputSchema,
tool_description="Measures return volatility for specified token"
)
# Example 3: Create a workflow reading tool
workflow_tool = toolkit.create_read_workflow_tool(
workflow_contract_address="0x58826c6dc9A608238d9d57a65bDd50EcaE27FE99",
tool_name="ETH_Price_Forecast",
tool_description="Reads latest forecast for ETH price",
output_formatter=lambda x: f"Price change forecast: {
format(float(x.numbers['regression_output'].item()), '.2%')
}"
)
# Add tools to the toolkit
toolkit.add_tool(volatility_tool)
toolkit.add_tool(token_volatility_tool)
toolkit.add_tool(workflow_tool)
# Get all tools
tools = toolkit.get_tools()
# Use with an agent
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
llm = ChatOpenAI()
agent_executor = create_react_agent(llm, tools)
example_query ="What's the current volatility of ETH/USDT?"
events = agent_executor.stream(
{"messages": [("user", example_query)]},
stream_mode="values",
)
for event in events:
event["messages"][-1].pretty_print()
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
File details
Details for the file langchain_opengradient-0.1.1.tar.gz
.
File metadata
- Download URL: langchain_opengradient-0.1.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0e78f5b7b59cbf14b8edb6a85b402489635b5126cfa3faed6900d98b7493003 |
|
MD5 | 93f7beb783d3cd4f187b50436ccce8d0 |
|
BLAKE2b-256 | ca886ec6011d8af32b976c3a01dd4640c02bcce4c1029e3e26938078da2c5a15 |
File details
Details for the file langchain_opengradient-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: langchain_opengradient-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.1.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64b75c2e9c718f1beaabf46119d3a2cb86be2ecdf4d670a274efd634e972ba07 |
|
MD5 | 249ca985f00792915b184d9dc2c0b973 |
|
BLAKE2b-256 | 2ec854780dfe3ea2683d2447d1b15642af596f9054df638a3626ae5fb6bc6605 |