File size: 31,582 Bytes
046c24e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdf3c8d
046c24e
 
 
 
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import streamlit as st
import altair as alt
import boto3
from botocore.exceptions import ClientError
import json
from langchain_openai import OpenAI
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage
from pptx import Presentation
import chromadb
import hashlib
import os
import json


def get_secret():

    secret_name = "openai-api-key"
    region_name = "us-east-2"

    # Create a Secrets Manager client
    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )

    try:
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as e:
        # For a list of exceptions thrown, see
        # https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
        raise e

    secret = json.loads(get_secret_value_response['SecretString'])['OPENAI_API_KEY']
    return secret

    # Your code goes here.



def completion(prompt: str, model_name: str) -> str:
    chat = ChatOpenAI(temperature=0, model_name=model_name, openai_api_key=OPENAI_API_KEY)
    messages = [
        HumanMessage(
            content=prompt
        )
    ]
    return chat(messages).content

def download_ppt():
    s3 = boto3.resource('s3')
    s3_bucket = s3.Bucket("dev-anvai-docs")
    s3_bucket.download_file("tm_poc_rfp/3. Energy AMS.pptx", "3. Energy AMS.pptx")

def read_ppt():
    prs = Presentation("3. Energy AMS.pptx")
    slides = []
    # text_runs will be populated with a list of strings,
    # one for each text run in presentation
    text_runs = []
    ppt_text = ""
    page_titles = []
    cnt = 0
    slides_to_index = dict()
    for slide in prs.slides:
        title = ""
        count = 0
        page_slide = ""
        for shape in slide.shapes:
            if shape.has_text_frame:
                ppt_text = ppt_text.strip() + "\n" +  shape.text.replace("\n"," ").strip()
                page_slide = page_slide.strip() + "\n" +  shape.text.replace("\n"," ").strip()
                if count == 0:
                    page_titles.append(shape.text.replace("\n"," ").strip())
                    count+=1
            
            if shape.has_table:
                for row in shape.table.rows:
                    for cell in row.cells:
                        ppt_text = ppt_text.strip() + "\n" + cell.text.replace("\n"," ").strip()
                        page_slide = page_slide.strip() + "\n" +  cell.text.replace("\n"," ").strip()
                        if count == 0:
                            page_titles.append(cell.text.replace("\n"," ").strip())
                            count+=1
        slides.append(page_slide)
        slides_to_index[page_slide] = cnt
        cnt+=1
    return slides, page_titles
    
def create_summary(slides, page_titles):
    summary_to_index = dict()
    summaries_of_slides = []
    if os.path.exists("./summaries.txt"):
        file = open("./summaries.txt", "r")
        count = 0
        for val in file:
            val = val.replace("\n", " ").strip()
            summaries_of_slides.append(val)
            summary_to_index[val] = count
            count+=1
    else:
        file = open("summaries.txt", "w")
        for i in range(len(slides)):
            slide = slides[i]
            st.write()
            title = page_titles[i]
            st.write()
            system_prompt = get_system_message(title)
            st.write()
            entire_prompt = create_entire_prompt(system_prompt, slide, "analyst", "energy", "", title)
            st.write()
            summary = completion(entire_prompt, "gpt-4o")
            st.write()
            summary = summary.replace("\n", " ").strip()
            print("***********************")
            print("Title: " + title)
            summaries_of_slides.append(summary)
            summary_to_index[summary] = i
            #display(Markdown(summary))
            print("***********************")
            file.write(summary + "\n")
        file.close()
    return summaries_of_slides, summary_to_index

def update_chromadb(slides):
    ids_list = [str(hashlib.sha256(t.encode())) for t in slides]
    ids = []
    count = 0
    for val in ids_list:
        ids.append(val + "_" + str(count))
        count+=1
    
    metadatas = []
    for i in range(len(slides)):
        metadatas.append({"page_no":i+1})

        
    if os.path.exists("./chromadb_new_8/"):
        chroma_client = chromadb.PersistentClient(path="./chromadb_new_8/")
        slides_collection = chroma_client.get_or_create_collection(name="tech_mahindra_slides_poc_energy_new_8")
    else:
        chroma_client = chromadb.PersistentClient(path="./chromadb_new_8/")
        slides_collection = chroma_client.get_or_create_collection(name="tech_mahindra_slides_poc_energy_new_8")
        slides_collection.add(
            documents=slides,
            ids=ids,
            metadatas=metadatas
        )
    return slides_collection

