BaseSynthesizer
class.get_response
method to include your custom logic for injecting metadata.from llama_index.core.response_synthesizers import BaseSynthesizer from llama_index.core import PromptTemplate class CustomMetadataSynthesizer(BaseSynthesizer): def get_response( self, query_str: str, text_chunks: Sequence[str], **response_kwargs: Any, ) -> RESPONSE_TEXT_TYPE: # Inject metadata into the context metadata = response_kwargs.get('metadata', {}) context_with_metadata = f"Metadata: {metadata}\n\nContext:\n" + "\n".join(text_chunks) # Create a custom prompt template that includes metadata custom_prompt = PromptTemplate( "Given the following context and metadata, please answer the question:\n" "Context and Metadata: {context_str}\n" "Question: {query_str}\n" "Answer: " ) # Format the prompt with the context (including metadata) and query formatted_prompt = custom_prompt.format( context_str=context_with_metadata, query_str=query_str ) # Use the LLM to generate a response response = self._llm.predict(formatted_prompt) return response
image_to_image_retrieve
method with a string or QueryBundle, it creates a QueryBundle with an empty query string and the provided image path:if isinstance(str_or_query_bundle, str): # leave query_str as empty since we are using image_path for image retrieval str_or_query_bundle = QueryBundle( query_str="", image_path=str_or_query_bundle )
llm_response = self._multi_modal_llm.complete( prompt=fmt_prompt, image_documents=[ image_node.node for image_node in image_nodes if isinstance(image_node.node, ImageNode) ], )
llm_response = self.multi_modal_llm.complete( prompt=fmt_prompt, image_documents=[n.node for n in reranked_image_nodes], )
documents = SimpleDirectoryReader("./mixed_wiki/").load_data() index = MultiModalVectorStoreIndex.from_documents( documents, storage_context=storage_context, )
text_store = QdrantVectorStore( client=client, collection_name="text_collection" ) image_store = QdrantVectorStore( client=client, collection_name="image_collection" ) storage_context = StorageContext.from_defaults( vector_store=text_store, image_store=image_store )
Settings.embed_model = CohereEmbedding( api_key=os.environ["COHERE_API_KEY"], model_name="embed-english-v3.0", # current v3 models support multimodal embeddings )
retriever_engine = index.as_retriever(image_similarity_top_k=4) retrieval_results = retriever_engine.image_to_image_retrieve( "./mixed_wiki/2.jpg" )
delete_large_images(data_path)