langchain icon indicating copy to clipboard operation
langchain copied to clipboard

DOC: Documented example doesn't work

Open gwc4github opened this issue 1 year ago • 3 comments

Issue with current documentation:

This is for the documentation page: https://python.langchain.com/en/latest/modules/agents/agents/examples/structured_chat.html This does not work in Jupyter or if you download it to a .py file and run it. You do get 2 different errors though. When you run this in Jupyter you get this error:

---------------------------------------------------------------------------
ConfigError                               Traceback (most recent call last)
Cell In[29], line 3
      1 async_browser = create_async_playwright_browser()
      2 # sync_browser = None # create_sync_playwright_browser()
----> 3 browser_toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)
      4 tools = browser_toolkit.get_tools()

File ~/Google Drive/PycharmProjectsLocal/pythonProject2/venv/lib/python3.8/site-packages/langchain/agents/agent_toolkits/playwright/toolkit.py:83, in PlayWrightBrowserToolkit.from_browser(cls, sync_browser, async_browser)
     81 # This is to raise a better error than the forward ref ones Pydantic would have
     82 lazy_import_playwright_browsers()
---> 83 return cls(sync_browser=sync_browser, async_browser=async_browser)

File ~/Google Drive/PycharmProjectsLocal/pythonProject2/venv/lib/python3.8/site-packages/pydantic/main.py:339, in pydantic.main.BaseModel.__init__()

File ~/Google Drive/PycharmProjectsLocal/pythonProject2/venv/lib/python3.8/site-packages/pydantic/main.py:1076, in pydantic.main.validate_model()

File ~/Google Drive/PycharmProjectsLocal/pythonProject2/venv/lib/python3.8/site-packages/pydantic/fields.py:860, in pydantic.fields.ModelField.validate()

ConfigError: field "sync_browser" not yet prepared so type is still a ForwardRef, you might need to call PlayWrightBrowserToolkit.update_forward_refs().

When you run it as a .py file you get this error:

Connected to pydev debugger (build 231.9011.38)
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1496, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/greggwcasey/Google Drive/PycharmProjectsLocal/pythonProject2/structured_chat.py", line 76
    response = await agent_chain.arun(input="Hi I'm Erica.")
               ^
SyntaxError: 'await' outside function
python-BaseException

Idea or request for content:

I would like to have a working example that I could download and run on my Jupyter server or Colab and it work as-is.

gwc4github avatar Jun 09 '23 18:06 gwc4github

Both errors come from your environment and not the code in itself.

The first is related to your playwright configuration.

The second is related to the iPython version since inline await was a feature added in version 7.

Xmaster6y avatar Jun 12 '23 09:06 Xmaster6y

For "await" - it has to be done within an outer async function async def main(): ....

vowelparrot avatar Jun 14 '23 13:06 vowelparrot

Not necessarily, in notebooks (recent enough) inline await is possible.

Xmaster6y avatar Jun 14 '23 14:06 Xmaster6y

Right! In notebooks there is already an event loop running, but in the exported python script that breaks down, so you'd have to either wrap in an async call or function or use a sync browser

vowelparrot avatar Jun 14 '23 19:06 vowelparrot

Ok. Thanks. I'll try to make time to create a working example and create a PR.

gwc4github avatar Jun 14 '23 19:06 gwc4github

To be clear, it didn't work for me in or out of a notebook.

gwc4github avatar Jun 14 '23 19:06 gwc4github

What are your python and playwright and pydantic versions

vowelparrot avatar Jun 14 '23 20:06 vowelparrot

Ok. Thanks. I'll try to make time to create a working example and create a PR.

@gwc4github are you able to solve this issue?

kakkar87 avatar Jul 04 '23 06:07 kakkar87

Ok. Thanks. I'll try to make time to create a working example and create a PR.

@gwc4github are you able to solve this issue?

@kakkar87 No I have not. I am currently looking for another way to pull data from HTML since I think PlayWright is not complete. If you would like to work together on getting something working, DM me. I want to get a tool working that will allow an Agent to get data from a provided url and use it as input to future tasks. I am testing something now but need to fix an unrelated problem first.

gwc4github avatar Jul 04 '23 15:07 gwc4github

@gwc4github You can try to import both create_async_playwright_browser and create_sync_playwright_browser For example:

 from langchain.tools.playwright.utils import (
    create_async_playwright_browser,
    create_sync_playwright_browser, # A synchronous browser is available, though it isn't compatible with jupyter.
)

lumenintellects avatar Aug 16 '23 21:08 lumenintellects

I've hit the same problem, in Colab, following the structured tool tutorial. Updating both the sync/async playwright didn't work either.

The original code:

from langchain.agents.agent_toolkits import PlayWrightBrowserToolkit
from langchain.tools.playwright.utils import (
    create_async_playwright_browser,
    create_sync_playwright_browser, # A synchronous browser is available, though it isn't compatible with jupyter.
)

