A FastAPI service that provides a REST API for interacting with the Gemma language model through Ollama. The service is designed to be flexible and can be used for various natural language processing tasks by customizing the system prompt.
- Generic REST API for LLM interaction
- Configurable system prompts for different use cases
- Docker containerization
- Automatic model loading
- Health check endpoint
- Retry mechanism for reliability
- JSON response formatting
- Docker and Docker Compose
- Clone the repository:
git clone https://github.com/JacobOmateq/gemma-api
cd gemma-api- Start the service:
./start.shOr for a complete rebuild:
./rebuild.shThe default system prompt is configured in llm_settings.json. You can customize it for your specific use case:
{
"model": "gemma3:1b",
"system_prompt": "Your custom prompt here..."
}curl --location 'http://localhost:8000/generate' \
--header 'Content-Type: application/json' \
--data '{
"text": "Your input text here",
"system_prompt": "Optional custom prompt to define the task"
}'Example use cases:
- Information Extraction:
{
"text": "Meeting with John tomorrow at 2 PM",
"system_prompt": "Extract event details and return them as JSON with fields: type, person, time"
}- Text Classification:
{
"text": "I love this product, it works great!",
"system_prompt": "Analyze the sentiment of this text and return JSON with fields: sentiment, confidence"
}- Structured Data Generation:
{
"text": "Create a character profile for a fantasy story",
"system_prompt": "Generate a character profile and return as JSON with fields: name, race, class, abilities, background"
}{
"content": {
// JSON formatted response
// Structure depends on the system prompt
}
}curl http://localhost:8000/healthResponse:
{
"status": "healthy",
"ollama": "connected"
}gemma-api/
├── main.py # FastAPI application
├── llm_settings.json # Model and prompt configuration
├── start.sh # Service startup script
├── rebuild.sh # Rebuild script for development
├── Dockerfile # Container definition
└── docker-compose.yml
start.sh: Initial startup of the servicerebuild.sh: Complete rebuild of the containers (useful during development)
During development, use the rebuild script to apply changes:
./rebuild.shThis will:
- Stop existing containers
- Rebuild images from scratch
- Start the services
- Pull the required Ollama model
View service logs:
docker-compose logs -fView specific service logs:
docker-compose logs -f api # For the API service
docker-compose logs -f ollama # For the Ollama service-
If the service returns
nullresponses, check:- Ollama model availability
- JSON parsing in the response
- System prompt formatting
-
If the service is unhealthy:
- Verify Ollama container is running
- Check resource availability
- Review API logs
MIT