Skip to content

Commit

Permalink
custom openai model and host
Browse files Browse the repository at this point in the history
  • Loading branch information
kukume committed Sep 24, 2024
1 parent 66a4fd2 commit 651fbbf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/me/kuku/telegram/entity/BotConfigEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class BotConfigEntity {
var updatePush: Status = Status.OFF

var openaiToken: String = ""
var openaiUrl: String = ""
var openaiModel: String = ""

fun twoCaptchaKey() = twoCaptchaKey.ifEmpty { null }
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/me/kuku/telegram/extension/OpenaiExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.kuku.telegram.extension
import com.aallam.openai.api.chat.*
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import com.aallam.openai.client.OpenAIHost
import com.pengrad.telegrambot.model.PhotoSize
import com.pengrad.telegrambot.model.request.ParseMode
import com.pengrad.telegrambot.request.EditMessageText
Expand Down Expand Up @@ -57,8 +58,10 @@ class OpenaiExtension(

val botConfigEntity = botConfigService.find()
if (botConfigEntity.openaiToken.ifEmpty { "" }.isEmpty()) error("not setting openai token")
val openaiHost = if (botConfigEntity.openaiUrl.isEmpty()) OpenAIHost.OpenAI else OpenAIHost(botConfigEntity.openaiUrl)
val openaiModel = botConfigEntity.openaiModel.ifEmpty { "gpt-4o-mini" }

val openai = OpenAI(botConfigEntity.openaiToken)
val openai = OpenAI(botConfigEntity.openaiToken, host = openaiHost)

val chatMessage = ChatMessage(
role = ChatRole.User,
Expand All @@ -73,7 +76,7 @@ class OpenaiExtension(
cacheBody.add(chatMessage)

val request = ChatCompletionRequest(
model = ModelId("gpt-4o-mini"),
model = ModelId(openaiModel),
messages = cacheBody,
streamOptions = streamOptions {
includeUsage = true
Expand All @@ -85,7 +88,7 @@ class OpenaiExtension(
val sendMessageObject = response.message()
val sendMessageId = sendMessageObject.messageId()
var openaiText = ""
var prefix = ">model: gpt\\-4o\\-mini\n"
var prefix = ">model: ${openaiModel.replace("-", "\\-")}\n"
var alreadySendText = ""
var i = 5
openai.chatCompletions(request).onEach {
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/me/kuku/telegram/extension/PushExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ class PushBody {
}

enum class Type {
TEXT, IMAGE, VIDEO, FILE
}
TEXT, IMAGE, VIDEO, FILE }


}
20 changes: 19 additions & 1 deletion src/main/kotlin/me/kuku/telegram/extension/SettingExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ class SettingExtension(
val clearLog = inlineKeyboardButton("清空日志", "settingsClearLog")
val updatePush = inlineKeyboardButton("${init().updatePush}github更新推送", "githubUpdatePushSwitch")
val openai = inlineKeyboardButton("设置openai的token", "openaiToken")
val openaiUrl = inlineKeyboardButton("设置openai的url", "openaiUrl")
val openaiModel = inlineKeyboardButton("设置openai的model", "openaiModel")
return InlineKeyboardMarkup(
arrayOf(blackSetting, adminSetting),
arrayOf(url),
arrayOf(twoCaptcha),
arrayOf(sendLog, clearLog),
arrayOf(updatePush),
arrayOf(openai)
arrayOf(openai, openaiUrl, openaiModel)
)
}

Expand Down Expand Up @@ -157,6 +159,22 @@ class SettingExtension(
botConfigService.save(entity)
editMessageText("设置openai的token成功")
}
callback("openaiUrl") {
editMessageText("请发送openai的url")
val url = nextMessage().text()
val entity = init()
entity.openaiUrl = url
botConfigService.save(entity)
editMessageText("设置openai的url成功")
}
callback("openaiModel") {
editMessageText("请发送openai的model")
val model = nextMessage().text()
val entity = init()
entity.openaiModel = model
botConfigService.save(entity)
editMessageText("设置openai的model成功")
}
}

fun TelegramSubscribe.log() {
Expand Down

0 comments on commit 651fbbf

Please sign in to comment.