# This import is required only for jupyter notebooks, since they have their own eventloop
import nest_asyncio
nest_asyncio.apply()
async_browser = create_async_playwright_browser()
browser_toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)
tools = browser_toolkit.get_tools()

produces

ConfigError: field "sync_browser" not yet prepared so type is still a ForwardRef, you might need to call PlayWrightBrowserToolkit.update_forward_refs().

If I add that line in,

from langchain.agents.agent_toolkits import PlayWrightBrowserToolkit
from langchain.tools.playwright.utils import (
    create_async_playwright_browser,
    create_sync_playwright_browser, # A synchronous browser is available, though it isn't compatible with jupyter.
)

# This import is required only for jupyter notebooks, since they have their own eventloop
import nest_asyncio
nest_asyncio.apply()
async_browser = create_async_playwright_browser()
PlayWrightBrowserToolkit.update_forward_refs()
browser_toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)
tools = browser_toolkit.get_tools()

It errors on the update_forward_refs() line with

NameError: name 'SyncBrowser' is not defined

Not sure what to do here. Anything else I could try?

Full output
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

[<ipython-input-62-7ea7004cf8aa>](https://localhost:8080/#) in <cell line: 11>()
      9 nest_asyncio.apply()
     10 async_browser = create_async_playwright_browser()
---> 11 PlayWrightBrowserToolkit.update_forward_refs()
     12 browser_toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)
     13 tools = browser_toolkit.get_tools()

9 frames

