Welcome to the Solar Backend FastAPI project!
This project is designed to provide backend services for a LLM chat application using Upstage Solar API.
This repository contains simple RAGs for using the Layout Analysis, Embedding, and Chat features of the Upstage API.
This is an example project for developing an Upstage Solar API service based on FastAPI.
- FastAPI for building APIs
- Integration with OpenAI for using Upstage API
- Embedding and Layout Analysis
- RAG for Chat API
- Configuration management
- Error handling and logging
- Modular structure for scalability
To get started with this project, follow these steps:
-
Clone the repository:
$ git clone https://github.com/ilevk/solar-backend-fastapi-advanced.git $ cd solar-backend-fastapi-advanced
-
Set up a virtual environment:
- You can use any virtual environment tools.
$ conda create --name solar python=3.12 $ conda activate solar
-
Install dependencies:
$ pip install poetry, pre-commit $ pre-commit install $ poetry install
-
Set up environment variables:
Create a
.env
file in the root directory and add the necessary environment variables. For example:API_KEY=your_upstage_api_key
-
Install Docker:
We will use Docker to run a ChromaDB instance. so you need to install docker, docker-compose.
Follow the instructions on the Official Docker website, Install Docker Engine to install Docker on your machine.
Follow the instructions on the Official Docker website, Install Docker Compose to install Docker Compose on your machine.
-
Run ChromaDB for Vector Search
Run a ChromaDB instance using docker-compose:
$ git clone https://github.com/chroma-core/chroma $ docker-compose up -d # start the ChromaDB instance $ docker-compose down # stop the ChromaDB instance
To run the application, use the following command:
$ uvicorn app.main:app --host {host} --port {port}
Here is an overview of the project structure:
solar-backend-fastapi-advanced/
├── app/
│ ├── clients/
│ │ ├── open_ai.py
│ │ └── upstage.py
│ ├── core/
│ │ ├── errors/
│ │ ├── config.py
│ │ └── db.py
│ │ ├── dependencies.py
│ │ ├── lifespan.py
│ │ ├── logger.py
│ ├── models/
│ │ └── schemas/
│ ├── routers/
│ ├── services/
│ │ ├── chat.py
│ │ ├── embedding.py
│ │ └── service_factory.py
│ └── main.py
├── .gitignore
├── .pre-commit-config.yaml
├── LICENSE
├── poetry.lock
├── pyproject.toml
├── README.md
└── start.sh
This directory contains the main application code.
-
Contains client classes for interacting with external APIs.
open_ai.py
: Client for interacting with the OpenAI API.- chat, stream_chat, embedding.
upstage.py
: Client for interacting with the Upstage API.- layout_anlaysis.
-
Contains core functionalities and configurations.
errors
: Custom error classes and handlers.config.py
: Configuration settings for the application, including environment variables.db.py
: ChromaDB connection and setup.dependencies.py
: Dependency injection for FastAPI.lifespan.py
: Application lifecycle events, such as startup and shutdown.logger.py
: Setup for logging.
-
Contains Pydantic models and schemas used for data validation and serialization.
- chat, document, embedding, etc.
-
Contains the API route definitions.
chat.py
: Routes for chat operations.embedding.py
: Routes for embedding operations.
-
Contains business logic and service classes.
chat.py
: Services related to chat operations.embedding.py
: Services related to embedding operations.service_factory.py
: Factory class for creating service instances.
-
main.py
: The entry point of the application. It initializes the FastAPI app and includes the application lifecycle events.
Configuration is managed using environment variables. You can set these variables in a .env file in the root directory.
This project is licensed under the MIT License. See the LICENSE file for more details.