def get_definitions(section):
    if section == "Executive Summary":
        defs = f"""

            An executive summary for the proposal based on the provided RFP should contain the following topics:

            • Introduction: Brief overview of the vendor’s company and its relevance to the project.

            • Understanding of Customer Needs: Demonstration of understanding of Customer’s requirements and challenges.

            • Proposed Solution: High-level summary of the proposed managed services and how they align with the scope of services.

            • Key Benefits: Highlight the main benefits and value propositions of the proposed solution.

            • Vendor Qualifications: Briefly outline the vendor’s relevant experience, expertise, and key personnel.

            • Project Approach and Methodology: Overview of the approach and methodologies to be used for service delivery.

            • Cost Summary: High-level overview of the proposed cost and any financial benefits.

            • Timeline and Milestones: Summary of the project timeline and key milestones.

            • Risk Management: Brief outline of how potential risks will be identified and managed.

            • Conclusion and Call to Action: Reaffirm the vendor’s commitment and suggest next steps for moving forward with the proposal.

        """
    elif section == "Cost Optimization Levers":
        defs = f"""The “Cost Optimization Levers” section of a proposal based on the provided RFP should contain the following topics:

    •	Competitive Bidding: Leveraging multiple vendors to drive down costs.

    •	Volume Discounts: Securing lower prices through bulk purchasing.

    •	Long-term Contracts: Establishing long-term agreements to benefit from negotiated discounts.

    •	Standardization: Reducing costs by standardizing components and processes.

    •	Value Engineering: Implementing design improvements to enhance cost efficiency.

    •	Outsourcing: Delegating non-core activities to specialized vendors.

    •	Technology and Automation: Using technology to streamline processes and reduce labor costs.

    •	Supplier Relationship Management: Building strong relationships with suppliers for better terms.

    •	Flexible Payment Terms: Negotiating favorable payment conditions to improve cash flow.

    •	Risk Sharing: Structuring contracts to share risks between parties.

    •	Performance-based Incentives: Encouraging cost savings through performance incentives.

    •	Sourcing Alternatives: Exploring alternative sourcing options for cost advantages.

    •	Efficient Resource Allocation: Optimizing resource utilization to minimize waste and reduce costs.

    •	Service Level Agreements (SLAs): Ensuring adherence to SLAs to avoid penalties and manage costs.

    •	Continuous Improvement: Implementing ongoing process improvements to enhance efficiency and reduce costs.

    """
    elif section == "Case Studies":
        defs = f"""

        Case studies in a proposal based on the provided RFP should contain the following topics:

        •	Project Overview: Brief description of the project, including client background.

        •	Client Information: Basic details about the client (industry, size, location).

        •	Challenges and Objectives: Specific challenges faced by the client and the objectives they aimed to achieve.

        •	Solution Provided: Detailed explanation of the solution or services delivered by your company.

        •	Implementation Process: Key steps taken during the implementation, including methodologies used.

        •	Results and Outcomes: Quantifiable results and benefits achieved, such as cost savings, improved efficiency, or enhanced performance.

        •	Client Testimonial: Feedback or quotes from the client about their satisfaction with the services provided.

        •	Lessons Learned: Insights gained from the project that demonstrate your ability to adapt and improve.

        •	Visuals and Data: Charts, graphs, and images to illustrate key points and results.

        •	Relevance to Current RFP: Explanation of how the case study is relevant to the current RFP and how similar strategies can be applied.            

        """
    elif section == "Delivery Model":
        defs = f"""

        The delivery model for a proposal based on the provided RFP should contain the following topics:

        •	Project Phases: Breakdown of distinct project phases (e.g., planning, design, implementation, testing, maintenance).

        •	Methodologies: Specific methodologies and frameworks to be used (e.g., Agile, Waterfall).

        •	Timeline and Milestones: High-level timeline including key milestones and deliverables.

        •	Resource Allocation: Roles, responsibilities, and time commitment of team members.

        •	Communication Plan: Strategies for regular communication and updates with stakeholders.

        •	Risk Management: Identification, assessment, and mitigation of potential risks.

        •	Quality Assurance: Processes and standards for ensuring quality deliverables.

        •	Change Management: Procedures for managing scope, schedule, and resource changes.

        •	Performance Monitoring: Mechanisms for tracking and reporting project performance.

        •	Client Involvement: Level of client involvement and collaboration required.

        •	Tools and Technologies: Tools and technologies to be used throughout the project.

        •	Support and Maintenance: Post-delivery support and maintenance plans.

        •	Compliance and Security: Measures to ensure compliance with regulations and security standards.

        •	Continuous Improvement: Processes for ongoing evaluation and improvement of services.

        """
    elif section == "Transition Approach":
        defs = f"""

        The transition approach for a proposal based on the provided RFP should contain the following topics:

        •	Transition Plan: Comprehensive outline of steps and activities required for the transition.

        •	Current State Assessment: Analysis of the existing systems, processes, and resources.

        •	Future State Definition: Description of the desired future state and objectives.

        •	Transition Phases: Breakdown of the transition into manageable phases.

        •	Resource Allocation: Roles and responsibilities of the transition team.

        •	Risk Management: Identification and mitigation of potential transition risks.

        •	Change Management: Strategies for managing the human aspect of the transition.

        •	Communication Plan: Detailed plan for stakeholder communication throughout the transition.

        •	Training and Support: Plans for training users and providing support during the transition.

        •	Performance Monitoring: Mechanisms for monitoring progress and success metrics.

        •	Cutover Strategy: Specific plans for the actual switch-over to the new state.

        •	Post-Transition Support: Arrangements for ongoing support and stabilization after the transition.

        """
    elif section == "Transition Risk":
        defs = f"""

        The transition risk section for a proposal based on the provided RFP should contain the following topics:

        •	Risk Identification: List of potential risks associated with the transition.

        •	Risk Assessment: Evaluation of the likelihood and impact of each identified risk.

        •	Mitigation Strategies: Specific actions and plans to reduce or eliminate risks.

        •	Contingency Plans: Plans for addressing risks if they materialize.

        •	Risk Monitoring: Processes for ongoing monitoring of risks throughout the transition.

        •	Stakeholder Involvement: Roles and responsibilities of stakeholders in risk management.

        •	Communication Plan: Strategies for communicating risks and mitigation plans to stakeholders.

        •	Resource Allocation for Risk Management: Dedicated resources and tools for managing risks.

        •	Lessons Learned: Insights from previous transitions to inform risk management strategies.

        """
    elif section == "Governance":
        defs = f"""

        The governance section for a proposal based on the provided RFP should contain the following topics:

        •	Governance Structure: Organizational structure, including roles and responsibilities.

        •	Decision-Making Processes: Procedures for making decisions and resolving issues.

        •	Performance Monitoring: Methods for tracking project progress and performance.

        •	Risk Management: Processes for identifying, assessing, and mitigating risks.

        •	Compliance and Standards: Ensuring adherence to regulations and standards.

        •	Quality Assurance: Measures for maintaining quality throughout the project.

        •	Communication Plan: Strategies for stakeholder communication and reporting.

        •	Stakeholder Engagement: Involvement of key stakeholders in governance activities.

        •	Documentation and Reporting: Maintenance of comprehensive records and regular reporting.

        •	Continuous Improvement: Processes for ongoing evaluation and enhancement of governance practices.

        """
    elif section == "Team Structure":
        defs = f"""

        The team structure section for a proposal based on the provided RFP should contain the following topics:

        •	Organizational Chart: Visual representation of the team hierarchy.

        •	Roles and Responsibilities: Detailed descriptions of each team member’s role and responsibilities.

        •	Key Personnel: Identification and profiles of key team members, including their qualifications and relevant experience.

        •	Team Composition: Overview of the team composition, including the number of members and their areas of expertise.

        •	Project Manager: Identification of the project manager, including their qualifications and specific responsibilities.

        •	Specialists and Subject Matter Experts (SMEs): Identification of any specialists or SMEs who will provide expertise in specific areas.

        •	Team Location: Information about where the team members will be located (on-site, off-site, or distributed).

        •	Resource Allocation: Time commitment and availability of each team member.

        •	Communication and Collaboration: Strategies for communication and collaboration among team members.

        •	Training and Development: Information on any training or development programs for the team.

        •	Contingency Plans: Plans for addressing changes in the team structure, such as the replacement of key personnel.

    """
    elif section == "Application Based Pricing":
        defs = f"""

        The application-based pricing section for a proposal based on the provided RFP should contain the following topics:

        •	Pricing Model Overview: Brief description of the application-based pricing approach.

        •	Use Case Analysis: Identification of specific use cases and applications.

        •	Value-Based Pricing: Pricing aligned with the value provided by each application.

        •	Customization Costs: Pricing for tailored solutions based on specific client needs.

        •	Scalability: Pricing structure based on the scale of application usage.

        •	Functionality and Features: Costs associated with different levels of functionality and features.

        •	Licensing Models: Various licensing options (per-user, per-transaction, per-instance).

        •	Implementation and Support Costs: Breakdown of implementation and ongoing support costs.

        •	Performance Metrics: Linking pricing to performance metrics and service levels.

        •	Usage Patterns: Pricing based on expected usage patterns (peak times, frequency).

        •	Compliance and Security: Additional costs for compliance and enhanced security measures.

        •	Market Comparison: Comparison with market rates and competitive pricing strategies.

        •	Client-Specific Factors: Adjustments based on client-specific requirements and constraints.

        •	Total Cost of Ownership (TCO): Overview of the total cost over the lifecycle of the application.

        •	Payment Terms: Details of payment schedules and terms.        

        """
    elif section == "Similar Customer Experience":
        defs = f"""

        The similar customer experience section for a proposal based on the provided RFP should contain the following topics:

        •	Client Background: Brief description of past clients similar to the Customer.

        •	Project Overview: Summary of the projects conducted for these clients.

        •	Challenges and Objectives: The challenges faced and objectives set by the previous clients.

        •	Solutions Provided: Description of the solutions and services delivered.

        •	Results and Outcomes: Quantifiable results and benefits achieved (e.g., cost savings, improved efficiency).

        •	Key Metrics: Specific performance metrics and KPIs that were met or exceeded.

        •	Client Testimonials: Feedback or quotes from these clients highlighting their satisfaction.

        •	Relevance to Current RFP: Explanation of how these experiences are relevant to the current RFP and how similar strategies can be applied.

        •	Lessons Learned: Key insights and improvements gained from these past projects.

        •	Project Timelines and Budget Adherence: Information on how timelines and budgets were managed and adhered to.

        •	Innovative Solutions: Examples of innovative approaches or technologies used in similar projects.

        """
        
    return defs

