swiftide_indexing/transformers/
metadata_title.rs

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