|
| 1 | +from fastapi import FastAPI, HTTPException |
| 2 | +from deepsearcher.configuration import Configuration, init_config |
| 3 | +from deepsearcher.offline_loading import load_from_local_files, load_from_website |
| 4 | +from deepsearcher.online_query import query |
| 5 | +import uvicorn |
| 6 | +from config import Settings |
| 7 | + |
| 8 | +app = FastAPI() |
| 9 | + |
| 10 | +settings = Settings() |
| 11 | + |
| 12 | +config = Configuration() |
| 13 | + |
| 14 | +config.set_provider_config( |
| 15 | + "llm", |
| 16 | + settings.llm_provider, |
| 17 | + { |
| 18 | + "model": settings.llm_model, |
| 19 | + "api_key": settings.llm_api_key |
| 20 | + } |
| 21 | +) |
| 22 | + |
| 23 | +init_config(config) |
| 24 | + |
| 25 | +@app.post("/load-files/") |
| 26 | +def load_files(paths: list[str], collection_name: str = None, collection_description: str = None): |
| 27 | + try: |
| 28 | + load_from_local_files(paths_or_directory=paths, collection_name=collection_name, collection_description=collection_description) |
| 29 | + return {"message": "Files loaded successfully."} |
| 30 | + except Exception as e: |
| 31 | + raise HTTPException(status_code=500, detail=str(e)) |
| 32 | + |
| 33 | +@app.post("/load-website/") |
| 34 | +def load_website(urls: str, collection_name: str = None, collection_description: str = None): |
| 35 | + try: |
| 36 | + load_from_website(urls=urls, collection_name=collection_name, collection_description=collection_description) |
| 37 | + return {"message": "Website loaded successfully."} |
| 38 | + except Exception as e: |
| 39 | + raise HTTPException(status_code=500, detail=str(e)) |
| 40 | + |
| 41 | +@app.get("/query/") |
| 42 | +def perform_query(original_query: str, max_iter: int = 3): |
| 43 | + try: |
| 44 | + result = query(original_query, max_iter) |
| 45 | + return {"result": result} |
| 46 | + except Exception as e: |
| 47 | + raise HTTPException(status_code=500, detail=str(e)) |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + uvicorn.run(app, host="0.0.0.0", port=8000) |
0 commit comments