File size: 8,018 Bytes
c6e5236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab8a9c
c6e5236
 
 
 
 
 
 
66de7f2
c6e5236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab8a9c
c6e5236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab8a9c
 
c6e5236
 
 
 
 
 
 
 
 
 
 
 
 
 
1ab8a9c
 
c6e5236
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import os
import streamlit as st
import streamlit.components.v1 as components
import openai
from llama_index.llms.openai import OpenAI

import os
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex, StorageContext, PropertyGraphIndex
from llama_index.core.indices.property_graph import (
    ImplicitPathExtractor,
    SimpleLLMPathExtractor,
)
from llama_index.retrievers.bm25 import BM25Retriever
from llama_index.core.retrievers import BaseRetriever
from llama_index.core.node_parser import SentenceSplitter
from llama_index.embeddings.openai import OpenAIEmbedding
from llmlingua import PromptCompressor
from rouge_score import rouge_scorer
from semantic_text_similarity.models import WebBertSimilarity
import nest_asyncio

# Apply nest_asyncio
nest_asyncio.apply()

# OpenAI credentials
key = os.getenv('OPENAI_API_KEY')
openai.api_key = key 
os.environ["OPENAI_API_KEY"] = key

# Streamlit UI
st.title("Prompt Optimization for One-Stop Policy QA Bot")

uploaded_files = st.file_uploader("Upload a PDF file", type="pdf", accept_multiple_files=True)

if uploaded_files:
    for uploaded_file in uploaded_files:
        reader = SimpleDirectoryReader(input_files=[f"../data/{uploaded_file.name}"])
        documents = reader.load_data()
        st.success("File uploaded...")

        # Indexing
        index = PropertyGraphIndex.from_documents(
            documents,
            embed_model=OpenAIEmbedding(model_name="text-embedding-3-small"),
            kg_extractors=[
                ImplicitPathExtractor(),
                SimpleLLMPathExtractor(
                    llm=OpenAI(model="gpt-3.5-turbo", temperature=0.3),
                    num_workers=4,
                    max_paths_per_chunk=10,
                ),
            ],
            show_progress=True,
        )

        # Save Knowlege Graph
        index.property_graph_store.save_networkx_graph(name="../data/kg.html")

        # Display the graph in Streamlit
        st.success("File Processed...")
        st.success("Creating Knowledge Graph...")
        HtmlFile = open("../data/kg.html", 'r', encoding='utf-8')
        source_code = HtmlFile.read() 
        components.html(source_code, height= 500, width=700)

        # Retrieval
        kg_retriever = index.as_retriever(
            include_text=True,  # include source text, default True
        )

        # Generation
        model = "gpt-3.5-turbo"

        def get_context(query):
            contexts = kg_retriever.retrieve(query)
            context_list = [n.text for n in contexts]
            return context_list
        

        def res(prompt):

            response = openai.chat.completions.create(
                model=model,
                messages=[
                    {"role":"system",
                     "content":"You are a helpful assistant who answers from the following context. If the answer can't be found in context, politely refuse"
                    },
                    {"role": "user",
                     "content": prompt,
                    }
                ]
            )

            return [response.usage.prompt_tokens, response.usage.completion_tokens, response.usage.total_tokens, response.choices[0].message.content]


        # Initialize session state for token summary, evaluation details, and chat messages
        if "token_summary" not in st.session_state:
            st.session_state.token_summary = []
        if "messages" not in st.session_state:
            st.session_state.messages = []

        # Display chat messages from history on app rerun
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])

        # Accept user input
        if prompt := st.chat_input("Enter your query:"):
            st.success("Fetching info...")
            # Add user message to chat history
            st.session_state.messages.append({"role": "user", "content": prompt})
            with st.chat_message("user"):
                st.markdown(prompt)

            # Generate response
            # st.success("Fetching info...")
            context_list = get_context(prompt)
            context = " ".join(context_list)


            # Original prompt response
            full_prompt = "\n\n".join([context + prompt])
            orig_res = res(full_prompt)
            st.session_state.messages.append({"role": "assistant", "content": "Generating Original prompt response..."})
            st.session_state.messages.append({"role": "assistant", "content": orig_res[3]})
            st.success("Generating Original prompt response...")
            with st.chat_message("assistant"):
                st.markdown(orig_res[3])

            # Compressed Response
            st.session_state.messages.append({"role": "assistant", "content": "Generating Optimized prompt response..."})
            st.success("Generating Optimized prompt response...")

            llm_lingua = PromptCompressor(
            model_name="microsoft/llmlingua-2-xlm-roberta-large-meetingbank",
            use_llmlingua2=True, device_map="mps"
            )

            def prompt_compression(context, rate=0.5):
                compressed_context = llm_lingua.compress_prompt(
                    context,
                    rate=rate,
                    force_tokens=["!", ".", "?", "\n"],
                    drop_consecutive=True,
                )
                return compressed_context
            compressed_context = prompt_compression(context)
            full_prompt = "\n\n".join([compressed_context['compressed_prompt'] + prompt])
            compressed_res = res(full_prompt)
            st.session_state.messages.append({"role": "assistant", "content": compressed_res[3]})
            with st.chat_message("assistant"):
                st.markdown(compressed_res[3])

            # Save token summary and evaluation details to session state
            scorer = rouge_scorer.RougeScorer(['rougeL'], use_stemmer=True)
            scores = scorer.score(compressed_res[3],orig_res[3])
            webert_model = WebBertSimilarity(device='cpu')
            similarity_score = webert_model.predict([(compressed_res[3], orig_res[3])])[0] / 5 * 100
         

            # Display token summary
            st.session_state.messages.append({"role": "assistant", "content": "Token Length Summary..."})
            st.success('Token Length Summary...')
            st.session_state.messages.append({"role": "assistant", "content": f"Original Prompt has {orig_res[0]} tokens"})
            st.write(f"Original Prompt has {orig_res[0]} tokens")
            st.session_state.messages.append({"role": "assistant", "content": f"Optimized Prompt has {compressed_res[0]} tokens"})
            st.write(f"Optimized Prompt has {compressed_res[0]} tokens")

            st.session_state.messages.append({"role": "assistant", "content": "Comparing Original and Optimized Prompt Response..."})
            st.success("Comparing Original and Optimized Prompt Response...")
            st.session_state.messages.append({"role": "assistant", "content": f"Rouge Score : {scores['rougeL'].fmeasure * 100}"})
            st.write(f"Rouge Score : {scores['rougeL'].fmeasure * 100}")
            st.session_state.messages.append({"role": "assistant", "content": f"Semantic Text Similarity Score : {similarity_score}"})
            st.write(f"Semantic Text Similarity Score : {similarity_score}")

            st.write(" ")
            # origin_tokens = compressed_context['origin_tokens']
            # compressed_tokens = compressed_context['compressed_tokens']
            origin_tokens = orig_res[0]
            compressed_tokens = compressed_res[0]
            saving = (origin_tokens - compressed_tokens) * 0.06 / 1000
            st.session_state.messages.append({"role": "assistant", "content": f"The optimized prompt has ${saving:.4f} saved in GPT-4."})
            st.success(f"The optimized prompt has ${saving:.4f} saved in GPT-4.")