community: Add OpenGradient integration (Toolkit) (#30190)

Commandeering https://github.com/langchain-ai/langchain/pull/30135

---------

Co-authored-by: kylexqian <kylexqian@gmail.com>
This commit is contained in:
ccurme 2025-03-09 18:08:07 -04:00 committed by GitHub
parent b209d46eb3
commit 67aff1648b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 490 additions and 1 deletions

View File

@ -0,0 +1,138 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"sidebar_label: OpenGradient\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# OpenGradient\n",
"[OpenGradient](https://www.opengradient.ai/) is a decentralized AI computing network enabling globally accessible, permissionless, and verifiable ML model inference.\n",
"\n",
"The OpenGradient langchain package currently offers a toolkit that allows developers to build their own custom ML inference tools for models on the OpenGradient network. This was previously a challenge because of the context-window polluting nature of large model parameters -- imagine having to give your agent a 200x200 array of floating-point data!\n",
"\n",
"The toolkit solves this problem by encapsulating all data processing logic within the tool definition itself. This approach keeps the agent's context window clean while giving developers complete flexibility to implement custom data processing and live-data retrieval for their ML models."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installation and Setup\n",
"Ensure that you have an OpenGradient API key in order to access the OpenGradient network. If you already have an API key, simply set the environment variable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"!export OPENGRADIENT_PRIVATE_KEY=\"your-api-key\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you need to set up a new API key, download the opengradient SDK and follow the instructions to initialize a new configuration."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"!pip install opengradient\n",
"!opengradient config init"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once you have set up your API key, install the langchain-opengradient package."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "powershell"
}
},
"outputs": [],
"source": [
"pip install -U langchain-opengradient"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## OpenGradient Toolkit\n",
"The OpenGradientToolkit empowers developers to create specialized tools based on [ML models](https://hub.opengradient.ai/models) and [workflows](https://docs.opengradient.ai/developers/sdk/ml_workflows.html) deployed on the OpenGradient decentralized network. This integration enables LangChain agents to access powerful ML capabilities while maintaining efficient context usage.\n",
"\n",
"### Key Benefits\n",
"* 🔄 Real-time data integration - Process live data feeds within your tools\n",
"\n",
"* 🎯 Dynamic processing - Custom data pipelines that adapt to specific agent inputs\n",
"\n",
"* 🧠 Context efficiency - Handle complex ML operations without flooding your context window\n",
"\n",
"* 🔌 Seamless deployment - Easy integration with models already on the OpenGradient network\n",
"\n",
"* 🔧 Full customization - Create and deploy your own specific models through the [OpenGradient SDK](https://docs.opengradient.ai/developers/sdk/model_management.html), then build custom tools from them\n",
"\n",
"* 🔐 Verifiable inference - All inferences run on the decentralized OpenGradient network, allowing users to choose various flavors of security such as ZKML and TEE for trustless, verifiable model execution\n",
"\n",
"For detailed examples and implementation guides, check out our [comprehensive tutorial](/docs/integrations/tools/opengradient_toolkit.ipynb)."
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 1
}

View File

