swiftide_indexing/transformers/metadata_title.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
//! Generate a title and adds it as metadata
use anyhow::Result;
use async_trait::async_trait;
use swiftide_core::{indexing::Node, Transformer};
/// This module defines the `MetadataTitle` struct and its associated methods,
/// which are used for generating metadata in the form of a title
/// for a given text. It interacts with a client (e.g., `OpenAI`) to generate
/// these questions and answers based on the text chunk in an `Node`.
/// `MetadataTitle` is responsible for generating a title
/// for a given text chunk. It uses a templated prompt to interact with a client
/// that implements the `SimplePrompt` trait.
#[swiftide_macros::indexing_transformer(
    metadata_field_name = "Title",
    default_prompt_file = "prompts/metadata_title.prompt.md"
)]
pub struct MetadataTitle {}
#[async_trait]
impl Transformer for MetadataTitle {
    /// Transforms an `Node` by generating questions and answers
    /// based on the text chunk within the node.
    ///
    /// # Arguments
    ///
    /// * `node` - The `Node` containing the text chunk to process.
    ///
    /// # Returns
    ///
    /// A `Result` containing the transformed `Node` with added metadata,
    /// or an error if the transformation fails.
    ///
    /// # Errors
    ///
    /// This function will return an error if the client fails to generate
    /// questions and answers from the provided prompt.
    #[tracing::instrument(skip_all, name = "transformers.metadata_title")]
    async fn transform_node(&self, mut node: Node) -> Result<Node> {
        let prompt = self.prompt_template.to_prompt().with_node(&node);
        let response = self.prompt(prompt).await?;
        node.metadata.insert(NAME, response);
        Ok(node)
    }
    fn concurrency(&self) -> Option<usize> {
        self.concurrency
    }
}
#[cfg(test)]
mod test {
    use swiftide_core::MockSimplePrompt;
    use super::*;
    #[test_log::test(tokio::test)]
    async fn test_template() {
        let template = default_prompt();
        let prompt = template.to_prompt().with_node(&Node::new("test"));
        insta::assert_snapshot!(prompt.render().await.unwrap());
    }
    #[tokio::test]
    async fn test_metadata_title() {
        let mut client = MockSimplePrompt::new();
        client
            .expect_prompt()
            .returning(|_| Ok("A Title".to_string()));
        let transformer = MetadataTitle::builder().client(client).build().unwrap();
        let node = Node::new("Some text");
        let result = transformer.transform_node(node).await.unwrap();
        assert_eq!(result.metadata.get("Title").unwrap(), "A Title");
    }
}