Skip to content

Spring AI

Spring AI integrates LLMs into enterprise systems. Wrap the underlying LLM client via Spring configuration to normalize and score combined inputs before generation.

import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GlitchConfig {
// Bean: Creates the OpenAI API client configured to use Glitch
@Bean
public OpenAiApi openAiApi() {
return new OpenAiApi(
"https://api.golabrat.ai/v1", // Glitch base URL
System.getenv("GLITCH_API_KEY"), // Your Glitch API key
null // Uses default HTTP client
);
}
// Bean: Creates the chat client that uses the Glitch-configured API
@Bean
public OpenAiChatClient openAiChatClient(OpenAiApi openAiApi) {
return new OpenAiChatClient(openAiApi, OpenAiChatOptions.builder()
.withModel("gpt-4")
.build());
}
}
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ChatService {
@Autowired
private ChatClient chatClient;
public String chat(String userMessage) {
// All LLM calls are automatically secured via Glitch
return chatClient.call(
new Prompt(userMessage)
).getResult().getOutput().getContent();
}
}

For pre-screening content before sending to Spring AI, or when you need structured detection results, use the /v1/detect endpoint:

import org.springframework.web.client.RestClient;
import org.springframework.http.ResponseEntity;
import java.util.Map;
@Configuration
public class GlitchRestClientConfig {
@Bean
public RestClient glitchRestClient() {
return RestClient.builder()
.baseUrl("https://api.golabrat.ai/v1")
.defaultHeader("Authorization", "Bearer " + System.getenv("GLITCH_API_KEY"))
.build();
}
public boolean preScreenContent(String content) {
Map<String, Object> requestBody = Map.of(
"content", content,
"direction", "input"
);
ResponseEntity<Map> response = glitchRestClient().post()
.uri("/detect")
.body(requestBody)
.retrieve()
.toEntity(Map.class);
Map<String, Object> result = response.getBody();
Boolean blocked = (Boolean) result.get("blocked");
return Boolean.TRUE.equals(blocked);
}
}

Handle security blocks in your service layer:

import org.springframework.ai.chat.ChatResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
@Service
public class ChatService {
@Autowired
private ChatClient chatClient;
public String chat(String userMessage) {
try {
ChatResponse response = chatClient.call(new Prompt(userMessage));
return response.getResult().getOutput().getContent();
} catch (Exception e) {
// Check if it's a security block (403)
if (e.getMessage() != null && e.getMessage().contains("403")) {
throw new ResponseStatusException(
HttpStatus.FORBIDDEN,
"Request blocked by security policy"
);
}
throw e;
}
}
}