[/usr/local/lib/python3.10/dist-packages/pydantic/v1/main.py](https://localhost:8080/#) in update_forward_refs(cls, **localns)
    813         Try to update ForwardRefs on fields based on this Model, globalns and localns.
    814         """
--> 815         update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns)
    816 
    817     def __iter__(self) -> 'TupleGenerator':

[/usr/local/lib/python3.10/dist-packages/pydantic/v1/typing.py](https://localhost:8080/#) in update_model_forward_refs(model, fields, json_encoders, localns, exc_to_suppress)
    552     for f in fields:
    553         try:
--> 554             update_field_forward_refs(f, globalns=globalns, localns=localns)
    555         except exc_to_suppress:
    556             pass

[/usr/local/lib/python3.10/dist-packages/pydantic/v1/typing.py](https://localhost:8080/#) in update_field_forward_refs(field, globalns, localns)
    518     if field.type_.__class__ == ForwardRef:
    519         prepare = True
--> 520         field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
    521     if field.outer_type_.__class__ == ForwardRef:
    522         prepare = True

[/usr/local/lib/python3.10/dist-packages/pydantic/v1/typing.py](https://localhost:8080/#) in evaluate_forwardref(type_, globalns, localns)
     64         # Even though it is the right signature for python 3.9, mypy complains with
     65         # `error: Too many arguments for "_evaluate" of "ForwardRef"` hence the cast...
---> 66         return cast(Any, type_)._evaluate(globalns, localns, set())
     67 
     68 

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in _evaluate(self, globalns, localns, recursive_guard)
    697                 allow_special_forms=self.__forward_is_class__,
    698             )
--> 699             self.__forward_value__ = _eval_type(
    700                 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
    701             )

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in _eval_type(t, globalns, localns, recursive_guard)
    327         return t._evaluate(globalns, localns, recursive_guard)
    328     if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329         ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330         if ev_args == t.__args__:
    331             return t

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in <genexpr>(.0)
    327         return t._evaluate(globalns, localns, recursive_guard)
    328     if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329         ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330         if ev_args == t.__args__:
    331             return t

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in _eval_type(t, globalns, localns, recursive_guard)
    325     """
    326     if isinstance(t, ForwardRef):
--> 327         return t._evaluate(globalns, localns, recursive_guard)
    328     if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
    329         ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in _evaluate(self, globalns, localns, recursive_guard)
    692                 )
    693             type_ = _type_check(
--> 694                 eval(self.__forward_code__, globalns, localns),
    695                 "Forward references must evaluate to types.",
    696                 is_argument=self.__forward_is_argument__,

/usr/local/lib/python3.10/dist-packages/langchain/agents/agent_toolkits/playwright/toolkit.py in <module>

NameError: name 'SyncBrowser' is not defined---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

[<ipython-input-62-7ea7004cf8aa>](https://localhost:8080/#) in <cell line: 11>()
      9 nest_asyncio.apply()
     10 async_browser = create_async_playwright_browser()
---> 11 PlayWrightBrowserToolkit.update_forward_refs()
     12 browser_toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)
     13 tools = browser_toolkit.get_tools()

9 frames

[/usr/local/lib/python3.10/dist-packages/pydantic/v1/main.py](https://localhost:8080/#) in update_forward_refs(cls, **localns)
    813         Try to update ForwardRefs on fields based on this Model, globalns and localns.
    814         """
--> 815         update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns)
    816 
    817     def __iter__(self) -> 'TupleGenerator':

[/usr/local/lib/python3.10/dist-packages/pydantic/v1/typing.py](https://localhost:8080/#) in update_model_forward_refs(model, fields, json_encoders, localns, exc_to_suppress)
    552     for f in fields:
    553         try:
--> 554             update_field_forward_refs(f, globalns=globalns, localns=localns)
    555         except exc_to_suppress:
    556             pass

[/usr/local/lib/python3.10/dist-packages/pydantic/v1/typing.py](https://localhost:8080/#) in update_field_forward_refs(field, globalns, localns)
    518     if field.type_.__class__ == ForwardRef:
    519         prepare = True
--> 520         field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
    521     if field.outer_type_.__class__ == ForwardRef:
    522         prepare = True

[/usr/local/lib/python3.10/dist-packages/pydantic/v1/typing.py](https://localhost:8080/#) in evaluate_forwardref(type_, globalns, localns)
     64         # Even though it is the right signature for python 3.9, mypy complains with
     65         # `error: Too many arguments for "_evaluate" of "ForwardRef"` hence the cast...
---> 66         return cast(Any, type_)._evaluate(globalns, localns, set())
     67 
     68 

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in _evaluate(self, globalns, localns, recursive_guard)
    697                 allow_special_forms=self.__forward_is_class__,
    698             )
--> 699             self.__forward_value__ = _eval_type(
    700                 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
    701             )

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in _eval_type(t, globalns, localns, recursive_guard)
    327         return t._evaluate(globalns, localns, recursive_guard)
    328     if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329         ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330         if ev_args == t.__args__:
    331             return t

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in <genexpr>(.0)
    327         return t._evaluate(globalns, localns, recursive_guard)
    328     if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
--> 329         ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
    330         if ev_args == t.__args__:
    331             return t

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in _eval_type(t, globalns, localns, recursive_guard)
    325     """
    326     if isinstance(t, ForwardRef):
--> 327         return t._evaluate(globalns, localns, recursive_guard)
    328     if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
    329         ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)

[/usr/lib/python3.10/typing.py](https://localhost:8080/#) in _evaluate(self, globalns, localns, recursive_guard)
    692                 )
    693             type_ = _type_check(
--> 694                 eval(self.__forward_code__, globalns, localns),
    695                 "Forward references must evaluate to types.",
    696                 is_argument=self.__forward_is_argument__,

/usr/local/lib/python3.10/dist-packages/langchain/agents/agent_toolkits/playwright/toolkit.py in <module>

NameError: name 'SyncBrowser' is not defined

mendhak avatar Aug 21 '23 16:08 mendhak

Hello folks, I am getting the error while using PlayWrightBrowserToolkit in Google Colab notebook.

##Code:

async_browser = create_async_playwright_browser()

async_browser = create_async_playwright_browser() PlayWrightBrowserToolkit.update_forward_refs() toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser) tools = toolkit.get_tools() tools

##Error NameError Traceback (most recent call last) in <cell line: 3>() 1 # async_browser = create_async_playwright_browser() 2 async_browser = create_async_playwright_browser() ----> 3 PlayWrightBrowserToolkit.update_forward_refs() 4 toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser) 5 tools = toolkit.get_tools()

9 frames /usr/local/lib/python3.10/dist-packages/langchain/agents/agent_toolkits/playwright/toolkit.py in

NameError: name 'SyncBrowser' is not defined

NameError Traceback (most recent call last) in <cell line: 3>() 1 # async_browser = create_async_playwright_browser() 2 async_browser = create_async_playwright_browser() ----> 3 PlayWrightBrowserToolkit.update_forward_refs() 4 toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser) 5 tools = toolkit.get_tools()

9 frames /usr/local/lib/python3.10/dist-packages/langchain/agents/agent_toolkits/playwright/toolkit.py in

NameError: name 'SyncBrowser' is not defined

Can anybody explain, how we can solve this issue??

gurutech1416 avatar Sep 20 '23 08:09 gurutech1416

Restart notebook after install playwright, then run again, work for me

nithiroj avatar Nov 21 '23 10:11 nithiroj

Hi, @gwc4github,

I'm helping the LangChain team manage their backlog and am marking this issue as stale. From what I understand, the community has provided insights into the errors related to the structured_chat.html example not functioning properly in Jupyter or when run as a .py file. There are suggestions regarding the environment, iPython version, the use of inline await, and importing both create_async_playwright_browser and create_sync_playwright_browser. Additionally, there are recommendations to update forward references and restart the notebook after installing playwright. You expressed intent to create a working example and a pull request to address the issue.

Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days. Thank you!

dosubot[bot] avatar Feb 20 '24 16:02 dosubot[bot]

I leave the work of making sure these things are working to your team. I'm building a product and company now so unfortunately I don't have time.
I think the problem with libraries like these is that there are so many different related projects that it's hard to get one finished.

gwc4github avatar Feb 24 '24 13:02 gwc4github