Skip to main content

LangChain tools for easycome social media data APIs

Project description

EasyCome LangChain SDK

easycome 社媒数据 API 无缝接入 LangChain 智能体。

安装

pip install easycome-langchain

快速开始

import os
os.environ["EASYCOME_BASE_URL"] = "https://www.easycome.com.cn"
os.environ["EASYCOME_API_KEY"] = "dg_xxxxxxxxxxxx"
os.environ["EASYCOME_COOKIE_XHS"] = "a1=xxx; web_session=yyy"

from easycome_langchain import get_tools
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

tools = get_tools()  # 自动发现所有 API
llm = ChatOpenAI(model="gpt-4o")
agent = create_react_agent(llm, tools)

result = agent.invoke({
    "messages": [{"role": "user", "content": "搜索小红书关于"成都美食"的笔记,只看前3条"}]
})

环境变量

变量 必填 说明
EASYCOME_BASE_URL API 基础 URL
EASYCOME_API_KEY API 密钥 (dg_ 开头)
EASYCOME_COOKIE_XHS 按需 小红书平台 Cookie
EASYCOME_COOKIE_DOUYIN 按需 抖音平台 Cookie
EASYCOME_COOKIE_BIlibili 按需 B站平台 Cookie
EASYCOME_COOKIE_WEIBO 按需 微博平台 Cookie
EASYCOME_COOKIE_ZHIHU 按需 知乎平台 Cookie
EASYCOME_COOKIES 按需 JSON 格式: {"xhs":"...", "bilibili":"..."}

API

get_tools(...)

主入口,返回 list[StructuredTool] 供 LangChain Agent 使用。

from easycome_langchain import get_tools

# 获取所有工具
tools = get_tools()

# 筛选特定平台
xhs_tools = get_tools(platform="xhs")

# 筛选特定分类
stock_tools = get_tools(category="stock")

# 只获取指定工具
tools = get_tools(slugs=["xhs_search", "bilibili_video_detail"])

# 显式传参(替代环境变量)
tools = get_tools(
    base_url="https://www.easycome.com.cn",
    api_key="dg_xxx",
    cookies={"xhs": "a1=xxx; web_session=yyy"},
)

EasyComeClient

底层 REST 客户端,也可以独立使用。

from easycome_langchain import EasyComeClient

client = EasyComeClient(
    base_url="https://www.easycome.com.cn",
    api_key="dg_xxx",
    cookies={"xhs": "a1=xxx"},
)

# 同步调用
data = client.call_sync("xhs_search", {"keyword": "美食", "page": "1"})

# 异步调用
data = await client.call("xhs_search", {"keyword": "美食"})

fetch_catalog()

获取 API 目录(不依赖 LangChain)。

from easycome_langchain import fetch_catalog

entries = fetch_catalog("https://www.easycome.com.cn")
for e in entries:
    print(f"{e.slug}: {e.name} [{e.platform}]")

Cookie 管理

SDK 自动处理 Cookie 注入——Agent 不需要知道 Cookie 参数的存在。

  • Cookie 按平台配置(不是按端点)
  • 创建 Tool 时自动匹配 platform → cookie
  • 端点需要 cookie 但未配置时,调用会抛出 CookieRequiredError

多平台 Cookie 示例:

os.environ["EASYCOME_COOKIE_XHS"] = "a1=xxx; web_session=yyy"
os.environ["EASYCOME_COOKIE_BIlibili"] = "SESSDATA=xxx; bili_jct=yyy"
os.environ["EASYCOME_COOKIE_WEIBO"] = "SUB=xxx"

tools = get_tools()  # 自动匹配各平台的 Cookie

异常处理

from easycome_langchain import (
    AuthError,         # API Key 无效
    QuotaError,        # 余额不足
    RateLimitError,    # 请求过于频繁
    UpstreamError,     # 上游平台返回错误
    NotFoundError,     # 端点不存在
    CookieRequiredError,  # 缺少必需的 Cookie
)

结合 LangGraph 使用

from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
from easycome_langchain import get_tools

tools = get_tools(platform="xhs")
agent = create_react_agent(ChatOpenAI(model="gpt-4o"), tools)

async for event in agent.astream_events(
    {"messages": [{"role": "user", "content": "帮我分析小红书'穿搭'话题的热门笔记"}]},
    version="v2",
):
    # 处理流式事件
    pass

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easycome_langchain-0.1.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

easycome_langchain-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file easycome_langchain-0.1.0.tar.gz.

File metadata

  • Download URL: easycome_langchain-0.1.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for easycome_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 936e979143d1d2e3753dd23fc2930a75c0b5749379e00f2aa19f3831a172bd72
MD5 2fe1952822f029aec880a90817fd13d8
BLAKE2b-256 201c835de82400b23f0e85a621ffb6d9f06be6bae6e80e6b88889b0974e77672

See more details on using hashes here.

File details

Details for the file easycome_langchain-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for easycome_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36da124df4d125c2899ae140513ee76dc5a9786e4df407b207382cedebd449b9
MD5 1a073b0eb7bb408f6377e56a979adc61
BLAKE2b-256 45c6bd9d8d4609b92d0deab933bb15375059f9d801899ea3b5ef980b5756296f

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