twitter-langchain


Nametwitter-langchain JSON
Version 0.0.11 PyPI version JSON
download
home_pageNone
SummaryTwitter Langchain Toolkit
upload_time2025-01-24 23:00:04
maintainerNone
docs_urlNone
authorJohn Peterson
requires_python<4.0,>=3.10
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Twitter (X) Langchain Toolkit
Twitter integration with Langchain to enable agentic workflows using the core primitives defined in `cdp-agentkit-core`.

This toolkit contains tools that enable an LLM agent to interact with [Twitter](https://developer.x.com/en/docs/x-api). The toolkit provides a wrapper around the Twitter (X) API, allowing agents to perform social operations like posting text.

## Setup

### Prerequisites

- [OpenAI API Key](https://platform.openai.com/api-keys)
- [Twitter (X) App Developer Keys](https://developer.x.com/en/portal/dashboard)
- Python 3.10 or higher

### Installation

```bash
pip install twitter-langchain
```

### Environment Setup

Set the following environment variables:

```bash
export OPENAI_API_KEY=<your-openai-api-key>
export TWITTER_API_KEY=<your-api-key>
export TWITTER_API_SECRET=<your-api-secret>
export TWITTER_ACCESS_TOKEN=<your-access-token>
export TWITTER_ACCESS_TOKEN_SECRET=<your-access-token-secret>
export TWITTER_BEARER_TOKEN=<your-bearer-token>
```

## Usage

### Basic Setup

```python
from twitter_langchain import (
    TwitterApiWrapper,
    TwitterToolkit
)

# Initialize TwitterApiwrapper
twitter_api_wrapper = TwitterApiWrapper()

# Create TwitterToolkit from the api wrapper
twitter_toolkit = TwitterToolkit.from_twitter_api_wrapper(twitter_api_wrapper)
```

View available tools:
```python
tools = twitter_toolkit.get_tools()
for tool in tools:
    print(tool.name)
```

### Available Tools

The toolkit provides the following tools:

1. **account_details** - Get the authenticated account details
2. **account_mentions** - Get mentions for the account
3. **post_tweet** - Post a tweet to the account
3. **post_tweet_reply** - Post a reply to a tweet on Twitter

### Using with an Agent

```python
import uuid

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from langgraph.prebuilt import create_react_agent

llm = ChatOpenAI(model="gpt-4o-mini")

# Create agent
agent_executor = create_react_agent(llm, tools)

# Example - post tweet
events = agent_executor.stream(
    {
        "messages": [
            HumanMessage(content=f"Please post 'hello, world! {uuid.uuid4().hex}' to twitter"),
        ],
    },
    stream_mode="values",
)

for event in events:
    event["messages"][-1].pretty_print()
```

Expected output:
```
================================ Human Message =================================
Please post 'hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa' to twitter
================================== Ai Message ==================================
Tool Calls:
    post_tweet (call_xVx4BMCSlCmCcbEQG1yyebbq)
    Call ID: call_xVx4BMCSlCmCcbEQG1yyebbq
    Args:
        tweet: hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa
================================= Tool Message =================================
Name: post_tweet
Successfully posted!
================================== Ai Message ==================================
The message "hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa" has been successfully posted to Twitter!
```

## Examples

Check out [twitter-langchain/examples](../examples) for inspiration and help getting started!
- [Chatbot Python](../examples/chatbot-python/README.md): Simple example of a Python Chatbot that can interact on Twitter (X), using OpenAI.

## Contributing

See [CONTRIBUTING.md](../../CONTRIBUTING.md) for detailed setup instructions and contribution guidelines.


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "twitter-langchain",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "John Peterson",
    "author_email": "john.peterson@coinbase.com",
    "download_url": "https://files.pythonhosted.org/packages/88/1d/8efcd67a20861c2f3860f1bd6a3f97b80f1701e6b501d4f1b6553904475f/twitter_langchain-0.0.11.tar.gz",
    "platform": null,
    "description": "# Twitter (X) Langchain Toolkit\nTwitter integration with Langchain to enable agentic workflows using the core primitives defined in `cdp-agentkit-core`.\n\nThis toolkit contains tools that enable an LLM agent to interact with [Twitter](https://developer.x.com/en/docs/x-api). The toolkit provides a wrapper around the Twitter (X) API, allowing agents to perform social operations like posting text.\n\n## Setup\n\n### Prerequisites\n\n- [OpenAI API Key](https://platform.openai.com/api-keys)\n- [Twitter (X) App Developer Keys](https://developer.x.com/en/portal/dashboard)\n- Python 3.10 or higher\n\n### Installation\n\n```bash\npip install twitter-langchain\n```\n\n### Environment Setup\n\nSet the following environment variables:\n\n```bash\nexport OPENAI_API_KEY=<your-openai-api-key>\nexport TWITTER_API_KEY=<your-api-key>\nexport TWITTER_API_SECRET=<your-api-secret>\nexport TWITTER_ACCESS_TOKEN=<your-access-token>\nexport TWITTER_ACCESS_TOKEN_SECRET=<your-access-token-secret>\nexport TWITTER_BEARER_TOKEN=<your-bearer-token>\n```\n\n## Usage\n\n### Basic Setup\n\n```python\nfrom twitter_langchain import (\n    TwitterApiWrapper,\n    TwitterToolkit\n)\n\n# Initialize TwitterApiwrapper\ntwitter_api_wrapper = TwitterApiWrapper()\n\n# Create TwitterToolkit from the api wrapper\ntwitter_toolkit = TwitterToolkit.from_twitter_api_wrapper(twitter_api_wrapper)\n```\n\nView available tools:\n```python\ntools = twitter_toolkit.get_tools()\nfor tool in tools:\n    print(tool.name)\n```\n\n### Available Tools\n\nThe toolkit provides the following tools:\n\n1. **account_details** - Get the authenticated account details\n2. **account_mentions** - Get mentions for the account\n3. **post_tweet** - Post a tweet to the account\n3. **post_tweet_reply** - Post a reply to a tweet on Twitter\n\n### Using with an Agent\n\n```python\nimport uuid\n\nfrom langchain_openai import ChatOpenAI\nfrom langchain_core.messages import HumanMessage\nfrom langgraph.prebuilt import create_react_agent\n\nllm = ChatOpenAI(model=\"gpt-4o-mini\")\n\n# Create agent\nagent_executor = create_react_agent(llm, tools)\n\n# Example - post tweet\nevents = agent_executor.stream(\n    {\n        \"messages\": [\n            HumanMessage(content=f\"Please post 'hello, world! {uuid.uuid4().hex}' to twitter\"),\n        ],\n    },\n    stream_mode=\"values\",\n)\n\nfor event in events:\n    event[\"messages\"][-1].pretty_print()\n```\n\nExpected output:\n```\n================================ Human Message =================================\nPlease post 'hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa' to twitter\n================================== Ai Message ==================================\nTool Calls:\n    post_tweet (call_xVx4BMCSlCmCcbEQG1yyebbq)\n    Call ID: call_xVx4BMCSlCmCcbEQG1yyebbq\n    Args:\n        tweet: hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa\n================================= Tool Message =================================\nName: post_tweet\nSuccessfully posted!\n================================== Ai Message ==================================\nThe message \"hello, world! c4b8e3744c2e4345be9e0622b4c0a8aa\" has been successfully posted to Twitter!\n```\n\n## Examples\n\nCheck out [twitter-langchain/examples](../examples) for inspiration and help getting started!\n- [Chatbot Python](../examples/chatbot-python/README.md): Simple example of a Python Chatbot that can interact on Twitter (X), using OpenAI.\n\n## Contributing\n\nSee [CONTRIBUTING.md](../../CONTRIBUTING.md) for detailed setup instructions and contribution guidelines.\n\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Twitter Langchain Toolkit",
    "version": "0.0.11",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "7ad116c0d5d07718a96954588cfc9cf6734472eb55c212b5199b6ad84b95ad8a",
                "md5": "0ac152b23ede14bbb1aff2c8ac35494c",
                "sha256": "42657011c4752066862bf4de2bd36e69913654ea02bab6d9f995ce40793db461"
            },
            "downloads": -1,
            "filename": "twitter_langchain-0.0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0ac152b23ede14bbb1aff2c8ac35494c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 6252,
            "upload_time": "2025-01-24T23:00:03",
            "upload_time_iso_8601": "2025-01-24T23:00:03.453473Z",
            "url": "https://files.pythonhosted.org/packages/7a/d1/16c0d5d07718a96954588cfc9cf6734472eb55c212b5199b6ad84b95ad8a/twitter_langchain-0.0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "881d8efcd67a20861c2f3860f1bd6a3f97b80f1701e6b501d4f1b6553904475f",
                "md5": "7dc9b66475113d1fd9cabdd000b1780f",
                "sha256": "34445bbc0bcd2684197b917aff8e5e5f3db164d38e754fb67f6dc2359ca194c3"
            },
            "downloads": -1,
            "filename": "twitter_langchain-0.0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "7dc9b66475113d1fd9cabdd000b1780f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 4782,
            "upload_time": "2025-01-24T23:00:04",
            "upload_time_iso_8601": "2025-01-24T23:00:04.401541Z",
            "url": "https://files.pythonhosted.org/packages/88/1d/8efcd67a20861c2f3860f1bd6a3f97b80f1701e6b501d4f1b6553904475f/twitter_langchain-0.0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-24 23:00:04",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "twitter-langchain"
}
        
Elapsed time: 1.39263s