def get_prompt(context, objectives, query, definition, name, titles):
    return f"""

    ----- BEGIN Context -----

    {context}

    ----- END Context -----



    ----- BEGIN Objectives -----

    {objectives}

    ----- END Objectives -----

    

    ----- BEGIN Query -----

    {query}

    ----- END Query -----

    

    ----- BEGIN Context Titles -----

    {titles}

    ----- END Context Titltes -----



    System:\nYou are an expert question answering system.\n

    Given a context, a set of objectives and a query, you should generate the answer to the query.\n\n

    Please keep in mind that the audience of your response is an analyst.\n

    Please use the context to answer the given query.\n

    Take a deep breath, reflect on the answer, read the entire context provided and then generate the final answer.\n

    Please don't output anything else but the answer to the query.\n

    Please output at least a few paragraphs.\n

    Please pay special attention to numbers, like percentages, income, revenue.\n

    Please pay special attention to keywords like "increase", "increased", "decrease", "decreased", "critical" etc.\n

    Always try to include numbers in the output, if they are present in the context.\n

    Please generate output as a bulleted list, with bolded titles of the bulleted text.\n

    Please use only the context, objectives, query, context titles and definition provided.\n

    Please don't use your world knowledge to construct answers. Please use only the information provided.\b\b

  

    User: Given the definition of a {name} below, generate an answer, given the context below.\n

    You are an expert question answering system.\n

    Given a context, a set of objectives and a query, you should generate the answer to the query.\n\n

    Please keep in mind that the audience of your response is an analyst.\n

    Please use the context to answer the given query.\n

    Take a deep breath, reflect on the answer, read the entire context provided and then generate the final answer.\n

    Please don't output anything else but the answer to the query.\n

    Please output at least a few paragraphs.\n

    Please pay special attention to numbers, like percentages, income, revenue.\n

    Please pay special attention to keywords like "increase", "increased", "decrease", "decreased", "critical" etc.\n

    Always try to include numbers in the output, if they are present in the context.\n

    Please generate output as a bulleted list, with bolded titles of the bulleted text.\n

    Please use only the context, objectives, query, context titles and definition provided.\n

    Please don't use your world knowledge to construct answers. Please use only the information provided.\b\b

  

    ----- BEGIN Definition of a {name} -----

    {definition}

    ----- END Definition of a {name} -----

    """
