File size: 1,760 Bytes
93f7f1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_core.vectorstores import VectorStore
from langchain_core.retrievers import BaseRetriever
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Milvus
from langchain.retrievers import MultiVectorRetriever
from langchain.storage import InMemoryStore
import numpy as np

def query_pipeline(question, texts, tables, pictures, embeddings_model, llm_model):
    """
    Process a question through the RAG pipeline.
    
    Args:
        question: The user's question
        texts: List of text documents
        tables: List of table documents
        pictures: List of image description documents
        embeddings_model: Model for generating embeddings
        llm_model: LLM for generating answers
        
    Returns:
        str: The generated answer
    """
    # Combine all document types
    all_docs = texts + tables + pictures
    
    # Create vector store and retriever
    vectorstore = Milvus.from_documents(
        all_docs,
        embeddings_model,
        connection_args={"host": "127.0.0.1", "port": "19530"}
    )
    
    retriever = vectorstore.as_retriever()
    
    # Retrieve relevant documents
    relevant_docs = retriever.get_relevant_documents(question)
    
    # Format context for LLM
    context = "\n\n".join([doc.page_content for doc in relevant_docs])
    
    # Generate answer
    prompt = f"""
    You are an AI assistant answering questions based on the provided context.
    Use only the information from the context to answer the question.
    If you don't know the answer, say "I don't know".
    
    Context:
    {context}
    
    Question: {question}
    Answer:
    """
    
    response = llm_model.invoke(prompt)
    
    return response.content