@ -0,0 +1,345 @@
{
"cells": [
{
"cell_type": "raw",
"id": "afaf8039",
"metadata": {},
"source": [
"---\n",
"sidebar_label: OpenGradient\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "e49f1e0d",
"metadata": {},
"source": [
"# OpenGradientToolkit\n",
"\n",
"This notebook shows how to build tools using the OpenGradient toolkit. This toolkit gives users the ability to create custom tools based on models and workflows on the [OpenGradient network](https://www.opengradient.ai/).\n",
"\n",
"## Setup\n",
"\n",
"Ensure that you have an OpenGradient API key in order to access the OpenGradient network. If you already have an API key, simply set the environment variable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f7303e1",
"metadata": {},
"outputs": [],
"source": [
"!export OPENGRADIENT_PRIVATE_KEY=\"your-api-key\""
]
},
{
"cell_type": "markdown",
"id": "0a7af45e",
"metadata": {},
"source": [
"If you need to set up a new API key, download the opengradient SDK and follow the instructions to initialize a new configuration."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7777f1e",
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"!pip install opengradient\n",
"!opengradient config init"
]
},
{
"cell_type": "markdown",
"id": "0730d6a1-c893-4840-9817-5e5251676d5d",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"This toolkit lives in the `langchain-opengradient` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "652d6238-1f87-422a-b135-f5abbb8652fc",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-opengradient"
]
},
{
"cell_type": "markdown",
"id": "a38cde65-254d-4219-a441-068766c0d4b5",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Now we can instantiate our toolkit with the API key from before."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
"metadata": {},
"outputs": [],
"source": [
"from langchain_opengradient import OpenGradientToolkit\n",
"\n",
"toolkit = OpenGradientToolkit(\n",
" # Not required if you have already set the environment variable OPENGRADIENT_PRIVATE_KEY\n",
" private_key=\"your-api-key\"\n",
")"
]
},
{
"cell_type": "markdown",
"id": "ad986625",
"metadata": {},
"source": [
"## Build your own tools\n",
"The OpenGradientToolkit offers two main methods for creating custom tools:\n",
"\n",
"### 1. Create a tool to run ML models\n",
"You can create tools that leverage ML models deployed on the [OpenGradient model hub](https://hub.opengradient.ai/). User-created models can be uploaded, inferenced, and shared to the model hub through the [OpenGradient SDK](https://docs.opengradient.ai/developers/sdk/model_management.html).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7a03746",
"metadata": {},
"outputs": [],
"source": [
"import opengradient as og\n",
"from pydantic import BaseModel, Field\n",
"\n",
"\n",
"# Example 1: Simple tool with no input schema\n",
"def price_data_provider():\n",
" \"\"\"Function that provides input data to the model.\"\"\"\n",
" return {\n",
" \"open_high_low_close\": [\n",
" [2535.79, 2535.79, 2505.37, 2515.36],\n",
" [2515.37, 2516.37, 2497.27, 2506.94],\n",
" [2506.94, 2515, 2506.35, 2508.77],\n",
" [2508.77, 2519, 2507.55, 2518.79],\n",
" [2518.79, 2522.1, 2513.79, 2517.92],\n",
" [2517.92, 2521.4, 2514.65, 2518.13],\n",
" [2518.13, 2525.4, 2517.2, 2522.6],\n",
" [2522.59, 2528.81, 2519.49, 2526.12],\n",
" [2526.12, 2530, 2524.11, 2529.99],\n",
" [2529.99, 2530.66, 2525.29, 2526],\n",
" ]\n",
" }\n",
"\n",
"\n",
"def format_volatility(inference_result):\n",
" \"\"\"Function that formats the model output.\"\"\"\n",
" return format(float(inference_result.model_output[\"Y\"].item()), \".3%\")\n",
"\n",
"\n",
"# Create the tool\n",
"volatility_tool = toolkit.create_run_model_tool(\n",
" model_cid=\"QmRhcpDXfYCKsimTmJYrAVM4Bbvck59Zb2onj3MHv9Kw5N\",\n",
" tool_name=\"eth_volatility\",\n",
" model_input_provider=price_data_provider,\n",
" model_output_formatter=format_volatility,\n",
" tool_description=\"Generates volatility measurement for ETH/USDT trading pair\",\n",
" inference_mode=og.InferenceMode.VANILLA,\n",
")\n",
"\n",
"\n",
"# Example 2: Tool with input schema from the agent\n",
"class TokenInputSchema(BaseModel):\n",
" token: str = Field(description=\"Token name (ethereum or bitcoin)\")\n",
"\n",
"\n",
"def token_data_provider(**inputs):\n",
" \"\"\"Dynamic function that changes behavior based on agent input.\"\"\"\n",
" token = inputs.get(\"token\")\n",
" if token == \"bitcoin\":\n",
" return {\"price_series\": [100001.1, 100013.2, 100149.2, 99998.1]}\n",
" else: # ethereum\n",
" return {\"price_series\": [2010.1, 2012.3, 2020.1, 2019.2]}\n",
"\n",
"\n",
"# Create the tool with schema\n",
"token_tool = toolkit.create_run_model_tool(\n",
" model_cid=\"QmZdSfHWGJyzBiB2K98egzu3MypPcv4R1ASypUxwZ1MFUG\",\n",
" tool_name=\"token_volatility\",\n",
" model_input_provider=token_data_provider,\n",
" model_output_formatter=lambda x: format(float(x.model_output[\"std\"].item()), \".3%\"),\n",
" tool_input_schema=TokenInputSchema,\n",
" tool_description=\"Measures return volatility for a specified token\",\n",
")\n",
"\n",
"# Add tools to the toolkit\n",
"toolkit.add_tool(volatility_tool)\n",
"toolkit.add_tool(token_tool)"
]
},
{
"cell_type": "markdown",
"id": "45627b99",
"metadata": {},
"source": [
"### 2. Create a tool to read workflow results\n",
"\n",
"Read workflows are scheduled inferences that regularly run models stored on smart-contracts with live oracle data. More information on these can be [found here](https://docs.opengradient.ai/developers/sdk/ml_workflows.html).\n",
"\n",
"You can create tools that read results from workflow smart contracts:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "197cccbd",
"metadata": {},
"outputs": [],
"source": [
"# Create a tool to read from a workflow\n",
"forecast_tool = toolkit.create_read_workflow_tool(\n",
" workflow_contract_address=\"0x58826c6dc9A608238d9d57a65bDd50EcaE27FE99\",\n",
" tool_name=\"ETH_Price_Forecast\",\n",
" tool_description=\"Reads latest forecast for ETH price from deployed workflow\",\n",
" output_formatter=lambda x: f\"Price change forecast: {format(float(x.numbers['regression_output'].item()), '.2%')}\",\n",
")\n",
"\n",
"# Add the tool to the toolkit\n",
"toolkit.add_tool(forecast_tool)"
]
},
{
"cell_type": "markdown",
"id": "5c5f2839-4020-424e-9fc9-07777eede442",
"metadata": {},
"source": [
"## Tools\n",
"\n",
"Use the built in `get_tools()` method to view a list of the available tools within the OpenGradient toolkit."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51a60dbe-9f2e-4e04-bb62-23968f17164a",
"metadata": {},
"outputs": [],
"source": [
"tools = toolkit.get_tools()\n",
"\n",
"# View tools\n",
"for tool in tools:\n",
" print(tool)"
]
},
{
"cell_type": "markdown",
"id": "dfe8aad4-8626-4330-98a9-7ea1ca5d2e0e",
"metadata": {},
"source": [
"## Use within an agent\n",
"Here's how to use your OpenGradient tools with a LangChain agent:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "310bf18e-6c9a-4072-b86e-47bc1fcca29d",
"metadata": {},
"outputs": [],
"source": [
"from langchain_openai import ChatOpenAI\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"# Initialize LLM\n",
"llm = ChatOpenAI(model=\"gpt-4o\")\n",
"\n",
"# Create tools from the toolkit\n",
"tools = toolkit.get_tools()\n",
"\n",
"# Create agent\n",
"agent_executor = create_react_agent(llm, tools)\n",
"\n",
"# Example query for the agent\n",
"example_query = \"What's the current volatility of ETH?\"\n",
"\n",
"# Execute the agent\n",
"events = agent_executor.stream(\n",
" {\"messages\": [(\"user\", example_query)]},\n",
" stream_mode=\"values\",\n",
")\n",
"for event in events:\n",
" event[\"messages\"][-1].pretty_print()"
]
},
{
"cell_type": "markdown",
"id": "2bb2b716",
"metadata": {},
"source": [
"Here's a sample output of everything put together:\n",
"\n",
"```\n",
"================================ Human Message =================================\n",
"\n",
"What's the current volatility of ETH?\n",
"================================== Ai Message ==================================\n",
"Tool Calls:\n",
" eth_volatility (chatcmpl-tool-d66ab9ee8f2c40e5a2634d90c7aeb17d)\n",
" Call ID: chatcmpl-tool-d66ab9ee8f2c40e5a2634d90c7aeb17d\n",
" Args:\n",
"================================= Tool Message =================================\n",
"Name: eth_volatility\n",
"\n",
"0.038%\n",
"================================== Ai Message ==================================\n",
"\n",
"The current volatility of the ETH/USDT trading pair is 0.038%.\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "268bc64a",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"See the [Github page](https://github.com/OpenGradient/og-langchain) for more detail."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -59,7 +59,10 @@ def check_header_order(path: Path) -> None:
if doc_dir not in INFO_BY_DIR:
# Skip if not a directory we care about
return
headers = _get_headers(doc_dir)
if "toolkit" in path.name:
headers = _get_headers("toolkits")
else:
headers = _get_headers(doc_dir)
issue_number = INFO_BY_DIR[doc_dir].get("issue_number", "nonexistent")
print(f"Checking {doc_dir} page {path}")

View File

@ -510,3 +510,6 @@ packages:
name_title: Valthera
path: .
repo: valthera/langchain-valthera
- name: langchain-opengradient
path: .
repo: OpenGradient/og-langchain