def get_system_message(topic):
    return f"""

        You are an expert trained to summarize the given text without generalist statements and without fluff.\n

        Your task is to read the given context paragraphs and provide a summary that captures the main points.\n

        The user is interested in this topic: {topic}\n

        Please include at least five sentences for the topic in a bulleted list.

        Take a deep breath, and think step by step, but do not return the thought process in the summary.\n

        Please include at least five sentences for each bullet point .\n

        Please output the summary and nothing else.\n

        Please include all numbers in the summary, like percentages, increases, decreases, market share, cost reduction, cost increase etc.\n

        Please pay special attention to numbers, like percentages, income, revenue.\n

        Please pay special attention to keywords like "increase", "increased", "decrease", "decreased", "critical" etc.\n

        Please use only the context, objectives, query, context titles and definition provided.\n

        Please don't use your world knowledge to construct answers. Please use only the information provided.\n\n"""

def get_objectives():
    return f"""

                1.0      Introduction

                Customer is a company that uses its innovative technology and research capabilities to make advanced fuel systems and aftermarket products which improve fuel efficiency and reduce emissions in vehicles around the world. Headquartered in the USA. It employs approximately 13,200 employees across the globe.

                Customer is seeking Proposals from Bidder(s) (hereinafter “Bidder” or “Bidders” or “Supplier”) who can provide best in class managed services to reduce the costs, innovate, and deliver run and maintain the services (the “Services”) to optimize resources and capital. The purpose of this Request for Proposal (“RFP”) is to provide a fair evaluation for all Bidders and to provide the Bidders with the evaluation criteria against which they will be judged.

                2.0      Purpose, Description, and Objectives

                2.1          Purpose

                This RFP is being issued to collect Proposals relating to Customer’s request for Managed Services for the Customer IT organization to identify a strategic business partner to reduce run and maintain costs and to empower Customer resources to focus on the strategic projects and initiatives.

                 

                2.2          Description and Scope

                The scope of services requested by the Customer includes

                Cross Functional Services

                Application Support Services

                Application Development Services

                Database services

                Cloud Services

                Data Center Services

                Data Network Services

                End User Computing Services

                Service Desk Services

                Reporting and Documentation

                Disaster Recovery and Business Continuity

                 

                2.3          Objectives

                The following are the key high-level objectives the Customer expects to achieve:

                ·         A business partnership that brings a competitive advantage to Customer,

                ·         Provide the most cost-effective managed services while adhering to Service Level Agreements (SLAs)

                ·         Reduce / eliminate risk of constrained resources,

                ·         Have a scalable and flexible resource pool for changing business needs,

                ·         Standardize and improve service management capabilities,

                ·         Improve performance reporting and service integration,

                ·         Enhance capabilities to leverage new technologies and offer innovative solutions, 

                ·         Improve currency of infrastructure environment and related security posture.

            """

