Skip to content

Commit

Permalink
✨ provide a way to switch back to onboarding when in querying state
Browse files Browse the repository at this point in the history
  • Loading branch information
ff137 committed Nov 15, 2023
1 parent c479fc3 commit 8886e00
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
61 changes: 34 additions & 27 deletions src/xyz/didx/ai/AiHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,40 +82,47 @@ object AiHandler {

case ChatState.QueryingOpportunities =>
IO(Try {
val embeddingMatchOpt: Option[EmbeddingMatch[TextSegment]] =
EmbeddingHandler.findMostRelevantFromQuery(input)

embeddingMatchOpt match
case None => (
"Apologies, we couldn't find a relevant opportunity. Please try a different request!",
state
input.toLowerCase.contains("onboard") match
case true => // provide a way to switch back to onboarding, out from querying opportunities
(
OnboardingHandler.getResponse(input, conversationId, telNo, cleanSlate = true).nextMessageToUser,
ChatState.Onboarding
)
case Some(embeddingMatch) =>
val logResult =
s"From user request: $input\n" +
"Got embedding match with: " +
s"score = ${embeddingMatch.score()}, " +
s"embedded = ${embeddingMatch.embedded()}, " +
s"embeddingId = ${embeddingMatch.embeddingId()}"
case false => // otherwise, query opportunities as usual:
val embeddingMatchOpt: Option[EmbeddingMatch[TextSegment]] =
EmbeddingHandler.findMostRelevantFromQuery(input)

embeddingMatchOpt match
case None => (
"Apologies, we couldn't find a relevant opportunity. Please try a different request!",
state
)
case Some(embeddingMatch) =>
val logResult =
s"From user request: $input\n" +
"Got embedding match with: " +
s"score = ${embeddingMatch.score()}, " +
s"embedded = ${embeddingMatch.embedded()}, " +
s"embeddingId = ${embeddingMatch.embeddingId()}"

scribe.info(logResult)
scribe.info(logResult)

val topMatch: TextSegment = embeddingMatch.embedded()
val id: String = topMatch.metadata("id")
val title: String = topMatch.metadata("title")
val organisation: String = topMatch.metadata("organisationName")
val opportunityUrl: String = topMatch.metadata("opportunityURL")
val topMatch: TextSegment = embeddingMatch.embedded()
val id: String = topMatch.metadata("id")
val title: String = topMatch.metadata("title")
val organisation: String = topMatch.metadata("organisationName")
val opportunityUrl: String = topMatch.metadata("opportunityURL")

val backupUrl: String = s"https://app.yoma.world/opportunities/$id"
val backupUrl: String = s"https://app.yoma.world/opportunities/$id"

val url = opportunityUrl match
case null | "null" | "" => backupUrl // handle potential edge cases
case _ => opportunityUrl
val url = opportunityUrl match
case null | "null" | "" => backupUrl // handle potential edge cases
case _ => opportunityUrl

val response: String =
s"You might be interested in: $title, by $organisation. Here's a link to the opportunity page: $url"
val response: String =
s"You might be interested in: $title, by $organisation. Here's a link to the opportunity page: $url"

(response, state)
(response, state)
}.toEither.left.map(e => new Error(e.getMessage())))

case ChatState.Done => IO(Right(("Thank you for using DawnPatrol! Goodbye!", state)))
Expand Down
22 changes: 16 additions & 6 deletions src/xyz/didx/ai/handler/Onboarding.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ object OnboardingHandler {
// Define a map from conversationId to JvmPromptBuilder
private val builders: mutable.Map[String, PromptBuilder] = mutable.Map()

def getResponse(input: String, conversationId: String, telNo: Option[String] = None): OnboardingResult = {
def getResponse(
input: String,
conversationId: String,
telNo: Option[String] = None,
cleanSlate: Boolean = false
): OnboardingResult = {
scribe.info(
s"Get OnboardingHandler response for message: $input, for conversationId: $conversationId"
)
// Get the builder for this conversationId, or create a new one if it doesn't exist
val builder = builders.getOrElseUpdate(
conversationId,
AgentScript.createYomaOnboardingBuilder(telNo)
)

val defaultPromptBuilder = AgentScript.createYomaOnboardingBuilder(telNo)
val builder: PromptBuilder = cleanSlate match
case true =>
// We want to restart the onboarding on a clean slate, so update builders and use default
builders.update(conversationId, defaultPromptBuilder)
defaultPromptBuilder
case false =>
// Get the builder for this conversationId, or create a new one if it doesn't exist
builders.getOrElseUpdate(conversationId, defaultPromptBuilder)

// Add the user message to the builder
builder.addUserMessage(input)
Expand Down

0 comments on commit 8886e00

Please sign in to comment.