Skip to content

Commit

Permalink
feat : convert endpoint to post
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Apr 1, 2024
1 parent da51ca5 commit d5232a6
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ AI implementations using java, stores and either of Langchain4j or springai fram
| [pgvector springai](./pgvector-springai) | Embeddings implementation using springai and pgvector |
| [playground](./playground) | AI playground using Langchain4j |
| [rag langchain4j AllMiniLmL6V2](./rag-langchain4j-AllMiniLmL6V2-llm) | RAG Implementation using Langchain4j, PGVector store and openai LLM |
| [rag springai ollama llm](./rag-springai-ollama-llm) | RAG Implementation using springai, PGVector store and ollama LLM with orca-mini model |
| [rag springai ollama llm](./rag-springai-ollama-llm) | RAG Implementation using springai, Redis store and ollama LLM with llama2 model |
| [rag springai openai llm](./rag-springai-openai-llm) | RAG Implementation using springai, PGVector store and openai LLM |

2 changes: 1 addition & 1 deletion rag-springai-ollama-llm/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.7'
services:
ollama:
image: langchain4j/ollama-orca-mini:latest
image: langchain4j/ollama-llama2:latest
ports:
- '11434:11434'
redis-stack:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.learning.ai.llmragwithspringai.controller;

import com.learning.ai.llmragwithspringai.model.request.AIChatRequest;
import com.learning.ai.llmragwithspringai.model.response.AIChatResponse;
import com.learning.ai.llmragwithspringai.service.AIChatService;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import java.util.Map;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/ai")
Expand All @@ -26,15 +22,10 @@ public AiController(AIChatService aiChatService) {
this.aiChatService = aiChatService;
}

@GetMapping("/chat")
Map<String, String> ragService(
@RequestParam
@NotBlank(message = "Query cannot be empty")
@Size(max = 255, message = "Query exceeds maximum length")
@Pattern(regexp = "^[a-zA-Z0-9 ]*$", message = "Invalid characters in query")
String question) {
String chatResponse = aiChatService.chat(question);
@PostMapping("/chat")
AIChatResponse ragService(@RequestBody @Valid AIChatRequest aiChatRequest) {
String chatResponse = aiChatService.chat(aiChatRequest.question());
LOGGER.info("chatResponse :{}", chatResponse);
return Map.of("response", chatResponse);
return new AIChatResponse(chatResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.learning.ai.llmragwithspringai.model.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import java.io.Serializable;

public record AIChatRequest(
@NotBlank(message = "Query cannot be empty")
@Size(max = 800, message = "Query exceeds maximum length")
@Pattern(regexp = "^[a-zA-Z0-9 ?]*$", message = "Invalid characters in query")
String question)
implements Serializable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.learning.ai.llmragwithspringai.model.response;

public record AIChatResponse(String queryResponse) {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ spring.application.name=rag-springai-ollama-llm
spring.threads.virtual.enabled=true
spring.mvc.problemdetails.enabled=true

spring.ai.ollama.chat.options.model=orca-mini
spring.ai.ollama.chat.options.responseFormat=json_object
spring.ai.ollama.chat.options.model=llama2

spring.ai.ollama.embedding.options.model=orca-mini
spring.ai.ollama.embedding.options.model=llama2

spring.ai.vectorstore.redis.index=vector_store
spring.ai.vectorstore.redis.prefix=ai
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import static org.hamcrest.Matchers.is;

import com.learning.ai.llmragwithspringai.config.AbstractIntegrationTest;
import com.learning.ai.llmragwithspringai.model.request.AIChatRequest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
Expand All @@ -25,20 +27,23 @@ void setUp() {

@Test
void testRag() {
given().param("question", "Is Rohit Sharma batsman")
given().contentType(ContentType.JSON)
.body(new AIChatRequest("Is Rohit Sharma batsman?"))
.when()
.get("/api/ai/chat")
.post("/api/ai/chat")
.then()
.statusCode(200)
.body("response", containsString("Yes"))
.log();
.body("queryResponse", containsString("Yes"))
.log()
.all();
}

@Test
void testEmptyQuery() {
given().param("question", "")
given().contentType(ContentType.JSON)
.body(new AIChatRequest(""))
.when()
.get("/api/ai/chat")
.post("/api/ai/chat")
.then()
.statusCode(400)
.header("Content-Type", is("application/problem+json"))
Expand All @@ -53,9 +58,10 @@ void testEmptyQuery() {
@Test
void testLongQueryString() {
String longQuery = "a".repeat(1000); // Example of a very long query string
given().param("question", longQuery)
given().contentType(ContentType.JSON)
.body(new AIChatRequest(longQuery))
.when()
.get("/api/ai/chat")
.post("/api/ai/chat")
.then()
.statusCode(400)
.header("Content-Type", is("application/problem+json"))
Expand All @@ -69,9 +75,10 @@ void testLongQueryString() {

@Test
void testSpecialCharactersInQuery() {
given().param("question", "@#$%^&*()")
given().contentType(ContentType.JSON)
.body(new AIChatRequest("@#$%^&*()"))
.when()
.get("/api/ai/chat")
.post("/api/ai/chat")
.then()
.statusCode(400)
.header("Content-Type", is("application/problem+json"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class TestLlmRagWithSpringAiApplication {

@Bean
OllamaContainer ollama(DynamicPropertyRegistry properties) {
OllamaContainer ollama = new OllamaContainer(DockerImageName.parse("langchain4j/ollama-orca-mini:latest")
.asCompatibleSubstituteFor("ollama/ollama"));
OllamaContainer ollama = new OllamaContainer(
DockerImageName.parse("langchain4j/ollama-llama2:latest").asCompatibleSubstituteFor("ollama/ollama"));
properties.add("spring.ai.ollama.base-url", ollama::getEndpoint);
return ollama;
}
Expand Down

0 comments on commit d5232a6

Please sign in to comment.