def create_entire_prompt(system, text, audience, domain, objectives, title):
    prompt = f"""

    System: {system}

    

    User:

    --- Context Begin ---

    {text}

    --- Context End ---

    

    --- Domain Begin ---

    {domain}

    --- Domain End ---



    --- Objectives Begin ---

    {objectives}

    --- Objectives End ---



    --- Instructions Begin ---

    You will generate verbose, entity-dense summaries of the given text.\n

    Please generate a summary in highly technical language.\n

    The summary should be for an audience of {audience}.\n

    The summary should be for the topic of {title}.\n

    The summary should be bulleted with the above topics as the titles with a few sentences written about the topic.\n

    Take a deep breath, and think step by step, but do not return the thought process in the summary.\n

    Please include at least five sentences for each bullet point .\n

    Please output the summary and nothing else.\n

    Please include all numbers in the summary, like percentages, increases, decreases, market share, cost reduction, cost increase etc.\n

    Please pay special attention to numbers, like percentages, income, revenue.\n

    Please pay special attention to keywords like "increase", "increased", "decrease", "decreased", "critical" etc.\n

    Please use only the context, objectives, query, context titles and definition provided.\n

    Please don't use your world knowledge to construct answers. Please use only the information provided.\n\n

  

    --- Instructions End ---\n

    """
    return prompt

