-
Notifications
You must be signed in to change notification settings - Fork 136
AI: Generate product sharing message part 2: Functionality #9236
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
Merged
JorgeMucientes
merged 34 commits into
trunk
from
feature/ai-share-description-functionality
Jun 16, 2023
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
c372cf1
Add: Use latest fluxC version to bring in JetpackAIStore.
hafizrahman b7d8cf6
Add: AI Repository class.
hafizrahman 6eb314c
Add: AI Prompts class.
hafizrahman f6d69e3
Feat: fetch sharing suggestion from AI and display it.
hafizrahman 3d395f0
Feat: handle share message editing. To be used later for actual sharing.
hafizrahman 1d09516
Refactor: use fold instead for result handling.
hafizrahman 76480e7
Refactor: Remove unneeded LoadingState and simplify code.
hafizrahman 42b5ba1
Add: state update on result failure.
hafizrahman 5e80da3
Refactor: make separate variables for button labels as they're to be …
hafizrahman 1149c51
Add: result logging in AI Repository's fetching.
hafizrahman e13cbfd
Add: display error message on the UI during AI fetching error.
hafizrahman b07c7db
Update: Prompt for product sharing.
hafizrahman be9fb50
Feat: Open sharing intent when "Share" button in the AI sharing botto…
hafizrahman 4765dad
Refactor: put sharing subject formatting in viewmodel instead.
hafizrahman a0171df
Feat: Make info button open Automattic's AI guidelines page.
hafizrahman 5872af6
Add: Analytics events for AI sharing.
hafizrahman ce3f00e
Add: Analytics tracking for sheet displayed.
hafizrahman 801311c
Add: Analytics tracking keys for AI sharing.
hafizrahman 010290d
Add: Analytics tracking for button tapped.
hafizrahman fcab87a
Add: Analytics tracking for share button tapped (with some refactoring).
hafizrahman b5d5588
Add: Analytics tracking for share dialog dismissed.
hafizrahman 92786e6
Add: Analytics tracking for AI message generated.
hafizrahman 188d0ef
Add: Analytics tracking for AI message generation failure.
hafizrahman 1a92fbf
Refactor: Make onGenerateButtonClicked() smaller function.
hafizrahman f60dfd5
Feat: enable Sharing product AI in feature flag.
hafizrahman e3a7be7
Add: release note.
hafizrahman f59f569
Fix: Detekt.
hafizrahman 4c51592
Fix: use correct tracking for share.
hafizrahman 61b0269
Refactor: Open product sharing feature only to WordPress.com-hosted s…
hafizrahman 08d1c53
Use the isWPComAtomic property to correctly determine if site is wpcom
JorgeMucientes 99eea26
Merge branch 'trunk' into feature/ai-share-description-functionality
JorgeMucientes 501a406
Fix detekt indentation issue after merge with trunk
JorgeMucientes 1a2c6e5
Fix: add checking for allowing a site to use sharing with AI.
hafizrahman c1903e4
Fix detekt spacing issue
JorgeMucientes 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
30 changes: 30 additions & 0 deletions
30
WooCommerce/src/main/kotlin/com/woocommerce/android/ai/AIPrompts.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,30 @@ | ||
| package com.woocommerce.android.ai | ||
|
|
||
| object AIPrompts { | ||
| private const val PRODUCT_DESCRIPTION_PROMPT = "Write a description for a product with title \"%1\$s\"%2\$s.\n" + | ||
| "Identify the language used in the product title and use the same language in your response.\n" + | ||
| "Make the description 50-60 words or less.\n" + | ||
| "Use a 9th grade reading level.\n" + | ||
| "Perform in-depth keyword research relating to the product in the same language of the product title, " + | ||
| "and use them in your sentences without listing them out." | ||
|
|
||
| fun generateProductDescriptionPrompt(name: String, features: String = ""): String { | ||
| val featuresPart = if (features.isNotEmpty()) " and features: \"$features\"" else "" | ||
| return String.format(PRODUCT_DESCRIPTION_PROMPT, name, featuresPart) | ||
| } | ||
|
|
||
| private const val PRODUCT_SHARING_PROMPT = "Your task is to help a merchant create a message to share with " + | ||
| "their customers a product named \"%1\$s\". More information about the product:\n" + | ||
| "%2\$s\n" + | ||
| "- Product URL: %3\$s.\n" + | ||
| "Identify the language used in the product name and product description, if any, to use in your response.\n" + | ||
| "The length should be up to 3 sentences.\n" + | ||
| "Use a 9th grade reading level.\n" + | ||
| "Add related hashtags at the end of the message.\n" + | ||
| "Do not include the URL in the message." | ||
|
|
||
| fun generateProductSharingPrompt(name: String, url: String, description: String = ""): String { | ||
| val descriptionPart = if (description.isNotEmpty()) "- Product description: \"$description\"" else "" | ||
| return String.format(PRODUCT_SHARING_PROMPT, name, descriptionPart, url) | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
WooCommerce/src/main/kotlin/com/woocommerce/android/ai/AIRepository.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,45 @@ | ||
| package com.woocommerce.android.ai | ||
|
|
||
| import com.woocommerce.android.util.WooLog | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import org.wordpress.android.fluxc.model.SiteModel | ||
| import org.wordpress.android.fluxc.network.rest.wpcom.jetpackai.JetpackAIRestClient.JetpackAICompletionsResponse | ||
| import org.wordpress.android.fluxc.store.jetpackai.JetpackAIStore | ||
| import java.lang.Exception | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class AIRepository @Inject constructor( | ||
| private val jetpackAIStore: JetpackAIStore | ||
| ) { | ||
| suspend fun fetchJetpackAICompletionsForSite( | ||
| site: SiteModel, | ||
| prompt: String, | ||
| skipCache: Boolean = false | ||
| ): Result<String> = withContext(Dispatchers.IO) { | ||
| jetpackAIStore.fetchJetpackAICompletionsForSite(site, prompt, skipCache).run { | ||
| when (this) { | ||
| is JetpackAICompletionsResponse.Success -> { | ||
| WooLog.d(WooLog.T.AI, "Fetching Jetpack AI completions succeeded") | ||
| Result.success(completion) | ||
| } | ||
| is JetpackAICompletionsResponse.Error -> { | ||
| WooLog.w(WooLog.T.AI, "Fetching Jetpack AI completions failed: $message") | ||
| Result.failure(this.mapToException()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| data class JetpackAICompletionsException( | ||
| val errorMessage: String, | ||
| val errorType: String | ||
| ) : Exception(errorMessage) | ||
|
|
||
| private fun JetpackAICompletionsResponse.Error.mapToException() = | ||
| JetpackAICompletionsException( | ||
| errorMessage = message ?: "Unable to fetch AI completions", | ||
| errorType = type.name | ||
| ) | ||
| } |
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused yet, but will be very soon, so I thought to add it right away.