본문으로 건너뛰기

Java를 활용한 프리셋 API

이 예제 코드는 LaaS에서 preset API를 활용하는 방법을 설명합니다.

1. 배포된 프리셋 조회

  • 배포된 프리셋의 정보를 조회 할 때 사용
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

public class PresetInfoExample {
private static final RestTemplate restTemplate = new RestTemplate();

public static void requestPresetInfo() {
String project = "YOUR_PROJECT_CODE";
String apiKey = "YOUR_API_KEY";
String hash = "YOURE_PRESET_HASH";

// Set request url
String laasPresetUrl = UriComponentsBuilder
.fromHttpUrl("https://api-laas.wanted.co.kr/api/preset/{hash}")
.buildAndExpand(hash)
.toString();

// Set the headers
HttpHeaders headers = new HttpHeaders();
headers.set("project", project);
headers.set("apiKey", apiKey);

// Create the HTTP Entity with headers
HttpEntity httpEntity = new HttpEntity(headers);

try {
// Make the GET request
ResponseEntity<String--> response = restTemplate.exchange(
laasPresetUrl,
HttpMethod.GET,
httpEntity,
String.class
);

// Print the response or handle it as needed
System.out.println("Response: " + response.getBody());

} catch (Exception e) {
// Handle any exceptions
e.printStackTrace();
}
}
}

2. Chat 호출

  • 배포된 프리셋을 사용하여 LLM의 chat API를 호출

case1) 기본 프리셋 호출

import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

public class PresetChatCompletionExample {

private static final RestTemplate restTemplate = new RestTemplate();

public static void requestChatCompletion() {
String projectCode = "YOUR_PROJECT_CODE";
String apiKey = "YOUR_API_KEY";
String hash = "YOURE_PRESET_HASH";

// Set request url
String laasChatUrl = UriComponentsBuilder
.fromHttpUrl("https://api-laas.wanted.co.kr/api/preset/v2/chat/completions")
.toUriString();

// Set the headers
HttpHeaders headers = new HttpHeaders();
headers.set("project", projectCode);
headers.set("apiKey", apiKey);
headers.set("Content-Type", "application/json; charset=utf-8");

// Set the request
Map<String, Object--> request = Map.of(
"hash", hash // Set the hash
);

// Create the HTTP Entity with headers and body
HttpEntity<map<string, object="">&gt; entity = new HttpEntity&lt;&gt;(request, headers);

try {
// Make the POST request
ResponseEntity<string> response = restTemplate.exchange(
laasChatUrl,
HttpMethod.POST,
entity,
String.class
);

// Print the response or handle it as needed
System.out.println("Response: " + response.getBody());

} catch (Exception e) {
// Handle any exceptions
e.printStackTrace();
}
}
}

위의 코드는 에디터를 다음과 같이 설정 했을 때와 동일하게 처리됩니다.

image-20241105-043122.png

case2) 가변 포함 프리셋 호출

  • 가변값 question원티드랩으로 설정한 예시입니다.
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

public class PresetChatCompletionExample {

private static final RestTemplate restTemplate = new RestTemplate();

public static void requestChatCompletion() {
String projectCode = "YOUR_PROJECT_CODE";
String apiKey = "YOUR_API_KEY";
String hash = "YOURE_PRESET_HASH";

// Set request url
String laasChatUrl = UriComponentsBuilder
.fromHttpUrl("https://api-laas.wanted.co.kr/api/preset/v2/chat/completions")
.toUriString();

// Set the headers
HttpHeaders headers = new HttpHeaders();
headers.set("project", projectCode);
headers.set("apiKey", apiKey);
headers.set("Content-Type", "application/json; charset=utf-8");

// Set the request
Map<String, Object--> request = Map.of(
"hash", hash, // Set the hash
"params", Map.of( // Set the params
"question", "원티드랩"
)
);

// Create the HTTP Entity with headers and body
HttpEntity<map<string, object="">&gt; entity = new HttpEntity&lt;&gt;(request, headers);

try {
// Make the POST request
ResponseEntity<string> response = restTemplate.exchange(
laasChatUrl,
HttpMethod.POST,
entity,
String.class
);

// Print the response or handle it as needed
System.out.println("Response: " + response.getBody());

} catch (Exception e) {
// Handle any exceptions
e.printStackTrace();
}
}
}

