File size: 2,087 Bytes
8030307
 
6cafd4a
 
 
 
 
8030307
 
 
 
 
f54b448
8030307
a172499
8030307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f54b448
8030307
 
 
 
 
a172499
f54b448
8030307
f54b448
8030307
f54b448
8030307
f54b448
8030307
f54b448
 
 
 
 
a172499
f54b448
a172499
f54b448
 
 
a172499
 
8030307
f54b448
 
8030307
a172499
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
import faiss
import numpy as np
import os
import openai


openai.api_key = os.getenv("OPENAI_API_KEY")

def search_with_metadata_and_reranking(query, index, chunked_documents, model, filters=None, top_k=5):
    query_embedding = model.encode([query])
    distances, indices = index.search(query_embedding, top_k)
    results = [chunked_documents[i] for i in indices[0]]

    if filters:
        filtered_results = []
        for result in results:
            match = True
            for key, value in filters.items():
                if key == "categories":
                    categories = eval(result.metadata.get("categories", "[]"))
                    if value not in categories:
                        match = False
                        break
                else:
                    if result.metadata.get(key) != value:
                        match = False
                        break
            if match:
                filtered_results.append(result)
        results = filtered_results

    results.sort(key=lambda x: x.metadata['publish_date'], reverse=True)
    return results

def rag_based_generation(query, index, chunked_documents, model, filters=None, top_k=5):
    results = search_with_metadata_and_reranking(query, index, chunked_documents, model, filters, top_k)
    print("Results before filtering:", results)

    if not results:
        print("No relevant chunks found for the query.")
        return "No relevant information found."

    context = " ".join([result.page_content for result in results if result is not None])

    prompt = f"Based on the following information:\n{context}\n\nAnswer the question: {query}"

    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": prompt}
    ]

    response = openai.ChatCompletion.create(
        model="gpt-4o-mini",  
        max_tokens=1500,
        n=1,
        stop=None,
        temperature=0.2,
        messages=messages
    )

    generated_answer = response.choices[0].message['content'].strip()
    return generated_answer