본문으로 건너뛰기

Make the first API call

LLM API 호출 순서와 API로 구현할 수 있는 시나리오 예제를 설명합니다.

API 호출 순서

Initialize SDK

LLM SDK를 시작할 때 initialize API를 호출하여 LLM SDK를 초기화합니다.

val llm: LLM = LLM(context)
llm.initialize()

Register App

registerApp API를 호출하여 앱을 LLM 서버에 등록합니다.

val clientID = "your_client_id"
val clientSecret = "your_client_secret"
llm.registerApp(clientID, clientSecret, object : OnServerConnectionListener {
override fun onConnected() {
// LLM 서버에 등록이 완료되면 앱은 이벤트를 받을 수 있습니다.
}

override fun onFailed(msg: String) {
// LLM 서버 등록 실패가 일어나면 에러 메시지와 함께 이벤트를 앱으로 전달 합니다.
}
})

Set Model Parameter

setModelParameter API를 호출하여 LLM 모델 종류 및 설정 값을 변경할 수 있습니다.

// 앱에서 설정하지 않으면 기본 모델 설정 값으로 사용 됩니다.
// temperature : 0.7, token : 512, model : gpt-4o
// the range of temperature : from 0.0 to 2.0
// the range of token : from 1 to 1024
// the type of model : chatbaker-16k, gpt-4o-mini, gpt-4o

val temperature = "0.8"
val maxTokens = "1024"
val newModelName = "gpt-4o-mini"

llm.setModelParameter(ModelParameter.TEMPERATURE, temperature)
llm.setModelParameter(ModelParameter.MAX_TOKENS, maxTokens)
llm.setModelParameter(ModelParameter.MODEL_NAME, newModelName)

Generate Content

generateContent API를 호출하여 LLM 앱에서 입력받은 프롬프트 데이터를 LLM 서비스로 전달합니다. 이때 리스너를 같이 전달하며, LLM 서비스는 부분 응답 및 전체 응답을 생성하여 리스너를 통해 앱에 전달합니다.

llm.generateContent(prompt = prompt, object: ResultListener {
override fun onResponse(response: String, completed: Boolean) {
println("onResponse response: $response, complete: $completed")
}

override fun onError(reason: String) {
println("onError reason: $reason")
}
})

Start Chat

startChat API를 호출하여 대화를 시작할 수 있습니다. 이전 대화 이력을 입력하여 연속적인 대화를 할 수 있습니다.

val initialHistory = listOf(
LLMContent(role = "user", parts = mutableListOf(TextPart("안녕"))),
LLMContent("assistant", mutableListOf(TextPart("안녕하세요. 저는 LLM 입니다.")))
)

llm.startChat(history = initialHistory, { /** onSuccess */}, { /** onFailed */ println(it) })

Send Message

sendMessage API를 호출하여 LLM 앱에서 LLM 서비스로 메시지 스트림을 전달합니다. 이 API는 대화 및 채팅에 사용합니다.

val contents = LLMContent("user", listOf(TextPart(text = "서울 날씨 알려줘")))
llm.sendMessage(contents, object: ResultListener {
override fun onResponse(response: String, completed: Boolean) {
println("onResponse response: $response, complete: $completed")
}

override fun onError(reason: String) {
println("onError reason: $reason")
}
})

Release SDK

release API를 호출하여 LLM SDK 리소스를 해제합니다.

llm.release()