API 호출시 프리셋의 가변값이, params에 전달한 값으로 설정되어 호출됩니다.
에디터에서 설정한 값은 API 호출시 사용 되지 않기 때문에 params를 설정하지 않을 경우, 빈 값으로 설정되어 호출됩니다.

image-20241105-050819.png

case3) 추가 메시지를 전달하는 호출

  • 배포된 프리셋에는 시스템 메시지만 설정 되어 있다는 것을 가정하고 있습니다.

  • 배포된 프리셋에 원티드의 서비스를 알려줘 라는 유저 메시지를 추가하는 예시입니다.

import java.util.List;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

public class PresetChatCompletionExample {

private static final RestTemplate restTemplate = new RestTemplate();

public static void requestChatCompletion() {
String projectCode = "YOUR_PROJECT_CODE";
String apiKey = "YOUR_API_KEY";
String hash = "YOURE_PRESET_HASH";

// Set request url
String laasChatUrl = UriComponentsBuilder
.fromHttpUrl("https://api-laas.wanted.co.kr/api/preset/v2/chat/completions")
.toUriString();

// Set the headers
HttpHeaders headers = new HttpHeaders();
headers.set("project", projectCode);
headers.set("apiKey", apiKey);
headers.set("Content-Type", "application/json; charset=utf-8");

// Set the request
Map<String, Object--> request = Map.of(
"hash", hash, // Set the hash
"messages", List.of( // Set the messages
Map.of( // Set the message
"role", "user",
"content", "원티드의 서비스를 알려줘"
)
)
);

// Create the HTTP Entity with headers and body
HttpEntity<map<string, object="">&gt; entity = new HttpEntity&lt;&gt;(request, headers);

try {
// Make the POST request
ResponseEntity<string> response = restTemplate.exchange(
laasChatUrl,
HttpMethod.POST,
entity,
String.class
);

// Print the response or handle it as needed
System.out.println("Response: " + response.getBody());

} catch (Exception e) {
// Handle any exceptions
e.printStackTrace();
}
}
}

프리셋에 포함시키지 않은 메시지를 추가할때 사용됩니다.
다음과 같이 유저 메시지를 추가한 것과 동일합니다.

image-20241105-144518.png

case4) 추가 메시지에 이미지를 포함한 호출

  • 유저 메시지에 이미지를 포함하는 예시입니다.

  • image_url에 image가 저장된 url을 설정한 예시입니다.

import java.util.List;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

public class PresetChatCompletionExample {

private static final RestTemplate restTemplate = new RestTemplate();

public static void createChatCompletion() {
String projectCode = "YOUR_PROJECT_CODE";
String apiKey = "YOUR_API_KEY";
String hash = "YOURE_PRESET_HASH";

// Set request url
String laasChatUrl = UriComponentsBuilder
.fromHttpUrl("https://api-laas.wanted.co.kr/api/preset/v2/chat/completions")
.toUriString();

// Set the headers
HttpHeaders headers = new HttpHeaders();
headers.set("project", projectCode);
headers.set("apiKey", apiKey);
headers.set("Content-Type", "application/json; charset=utf-8");

// Set the request
Map<String, Object--> request = Map.of(
"hash", hash, // Set the hash
"messages", List.of( // Set the messages
Map.of( // Set the message
"role", "user",
"content", List.of( // Set the contents
Map.of( // Set the image content
"type", "image_url",
"image_url" , Map.of(
"url", "https://static.wanted.co.kr/images/wdes/0_4.d217341b.jpg"
)
),
Map.of( // Set the text content
"type", "text",
"text", "해당 이미지의 기업은 어디인지 알려주세요."
)
)
)
)
);

// Create the HTTP Entity with headers and body
HttpEntity<map<string, object="">&gt; entity = new HttpEntity&lt;&gt;(request, headers);

try {
// Make the POST request
ResponseEntity<string> response = restTemplate.exchange(
laasChatUrl,
HttpMethod.POST,
entity,
String.class
);

// Print the response or handle it as needed
System.out.println("Response: " + response.getBody());

} catch (Exception e) {
// Handle any exceptions
e.printStackTrace();
}
}
}

유저 메시지에 이미지를 추가할때 사용됩니다.
다음과 같이 유저 메시지를 추가한 것과 동일합니다.

image-20241105-152558.png

  • image_url에 base64 인코딩 이미지 데이터를 설정한 예시입니다.

  • base64 데이터는 data URLs 포맷으로 구성되어야 합니다.

import java.util.List;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

