File size: 1,433 Bytes
95f7828
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Handles API logic for calling scraper & LLM
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, HttpUrl
from llm_module.message import chain
from scraper import multiple_links_scraping
from typing import Any
import logging

logger = logging.getLogger('fastapi_cli')
api_router = APIRouter(prefix='/researcher', tags=['Researcher'])

class InputField(BaseModel):
    url : str
    generic_message : str

class OutputField(BaseModel):
    company_summary : str
    customised_message : str


@api_router.post('/invoke', response_model=OutputField)
async def invoke_web_researcher(body: InputField):
    try:
        scraped_data = await multiple_links_scraping([body.url])
    except Exception as e:
        logger.error(f"Failed to scrape: {e}")
        raise HTTPException(status_code=500, detail="Internal Server Error")
    try:
        output = await chain.ainvoke({
            "generic_message": body.generic_message,
            "scraped_message": scraped_data[0]
        })

        if output is None :
            raise ValueError("LLM chain returned None values")
        print(output)
        return OutputField(
            company_summary=output['summary_output'],
            customised_message=output['generated_message']
        )
    except Exception as e:
        logger.error(f"Failed to invoke the chain: {e}")
        raise HTTPException(status_code=500, detail="Internal Server Error")