File size: 3,821 Bytes
d795c3d
 
d570bb8
 
a376030
9927698
2ee2aa4
a376030
d795c3d
a376030
 
d570bb8
a376030
2ee2aa4
 
 
a376030
 
 
 
 
 
 
 
0659c80
a376030
 
 
 
 
 
 
 
 
 
0659c80
a376030
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d795c3d
 
 
 
 
 
 
 
 
 
a376030
 
d795c3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import gradio as gr
from huggingface_hub import InferenceClient
import os
import openai
import pandas as pd
import faiss
import pickle
from sentence_transformers import SentenceTransformer


embedding_model = SentenceTransformer('nomic-ai/nomic-embed-text-v1.5', trust_remote_code=True)
openai.api_key = os.getenv("OPENAI_API_KEY")
db_index = faiss.read_index("db_index.faiss")
df = pd.read_csv('cleaned_data.csv')
with open('metadata_info.pkl', 'rb') as file:
    metadata_info = pickle.load(file)


def search(query):
    cleaned_query = query
    query_embedding = embedding_model.encode(cleaned_query).reshape(1, -1).astype('float32')
    D, I = db_index.search(query_embedding, k=10)
    results = []
    for idx in I[0]:
        if idx < 3327:
            doc_index = idx
            results.append({
                'type': 'metadata',
                'title': df.iloc[doc_index]['title'],
                'author': df.iloc[doc_index]['author'],
                'publish_date': df.iloc[doc_index]['publish_date'],
                'full_text': df.iloc[doc_index]['full_text'],
                'source': df.iloc[doc_index]['url']
            })
        else:
            chunk_index = idx - 3327
            metadata = metadata_info[chunk_index]
            doc_index = metadata['index']
            chunk_text = metadata['chunk']
            results.append({
                'type': 'content',
                'title': df.iloc[doc_index]['title'],
                'author': df.iloc[doc_index]['author'],
                'publish_date': df.iloc[doc_index]['publish_date'],
                'content': chunk_text,
                'source': df.iloc[doc_index]['url']
            })

    return results


def generate_answer(query):
    prompt = f"""
    Based on the following query from a user, please generate a detailed answer based on the context
    focusing on which is the best based on the query. You should responsd as you are a news and politician expert agent and are conversing with the
    user in a nice cordial way. If the query question is not in the context say I don't know, and always provide the url as the source of the information.
    Remove the special characters and (/n ) , make the output clean and concise.

    ###########
    query:
    "{query}"

    ########

    context:"
    "{search(query)}"
    #####

    Return in Markdown format with each hotel highlighted.
    """

    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, #higher temperature means more creative or more hallucination
        messages = messages


    )

    # Extract the generated response from the API response
    generated_text = response.choices[0].message['content'].strip()

    return generated_text


def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    response = generate_answer(message)
    yield response

"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
demo = gr.ChatInterface(
    respond,
    additional_inputs=[
        gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
        gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
        gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
        gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.95,
            step=0.05,
            label="Top-p (nucleus sampling)",
        ),
    ],
)


if __name__ == "__main__":
    demo.launch()