Scrapegraph-ai icon indicating copy to clipboard operation
Scrapegraph-ai copied to clipboard

TypeError: Type is not JSON serializable: numpy.float64

Open yiouyou opened this issue 9 months ago • 0 comments

Describe the bug

When run the library with langserve, it shows error as below:

ERROR:    Exception in ASGI application
RuntimeError: super(): __class__ cell not found

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 426, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__
    raise exc
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 65, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/routing.py", line 756, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/routing.py", line 776, in app
    await route.handle(scope, receive, send)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/routing.py", line 297, in handle
    await self.app(scope, receive, send)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/routing.py", line 77, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app
    raise exc
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    await app(scope, receive, sender)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/starlette/routing.py", line 75, in app
    await response(scope, receive, send)
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/sse_starlette/sse.py", line 255, in __call__
    async with anyio.create_task_group() as task_group:
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 597, in __aexit__
    raise exceptions[0]
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/sse_starlette/sse.py", line 258, in wrap
    await func()
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/sse_starlette/sse.py", line 245, in stream_response
    async for data in self.body_iterator:
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/langserve/api_handler.py", line 1106, in _stream_log
    "data": self._serializer.dumps(data).decode("utf-8"),
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/songz/miniconda3/envs/free/lib/python3.11/site-packages/langserve/serialization.py", line 168, in dumps
    return orjson.dumps(obj, default=default)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Type is not JSON serializable: numpy.float64

Additional context chain.py

import os
from dotenv import load_dotenv
load_dotenv()

from langchain_core.runnables import (
    RunnableLambda,
)
from langchain_core.pydantic_v1 import BaseModel, Field
import json
import time

from .util import ai_scrape


def func(_dict):
    start = time.time()
    url = _dict["url"]
    lang = _dict["lang"]
    prompt = _dict["prompt"]
    _res = ai_scrape(url, lang, prompt)
    end = time.time()
    _res["TIME(s)"] = f"{(end-start):.1f}"
    out_string = json.dumps(_res, ensure_ascii=False)
    return out_string


chain = RunnableLambda(func)


class Input(BaseModel):
    url: str = "https://perinim.github.io/projects" # https://perinim.github.io/projects, https://www.espncricinfo.com
    lang: str = "english" # english, chinese
    prompt: str = "List me all the articles" # List me all the articles, List me top latest article


chain = chain.with_types(input_type=Input)

util.py

from ._log import logger_api
import os, re
from typing import Literal
from scrapegraphai.graphs import SmartScraperGraph


def ai_scrape(
    url: str,
    lang: Literal["english", "chinese"],
    prompt: str,
):
    _res = {"answer": "", "DEBUG": "", "ERROR": ""}
    base_url = os.environ["ollama_url"]
    if lang == 'english':
        graph_config = {
            "llm": {
                "model": "ollama/mistral",
                "temperature": 0,
                "format": "json",
                "base_url": base_url,
            },
            "embeddings": {
                "model": "ollama/nomic-embed-text", # mxbai-embed-large
                "base_url": base_url,
            }
        }
    elif lang == 'chinese':
        graph_config = {
            "llm": {
                "model": "ollama/qwen:32b",
                "temperature": 0,
                "format": "json",
                "base_url": base_url,
            },
            "embeddings": {
                "model": "ollama/nomic-embed-text", # mxbai-embed-large
                "base_url": base_url,
            }
        }
    smart_scraper_graph = SmartScraperGraph(
        prompt=prompt,
        source=url, # also accepts a string with the already downloaded HTML code
        config=graph_config
    )
    out = smart_scraper_graph.run()
    logger_api.info(out)
    _res["answer"] = str(out)
    return _res

yiouyou avatar May 10 '24 05:05 yiouyou