Skip to content

Connection Error with OllamaFunctions in Langchain #2783

Closed
@quartermaine

Description

Description

I am attempting to replicate the Langchain tutorial in order to use OllamaFunctions for web extraction, as also demonstrated here in a Google Colab environment.

Code

[1] %%capture
     !pip install langchain_experimental


[2] from langchain_experimental.llms.ollama_functions import OllamaFunctions

     lm = OllamaFunctions(model="llama2:13b",
                      base_url="http://localhost:11434",
                      temperature=0)

[3] %%capture
     !pip install -q langchain-openai langchain playwright beautifulsoup4
     !playwright install


[4] import nest_asyncio
     nest_asyncio.apply()


[5] from langchain.chains import create_extraction_chain
     schema = {
          "properties": {
           "news_article_title": {"type": "string"},
           "news_article_summary": {"type": "string"},
              },
           "required": ["news_article_title", "news_article_summary"],
      }

     def extract(content: str, schema: dict):
      return create_extraction_chain(schema=schema, llm=llm, verbose=True).invoke(content)


[6] import pprint
     from langchain.text_splitter import RecursiveCharacterTextSplitter
     from langchain_community.document_loaders import AsyncChromiumLoader
     from langchain_community.document_transformers import BeautifulSoupTransformer

    def scrape_with_playwright(urls, schema):
         loader = AsyncChromiumLoader(urls)
         docs = loader.load()
         bs_transformer = BeautifulSoupTransformer()
         docs_transformed = bs_transformer.transform_documents(
                docs, tags_to_extract=["span"]
         )
         print("Extracting content with LLM")
         # Grab the first 1000 tokens of the site
         splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
                   chunk_size=1000,
                    chunk_overlap=0,
                     separators=["\n"]
          )
         splits = splitter.split_documents(docs_transformed)
         print("Number of splits:", len(splits))  # Add this debugging statement
         if splits:  # Check if splits list is not empty
                # Process the first split
                extracted_content = extract(schema=schema, content=splits[0].page_content) #  Line where error occurs
                pprint.pprint(extracted_content)
                return extracted_content
         else:
                 print("No splits found")  # Add this debugging statement
                 return None```

[7] urls = ["https://www.nytimes.com/"]
     extracted_content = scrape_with_playwright(urls, schema=schema) python

Error

But I am getting the following error:

ConnectionError: HTTPConnectionPool(host='localhost', port=11434): Max retries exceeded with url: /api/chat/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f7b19911300>: Failed to establish a new connection: [Errno 111] Connection refused'))

Metadata

Assignees

No one assigned

    Labels

    needs more infoMore information is needed to assistquestionGeneral questions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions