Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ internal val PageMetadata.shouldUseReaderModeContent get() = isReaderable && !is
private val PageMetadata.systemPrompt get() = if (isRecipe) {
recipeInstructions(language)
} else {
defaultInstructions()
defaultInstructions(language)
}

internal fun defaultInstructions() = """
internal fun defaultInstructions(language: String) = """
You are a Content Summarizer. You create mobile-optimized summaries by
first understanding what users actually need from each type of content.

You MUST respond entirely in $language. Do not mix languages.
Translate all visible section headers and labels into $language.

Process:
Step 1: Identify and Adapt. Use tree of thought to determine:
What type of content is this? What would a mobile user want to extract?
Expand Down Expand Up @@ -58,7 +61,7 @@ internal fun recipeInstructions(language: String) = """
You are an expert at creating mobile-optimized recipe summaries.

You MUST respond entirely in $language. Do not mix languages.
Translate all visible section headers and labels into **{lang}**.
Translate all visible section headers and labels into $language.
Output ONLY the formatted result. Do not add any closing phrases.
If a field is null, empty, or missing, omit that section entirely.
Always replace placeholders with actual values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,95 @@ class SummarizationStoreTest {
)

assertEquals(expected, states)
assertEquals(Prompt(content, defaultInstructions()), llm.lastPrompt)
assertEquals(Prompt(content, defaultInstructions("en")), llm.lastPrompt)
}

@Test
fun `page language is forwarded to model for default case`() = runTest {
val llm = FakeLlm.successful
val provider = FakeCloudProvider(preparedState = CloudLlmProvider.State.Ready(llm))
val content = "this is expected content."
val pageTitle = "Article Headline"
val language = "de"
val store = SummarizationStore(
initialState = Inert(true),
reducer = ::summarizationReducer,
middleware = listOf(
SummarizationMiddleware(
llmProvider = provider,
settings = SummarizationSettings.inMemory(hasConsentedToShake = true),
contentProvider = { Result.success(Content(PageMetadata(listOf("Article"), 0, language, pageTitle = pageTitle), content)) },
errorReporter = noopReporter,
scope = backgroundScope,
dispatcher = StandardTestDispatcher(testScheduler),
),
),
)

val states = mutableListOf<SummarizationState>()
backgroundScope.launch {
store.stateFlow.toList(states)
}
testScheduler.advanceTimeBy(1.seconds)

store.dispatch(ViewAppeared)
testScheduler.advanceTimeBy(15.seconds)

val expected = listOf<SummarizationState>(
Inert(true),
Loading(provider.info),
Summarizing(provider.info, parser.parse("# $pageTitle\nThis is the article\n")),
Summarizing(provider.info, parser.parse("# $pageTitle\nThis is the article\nThis is some content...\n")),
Summarizing(provider.info, parser.parse("# $pageTitle\nThis is the article\nThis is some content...\nThis is some *bold* content.\n")),
Summarized(provider.info, parser.parse("# $pageTitle\nThis is the article\nThis is some content...\nThis is some *bold* content.\n")),
)

assertEquals(expected, states)
assertEquals(Prompt(content, defaultInstructions(language)), llm.lastPrompt)
}

@Test
fun `page language is forwarded to model for recipe case`() = runTest {
val llm = FakeLlm.successful
val provider = FakeCloudProvider(preparedState = CloudLlmProvider.State.Ready(llm))
val content = "this is expected content."
val pageTitle = "Article Headline"
val language = "de"
val store = SummarizationStore(
initialState = Inert(true),
reducer = ::summarizationReducer,
middleware = listOf(
SummarizationMiddleware(
llmProvider = provider,
settings = SummarizationSettings.inMemory(hasConsentedToShake = true),
contentProvider = { Result.success(Content(PageMetadata(listOf("Recipe"), 0, language, pageTitle = pageTitle), content)) },
errorReporter = noopReporter,
scope = backgroundScope,
dispatcher = StandardTestDispatcher(testScheduler),
),
),
)

val states = mutableListOf<SummarizationState>()
backgroundScope.launch {
store.stateFlow.toList(states)
}
testScheduler.advanceTimeBy(1.seconds)

store.dispatch(ViewAppeared)
testScheduler.advanceTimeBy(15.seconds)

val expected = listOf<SummarizationState>(
Inert(true),
Loading(provider.info),
Summarizing(provider.info, parser.parse("# $pageTitle\nThis is the article\n")),
Summarizing(provider.info, parser.parse("# $pageTitle\nThis is the article\nThis is some content...\n")),
Summarizing(provider.info, parser.parse("# $pageTitle\nThis is the article\nThis is some content...\nThis is some *bold* content.\n")),
Summarized(provider.info, parser.parse("# $pageTitle\nThis is the article\nThis is some content...\nThis is some *bold* content.\n")),
)

assertEquals(expected, states)
assertEquals(Prompt(content, recipeInstructions(language)), llm.lastPrompt)
}

@Test
Expand Down Expand Up @@ -322,7 +410,7 @@ class SummarizationStoreTest {

assertTrue(usingReaderContent)
assertEquals(expected, states)
assertEquals(Prompt(content, defaultInstructions()), llm.lastPrompt)
assertEquals(Prompt(content, defaultInstructions("en")), llm.lastPrompt)
}

@Test
Expand Down Expand Up @@ -512,7 +600,7 @@ class SummarizationStoreTest {
)

assertEquals(expected, states)
assertEquals(Prompt(content, defaultInstructions()), llm.lastPrompt)
assertEquals(Prompt(content, defaultInstructions("en")), llm.lastPrompt)
}

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal class DefaultSummarizationEligibilityChecker : SummarizationEligibility
return wordCount in WORD_COUNT_RANGE && language.inAcceptedLanguages()
}

private fun String.inAcceptedLanguages() = listOf("en").any { acceptedLang ->
private fun String.inAcceptedLanguages() = listOf("en", "fr", "es", "pt", "de", "ja").any { acceptedLang ->
this.contains(acceptedLang)
}

Expand Down
Loading