Skip to content
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

feat: search photo's caption #21

Merged
merged 4 commits into from
Oct 23, 2024
Merged

feat: search photo's caption #21

merged 4 commits into from
Oct 23, 2024

Conversation

ThaddeusJiang
Copy link
Owner

@ThaddeusJiang ThaddeusJiang commented Oct 23, 2024

  • keep silence when upload and no same photos found
  • style: 💄 clean code

Summary by CodeRabbit

  • New Features

    • Enhanced command handling for searching similar photos and finding identical photos.
    • Updated command descriptions for improved clarity.
    • New prompts for user input to enhance interaction.
  • Bug Fixes

    • Improved error handling and logging mechanisms for better reliability.
  • Refactor

    • Streamlined photo search functionality and input handling in the bot.
    • Simplified internal logic for searching and finding photos.

Copy link

coderabbitai bot commented Oct 23, 2024

Caution

Review failed

The head commit changed during the review from 3258ad7 to 6015163.

Walkthrough

The changes in this pull request involve significant updates to the SaveIt.Bot and SaveIt.TypesensePhoto modules. The command "search" has been renamed to "similar," with modifications to its handling logic, including new requirements for user input. The search_photos!/1 function has been streamlined, and the create_photo!/1 function now includes additional processing for input data. Overall, these changes enhance command functionality and input handling within the bot.

Changes

File Change Summary
lib/save_it/bot.ex - Renamed command "search" to "similar" with updated description.
- Modified handling logic for "search" and "similar" commands.
- Removed search_similar_photos_based_on_caption function, integrated functionality into handle.
- Simplified search_similar_photos, introduced find_same_photos.
lib/save_it/typesense_photo.ex - Added require Logger.
- Updated create_photo!/1 to convert belongs_to_id to string and add timestamp.
- Changed search_photos!/1 to accept a single argument and modified internal request body.
- Removed overloaded search_photos!/2 function.

Possibly related issues

Possibly related PRs

  • feat: search photos #12: This PR introduces functionality for searching and finding similar photos, which is directly related to the modifications made in the main PR regarding the "similar" command and photo search handling.

🐰 In the meadow, hopping with glee,
The bot's commands are clear as can be!
"Similar" now leads the way,
With photos to find, come what may.
A sprinkle of logic, a dash of delight,
User's joy shines ever so bright! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (7)
lib/save_it/typesense_photo.ex (3)

2-2: Consider adding logging statements for critical operations.

While requiring Logger is a good practice, there are no actual logging statements in the code. Consider adding logging for critical operations like photo creation, updates, and searches to aid in monitoring and debugging.

Example additions:

  def create_photo!(
        %{
          belongs_to_id: belongs_to_id
        } = photo_params
      ) do
+   Logger.debug("Creating photo with params: #{inspect(photo_params)}")
    photo_create_input =
      photo_params
      |> Map.put(:belongs_to_id, Integer.to_string(belongs_to_id))
      |> Map.put(:inserted_at, DateTime.utc_now() |> DateTime.to_unix())

    Typesense.create_document!(
      "photos",
      photo_create_input
    )
  end

Line range hint 29-41: Consider making the distance_threshold configurable.

The search function uses a hardcoded distance_threshold of 0.75, which is significantly higher than the 0.4 used in search_similar_photos!/2. This could lead to false positives in search results.

Consider making it configurable similar to search_similar_photos!/2:

-  def search_photos!(q) do
+  def search_photos!(q, opts \\ []) do
+    distance_threshold = Keyword.get(opts, :distance_threshold, 0.75)
     req_body = %{
       "searches" => [
         %{
           "query_by" => "image_embedding,caption",
           "q" => q,
           "collection" => "photos",
           "prefix" => false,
-          "vector_query" => "image_embedding:([], k: 5, distance_threshold: 0.75)",
+          "vector_query" => "image_embedding:([], k: 5, distance_threshold: #{distance_threshold})",
           "exclude_fields" => "image_embedding"
         }
       ]
     }

33-33: Document the search behavior with multiple fields.

The addition of "caption" to query_by improves search flexibility, but the behavior of searching across multiple fields should be documented. Does the order of fields affect search priority? Are results from both fields combined or prioritized?

Consider adding a module or function documentation:

@doc """
Searches photos using both image embeddings and captions.
Results are matched against either the image embedding vector or the caption text.
The search prioritizes matches in the order specified: image_embedding first, then caption.

## Parameters
  - q: The search query string
"""
lib/save_it/bot.ex (4)

26-28: Consider enhancing command descriptions for better user guidance.

The command descriptions could be more specific about their functionality:

-  command("similar", description: "Find similar photos")
-  command("search", description: "Search photos")
+  command("similar", description: "Find visually similar photos by uploading an image")
+  command("search", description: "Search photos by text description")

118-130: Enhance the empty search prompt with examples.

The current prompt could be more helpful by providing examples of successful search terms.

-        send_message(chat.id, "What do you want to search? animal, food, etc.")
+        send_message(chat.id, "Please enter what you want to search for. Examples:\n- cat\n- sunset\n- beach vacation")

137-139: Remove unreachable commented code.

This commented code block is noted as unreachable and should be removed to maintain code cleanliness.


321-332: Add documentation for distance threshold values.

The distance threshold values (0.1 for same photos, 0.4 for similar photos) should be documented to explain their significance and how they were determined.

