@@ -4,14 +4,16 @@ import cc.unitmesh.devti.connector.CodeCopilot
4
4
import cc.unitmesh.devti.settings.DevtiSettingsState
5
5
import kotlinx.serialization.decodeFromString
6
6
import kotlinx.serialization.json.Json
7
+ import okhttp3.OkHttpClient
8
+ import okhttp3.Request
7
9
8
10
9
11
class CustomConnector : CodeCopilot {
10
- val devtiSettingsState = DevtiSettingsState .getInstance()
11
-
12
- val url = devtiSettingsState?.customEngineServer ? : " "
13
- val key = devtiSettingsState?.customEngineToken ? : " "
14
- var promptConfig : PromptConfig ? = null
12
+ private val devtiSettingsState = DevtiSettingsState .getInstance()
13
+ private val url = devtiSettingsState?.customEngineServer ? : " "
14
+ private val key = devtiSettingsState?.customEngineToken ? : " "
15
+ private var promptConfig : PromptConfig ? = null
16
+ private var client = OkHttpClient ()
15
17
16
18
init {
17
19
val prompts = devtiSettingsState?.customEnginePrompts
@@ -33,13 +35,30 @@ class CustomConnector : CodeCopilot {
33
35
}
34
36
}
35
37
36
- private fun prompt (instruction : String , input : String ): String {
37
- // val retrofit = Retrofit.Builder()
38
- // .baseUrl(url)
39
- // .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
40
- // .build()
38
+ fun prompt (instruction : String , input : String ): String {
39
+ val body = okhttp3.RequestBody .create(
40
+ okhttp3.MediaType .parse(" application/json; charset=utf-8" ),
41
+ """
42
+ {
43
+ "instruction": "$instruction ",
44
+ "input": "$input ",
45
+ }
46
+ """ .trimIndent()
47
+ )
48
+
49
+ val builder = Request .Builder ()
50
+ if (key.isNotEmpty()) {
51
+ builder.addHeader(" Authorization" , " Bearer $key " )
52
+ }
53
+
54
+ val request = builder
55
+ .url(url)
56
+ .post(body)
57
+ .build()
58
+
59
+ val response = client.newCall(request).execute()
41
60
42
- return " "
61
+ return response.body()?.string() ? : " "
43
62
}
44
63
45
64
override fun codeCompleteFor (text : String , className : String ): String {
0 commit comments