File size: 14,014 Bytes
162f3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9603aa4
162f3ee
 
 
 
 
 
80a67e8
162f3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
725d75b
a956c77
 
162f3ee
 
 
 
 
 
 
 
 
 
61ae9bd
162f3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c6e8cc
162f3ee
7c6e8cc
162f3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c6e8cc
162f3ee
 
 
 
 
 
 
 
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import streamlit as st
import json
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.vectorstores import FAISS
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.document_loaders import JSONLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
import pandas as pd
from langchain.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import SentenceTransformerEmbeddings
import os
import cohere
from dotenv import load_dotenv


load_dotenv()
api_key = os.getenv('API_KEY')


client = cohere.Client(api_key)

# Define functions
def split_docs(documents, chunk_size=1000, chunk_overlap=20):
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)
    docs = text_splitter.split_documents(documents)
    return docs

def make_api_call(gpt_assistant_prompt, gpt_user_prompt):
    message = f"{gpt_assistant_prompt}\n\n{gpt_user_prompt}"
    temperature = 0.0
    max_tokens = 1000
    #frequency_penalty = 0.0

    response = client.chat(
        model="command",
        message=message,
        temperature=temperature,
        max_tokens=max_tokens,
    )
    return response.text

def condition_split(query):
    structure = {
        "condition_for_eligibility": {
            "logic": "form the logic from query above like (A and B) or C",
            "definitions": {
                "condition": "the text Condition extracted from the query"
            }
        },
        "promotion_offered": "Description of Promotion",
        "scheduling": "Offer Scheduling Details"
    }
    template_initial = f"""
    Given a text description of a customer offer that outlines eligibility criteria, the promotion offered, and the scheduling of the offer, your task is to parse the text and organize the information into a structured JSON format. The JSON structure should include three main components: condition_for_eligibility, promotion_offered, and scheduling. Use logical expressions to detail the conditions for eligibility.

    Text Description:
    {query}
    Your Objectives:

    Extract and Label Conditions for Eligibility:

    Identify specific metrics or actions (e.g., number of calls) that define eligibility.
    Use labels (A, B, C) for distinct conditions.
    Determine the logical relationship between these conditions (e.g., (A and B) or C).
    Outline the Promotion Offered:

    Identify the key benefit or discount promised to eligible customers.
    Determine the Scheduling for the Offer:

    Capture the frequency or timing of when the offer is made (e.g., daily, weekly).
    Structure Your Response as Follows:

    the structure is

    {structure}

    the json you produce, replace the sigle quates with doublequates to match json format the json.loads is expecting

    """
    gpt_assistant_prompt = """You are an expert in Semantic Understanding, Marketing, and Business Analysis.
    You need to extract specific information like conditions, scheduling details, and promotions from a paragraph, typically blending technical skills and domain-specific knowledge.
    Give only the JSON as the response.
    """
    gpt_user_prompt = template_initial
    resp = make_api_call(gpt_assistant_prompt, gpt_user_prompt)
    start_index = resp.find("{")
    end_index = resp.rfind("}")
    resp = resp.replace("'", '"')
    print(resp)
    json_data_string = resp[start_index:end_index + 1]
    json_data = json.loads(json_data_string)
    return json_data
def make_prompt(query, db1):
    matching_docs = db1.similarity_search(query, k=4)
    li = []
    for doc in matching_docs:
        js = json.loads(doc.page_content)
        s = f"{js['kpis']}: {js['description']}"
        li.append(s)
    final = ",/n ".join(li)

    prompt_template = f"""
    Given the following user query and matched information if the KPI's:

    User Query: {query}

    Matched KPI's:
    {final}

    Write a condition to fulfill the user query.

    IMPORTANT:
    - Use only the KPI's mentioned in the Matched KPI's.
    - Associate KPI's with the corresponding descriptions provided in the Matched KPI's.
    - provide only the condition in response
    - only the exact KPI and condition value is to be present in the response no other text should be present.
    - only use one matching KPI or the closer once
    """
    gpt_assistant_prompt = """You are an expert in Semantic Understanding, Marketing, and Business Analysis.
    You need to check whether the text matches. If the matching probability is high, then proceed with the requirement.
    """

    resp = make_api_call(gpt_assistant_prompt, prompt_template)
    return str(resp)
