Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHAT] Code improvement in ChatGPTProvider #4554

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ package org.apache.kyuubi.engine.chat.provider
import java.util
import java.util.concurrent.TimeUnit

import scala.collection.JavaConverters._

import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache}
import org.apache.http.{HttpHost, HttpStatus}
import org.apache.http.{HttpHeaders, HttpHost, HttpStatus}
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.{ContentType, StringEntity}
import org.apache.http.impl.client.{CloseableHttpClient, HttpClientBuilder}
import org.apache.http.message.BasicHeader
import org.apache.http.util.EntityUtils

import org.apache.kyuubi.config.KyuubiConf
Expand All @@ -39,11 +42,16 @@ class ChatGPTProvider(conf: KyuubiConf) extends ChatProvider {
s"which could be got at https://platform.openai.com/account/api-keys")
}

private val httpClient: CloseableHttpClient = HttpClientBuilder.create().build()
private val httpClient: CloseableHttpClient = {
HttpClientBuilder.create()
.setDefaultHeaders(List(
new BasicHeader(HttpHeaders.AUTHORIZATION, s"Bearer $gptApiKey")).asJava)
.build()
}

private val requestConfig = {
val connectTimeout = conf.get(KyuubiConf.ENGINE_CHAT_GPT_HTTP_CONNECT_TIMEOUT).asInstanceOf[Int]
val socketTimeout = conf.get(KyuubiConf.ENGINE_CHAT_GPT_HTTP_SOCKET_TIMEOUT).asInstanceOf[Int]
private val requestConfig: RequestConfig = {
val connectTimeout = conf.get(KyuubiConf.ENGINE_CHAT_GPT_HTTP_CONNECT_TIMEOUT).intValue()
val socketTimeout = conf.get(KyuubiConf.ENGINE_CHAT_GPT_HTTP_SOCKET_TIMEOUT).intValue()
val builder: RequestConfig.Builder = RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
Expand All @@ -70,8 +78,6 @@ class ChatGPTProvider(conf: KyuubiConf) extends ChatProvider {
messages.addLast(Message("user", q))

val request = new HttpPost("https://api.openai.com/v1/chat/completions")
request.addHeader("Authorization", "Bearer " + gptApiKey)

val req = Map(
"messages" -> messages,
"model" -> "gpt-3.5-turbo",
Expand All @@ -81,17 +87,17 @@ class ChatGPTProvider(conf: KyuubiConf) extends ChatProvider {
val entity = new StringEntity(mapper.writeValueAsString(req), ContentType.APPLICATION_JSON)
request.setEntity(entity)
request.setConfig(requestConfig)
val responseEntity = httpClient.execute(request)
val respJson = mapper.readTree(EntityUtils.toString(responseEntity.getEntity))
val statusCode = responseEntity.getStatusLine.getStatusCode
if (responseEntity.getStatusLine.getStatusCode == HttpStatus.SC_OK) {
val replyMessage = mapper.treeToValue[Message](
respJson.get("choices").get(0).get("message"))
messages.addLast(replyMessage)
replyMessage.content
} else {
messages.removeLast()
s"Chat failed. Status: $statusCode. ${respJson.get("error").get("message").asText}"
val response = httpClient.execute(request)
val respJson = mapper.readTree(EntityUtils.toString(response.getEntity))
response.getStatusLine.getStatusCode match {
case HttpStatus.SC_OK =>
val replyMessage = mapper.treeToValue[Message](
respJson.get("choices").get(0).get("message"))
messages.addLast(replyMessage)
replyMessage.content
case errorStatusCode =>
messages.removeLast()
s"Chat failed. Status: $errorStatusCode. ${respJson.get("error").get("message").asText}"
}
}

Expand Down