Showing posts with label AI. Show all posts
Showing posts with label AI. Show all posts

Spring Boot + Ollama + Spring AI: Building a Simple POST API

Introduction

As a developer with 9 years of experience in Java and Spring Boot, I’ve always been curious about how emerging technologies can blend with enterprise frameworks. Recently, I explored integrating Spring Boot with Ollama via Spring AI to build a lightweight POST API. The goal: send a question to the API and get an AI‑powered response back — all running locally.

This post walks through the setup, code, and demo, and reflects on why this integration excites me.


Project Setup

I generated the project using Spring Initializr with the following configuration:

  • Project: Maven
  • Language: Java
  • Spring Boot Version: 4.0.6
  • Group: com.github.aleem-raja.ai
  • Artifact: ollama
  • Dependencies: Spring Web, Ollama (Spring AI), Lombok





Configuration

In application.yml, I configured Ollama to connect to the local model:

spring:
application:
name: ollama
ai:
ollama:
chat:
options:
model: llama3.1:8b

(Tip: Run ollama list to see which models are installed. If you see errors like model 'mistral' not found, just pull the model with ollama pull mistral or switch to one you already have.)




Code Walkthrough

DTOs

Using Lombok to reduce boilerplate:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AskRequest {
    private String question;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class AskResponse {
    private String answer;
}

Controller

Expose a POST endpoint /ask:

@RestController
@RequestMapping("/api")
public class AskController {

    private final OllamaChatModel chatModel;

    public AskController(OllamaChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @PostMapping("/ask")
    public AskResponse ask(@RequestBody AskRequest request) {
        String response = chatModel.call(request.getQuestion());
        return new AskResponse(response);
    }
}

Demo

Run the app:

mvn spring-boot:run




Send a POST request:

curl -X POST http://localhost:8080/api/ask \
     -H "Content-Type: application/json" \
     -d '{"question":"Explain Spring Boot in simple terms"}'






Reflection

What excites me about this experiment is how enterprise frameworks like Spring Boot can integrate directly with local AI models. This opens up possibilities for:

  • Smart backend APIs
  • AI‑powered developer tools
  • Lightweight prototypes without cloud dependency

It’s a reminder that even after years of working with Java, there’s always room to explore new intersections between established tech and emerging trends.


Conclusion

This project is available on GitHub:
👉 springboot-ollama-ai-demo

Try it yourself, experiment with different models, and let me know how you’d use AI in your backend workflows. I’ll continue exploring streaming responses and advanced integrations — stay tuned for updates.

Running Local AI Coding Assistants on a 16GB RAM Laptop Using Ollama, Cline, and Continue (Real Experience with LLaMA 3.1)

 

💻 Introduction

Local AI development tools are becoming more practical every day. I recently tested running AI coding assistants completely offline on a 16GB RAM laptop using Ollama, Cline, and Continue inside VS Code.

The goal was to understand whether a mid-range laptop can handle real AI-assisted development without cloud APIs.

The results were surprisingly usable with some limitations.




🧠 System Setup

  • Laptop: 16GB RAM
  • CPU: Intel i7
  • GPU: NVIDIA RTX series
  • OS: Windows 11
  • IDE: Visual Studio Code
  • AI Runtime: Ollama
  • Extensions:
    • Cline
    • Continue

Model used:

  • LLaMA 3.1 8B (non-instruct version)


⚙️ Installation Process

The setup was straightforward:

  1. Install Ollama
  2. Run model:

    ollama run llama3.1:8b
  3. Install VS Code extensions:
    • Cline
    • Continue
  4. Connect both to:

    http://localhost:11434

No API keys or cloud setup were required.



⚠️ Issue Faced: Cline Timeout Error

Initially, Cline failed to complete tasks and showed:

“Ollama request timed out after 30 seconds”

This happened when generating larger outputs like Spring Boot projects.



🔧 Solution: Increasing Timeout in Cline

The issue was resolved by increasing the request timeout inside Cline settings.

After adjusting:

  • Long prompts completed successfully
  • Spring Boot project generation worked
  • No more abrupt failures

However, responses were slower due to local model constraints.



🚀 Using Cline with LLaMA 3.1

After fixing the timeout issue, Cline was able to:

  • Generate project structures
  • Create files in VS Code
  • Assist with backend APIs

However, since the model was not the instruct version:

  • Responses were less structured
  • Sometimes verbose or indirect
  • Slower reasoning in complex tasks


✍️ Using Continue Extension

Continue performed better in daily coding tasks.

It provided:

  • Faster responses
  • Better inline code suggestions
  • Stable interaction with local models

It worked best for:

  • Debugging code
  • Refactoring functions
  • Quick explanations


🧩 Performance on 16GB RAM

✅ Works well for:

  • Small to medium projects
  • REST API development
  • Code generation and fixes
  • Offline AI assistance

⚠️ Limitations:

  • Slow on large prompts
  • Not ideal for heavy multi-file automation
  • Performance depends heavily on model size


⚖️ Cline vs Continue

Cline:

  • Best for automation
  • Can generate files and structure projects
  • Slower with non-instruct models

Continue:

  • Faster and more responsive
  • Better for daily coding assistance
  • More stable with local models


🧠 Key Takeaways

  • Local AI tools can run on 16GB RAM systems
  • Configuration matters more than hardware alone
  • Timeout settings are critical for smooth usage
  • Model selection significantly impacts performance


🔥 Conclusion

Running AI coding tools locally is now practical even on mid-range laptops.

While it is not as fast as cloud-based AI, it provides:

  • Privacy
  • Offline capability
  • Zero API cost
  • Decent coding assistance

For developers exploring local AI workflows, this setup is a strong starting point.