Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ object Versions {
const val MATERIAL_DESIGN = "1.6.1"
const val MOCKK = "1.13.4"
const val MOCKK_COMMON = "1.12.5"
const val SPRING = "2.4.5"
}

object Dependencies {

object Spring {
const val SPRING_DEP = "org.springframework.boot:spring-boot-dependencies:${Versions.SPRING}"
const val SPRING_WEB = "org.springframework.boot:spring-boot-starter-web"
}

object Network {
const val KTOR_NEGOTIATION = "io.ktor:ktor-client-content-negotiation:${Versions.KTOR}"
const val KTOR_SERIALIZATION = "io.ktor:ktor-serialization-kotlinx-json:${Versions.KTOR}"
Expand Down
2 changes: 2 additions & 0 deletions sample/jvm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build
/src/main/resources/application.properties
30 changes: 30 additions & 0 deletions sample/jvm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# YChat GPT JVM Sample

This sample exposes some RESTful APIs to demonstrate how the YChat SDK can work in a backend environment.

## Setup

Before running the sample, you need to follow these steps:

1. Remove the suffix ".txt" from the `application.properties.txt` file located in `sample/jvm/main/src/resources`
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.

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.

## How it Works

After running the server, you can start playing around with the following endpoints:

### Completion Endpoint

This endpoint generates text based on the provided prompt.

##### Endpoint: http://localhost:[port_number]/api/ychat/completion

##### Parameters:

- input: The prompt for generating text.

##### Example:

`GET http://localhost:8080/api/ychat/completion?input="What is 1 + 1?"`
15 changes: 15 additions & 0 deletions sample/jvm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id("org.springframework.boot").version(Versions.SPRING)
java
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
implementation(project(":ychat"))
implementation(platform(Dependencies.Spring.SPRING_DEP))
implementation(Dependencies.Spring.SPRING_WEB)
}
12 changes: 12 additions & 0 deletions sample/jvm/src/main/java/co/yml/ychat/jvm/YChatApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package co.yml.ychat.jvm;

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

@SpringBootApplication
public class YChatApplication {

public static void main(String[] args) {
SpringApplication.run(YChatApplication.class, args);
}
}
19 changes: 19 additions & 0 deletions sample/jvm/src/main/java/co/yml/ychat/jvm/config/YChatConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package co.yml.ychat.jvm.config;

import co.yml.ychat.YChat;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class YChatConfig {

@Value("${apiKey}")
private String apiKey;

@Bean
public YChat provideYChat() throws IOException {
return YChat.create(apiKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package co.yml.ychat.jvm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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 co.yml.ychat.jvm.services.CompletionService;

@RestController
@RequestMapping("api/ychat")
public class YChatController {

@Autowired
private CompletionService completionService;

@GetMapping("completion")
public ResponseEntity<String> completion(
@RequestParam(value = "input", defaultValue = Defaults.COMPLETION_INPUT) String input
) throws Exception {
String result = completionService.getCompletionAnswer(input);
return ResponseEntity.ok(result);
}

private static class Defaults {
static final String COMPLETION_INPUT = "Say this is a test.";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package co.yml.ychat.jvm.services;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;
import co.yml.ychat.YChat;

@Service
public class CompletionService {

@Autowired
private YChat ychat;

private static final int MAX_TOKENS = 512;

public String getCompletionAnswer(String input) throws Exception {
final CompletableFuture<String> future = new CompletableFuture<>();
ychat.completion()
.setMaxTokens(MAX_TOKENS)
.setInput(input)
.execute(new CompletionCallbackResult(future));
return future.get();
}

private static class CompletionCallbackResult implements YChat.Callback<String> {

private final CompletableFuture<String> future;

CompletionCallbackResult(CompletableFuture<String> future) {
this.future = future;
}

@Override
public void onSuccess(String result) {
future.complete(result);
}

@Override
public void onError(@NotNull Throwable throwable) {
future.completeExceptionally(throwable);
}
}
}
1 change: 1 addition & 0 deletions sample/jvm/src/main/resources/application.properties.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apiKey=PUT_YOUR_KEY_HERE
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ dependencyResolutionManagement {
}

rootProject.name = "ychat-sdk"
include(":ychat")
include(":sample:android")
include(":ychat")
include(":sample:jvm")