crawl4ai icon indicating copy to clipboard operation
crawl4ai copied to clipboard

How to parse the crawled content?

Open zukangy opened this issue 2 years ago • 1 comments

Thanks for open-sourcing this awesome package!

I am currently experimenting with its functionalities, and want to learn how you parse the retrieved content.

Say on Crawl4AI.com, I wanted to crawl https://engineering.fb.com/2023/09/07/data-infrastructure/arcadia-end-to-end-ai-system-performance-simulator/, the retrieved content in markdown on the UI was pretty neat, but I couldn't seem to find a way to clean the HTML.

I did notice that the REST API template indicates "provider": "groq/llama3-70b-8192", does this mean you deploy an LLM for the raw HTML parsing?

Thanks!

Screenshot 2024-05-30 at 12 05 11 PM

zukangy avatar May 30 '24 19:05 zukangy

@zukangy I'm extremely sorry that I missed this issue, and I know it's been a few months since you posted this. Firstly, the library has changed enormously. We now have around 14,000 stars, and for what you're asking, you can basically combine our crawler with one of our large language model extraction strategies. I will leave the code here, and then you can see how it goes. In this code, you can see that we're using OpenAI, but technically, you can use any other provider as long as you have their API token or use a local LLM like Ollama, and then you can have fun with it. Please let me know if you have any issues, and again, I apologize for the delayed response.

async def extract_structured_data_using_llm(provider: str, api_token: str = None, extra_headers: Dict[str, str] = None):
    print(f"\n--- Extracting Structured Data with {provider} ---")
    
    if api_token is None and provider != "ollama":
        print(f"API token is required for {provider}. Skipping this example.")
        return

    extra_args = {}
    if extra_headers:
        extra_args["extra_headers"] = extra_headers

    async with AsyncWebCrawler(verbose=True) as crawler:
        result = await crawler.arun(
            url="https://openai.com/api/pricing/",
            word_count_threshold=1,
            extraction_strategy=LLMExtractionStrategy(
                provider=provider,
                api_token=api_token,
                schema=OpenAIModelFee.schema(),
                extraction_type="schema",
                instruction="""From the crawled content, extract all mentioned model names along with their fees for input and output tokens. 
                Do not miss any models in the entire content. One extracted model JSON format should look like this: 
                {"model_name": "GPT-4", "input_fee": "US$10.00 / 1M tokens", "output_fee": "US$30.00 / 1M tokens"}.""",
                extra_args=extra_args
            ),
            bypass_cache=True,
        )
        print(result.extracted_content)
        
async def main():
    await extract_structured_data_using_llm("huggingface/meta-llama/Meta-Llama-3.1-8B-Instruct", os.getenv("HUGGINGFACE_API_KEY"))
    await extract_structured_data_using_llm("openai/gpt-4", os.getenv("OPENAI_API_KEY"))
    await extract_structured_data_using_llm("ollama/llama3.2")   
    

unclecode avatar Oct 17 '24 07:10 unclecode