Skip to content

Commit e312930

Browse files
authored
Merge pull request #45 from yml-org/feature/CM-1214/jvm-sample
[CM-1214] Create JVM sample
2 parents 9ff8452 + 614466e commit e312930

File tree

10 files changed

+160
-1
lines changed

10 files changed

+160
-1
lines changed

buildSrc/src/main/kotlin/Dependencies.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ object Versions {
1616
const val MATERIAL_DESIGN = "1.6.1"
1717
const val MOCKK = "1.13.4"
1818
const val MOCKK_COMMON = "1.12.5"
19+
const val SPRING = "2.4.5"
1920
}
2021

2122
object Dependencies {
2223

24+
object Spring {
25+
const val SPRING_DEP = "org.springframework.boot:spring-boot-dependencies:${Versions.SPRING}"
26+
const val SPRING_WEB = "org.springframework.boot:spring-boot-starter-web"
27+
}
28+
2329
object Network {
2430
const val KTOR_NEGOTIATION = "io.ktor:ktor-client-content-negotiation:${Versions.KTOR}"
2531
const val KTOR_SERIALIZATION = "io.ktor:ktor-serialization-kotlinx-json:${Versions.KTOR}"

sample/jvm/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build
2+
/src/main/resources/application.properties

sample/jvm/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# YChat GPT JVM Sample
2+
3+
This sample exposes some RESTful APIs to demonstrate how the YChat SDK can work in a backend environment.
4+
5+
## Setup
6+
7+
Before running the sample, you need to follow these steps:
8+
9+
1. Remove the suffix ".txt" from the `application.properties.txt` file located in `sample/jvm/main/src/resources`
10+
2. Set your API key in the `apiKey` variable. Click [here](https://beta.openai.com/docs/api-reference/authentication) to get more information on how to get the api key.
11+
12+
After you configure, you can run the server with the following command: `./gradlew bootRun -Dserver.port=[port_number]`. Replace `[port_number]` with any unused port number such as `8080`, `8081`, etc.
13+
14+
## How it Works
15+
16+
After running the server, you can start playing around with the following endpoints:
17+
18+
### Completion Endpoint
19+
20+
This endpoint generates text based on the provided prompt.
21+
22+
##### Endpoint: http://localhost:[port_number]/api/ychat/completion
23+
24+
##### Parameters:
25+
26+
- input: The prompt for generating text.
27+
28+
##### Example:
29+
30+
`GET http://localhost:8080/api/ychat/completion?input="What is 1 + 1?"`

sample/jvm/build.gradle.kts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
plugins {
2+
id("org.springframework.boot").version(Versions.SPRING)
3+
java
4+
}
5+
6+
java {
7+
sourceCompatibility = JavaVersion.VERSION_1_8
8+
targetCompatibility = JavaVersion.VERSION_1_8
9+
}
10+
11+
dependencies {
12+
implementation(project(":ychat"))
13+
implementation(platform(Dependencies.Spring.SPRING_DEP))
14+
implementation(Dependencies.Spring.SPRING_WEB)
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package co.yml.ychat.jvm;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class YChatApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(YChatApplication.class, args);
11+
}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package co.yml.ychat.jvm.config;
2+
3+
import co.yml.ychat.YChat;
4+
import java.io.IOException;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
public class YChatConfig {
11+
12+
@Value("${apiKey}")
13+
private String apiKey;
14+
15+
@Bean
16+
public YChat provideYChat() throws IOException {
17+
return YChat.create(apiKey);
18+
}
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package co.yml.ychat.jvm.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
import co.yml.ychat.jvm.services.CompletionService;
10+
11+
@RestController
12+
@RequestMapping("api/ychat")
13+
public class YChatController {
14+
15+
@Autowired
16+
private CompletionService completionService;
17+
18+
@GetMapping("completion")
19+
public ResponseEntity<String> completion(
20+
@RequestParam(value = "input", defaultValue = Defaults.COMPLETION_INPUT) String input
21+
) throws Exception {
22+
String result = completionService.getCompletionAnswer(input);
23+
return ResponseEntity.ok(result);
24+
}
25+
26+
private static class Defaults {
27+
static final String COMPLETION_INPUT = "Say this is a test.";
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package co.yml.ychat.jvm.services;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Service;
6+
import java.util.concurrent.CompletableFuture;
7+
import co.yml.ychat.YChat;
8+
9+
@Service
10+
public class CompletionService {
11+
12+
@Autowired
13+
private YChat ychat;
14+
15+
private static final int MAX_TOKENS = 512;
16+
17+
public String getCompletionAnswer(String input) throws Exception {
18+
final CompletableFuture<String> future = new CompletableFuture<>();
19+
ychat.completion()
20+
.setMaxTokens(MAX_TOKENS)
21+
.setInput(input)
22+
.execute(new CompletionCallbackResult(future));
23+
return future.get();
24+
}
25+
26+
private static class CompletionCallbackResult implements YChat.Callback<String> {
27+
28+
private final CompletableFuture<String> future;
29+
30+
CompletionCallbackResult(CompletableFuture<String> future) {
31+
this.future = future;
32+
}
33+
34+
@Override
35+
public void onSuccess(String result) {
36+
future.complete(result);
37+
}
38+
39+
@Override
40+
public void onError(@NotNull Throwable throwable) {
41+
future.completeExceptionally(throwable);
42+
}
43+
}
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
apiKey=PUT_YOUR_KEY_HERE

settings.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ dependencyResolutionManagement {
1414
}
1515

1616
rootProject.name = "ychat-sdk"
17+
include(":ychat")
1718
include(":sample:android")
18-
include(":ychat")
19+
include(":sample:jvm")

0 commit comments

Comments
 (0)