Skip to content

Commit 54761b8

Browse files
committed
refactor(counit): refactor code to use new QAExample DTO and remove unnecessary dependencies on CoUnitApi #51
1 parent 8e2b15f commit 54761b8

File tree

7 files changed

+13
-139
lines changed

7 files changed

+13
-139
lines changed

src/main/kotlin/cc/unitmesh/devti/counit/CoUnitPreProcessor.kt

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package cc.unitmesh.devti.counit
22

33
import cc.unitmesh.devti.util.LLMCoroutineScope
4-
import cc.unitmesh.devti.counit.dto.ExplainQuery
5-
import cc.unitmesh.devti.counit.dto.QueryResult
64
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
75
import cc.unitmesh.devti.gui.chat.ChatContext
86
import cc.unitmesh.devti.gui.chat.ChatRole
@@ -15,7 +13,6 @@ import com.intellij.openapi.diagnostic.logger
1513
import com.intellij.openapi.project.Project
1614
import kotlinx.coroutines.launch
1715
// keep this import
18-
import kotlinx.serialization.decodeFromString
1916
import kotlinx.serialization.json.Json
2017

2118
const val CO_UNIT = "/counit"
@@ -56,20 +53,11 @@ class CoUnitPreProcessor(val project: Project) {
5653

5754
llmProvider.appendLocalMessage(result, ChatRole.Assistant)
5855

59-
val explain = try {
60-
json.decodeFromString<ExplainQuery>(extractJsonResponse(result))
61-
} catch (e: Exception) {
62-
LOG.error("parse result error: $e")
63-
return@launch
64-
}
65-
6656
val searchTip = "search API by query and hypothetical document"
6757
llmProvider.appendLocalMessage(searchTip, ChatRole.User)
6858
ui.addMessage(searchTip, true, searchTip)
6959

70-
val queryResult = coUnitPromptGenerator.semanticQuery(explain)
71-
72-
val related = buildDocAsContext(queryResult)
60+
val related = coUnitPromptGenerator.semanticQuery("") ?: ""
7361
if (related.isEmpty()) {
7462
val noResultTip = "no related API found"
7563
llmProvider.appendLocalMessage(noResultTip, ChatRole.Assistant)
@@ -85,32 +73,6 @@ class CoUnitPreProcessor(val project: Project) {
8573
}
8674
}
8775

88-
private fun buildDocAsContext(queryResult: QueryResult): String {
89-
val sb = StringBuilder()
90-
val normalDoc = queryResult.englishQuery
91-
if (normalDoc.isNotEmpty()) {
92-
sb.append("here is related API to origin query's result: \n```markdown\n")
93-
sb.append(normalDoc[0].displayText)
94-
sb.append("\n```\n")
95-
}
96-
97-
val nature = queryResult.naturalLangQuery
98-
if (nature.isNotEmpty()) {
99-
sb.append("here is natural language query's result: \n```markdown\n")
100-
sb.append(nature[0].displayText)
101-
sb.append("\n```\n")
102-
}
103-
104-
val hyde = queryResult.hypotheticalDocument
105-
if (hyde.isNotEmpty()) {
106-
sb.append("here is hypothetical document's result: \n```markdown\n")
107-
sb.append(hyde[0].displayText)
108-
sb.append("\n```\n")
109-
}
110-
111-
return sb.toString()
112-
}
113-
11476
/**
11577
* This method is used to extract JSON response from a given string.
11678
* It removes the leading and trailing ````json` tags from the string,
Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,15 @@
11
package cc.unitmesh.devti.counit
22

3-
import cc.unitmesh.devti.counit.client.CoUnitApi
4-
import cc.unitmesh.devti.counit.dto.ExplainQuery
5-
import cc.unitmesh.devti.counit.dto.PayloadType
6-
import cc.unitmesh.devti.counit.dto.QueryResponse
7-
import cc.unitmesh.devti.counit.dto.QueryResult
8-
import cc.unitmesh.devti.counit.configurable.customRagSettings
93
import com.intellij.openapi.components.Service
104
import com.intellij.openapi.project.Project
11-
import retrofit2.Retrofit
12-
import retrofit2.converter.gson.GsonConverterFactory
135

146
@Service(Service.Level.PROJECT)
157
class CoUnitPromptGenerator(val project: Project) {
168
fun findIntention(input: String): String? {
17-
val service: CoUnitApi = coUnitApi()
18-
val body = service.explainQuery(input).execute().body()
19-
return body?.prompt
9+
return null
2010
}
2111

22-
fun semanticQuery(query: ExplainQuery): QueryResult {
23-
val service: CoUnitApi = coUnitApi()
24-
val englishQuery: QueryResponse? = service.query(query.query, PayloadType.OpenApi).execute().body()
25-
val hydeDoc: QueryResponse? = service.query(query.hypotheticalDocument, PayloadType.OpenApi).execute().body()
26-
val naturalLangQuery: QueryResponse? = service.query(query.natureLangQuery, PayloadType.OpenApi).execute().body()
27-
28-
return QueryResult(
29-
englishQuery?.data ?: emptyList(),
30-
naturalLangQuery?.data ?: emptyList(),
31-
hydeDoc?.data ?: emptyList()
32-
)
33-
}
34-
35-
private fun coUnitApi(): CoUnitApi {
36-
val retrofit = Retrofit.Builder()
37-
.baseUrl(project.customRagSettings.serverAddress)
38-
.addConverterFactory(GsonConverterFactory.create())
39-
.build()
40-
41-
val service: CoUnitApi = retrofit.create(CoUnitApi::class.java)
42-
return service
12+
fun semanticQuery(query: String): String? {
13+
return null
4314
}
4415
}

src/main/kotlin/cc/unitmesh/devti/counit/client/CoUnitApi.kt

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/main/kotlin/cc/unitmesh/devti/counit/dto/ExplainQuery.kt

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/kotlin/cc/unitmesh/devti/counit/dto/PromptResult.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cc.unitmesh.devti.counit.dto
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class QAExample(
7+
val question: String,
8+
val answer: String,
9+
)

src/main/kotlin/cc/unitmesh/devti/counit/dto/QueryResponse.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)