Reddit 搜尋
在本筆記本中,我們將學習 Reddit 搜尋工具的工作原理。
首先,請確保您已使用以下命令安裝 praw
%pip install --upgrade --quiet praw
然後您需要設定適當的 API 金鑰和環境變數。您需要建立一個 Reddit 使用者帳戶並取得憑證。因此,請前往 https://www.reddit.com 並註冊,以建立 Reddit 使用者帳戶。
然後前往 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,
)
)
然後您可以設定您的查詢,例如,您想要查詢哪個 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 上的開放原始碼專案做出貢獻的技能。
影片連結:https://youtu.be/IdT1BhrSfdo?si=mV7xVpiyuhlD8Zrw
歡迎提供您的意見反應 貼文 URL:https://www.reddit.com/r/Python/comments/1823wr7/setup_github_copilot_in_visual_studio_code/ 貼文類別:不適用。分數:0
貼文標題:'使用 pygame 和 PySide6 製作的中國跳棋遊戲,支援自訂機器人' 使用者:HenryChess Subreddit:r/Python:正文:GitHub 連結:https://github.com/henrychess/pygame-chinese-checkers
我不確定這是否算作初學者或中級。 我認為我仍然處於初學者區域,所以我將其標記為初學者。
這是一個 2 到 3 名玩家的中國跳棋(又名 Sternhalma)遊戲。 我編寫的機器人很容易擊敗,因為它們主要用於除錯程式碼的遊戲邏輯部分。 但是,您可以編寫自己的自訂機器人。 github 頁面上有一個指南。 貼文 URL:https://www.reddit.com/r/Python/comments/181xq0u/a_chinese_checkers_game_made_with_pygame_and/ 貼文類別:不適用。分數:1
使用具有代理鏈的工具
Reddit 搜尋功能也作為多輸入工具提供。 在此範例中,我們改編docs 中的現有程式碼,並使用 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?")