Skip to content

Commit 90a1cd1

Browse files
committed
fix(qianfan): api and secret keys are not properly saved
1 parent cbcb5c0 commit 90a1cd1

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/clients/qianfan/QianfanClientConfiguration.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.intellij.openapi.vcs.ui.CommitMessage
77
import com.intellij.util.xmlb.annotations.Attribute
88
import com.intellij.util.xmlb.annotations.Transient
99
import dev.langchain4j.model.qianfan.QianfanChatModelNameEnum
10-
import java.util.*
1110
import javax.swing.Icon
1211

1312
class QianfanClientConfiguration : LLMClientConfiguration(
@@ -23,16 +22,9 @@ class QianfanClientConfiguration : LLMClientConfiguration(
2322
var secretKeyIsStored: Boolean = false
2423
@Transient
2524
var apiKey: String = ""
26-
2725
@Transient
2826
var secretKey: String = ""
2927

30-
@Attribute
31-
var apiKeyId: String = UUID.randomUUID().toString()
32-
33-
@Attribute
34-
var secretKeyId: String = UUID.randomUUID().toString()
35-
3628
companion object {
3729
const val CLIENT_NAME = "Qianfan"
3830
}
@@ -61,8 +53,6 @@ class QianfanClientConfiguration : LLMClientConfiguration(
6153
copy.id = id
6254
copy.apiKey = apiKey
6355
copy.secretKey = secretKey
64-
copy.secretKeyId = secretKeyId
65-
copy.apiKeyId = apiKeyId
6656
copy.name = name
6757
copy.host = host
6858
copy.modelId = modelId

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/clients/qianfan/QianfanClientPanel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class QianfanClientPanel(private val clientConfiguration: QianfanClientConfigura
2424
.bindText(getter = {""}, setter = {
2525
QianfanClientService.getInstance().saveApiKey(clientConfiguration, it)
2626
})
27-
.emptyText(if (clientConfiguration.apiKeyIsStored) message("settings.openAI.token.stored") else "")
27+
.emptyText(if (clientConfiguration.apiKeyIsStored) message("settings.openAI.token.stored") else "JzRxxxxxxxxxxxxxxxxxxxxx")
2828
.resizableColumn()
2929
.widthGroup("input")
3030
}
@@ -35,7 +35,7 @@ class QianfanClientPanel(private val clientConfiguration: QianfanClientConfigura
3535
.bindText(getter = {""}, setter = {
3636
QianfanClientService.getInstance().saveSecretKey(clientConfiguration, it)
3737
})
38-
.emptyText(if (clientConfiguration.secretKeyIsStored) message("settings.openAI.token.stored") else "")
38+
.emptyText(if (clientConfiguration.secretKeyIsStored) message("settings.openAI.token.stored") else "kSlxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
3939
.resizableColumn()
4040
.widthGroup("input")
4141
}

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/clients/qianfan/QianfanClientService.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class QianfanClientService(private val cs: CoroutineScope) : LLMClientService<Qi
2525
}
2626

2727
override suspend fun buildChatModel(client: QianfanClientConfiguration): ChatLanguageModel {
28-
val apiKey = client.apiKey.nullize(true) ?: retrieveToken(client.apiKeyId)?.toString(true)
29-
val secretKey = client.secretKey.nullize(true) ?: retrieveToken(client.secretKeyId)?.toString(true)
28+
val apiKey = client.apiKey.nullize(true) ?: retrieveToken(client.id + "apiKey")?.toString(true)
29+
val secretKey = client.secretKey.nullize(true) ?: retrieveToken(client.id + "secretKey")?.toString(true)
3030

3131
val builder = QianfanChatModel.builder()
3232
.baseUrl(client.host)
@@ -35,28 +35,32 @@ class QianfanClientService(private val cs: CoroutineScope) : LLMClientService<Qi
3535
.modelName(client.modelId)
3636
.temperature(client.temperature.toDouble())
3737
// Fix https://github.com/langchain4j/langchain4j/pull/1426. Remove this 'if' statement when langchain4j releases a new version that resolves this issue.
38-
if (client.modelId == QianfanChatModelNameEnum.ERNIE_SPEED_128K.modelName){
38+
if (client.modelId == QianfanChatModelNameEnum.ERNIE_SPEED_128K.modelName) {
3939
builder.endpoint("ernie-speed-128k")
4040
}
4141

4242
return builder.build()
4343
}
4444

45-
private fun saveToken(token: String, title: String) {
45+
fun saveApiKey(client: QianfanClientConfiguration, key: String) {
4646
cs.launch(Dispatchers.Default) {
4747
try {
48-
PasswordSafe.instance.setPassword(getCredentialAttributes(title), token)
48+
PasswordSafe.instance.setPassword(getCredentialAttributes(client.id + "apiKey"), key)
49+
client.apiKeyIsStored = true
4950
} catch (e: Exception) {
5051
sendNotification(Notification.unableToSaveToken(e.message))
5152
}
5253
}
5354
}
54-
fun saveApiKey(client: QianfanClientConfiguration, apiKey: String) {
55-
saveToken(apiKey, client.apiKeyId)
56-
client.apiKeyIsStored = true
57-
}
58-
fun saveSecretKey(client: QianfanClientConfiguration, secretKey: String) {
59-
saveToken(secretKey, client.secretKeyId)
60-
client.secretKeyIsStored = true
55+
56+
fun saveSecretKey(client: QianfanClientConfiguration, key: String) {
57+
cs.launch(Dispatchers.Default) {
58+
try {
59+
PasswordSafe.instance.setPassword(getCredentialAttributes(client.id + "secretKey"), key)
60+
client.secretKeyIsStored = true
61+
} catch (e: Exception) {
62+
sendNotification(Notification.unableToSaveToken(e.message))
63+
}
64+
}
6165
}
6266
}

0 commit comments

Comments
 (0)