section_question_mapping = {
    
    "Executive Summary": "Write an executive summary of a proposal in the energy domain.",
    "Cost Optimization Levers": "What can be said about Cost optimization levers?",
    "Case Studies": "What are the various case studies? What is the significance of the case studies?",
    "Delivery Model": "What is the Delivery Model?",
    "Transition Approach": "What Transition approach is followed?",
    "Transition Risk": "What is the Transition risk?",
    "Governance": "What kinds of Governance are followed?",
    "Team Structure": "What is the Team structure?",
    "Application Based Pricing": "What kinds of Application-based pricing is used?",
    "Similar Customer Experience": "What is Similar customer experience and what can be said about it?",
    "Ask a Question":""
}


    
def get_section_content(section, section_definition, section_query, page_titles, slides_collection):

    docs_rag = slides_collection.query(
            query_texts=[section_query + "\n\n" +section_definition],
            n_results=10
        )
    slides_retrieved = []
    for rag_doc_metadata in docs_rag["metadatas"][0]:
        slide_no = rag_doc_metadata["page_no"]
        slides_retrieved.append(slide_no)
    
    title_context = ""
    for no in slides_retrieved:
        title_context = title_context + "\n" + page_titles[no]
    
    slides_retrieved_str = [str(n) for n in slides_retrieved]
    
    print("*** Slides Retrieved: " + ",".join(slides_retrieved_str))
    
    rag_text = ""
    for rag_doc in docs_rag["documents"][0]:        
        rag_text = rag_text + "\n\n" + rag_doc
    
    if not section_definition:
        entire_prompt = get_prompt(rag_text, get_objectives(), section_query, section_definition, section, title_context)
    else:
        entire_prompt = get_prompt(rag_text, get_objectives(), section_query, section_definition, section, title_context)
    response = completion(entire_prompt, "gpt-4o")
    return response

def app(page_titles, slides_collection):

    st.write("\n")
    st.write("Request for Proposal Content Generator")
    messages = st.container()
    section = st.selectbox(
    "Select the section to generate PPT content or Select Ask a Question to get answers form the PPT content",
    (tuple(section_question_mapping.keys())),
    )
    if section == "Ask a Question":
        question = st.text_input("Type in your question and hit Enter")
        
        if question.strip():
            response = get_section_content(section, "", question, page_titles, slides_collection)
            st.write("User : ", question)
            st.write("Anvai RPF BOT : ",response)
    else:
        try:
            with open('response_cache.json', 'r') as openfile:
                response_cache = json.load(openfile)
        except Exception as e:
            response_cache = {}
            pass
        if not section in response_cache:
            section_definition = get_definitions(section)
            section_query = section_question_mapping[section]
            response_cache[section] = get_section_content(section, section_definition, section_query, page_titles, slides_collection)
        
        with open('response_cache.json', 'w') as outfile:
            json_object = json.dumps(response_cache)
            outfile.write(json_object)
            
        st.write("User : ", section_question_mapping[section])
        st.write("Anvai RPF BOT : ",response_cache[section])
if __name__=="__main__":
    OPENAI_API_KEY = get_secret()
    download_ppt()
    slides, page_titles = read_ppt()
    #summaries_of_slides, summary_to_index = create_summary(slides,page_titles)
    slides_collection = update_chromadb(slides)
    app(page_titles, slides_collection)