public class PresetChatCompletionExample {

private static final RestTemplate restTemplate = new RestTemplate();

public static void createChatCompletion() {
String projectCode = "YOUR_PROJECT_CODE";
String apiKey = "YOUR_API_KEY";
String hash = "YOURE_PRESET_HASH";

// Set request url
String laasChatUrl = UriComponentsBuilder
.fromHttpUrl("https://api-laas.wanted.co.kr/api/preset/v2/chat/completions")
.toUriString();

// Set the headers
HttpHeaders headers = new HttpHeaders();
headers.set("project", projectCode);
headers.set("apiKey", apiKey);
headers.set("Content-Type", "application/json; charset=utf-8");

// Set the request
Map<String, Object--> request = Map.of(
"hash", hash, // Set the hash
"messages", List.of( // Set the messages
Map.of( // Set the message
"role", "user",
"content", List.of( // Set the contents
Map.of( // Set the image content
"type", "image_url",
"image_url" , Map.of(
"url", "data:image/jpeg;base64,{base64_jpeg_image}"
)
),
Map.of( // Set the text content
"type", "text",
"text", "해당 이미지의 기업은 어디인지 알려주세요."
)
)
)
)
);

// Create the HTTP Entity with headers and body
HttpEntity<map<string, object="">&gt; entity = new HttpEntity&lt;&gt;(request, headers);

try {
// Make the POST request
ResponseEntity<string> response = restTemplate.exchange(
laasChatUrl,
HttpMethod.POST,
entity,
String.class
);

// Print the response or handle it as needed
System.out.println("Response: " + response.getBody());

} catch (Exception e) {
// Handle any exceptions
e.printStackTrace();
}
}
}

이미지 사용시 꼭 확인해주세요!

지원 이미지 형식

  • jpeg (image/jpeg)

  • png (image/png)

권장 이미지 크기

  • 가로/세로 중 더 긴 면 기준 1568 이하

이미지당 용량

  • 4MB 이하

메시지 당 이미지 개수

  • 최대 3개


case5) 추가 메시지에 PDF 문서를 포함한 호출

  • pdf 문서는 base64 인코딩된 data URLs만 지원합니다.

  • 프리셋에 이미 pdf 문서가 포함된 경우 호출이 제한됩니다.

  • document_url에 base64 인코딩 PDF 데이터를 설정한 예시입니다.

import java.util.List;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

public class PresetChatCompletionExample {

private static final RestTemplate restTemplate = new RestTemplate();

public static void createChatCompletion() {
String projectCode = "YOUR_PROJECT_CODE";
String apiKey = "YOUR_API_KEY";
String hash = "YOURE_PRESET_HASH";

// Set request url
String laasChatUrl = UriComponentsBuilder
.fromHttpUrl("https://api-laas.wanted.co.kr/api/preset/v2/chat/completions")
.toUriString();

// Set the headers
HttpHeaders headers = new HttpHeaders();
headers.set("project", projectCode);
headers.set("apiKey", apiKey);
headers.set("Content-Type", "application/json; charset=utf-8");

// Set the request
Map<String, Object--> request = Map.of(
"hash", hash, // Set the hash
"messages", List.of( // Set the messages
Map.of( // Set the message
"role", "user",
"content", List.of( // Set the contents
Map.of( // Set the pdf content
"type", "document",
"document_url" , Map.of(
"url", "data:application/pdf;base64,{base64_pdf_data}"
)
),
Map.of( // Set the text content
"type", "text",
"text", "이 문서의 내용을 요약해주세요"
)
)
)
)
);

// Create the HTTP Entity with headers and body
HttpEntity<map<string, object="">&gt; entity = new HttpEntity&lt;&gt;(request, headers);

try {
// Make the POST request
ResponseEntity<string> response = restTemplate.exchange(
laasChatUrl,
HttpMethod.POST,
entity,
String.class
);

// Print the response or handle it as needed
System.out.println("Response: " + response.getBody());

} catch (Exception e) {
// Handle any exceptions
e.printStackTrace();
}
}
}

PDF 사용시 꼭 확인해주세요!

최대 용량

  • 32MB 이하

페이지 수

  • 20장 이하

응답 Response

호출된 API는 해당 LLM에서 전달해주는 response와 동일하게 전달해줍니다.

{
"id": "string",
"object": "string",
"created": 0,
"model": "string",
"choices":
[
{
"index": 0,
"message": {
"role": "string",
"content": "string"
},
"finish_reason": "string"
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
}
}