Download README.md from Zamax14/api_rag: direct link, hf CLI and curl.
- Browser
- Download file 5.82 kB
-
https://huggingface.co/spaces/Zamax14/api_rag/resolve/main/README.md
- Command line
-
hf download hf://spaces/Zamax14/api_rag/README.md
-
curl -L -o README.md https://huggingface.co/spaces/Zamax14/api_rag/resolve/main/README.md
title: Api Rag
emoji: π¦
colorFrom: red
colorTo: green
sdk: docker
pinned: false
license: mit
REST-API LLM RAG π€
π Overview
This Python application provides an API service that retrieves answers to user queries based on the given context.
- Context Management: Context is managed and stored using Astra DB.
- Chat Service Interfaces: The application supports two interfaces: Hugging Face and OpenAI, both requiring an API key for access.
- Supported Document Formats: The app can process documents in various formats, such as PDF and TXT.
- Core Libraries: The project uses FastAPI and LangChain as its core libraries.
- Docker Ready: The project is configured for containerization with Docker.
π‘ For structured data types like CSV, XLSX, SQL, etc., it's recommended to utilize LLM Agents.
π‘ If you want to use a Hugging Face models, make sure to choose a larger model, such as one with 70B parameters, to achieve better results.
π¦ Requirements
- Python 3.8 or higher
- Docker
- Required libraries:
fastapihypercornwerkzeuglangchain(openai, community, huggingface, astradb)einopspython-multipartunstructured
π οΈ Configuration
To set up the application, follow these steps:
- Clone the repository and navigate to the project directory:
git clone http://iieg-app.jalisco.gob.mx/iieg/restapi-rag.git
cd restapi-rag
- Set the API key environment variable in the
rag/.envfile:
- Hugging Face: Set the API key in the
HF_TOKENenvironment variable. - OpenAI: Set the API key in the
OPENAI_API_KEYenvironment variable. - AstraDB: Set the API key in the
ASTRA_DB_APPLICATION_TOKENenvironment variable. - AstraDB Endpoint: Set the Endpoint URL in the
ASTRA_DB_API_ENDPOINTenvironment variable.
If both API keys, OpenAI or Hugging Face are configured, the OpenAI service will be used by default.
- Set the LLM chat and embedding model environment variables:
- Embedding Model: Set the embedding model in the
EMBEDDING_MODELenvironment variable. Defaultnomic-ai/nomic-embed-text-v1.5. - LLM Chat Model: Set the LLM chat model in the
LLM_MODELenvironment variable. Defaultmeta-llama/Llama-3.2-1B-Instruct
π‘ If you choose the Hugging Face LLM chat model, set the
HF_HOMEvariable tomodels, meaning the models will be stored in therag/modelsdirectory. This setup ensures that models used by the app are separated and ready for Docker containerization.
β οΈ If the chosen model is Meta Llama, you must request access to the repository. Simply go to the Hugging Face webpage and agree to their legal conditions.
- Set commplementary environment variable for
ntkl.
- NLTK Data: Specify the NLTK data path where the library will search for resources such as corpora and grammars. Default
/api.
β οΈ If you run the API in local env, ensure to set the environment variable in the
/home/.../restapi-rag/ragpath.
βοΈ Installation
Docker
Follow these steps to build the Docker image and run the container:
- Build the Docker image.
β οΈ Important: Before building the Docker image, make sure to run the app once to download the embeddings and/or LLM chat models in the directory specified by
HF_HOME. The path of the environment variable must bemodels.
docker build -t [image name] rag/
- Run the container.
docker run --rm --env-file rag/.env --name [container name] [image name]
Locally
To run the app locally, follow these steps:
- Install the required libraries from the
requirements.txtfile. - Set the environment variables as defined in the Configuration section.
- Start the app with the following command:
python -m uvicorn app.main:app
- Navigate to
http://127.0.0.1:8000/docsorhttp://127.0.0.1:8000/redocto access the app's documentation.
ποΈ Documentation
You can access the API documentation once the application is running. To do so, follow these steps:
- Run the container.
- Retrieve the container's IP address:
docker inspect [container name] | grep IPAddress
- Access the documentation via your web browser.
π‘ General documentation:
http://[IP]:8080/docsand endpoint documentation:http://[IP]:8080/redoc.
π€ Contributing
Contributions are welcome! Please feel free to open an issue for any suggestions or improvements.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments
- Hugging Face Transformers, LangChain, and Atra DB for providing the tools for working with LLMs.
- PyTorch for the deep learning framework.
- FastAPI for the web framework.
π TODO
- Create an endpoint that lists Astra DB collections.
- Implement an endpoint to update the collections.
- Implement an endpoint to delete a selected collection.
- Allow loading multiple files.
- Add endpoint for tabular data using Agents.
- Add endpoint for analyzing images.