跳到主要内容
在本笔记本中,我们将了解 Reddit 搜索工具的工作原理。首先,请确保您已通过以下命令安装了 praw:
pip install -qU  praw
然后,您需要设置正确的 API 密钥和环境变量。您需要创建一个 Reddit 用户帐户并获取凭据。因此,请访问 www.reddit.com 并注册以创建 Reddit 用户帐户。然后,通过访问 www.reddit.com/prefs/apps 并创建一个应用程序来获取您的凭据。您应该从创建应用程序中获得 client_id 和 secret。现在,您可以将这些字符串粘贴到 client_id 和 client_secret 变量中。注意:您可以为 user_agent 放置任何字符串
client_id = ""
client_secret = ""
user_agent = ""
from langchain_community.tools.reddit_search.tool import RedditSearchRun
from langchain_community.utilities.reddit_search import RedditSearchAPIWrapper

search = RedditSearchRun(
    api_wrapper=RedditSearchAPIWrapper(
        reddit_client_id=client_id,
        reddit_client_secret=client_secret,
        reddit_user_agent=user_agent,
    )
)
然后,您可以设置您的查询,例如,您要查询哪个 subreddit,要返回多少帖子,您希望结果如何排序等。
from langchain_community.tools.reddit_search.tool import RedditSearchSchema

search_params = RedditSearchSchema(
    query="beginner", sort="new", time_filter="week", subreddit="python", limit="2"
)
最后运行搜索并获取结果
result = search.run(tool_input=search_params.dict())
print(result)
这是一个打印结果的示例。注意:您可能会根据 subreddit 中最新的帖子获得不同的输出,但格式应该相似。
搜索 r/python 找到 2 个帖子:帖子标题:“在 Visual Studio Code 中设置 Github Copilot” 用户:Feisty-Recording-715 Subreddit:r/Python:正文:🛠️ 本教程非常适合希望加强对版本控制理解的初学者,或寻求在 Visual Studio Code 中设置 GitHub 快速参考的经验丰富的开发人员。 🎓 通过观看本视频,您将掌握自信地管理代码库、与他人协作以及为 GitHub 上的开源项目做出贡献的技能。 视频链接:youtu.be/IdT1BhrSfdo?si=mV7xVpiyuhlD8Zrw 欢迎您的反馈 帖子 URL:www.reddit.com/r/Python/comments/1823wr7/setup_github_copilot_in_visual_studio_code/ 帖子类别:N/A。得分:0 帖子标题:“一个用 pygame 和 PySide6 制作的中国跳棋游戏,支持自定义机器人” 用户:HenryChess Subreddit:r/Python:正文:GitHub 链接:github.com/henrychess/pygame-chinese-checkers 我不确定这是否属于初级或中级。我认为我仍然处于初级阶段,所以我将其标记为初级。 这是一个 2 到 3 名玩家的中国跳棋(又称六角跳棋)游戏。我编写的机器人很容易击败,因为它们主要用于调试代码的游戏逻辑部分。但是,您可以编写自己的自定义机器人。GitHub 页面上有一个指南。帖子 URL:www.reddit.com/r/Python/comments/181xq0u/a_chinese_checkers_game_made_with_pygame_and/ 帖子类别:N/A。得分:1

将工具与代理链结合使用

Reddit 搜索功能也以多输入工具的形式提供。在此示例中,我们改编了 文档中的现有代码,并使用 ChatOpenAI 创建了一个带有内存的代理链。这个代理链能够从 Reddit 中提取信息并使用这些帖子响应后续输入。 要运行示例,请添加您的 Reddit API 访问信息,并从 OpenAI API 获取 OpenAI 密钥。
# Adapted code from /docs/modules/agents/how_to/sharedmemory_for_tools

from langchain.agents import AgentExecutor, StructuredChatAgent
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
from langchain_community.tools.reddit_search.tool import RedditSearchRun
from langchain_community.utilities.reddit_search import RedditSearchAPIWrapper
from langchain_core.prompts import PromptTemplate
from langchain.tools import Tool
from langchain_openai import ChatOpenAI

# Provide keys for Reddit
client_id = ""
client_secret = ""
user_agent = ""
# Provide key for OpenAI
openai_api_key = ""

template = """This is a conversation between a human and a bot:

{chat_history}

Write a summary of the conversation for {input}:
"""

prompt = PromptTemplate(input_variables=["input", "chat_history"], template=template)
memory = ConversationBufferMemory(memory_key="chat_history")

prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
suffix = """Begin!"

{chat_history}
Question: {input}
{agent_scratchpad}"""

tools = [
    RedditSearchRun(
        api_wrapper=RedditSearchAPIWrapper(
            reddit_client_id=client_id,
            reddit_client_secret=client_secret,
            reddit_user_agent=user_agent,
        )
    )
]

prompt = StructuredChatAgent.create_prompt(
    prefix=prefix,
    tools=tools,
    suffix=suffix,
    input_variables=["input", "chat_history", "agent_scratchpad"],
)

llm = ChatOpenAI(temperature=0, openai_api_key=openai_api_key)

llm_chain = LLMChain(llm=llm, prompt=prompt)
agent = StructuredChatAgent(llm_chain=llm_chain, verbose=True, tools=tools)
agent_chain = AgentExecutor.from_agent_and_tools(
    agent=agent, verbose=True, memory=memory, tools=tools
)

# Answering the first prompt requires usage of the Reddit search tool.
agent_chain.run(input="What is the newest post on r/langchain for the week?")
# Answering the subsequent prompt uses memory.
agent_chain.run(input="Who is the author of the post?")

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.