Skip to content

Commit

Permalink
feat : adds workflow and expose endpoints for chat
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Mar 27, 2024
1 parent cc9452e commit fdb1bd8
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 10 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/chatmodel-springai.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: chatmodel-springai CI Build

on:
push:
paths:
- "chatmodel-springai/**"
branches: [main]
pull_request:
paths:
- "chatmodel-springai/**"
types:
- opened
- synchronize
- reopened

jobs:
build:
name: Run Unit & Integration Tests
runs-on: ubuntu-latest
defaults:
run:
working-directory: chatmodel-springai
strategy:
matrix:
distribution: [ 'temurin' ]
java: [ '21' ]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Set up JDK ${{ matrix.java }}
uses: actions/[email protected]
with:
java-version: ${{ matrix.java }}
distribution: ${{ matrix.distribution }}
cache: 'maven'
- name: Build and analyze
run: ./mvnw clean verify
39 changes: 39 additions & 0 deletions .github/workflows/pgvector-springai.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: pgvector-springai CI Build

on:
push:
paths:
- "pgvector-springai/**"
branches: [main]
pull_request:
paths:
- "pgvector-springai/**"
types:
- opened
- synchronize
- reopened

jobs:
build:
name: Run Unit & Integration Tests
runs-on: ubuntu-latest
defaults:
run:
working-directory: pgvector-springai
strategy:
matrix:
distribution: [ 'temurin' ]
java: [ '21' ]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

- name: Set up JDK ${{ matrix.java }}
uses: actions/[email protected]
with:
java-version: ${{ matrix.java }}
distribution: ${{ matrix.distribution }}
cache: 'maven'
- name: Build and analyze
run: ./mvnw clean verify
64 changes: 61 additions & 3 deletions chatmodel-springai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,72 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.ai</groupId>
<artifactId>chatmodel-springai</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
<spring-ai.version>0.8.1</spring-ai.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>
12 changes: 12 additions & 0 deletions chatmodel-springai/src/main/java/com/example/ai/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.ai;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
7 changes: 0 additions & 7 deletions chatmodel-springai/src/main/java/com/example/ai/Main.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.ai.controller;

import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
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 java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/ai")
public class ChatController {

private final ChatClient chatClient;

ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
}

@GetMapping("/chat")
Map<String,String> chat(@RequestParam String question) {
var response = chatClient.call(question);
return Map.of("question", question, "answer", response);
}

@GetMapping("/chat-with-prompt")
Map<String,String> chatWithPrompt(@RequestParam String subject) {
PromptTemplate promptTemplate = new PromptTemplate("Tell me a joke about {subject}");
Prompt prompt = promptTemplate.create(Map.of("subject", subject));
ChatResponse response = chatClient.call(prompt);
String answer = response.getResult().getOutput().getContent();
return Map.of( "answer", answer);
}

@GetMapping("/chat-with-system-prompt")
Map<String,String> chatWithSystemPrompt(@RequestParam String subject) {
SystemMessage systemMessage = new SystemMessage("You are a sarcastic and funny chatbot");
UserMessage userMessage = new UserMessage("Tell me a joke about " + subject);
Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
ChatResponse response = chatClient.call(prompt);
String answer = response.getResult().getOutput().getContent();
return Map.of( "answer", answer);
}
}
6 changes: 6 additions & 0 deletions chatmodel-springai/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spring.threads.virtual.enabled=true

spring.ai.openai.api-key=demo
spring.ai.openai.base-url=http://langchain4j.dev/demo/openai
spring.ai.openai.chat.options.model=gpt-3.5-turbo
spring.ai.openai.chat.options.temperature=0.7
5 changes: 5 additions & 0 deletions pgvector-springai/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
spring.ai.openai.api-key=demo
spring.ai.openai.base-url=http://langchain4j.dev/demo/openai
spring.ai.openai.image.enabled=false
spring.ai.openai.chat.options.temperature=0.7
spring.ai.openai.chat.options.model=gpt-3.5-turbo

spring.threads.virtual.enabled=true


#PgVector
spring.ai.vectorstore.pgvector.dimensions=384
Expand Down

0 comments on commit fdb1bd8

Please sign in to comment.