Skip to main content

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()

Metadata

Release files for langchain-opengradient 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-opengradient 0.1.2
File Size Uploaded Provenance Actions
langchain_opengradient-0.1.2.tar.gz 6.1 kB Without provenance Details for langchain_opengradient-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for langchain-opengradient 0.1.2
File Interpreter ABI Platform Actions
langchain_opengradient-0.1.2-py3-none-any.whl Python 3 none any Details for langchain_opengradient-0.1.2-py3-none-any.whl

Total release size: 13.5 kB

Release files / langchain_opengradient-0.1.2.tar.gz

Download URL langchain_opengradient-0.1.2.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a731de936e0362d8c6858843b24e59b3e90334787a64d53ad0a280295300d130
BLAKE2b-256 checksum
How to use checksums
349c5eae573760ccec4ebbe7544e57523b9cd34119b630914ee84bf4f2f65ced
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.13.2 Darwin/24.1.0

Release files / langchain_opengradient-0.1.2-py3-none-any.whl

Download URL langchain_opengradient-0.1.2-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
84854b1c3dfaeb52a3dae3957cd2724cae28ba04d71326c651efc9d072218fd3
BLAKE2b-256 checksum
How to use checksums
b298a5578f61b5cd65cccb5f19353885b6b90a965711eee05f259d609cb259f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.1 CPython/3.13.2 Darwin/24.1.0

Security for langchain-opengradient 0.1.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page