Skip to main content
Open In ColabOpen on GitHub

Reddit 搜索

在这个笔记本中,我们学习一下Reddit搜索工具的工作原理。
首先确保您已使用以下命令安装了 praw:

%pip install --upgrade --quiet  praw

然后您需要设置适当的API密钥和环境变量。您需要创建一个Reddit用户账户并获取凭证。因此,请前往https://www.reddit.com并完成注册。
然后前往https://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,
)
)

您可以通过设置查询来举例说明,例如想要查询哪个子版块、返回多少个帖子、结果应该如何排序等。

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)

这里是一个打印结果的示例。
Note: 你可能会根据 Reddit 子版块最新的帖子获得不同的输出结果,但格式应该相似。

搜索 r/python 发现 2 条帖子: 帖子标题: '在 Visual Studio Code 中设置 Github Copilot' 用户: Feisty-Recording-715 子版块: r/Python: 文本内容: 🛠️ 本教程非常适合希望增强对版本控制理解的初学者,也适合寻求快速参考以在 Visual Studio Code 中设置 GitHub 的经验丰富的开发人员。

🎓 到了这个视频的结尾,你将掌握管理代码库、与其他开发者协作以及在GitHub上贡献开源项目所需的技能。

Video link: https://youtu.be/IdT1BhrSfdo?si=mV7xVpiyuhlD8Zrw

您的反馈是welcome 发布链接: https://www.reddit.com/r/Python/comments/1823wr7/setup_github_copilot_in_visual_studio_code/ 类别: N/A. 分数: 0

Post Title: '一个使用pygame和PySide6编写的中国象棋游戏,支持自定义机器人'

我不确定这应该算作初学者还是中级。我觉得我还在初学者的阶段,所以我标记为初学者。

这是一款由2至3名玩家玩的国际跳棋(又称 Sternhalma)游戏。我编写的机器人比较容易击败,主要是为了调试代码中的游戏逻辑部分。不过,你也可以编写自己的自定义机器人。有关指南可以在github页面上找到。 发布链接: https://www.reddit.com/r/Python/comments/181xq0u/a_chinese_checkers_game_made_with_pygame_and/ 帖子分类: 无。 分数: 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_core.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?")