def replace_with_shielding(json_data, condition_string):
    placeholders = {key: f"__{key}__" for key in json_data.keys()}
    for key, placeholder in placeholders.items():
        condition_string = condition_string.replace(key, placeholder)

    stop_processing = False
    for key, value in json_data.items():
        if stop_processing:
            break

        if "The provided KPI's do not match the user query." in str(value):
            stop_processing = True
            condition_string = condition_string.replace(placeholders[key], "Description not matching with kpi so update kpi and description in kpi's json file")
        else:
            condition_string = condition_string.replace(placeholders[key], str(value))

    if "Description not matching with kpi so update kpi and description in kpi's json file" in condition_string:
        stop_index = condition_string.index("Description not matching with kpi so update kpi and description in kpi's json file")
        condition_string = condition_string[:stop_index] + "Description not matching with kpi so update kpi and description in kpi's json file"

    return condition_string
def form_json_rule(condition_string):
    final_template = """
    The task is to generate a json format with the help of english text.
    I will help you with some details about the conversion.
    a sample rule json structure is
    {"featureId":"","appName":"","username":"","password":"","reqTxnId":"","msgOrigin":"","msgDest":"","timestamp":"","id":"","ruletype":"","data":{"detail":{"rules":{"id":"0","pid":"#","childrens":[{"id":"0_0","pid":"0","type":"conditions","option":"All","childrens":[{"id":"0_0_0","pid":"0_0","type":"condition","profile":{"id":1,"name":"P_AON"},"operator":">","values":{"value":"30"}},{"id":"0_0_1","pid":"0_0","type":"condition","profile":{"id":862,"name":"P_DEVICE_TYPE"},"operator":"=","values":{"value":"PHONE"}},{"id":"0_0_2","pid":"0_0","type":"action","action":{"id":98,"name":"Mobile App Notification"},"field":[{"name":"ActionID","value":"0_0_2"},{"name":"ActionName","value":""},{"name":"ActionCall","value":""}],"request":{"field":[]}}]}]}}}}

    Root Level Properties
    featureId: String. A unique identifier for the feature.
    appName: String. The name of the application.
    username: String. The username for authentication.
    password: String. The password for authentication.
    reqTxnId: String. A unique transaction identifier.
    msgOrigin: String. The origin of the message.
    msgDest: String. The destination of the message.
    timestamp: String (ISO 8601 format). The timestamp of the request or action.
    id: String. A unique identifier for this particular instance.
    ruletype: String. The type of rule being defined.
    Data and Detail Section
    data: Object container.
    detail: Object within 'data'.
    rules: Object representing the rule logic.
    id: String. The unique identifier of the rule.
    pid: String. The parent identifier of the rule.
    childrens: Array of child objects. Each object represents a condition or an action.
    type: String. Specifies if it's a 'condition' or 'action'
    if inside childrens if type is condition then the option  can come as  a logical operator like 'All' or 'Any'.
    ALL is like an and operation where as Any is like an OR operation
    If type is 'any' or 'and', it contains a conditions array with condition objects.
    If type is 'condition' or 'action', it follows the respective structures below.
    Condition Structure
    id: String. The unique identifier of the condition.
    pid: String. The parent identifier of the condition.
    profile: Object containing details of the condition.
    name: String. The name of the condition.
    id: String. The unique identifier of the condition.
    operator: String. The operation applied in the condition (e.g., '=', '<>').
    values: String. The values to check against in the condition.
    isTextMode: Boolean. Indicates text mode evaluation.
    Action Structure
    id: String. The unique identifier of the action.
    pid: String. The parent identifier of the action.
    action: Object containing details of the action.
    name: String. The name of the action.
    id: String. The unique identifier of the action.
    field: Object of parameters related to the action (key-value pairs).
    request (Optional): Object detailing an external service request (key-value pairs).
    isTextMode: Boolean. Indicates text mode processing.
    Schedule Section
    schedule: Object defining scheduling details.
    field: Array of scheduling parameter objects.
    Each object contains scheduling details (key-value pairs) like ScheduleName, ScheduleType, CAMPAIGN_NAME, ExpiryDate.

    now from the information provided below convert it to json
    shielded_replaced_string
    """
    final_template = final_template.replace("shielded_replaced_string", condition_string)
    gpt_assistant_prompt2 = """You are a json rule maker. You make rules based on the structure provided.
    IMPORTANT:
    Only give the valid json as response no other text should be present.
    Give the response json in compact form.
    the response should be exactly same to the template used in the prompt
    """
    resp = make_api_call(gpt_assistant_prompt2, final_template)
    return resp
