-
Notifications
You must be signed in to change notification settings - Fork 26
[LLM] Refactor LLMWithFeedbackCycle and RequestManager #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanielRendox
wants to merge
22
commits into
development
Choose a base branch
from
danielrendox/refactor/request_manager
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6aee4de
Remove Generic Error type for Result
DanielRendox 45f1eb3
Introduce Coroutines to RequestManager
DanielRendox 771551d
Debug all models and make some adjustments
DanielRendox d55b77e
Migrate to kotlinx-serialization and make code more readable
DanielRendox 3a61215
Fix bug with ChatHistory
DanielRendox 60cf966
Refactor LLMWithFeedbackCycle.kt
DanielRendox aab1dee
Handle process cancellation gracefully
DanielRendox d1effc8
Fix minor issues
DanielRendox 0e0099f
Add custom exception for test saving failure
DanielRendox 4092589
Fix LLM bugs
DanielRendox 93264f1
Remove kotlinVersion from gradle.properties
DanielRendox 744f71b
Fix ktlint issues
DanielRendox bd10ece
reimplement project for testing creation in the SettingsArgumentsLlmE…
arksap2002 deb362d
refactor: replace direct service calls with Mockito mocks in settings…
arksap2002 17bfc00
refactor: optimize imports in settings tests and remove redundant re-…
arksap2002 c8f5bf7
refactor: improve error handling and response assembling logic in col…
arksap2002 2824f3b
refactor: improve error handling and response assembling logic in col…
arksap2002 2c794ce
Repair GeminiRequestManagerTest after RequestManager class changes
DanielRendox df59e10
Remove leftover generatedTestSuite lateinit var from LLMWithFeedbackC…
DanielRendox 1f1179e
Fix ktlint issues
DanielRendox 4611aa1
Clear testsAssembler before each feedback iteration
vsantele 0b23670
Resolve code review comments
DanielRendox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 10 additions & 9 deletions
19
core/src/main/kotlin/org/jetbrains/research/testspark/core/data/ChatMessage.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,20 @@ | ||
| package org.jetbrains.research.testspark.core.data | ||
|
|
||
| open class ChatMessage protected constructor( | ||
| data class ChatMessage( | ||
| val role: ChatRole, | ||
| val content: String, | ||
| val contentBuilder: StringBuilder, | ||
| ) { | ||
| enum class ChatRole { | ||
| User, | ||
| Assistant, | ||
| } | ||
| } | ||
|
|
||
| class ChatUserMessage( | ||
| content: String, | ||
| ) : ChatMessage(ChatRole.User, content) | ||
| val content: String | ||
| get() = contentBuilder.toString() | ||
|
|
||
| companion object { | ||
| fun createUserMessage(message: String) = ChatMessage(ChatRole.User, StringBuilder(message)) | ||
|
|
||
| class ChatAssistantMessage( | ||
| content: String, | ||
| ) : ChatMessage(ChatRole.Assistant, content) | ||
| fun createAssistantMessage(message: String) = ChatMessage(ChatRole.Assistant, StringBuilder(message)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions
14
core/src/main/kotlin/org/jetbrains/research/testspark/core/error/Result.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...rc/main/kotlin/org/jetbrains/research/testspark/core/generation/llm/ChatSessionManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package org.jetbrains.research.testspark.core.generation.llm | ||
|
|
||
| import io.github.oshai.kotlinlogging.KotlinLogging | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.flow.onEach | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.sync.withLock | ||
| import org.jetbrains.research.testspark.core.data.ChatMessage | ||
| import org.jetbrains.research.testspark.core.error.LlmError | ||
| import org.jetbrains.research.testspark.core.error.Result | ||
| import org.jetbrains.research.testspark.core.generation.llm.network.RequestManager | ||
| import org.jetbrains.research.testspark.core.generation.llm.network.model.LlmParams | ||
|
|
||
| class ChatSessionManager( | ||
| private val requestManager: RequestManager, | ||
| private val llmParams: LlmParams, | ||
| ) { | ||
| private val mutex = Mutex() | ||
| private val chatHistory = mutableListOf<ChatMessage>() | ||
| private val log = KotlinLogging.logger {} | ||
|
|
||
| suspend fun request( | ||
| prompt: String, | ||
| isUserFeedback: Boolean, | ||
| ): Flow<Result<String>> { | ||
| log.info { "Sending Request..." } | ||
|
|
||
| recordChatMessage(isUserFeedback, ChatMessage.Companion.createUserMessage(message = prompt)) | ||
|
|
||
| val chatHistory = | ||
| if (isUserFeedback) { | ||
| chatHistory + ChatMessage.createUserMessage(prompt) | ||
| } else { | ||
| chatHistory | ||
| } | ||
|
|
||
| return requestManager | ||
| .sendRequest( | ||
| llmParams, | ||
| chatHistory, | ||
| ).map { result -> | ||
| val responseString = result.getDataOrNull() | ||
| if (responseString != null && responseString.isEmpty()) { | ||
| Result.Failure(error = LlmError.EmptyLlmResponse) | ||
| } else { | ||
| result | ||
| } | ||
| }.onEach { result -> | ||
| val rawText = result.getDataOrNull() | ||
| if (rawText.isNullOrEmpty().not()) { | ||
| recordChatMessage( | ||
| isUserFeedback, | ||
| ChatMessage.Companion.createAssistantMessage(message = rawText), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private suspend fun recordChatMessage( | ||
| isUserFeedback: Boolean, | ||
| message: ChatMessage, | ||
| ) { | ||
| if (isUserFeedback) return | ||
| mutex.withLock { | ||
| when (message.role) { | ||
| ChatMessage.ChatRole.User -> chatHistory.add(message) | ||
| ChatMessage.ChatRole.Assistant -> { | ||
| val lastMessage = chatHistory.lastOrNull() | ||
| if (lastMessage != null && lastMessage.role == ChatMessage.ChatRole.Assistant) { | ||
| lastMessage.contentBuilder.append(message.content) | ||
| } else { | ||
| chatHistory.add(message) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.