Skip to content

Commit

Permalink
feat(counit): align to new APIs datastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 25, 2023
1 parent 8578bda commit 1150f54
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
30 changes: 18 additions & 12 deletions src/main/kotlin/cc/unitmesh/devti/counit/CoUnitPreProcessor.kt
Expand Up @@ -2,7 +2,7 @@ package cc.unitmesh.devti.counit

import cc.unitmesh.devti.LLMCoroutineScope
import cc.unitmesh.devti.counit.dto.ExplainQuery
import cc.unitmesh.devti.counit.dto.QueryResponse
import cc.unitmesh.devti.counit.dto.QueryResult
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
import cc.unitmesh.devti.gui.chat.ChatContext
import cc.unitmesh.devti.gui.chat.ChatRole
Expand Down Expand Up @@ -69,7 +69,7 @@ class CoUnitPreProcessor(val project: Project) {
llmProvider.appendLocalMessage(searchTip, ChatRole.User)
ui.addMessage(searchTip, true, searchTip)

val queryResult = coUnitPromptGenerator.queryTool(explain.query, explain.hypotheticalDocument)
val queryResult = coUnitPromptGenerator.semanticQuery(explain)

val related = buildDocAsContext(queryResult)
llmProvider.appendLocalMessage(related, ChatRole.User)
Expand All @@ -80,24 +80,30 @@ class CoUnitPreProcessor(val project: Project) {
}
}

private fun buildDocAsContext(queryResult: Pair<QueryResponse?, QueryResponse?>): String {
private fun buildDocAsContext(queryResult: QueryResult): String {
val sb = StringBuilder()
val normalDoc = queryResult.first
if (normalDoc != null && normalDoc.data.isNotEmpty()) {
val normalDoc = queryResult.normalQuery
if (normalDoc.isNotEmpty()) {
sb.append("here is related API to origin query's result: \n```markdown\n")
sb.append(Json.encodeToString(normalDoc.data[0].displayText))
sb.append(Json.encodeToString(normalDoc[0].displayText))
sb.append("\n```\n")
}

val hypoDoc = queryResult.second
if (hypoDoc != null && hypoDoc.data.isNotEmpty()) {
sb.append("here is hypothetical document which related to origin query's result: \n```markdown\n")
sb.append(Json.encodeToString(hypoDoc.data[0].displayText))
val nature = queryResult.natureLangQuery
if (nature.isNotEmpty()) {
sb.append("here is nature language query's result: \n```markdown\n")
sb.append(Json.encodeToString(nature[0].displayText))
sb.append("\n```\n")
}

val related = sb.toString()
return related
val hyde = queryResult.hypotheticalDocument
if (hyde.isNotEmpty()) {
sb.append("here is hypothetical document's result: \n```markdown\n")
sb.append(Json.encodeToString(hyde[0].displayText))
sb.append("\n```\n")
}

return sb.toString()
}

private fun fix(result: String): String {
Expand Down
15 changes: 11 additions & 4 deletions src/main/kotlin/cc/unitmesh/devti/counit/CoUnitPromptGenerator.kt
@@ -1,8 +1,10 @@
package cc.unitmesh.devti.counit

import cc.unitmesh.devti.counit.client.CoUnitApi
import cc.unitmesh.devti.counit.dto.ExplainQuery
import cc.unitmesh.devti.counit.dto.PayloadType
import cc.unitmesh.devti.counit.dto.QueryResponse
import cc.unitmesh.devti.counit.dto.QueryResult
import cc.unitmesh.devti.settings.configurable.coUnitSettings
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
Expand All @@ -23,10 +25,15 @@ class CoUnitPromptGenerator(val project: Project) {
return body?.prompt
}

fun queryTool(query: String, hypotheticalDocument: String): Pair<QueryResponse?, QueryResponse?> {
val normalQuery: QueryResponse? = service.query(query, PayloadType.OpenApi).execute().body()
val hydeDoc: QueryResponse? = service.query(hypotheticalDocument, PayloadType.OpenApi).execute().body()
fun semanticQuery(query: ExplainQuery): QueryResult {
val normalQuery: QueryResponse? = service.query(query.query, PayloadType.OpenApi).execute().body()
val hydeDoc: QueryResponse? = service.query(query.hypotheticalDocument, PayloadType.OpenApi).execute().body()
val natureLangQuery: QueryResponse? = service.query(query.natureLangQuery, PayloadType.OpenApi).execute().body()

return Pair(normalQuery, hydeDoc)
return QueryResult(
normalQuery?.data ?: emptyList(),
natureLangQuery?.data ?: emptyList(),
hydeDoc?.data ?: emptyList()
)
}
}
13 changes: 10 additions & 3 deletions src/main/kotlin/cc/unitmesh/devti/counit/dto/ExplainQuery.kt
@@ -1,17 +1,24 @@
package cc.unitmesh.devti.counit.dto

import kotlinx.serialization.SerialName
import cc.unitmesh.devti.counit.model.CodePayload
import kotlinx.serialization.Serializable

@Serializable
data class ExplainQuery(
val domain: String,
val query: String,
val hypotheticalDocument: String
val natureLangQuery: String,
val hypotheticalDocument: String,
)

class QueryResult(
val normalQuery: List<CodePayload>,
val natureLangQuery: List<CodePayload>,
val hypotheticalDocument: List<CodePayload>,
)

@Serializable
data class QAExample(
val question: String,
val answer: ExplainQuery
val answer: ExplainQuery,
)

0 comments on commit 1150f54

Please sign in to comment.