# Streamlit App
def main():
    st.title("Flight Data Analysis")

    uploaded_file = st.file_uploader("Choose a JSON file", type="jsnol")

    if uploaded_file is not None:
        with open("flight.jsnol", "wb") as f:
            f.write(uploaded_file.getvalue())

        documents = JSONLoader(file_path='flight.jsnol', jq_schema='.', text_content=False, json_lines=True).load()

        query = st.text_input("Enter your query")

        if st.button("Split Documents"):
            docs = split_docs(documents)
            st.write(f"Number of documents: {len(docs)}")
            st.json(docs)

        if st.button("Create Embeddings and Vector Store"):
            docs = split_docs(documents)
            embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
            db1 = Chroma.from_documents(
                documents=docs,
                embedding=embeddings,
                persist_directory="embeddings"
            )
            db1.persist()
            st.write("Embeddings and vector store created.")

        if st.button("Condition Split"):
            if query:
                json_data = condition_split(query)
                st.write("JSON Data:")
                st.json(json_data)
            else:
                st.write("Please enter a query.")

        if st.button("Make Prompt"):
            if query:
                docs = split_docs(documents)
                embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
                db1 = Chroma.from_documents(
                    documents=docs,
                    embedding=embeddings,
                    persist_directory="embeddings"
                )
                db1.persist()
                condition_string = make_prompt(query, db1)
                st.write("Condition String:")
                st.write(condition_string)
            else:
                st.write("Please enter a query.")

        if st.button("Replace with Shielding"):
            if query:
                docs = split_docs(documents)
                embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
                db1 = Chroma.from_documents(
                    documents=docs,
                    embedding=embeddings,
                    persist_directory="embeddings"
                )
                db1.persist()
                condition_string = make_prompt(query, db1)
                json_data = condition_split(query)
                shielded_replaced_string = replace_with_shielding(json_data, condition_string)
                st.write("Shielded Replaced String:")
                st.write(shielded_replaced_string)
            else:
                st.write("Please enter a query.")

        if st.button("Form JSON Rule"):
            if query:
                docs = split_docs(documents)
                embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
                db1 = Chroma.from_documents(
                    documents=docs,
                    embedding=embeddings,
                    persist_directory="embeddings"
                )
                db1.persist()
                condition_string = make_prompt(query, db1)
                json_data = condition_split(query)
                shielded_replaced_string = replace_with_shielding(json_data, condition_string)
                json_rule = form_json_rule(shielded_replaced_string)
                st.write("JSON Rule:")
                st.json(json_rule)
                st.download_button(
                    label="Download JSON Rule",
                    data=json.dumps(json_rule),
                    file_name="json_rule.json",
                    mime="application/json"
                )
            else:
                st.write("Please enter a query.")

if __name__ == '__main__':
    main()