1、先搞定环境和依赖

python3 -m venv .venv
source .venv/bin/activate
pip install playwright
playwright install
pip install pytest-playwright
pip install beautifulsoup4
pip install lxml

这三一定要装:

pip install langchain_community
pip install langchain_core
pip install langchain

2、运行

python ollama_playwright.py

直接报错

安装:


pip install beautifulsoup4

3、代码

# python3 -m venv .venv
# source .venv/bin/activate
# pip install playwright
# playwright install
# pip install pytest-playwright
# pip install beautifulsoup4
# pip install lxml
# pip install langchain_community
# pip install langchain_core
# pip install langchain

# https://python.langchain.com/docs/integrations/toolkits/playwright

from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
from langchain_community.chat_models import ChatOllama
from langchain.agents import AgentType, initialize_agent

#同步加载游览器的工具
from langchain_community.tools.playwright.utils import (
    create_sync_playwright_browser,  # A synchronous browser is available, though it isn't compatible with jupyter.\n",      },
)



ollama_llm = ChatOllama(model="mistral:latest")

sync_browser = create_sync_playwright_browser()
toolkit = PlayWrightBrowserToolkit.from_browser(sync_browser=sync_browser)
tools = toolkit.get_tools()

agent_chain = initialize_agent(
    tools,
    ollama_llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)
 
result = agent_chain.invoke("What are the headers on langchain.com?")
print(result)

然后解决一下总是说过时的那个问题



pip install langchainhub
# python3 -m venv .venv
# source .venv/bin/activate
# pip install playwright
# playwright install
# pip install pytest-playwright
# pip install beautifulsoup4
# pip install lxml
# pip install langchain_community
# pip install langchain_core
# pip install langchain

# https://python.langchain.com/docs/integrations/toolkits/playwright
from langchain import hub
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
from langchain_community.chat_models import ChatOllama
from langchain.agents import AgentExecutor, create_react_agent

# /home/lemonhall/langChain-Ollama/.venv/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:117: 
# LangChainDeprecationWarning: 
# The function `initialize_agent` was deprecated in LangChain 0.1.0 
# and will be removed in 0.2.0. Use Use new agent constructor methods like create_react_agent, 
# create_json_agent, create_structured_chat_agent, 
# etc. instead.

#同步加载游览器的工具
from langchain_community.tools.playwright.utils import (
    create_sync_playwright_browser,  # A synchronous browser is available, though it isn't compatible with jupyter.\n",      },
)


ollama_llm = ChatOllama(model="mistral:latest")

sync_browser = create_sync_playwright_browser()
toolkit = PlayWrightBrowserToolkit.from_browser(sync_browser=sync_browser)
tools = toolkit.get_tools()

prompt = hub.pull("hwchase17/react")
agent = create_react_agent(ollama_llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
 
result = agent_executor.invoke({"input": "What are the headers on langchain.com?"})
print(result)

然后就是一阵报错

我是真的服了,你的API要废弃了,然后你的所有的教程却又都是老的


暂时先这样吧

输出总是很不稳定就是了