A simple ASP.NET Core Web API that interfaces with an LLM model running in a Docker container. This API provides a RESTful interface to interact with LLM models like Llama2, allowing you to send prompts and receive text completions.
- .NET 8.0 SDK or later
- Docker (for running the LLM container)
- An LLM container running Ollama or compatible API
LLAMA2AI/
├── LLAMA2AI.API/ # ASP.NET Core Web API project
│ ├── Controllers/ # API controllers
│ ├── Models/ # Data models
│ ├── Services/ # Business logic services
│ └── appsettings.json # Configuration
└── README.md # This file
First, make sure you have your LLM running in a Docker container. For example, if using Ollama:
docker run -d -p 11434:11434 --name ollama ollama/ollamaThen pull and run a model (e.g., Llama2):
docker exec -it ollama ollama pull llama2Navigate to the API directory and run:
cd LLAMA2AI.API
dotnet restore
dotnet runThe API will be available at:
- HTTP:
http://localhost:5000orhttp://localhost:5001 - HTTPS:
https://localhost:5001orhttps://localhost:5001
Update appsettings.json if your LLM container is running on a different port or URL:
{
"LLMSettings": {
"BaseUrl": "http://localhost:11434",
"Model": "llama2",
"TimeoutSeconds": 300
}
}Send a prompt to the LLM and receive a completion.
Request Body:
{
"prompt": "What is the capital of France?",
"systemPrompt": "You are a helpful assistant.",
"model": "llama2",
"stream": false
}Response:
{
"response": "The capital of France is Paris.",
"model": "llama2",
"done": true,
"createdAt": "2024-01-01T00:00:00Z"
}Example using curl:
curl -X POST https://localhost:5001/api/llm/completion \
-H "Content-Type: application/json" \
-d '{"prompt": "Hello, how are you?"}' \
-kExample using PowerShell:
$body = @{
prompt = "What is artificial intelligence?"
} | ConvertTo-Json
Invoke-RestMethod -Uri "http://localhost:5000/api/llm/completion" `
-Method Post -Body $body -ContentType "application/json"Check if the API service is running.
Response:
{
"status": "healthy",
"service": "LLAMA2AI API"
}When running in Development mode, you can access the Swagger UI at:
http://localhost:5000/swagger(HTTP)https://localhost:5001/swagger(HTTPS)
This provides an interactive interface to test the API endpoints.
- BaseUrl: The base URL of your LLM container (default:
http://localhost:11434) - Model: The default model to use (default:
llama2) - TimeoutSeconds: Request timeout in seconds (default:
300)
You can also run the API in a Docker container:
docker build -t llama2ai-api -f Dockerfile .docker run -p 8080:8080 --name llama2ai-api llama2ai-apiNote: Make sure the LLM container is accessible from within the Docker network.
- Controllers/LLMController.cs: Contains the API endpoints
- Services/LLMService.cs: Handles communication with the LLM container
- Models/: Contains the data models for requests and responses
- appsettings.json: Application configuration
The API includes comprehensive error handling:
- 400 Bad Request: Invalid request (e.g., empty prompt)
- 503 Service Unavailable: LLM container is not reachable
- 504 Gateway Timeout: Request to LLM timed out
- 500 Internal Server Error: Unexpected errors
- The API is configured to allow CORS from any origin by default. Update
Program.csto restrict this in production. - Consider adding authentication/authorization for production use.
- Validate and sanitize inputs to prevent prompt injection attacks.
-
Cannot connect to LLM container:
- Ensure the LLM container is running (
docker ps) - Check the
BaseUrlinappsettings.json - Verify network connectivity
- Ensure the LLM container is running (
-
Timeouts:
- Increase
TimeoutSecondsinappsettings.json - Check if the model is loaded in the LLM container
- Increase
-
Port conflicts:
- Change the ports in
launchSettings.jsonif needed
- Change the ports in
This project is provided as-is for demonstration purposes.