Skip to content

Commit

Permalink
prevent locked state for chat if an exception occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
David Motsonashvili committed Dec 18, 2023
1 parent 18724d3 commit f3cf350
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ class Chat(private val model: GenerativeModel, val history: MutableList<Content>
suspend fun sendMessage(prompt: Content): GenerateContentResponse {
prompt.assertComesFromUser()
attemptLock()

val response = model.generateContent(*history.toTypedArray(), prompt)

lock.release()
history.add(prompt)
history.add(response.candidates.first().content)

return response
try {
val response = model.generateContent(*history.toTypedArray(), prompt)
history.add(prompt)
history.add(response.candidates.first().content)
return response
} finally {
lock.release()
}
}

/**
Expand Down

0 comments on commit f3cf350

Please sign in to comment.