+  @doc """
+  Finds exactly matching photos using a strict distance threshold of 0.1.
+  This threshold was determined through testing to minimize false positives.
+  """
   defp find_same_photos(photo_id) do
     TypesensePhoto.search_similar_photos!(
       photo_id,
       distance_threshold: 0.1
     )
   end

+  @doc """
+  Finds visually similar photos using a relaxed distance threshold of 0.4.
+  This threshold allows for finding photos with similar visual elements while
+  maintaining reasonable precision.
+  """
   defp search_similar_photos(photo_id) do
     TypesensePhoto.search_similar_photos!(
       photo_id,
       distance_threshold: 0.4
     )
   end
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b3ac13d and 77538fe.

📒 Files selected for processing (2)
  • lib/save_it/bot.ex (3 hunks)
  • lib/save_it/typesense_photo.ex (2 hunks)
🧰 Additional context used

Comment on lines 167 to 204
# caption: contains /similar or /search -> search similar photos; otherwise, find same photos
def handle({:message, %{chat: chat, caption: caption, photo: photos}}, ctx) do
photo = List.last(photos)

search_similar_photos_based_on_caption(photo, caption,
chat_id: chat.id,
bot_id: ctx.bot_info.id
)
file = ExGram.get_file!(photo.file_id)
photo_file_content = Telegram.download_file_content!(file.file_path)

bot_id = ctx.bot_info.id
chat_id = chat.id

caption =
if String.contains?(caption, ["/similar", "/search"]) do
""
else
caption
end

typesense_photo =
TypesensePhoto.create_photo!(%{
image: Base.encode64(photo_file_content),
caption: caption,
url: photo_url(bot_id, file.file_id),
belongs_to_id: chat_id
})

case caption do
"" ->
photos = search_similar_photos(typesense_photo["id"])
answer_photos(chat.id, photos)

_ ->
photos = find_same_photos(typesense_photo["id"])

case photos do
[] -> nil
_ -> answer_photos(chat.id, photos)
end
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor to reduce code duplication and simplify caption processing.

The photo processing logic is duplicated between this handler and the no-caption handler. Consider extracting common functionality into a private function.

Additionally, the caption processing could be simplified using pattern matching.

# Extract common photo processing logic
defp process_photo(photo, bot_id, chat_id, caption \\ "") do
  with {:ok, file} <- ExGram.get_file(photo.file_id),
       {:ok, content} <- Telegram.download_file_content(file.file_path) do
    TypesensePhoto.create_photo!(%{
      image: Base.encode64(content),
      caption: caption,
      url: photo_url(bot_id, file.file_id),
      belongs_to_id: chat_id
    })
  end
end

# Simplify caption processing using pattern matching
def handle({:message, %{chat: chat, caption: caption, photo: [_ | _] = photos} = msg}, ctx) do
  photo = List.last(photos)
  
  case process_photo(photo, ctx.bot_info.id, chat.id, caption) do
    {:ok, typesense_photo} ->
      case caption do
        "/similar" <> _ -> search_and_answer_similar(typesense_photo, chat.id)
        "/search" <> _ -> search_and_answer_similar(typesense_photo, chat.id)
        nil -> search_and_answer_same(typesense_photo, chat.id)
        _ -> search_and_answer_same(typesense_photo, chat.id)
      end
    {:error, reason} ->
      Logger.error("Failed to process photo: #{inspect(reason)}")
      send_message(chat.id, "Sorry, I couldn't process your photo. Please try again.")
  end
end

Comment on lines 141 to 165
# caption: nil -> find same photos
def handle({:message, %{chat: chat, caption: nil, photo: photos}}, ctx) do
photo = List.last(photos)

file = ExGram.get_file!(photo.file_id)
photo_file_content = Telegram.download_file_content!(file.file_path)

bot_id = ctx.bot_info.id
chat_id = chat.id

typesense_photo =
TypesensePhoto.create_photo!(%{
image: Base.encode64(photo_file_content),
caption: "",
url: photo_url(bot_id, file.file_id),
belongs_to_id: chat_id
})

photos = find_same_photos(typesense_photo["id"])

case photos do
[] -> nil
_ -> answer_photos(chat.id, photos)
end
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding error handling for photo processing.

The photo processing chain (file retrieval → content download → TypesensePhoto creation) could benefit from explicit error handling to provide better user feedback when operations fail.

-    file = ExGram.get_file!(photo.file_id)
-    photo_file_content = Telegram.download_file_content!(file.file_path)
+    with {:ok, file} <- ExGram.get_file(photo.file_id),
+         {:ok, photo_file_content} <- Telegram.download_file_content(file.file_path) do
+      # ... rest of the processing ...
+    else
+      {:error, reason} ->
+        Logger.error("Failed to process photo: #{inspect(reason)}")
+        send_message(chat.id, "Sorry, I couldn't process your photo. Please try again.")
+    end

Committable suggestion was skipped due to low confidence.

@ThaddeusJiang ThaddeusJiang changed the title feat: keep silence feat: search photo's caption Oct 23, 2024
@ThaddeusJiang ThaddeusJiang self-assigned this Oct 23, 2024
@ThaddeusJiang ThaddeusJiang merged commit 45cd19a into main Oct 23, 2024
1 check passed
@ThaddeusJiang ThaddeusJiang deleted the feat/keep-silence branch October 23, 2024 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant