File size: 9,566 Bytes
ee96e07
 
 
564b273
ee96e07
 
564b273
 
 
 
2c0bc7b
 
 
 
 
ee96e07
bca671c
0fa1995
 
 
c222558
 
 
0fa1995
 
0186a53
ee96e07
564b273
ee96e07
 
 
564b273
ee96e07
 
 
564b273
 
ee96e07
 
 
 
 
2c0bc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bb64b5
2c0bc7b
 
 
 
 
 
 
 
 
2bb64b5
2c0bc7b
 
 
 
 
 
 
 
 
 
 
bca671c
2c0bc7b
 
 
 
 
 
e6a0fdd
564b273
 
 
 
 
 
 
 
2c0bc7b
 
9ef50f6
2c0bc7b
564b273
 
 
b177ad3
fef584f
564b273
 
2c0bc7b
564b273
 
 
c222558
564b273
 
c222558
 
 
 
564b273
 
bca671c
564b273
9361c0b
 
564b273
 
 
966a5f8
d382891
5b83d67
d382891
966a5f8
5b83d67
 
 
 
 
564b273
2c0bc7b
 
 
966a5f8
06554f3
564b273
0fa1995
564b273
 
 
0fa1995
564b273
 
 
 
 
2c0bc7b
 
564b273
0fa1995
564b273
 
2c0bc7b
564b273
c222558
564b273
 
 
 
 
c222558
 
 
 
 
9361c0b
564b273
 
9361c0b
564b273
 
 
 
2c0bc7b
 
 
 
 
 
 
 
bca671c
2c0bc7b
 
 
 
 
3096734
bca671c
3096734
bca671c
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
import os
import streamlit as st
from datetime import datetime
import json
import requests
import uuid
from datetime import date, datetime
import requests
from pydantic import BaseModel, Field
from typing import Optional
from retriver import retriever
import pandas as pd
import os

df_chunks = pd.read_pickle('Chunks_Complete.pkl')

placeHolderPersona1 = """##Mission
Please create a highly targeted query for a semantic search engine. The query must represent the conversation to date. 
** You will be given the converstaion to date in the user prompt.
** If no converstaion provided then this is the first converstaion

##Rules
Ensure the query is concise
Do not respond with anything other than the query for the Semantic Search Engine.
Respond with just a plain string """

class ChatRequestClient(BaseModel):

    user_id: str
    user_input: str
    numberOfQuestions: int
    welcomeMessage: str
    llm1: str
    tokens1: int
    temperature1: float
    persona1SystemMessage: str
    persona2SystemMessage: str
    userMessage2: str
    llm2: str
    tokens2: int
    temperature2: float


def genuuid ():
    return uuid.uuid4()

def format_elapsed_time(time):
    # Format the elapsed time to two decimal places
    return "{:.2f}".format(time)

def process_search_results(search_results):
    """
    Processes search results to extract and organize metadata and other details.

    :param search_results: List of search result matches from Pinecone.
    :return: A list of dictionaries containing relevant metadata and scores.
    """
    processed_results = []

    for result in search_results:
        processed_results.append({
            "id": result['id'],
            "score": result['score'],
            "Title": result['metadata'].get('Title', ''),
            "ChunkText": result['metadata'].get('ChunkText', ''),
            "PageNumber": result['metadata'].get('PageNumber', ''),
            "Chunk": result['metadata'].get('Chunk', '')
        })

    return processed_results

def reconstruct_text_from_chunks(df_chunks):
    """
    Reconstructs a single string of text from the chunks in the DataFrame.

    :param df_chunks: DataFrame with columns ['Title', 'Chunk', 'ChunkText', 'TokenCount', 'PageNumber', 'ChunkID']
    :return: A string combining all chunk texts in order.
    """
    return " ".join(df_chunks.sort_values(by=['Chunk'])['ChunkText'].tolist())

def lookup_related_chunks(df_chunks, chunk_id):
    """
    Returns all chunks matching the title and page number of the specified chunk ID,
    including chunks from the previous and next pages, handling edge cases where
    there is no preceding or succeeding page.

    :param df_chunks: DataFrame with columns ['Title', 'Chunk', 'ChunkText', 'TokenCount', 'PageNumber', 'ChunkID']
    :param chunk_id: The unique ID of the chunk to look up.
    :return: DataFrame with all chunks matching the title and page range of the specified chunk ID.
    """
    target_chunk = df_chunks[df_chunks['ChunkID'] == chunk_id]
    if target_chunk.empty:
        raise ValueError("Chunk ID not found")

    title = target_chunk.iloc[0]['Title']
    page_number = target_chunk.iloc[0]['PageNumber']

    # Determine the valid page range
    min_page = df_chunks[df_chunks['Title'] == title]['PageNumber'].min()
    max_page = df_chunks[df_chunks['Title'] == title]['PageNumber'].max()

    page_range = [page for page in [page_number - 1, page_number, page_number + 1] if min_page <= page <= max_page]

    return df_chunks[(df_chunks['Title'] == title) & (df_chunks['PageNumber'].isin(page_range))]


def search_and_reconstruct(query, df_chunks, k):
    """
    Combines search, lookup of related chunks, and text reconstruction.

    :param query: The query string to search for.
    :param df_chunks: DataFrame with chunk data.
    :param namespace: Pinecone namespace to search within.
    :param top_k: Number of top search results to retrieve.
    :return: A list of dictionaries with document title, page number, and reconstructed text.
    """
    search_results = retriever(query, k)
    processed_results = process_search_results(search_results)

    reconstructed_results = []

    for result in processed_results:
        chunk_id = result['id']
        related_chunks = lookup_related_chunks(df_chunks, chunk_id)
        reconstructed_text = reconstruct_text_from_chunks(related_chunks)

        reconstructed_results.append({
            "Title": result['Title'],
            "score": result['score'],
            "PageNumber": result['PageNumber'],
            "ReconstructedText": reconstructed_text
        })

    return reconstructed_results

def call_chat_api(data: ChatRequestClient, k):
    url = "https://agent-builder-api.greensea-b20be511.northeurope.azurecontainerapps.io/chat/"
    # Validate and convert the data to a dictionary
    validated_data = data.dict()
    
    # Make the POST request to the FastAPI server
    response = requests.post(url, json=validated_data)
    
    if response.status_code == 200:
        body = response.json()
        query = body.get("content")
        final_results = search_and_reconstruct(query, df_chunks, k)
        return body, final_results # Return the JSON response if successful
    else:
        return "An error occured"  # Return the raw response text if not successful



# Title of the application
# st.image('agentBuilderLogo.png')
st.title('RAG Design and Evaluator')

# Sidebar for inputting personas
st.sidebar.image('cognizant_logo.jpg')
st.sidebar.header("Query Designer")
# st.sidebar.subheader("Welcome Message")
# welcomeMessage = st.sidebar.text_area("Define Intake Persona", value=welcomeMessage, height=300)
st.sidebar.subheader("Query Designer Config")
# numberOfQuestions = st.sidebar.slider("Number of Questions", min_value=0, max_value=10, step=1, value=5, key='persona1_questions')
persona1SystemMessage = st.sidebar.text_area("Query Designer System Message", value=placeHolderPersona1, height=300)

llm1 = st.sidebar.selectbox("Model Selection", ['GPT-4', 'GPT3.5'], key='persona1_size')
temp1 = st.sidebar.slider("Temperature", min_value=0.0, max_value=1.0, step=0.1, value=0.6, key='persona1_temp')
tokens1 = st.sidebar.slider("Tokens", min_value=0, max_value=4000, step=100, value=500, key='persona1_tokens')

k = st.sidebar.slider("Returned Docs", min_value=1, max_value=10, step=1, value=3, key='k')

st.sidebar.caption(f"Session ID: {genuuid()}")

# Main chat interface
st.markdown("""#### Query Translation in RAG Architecture

Query translation in a Retrieval-Augmented Generation (RAG) architecture is the process where an LLM acts as a translator between the user's natural language input and the retrieval system. 

##### Key Functions of Query Translation:
1. **Adds Context**  
   The LLM enriches the user's input with relevant context (e.g., expanding vague questions or specifying details) to make it more precise.
   
2. **Converts to Concise Query**  
   The LLM reformulates the input into a succinct and effective query optimized for the retrieval system's semantic search capabilities.

3. **Uses Concise Query to serach Vector DB**  
   The query is used to search the vector DB for suitable grounding information. 
            
##### Purpose
This ensures that the retrieval system receives a clear and focused query, increasing the relevance of the information it retrieves. The query translator acts as a bridge between human conversational language and the technical requirements of a semantic retrieval system.""")
# User ID Input
user_id = st.text_input("Experiment ID:", key="user_id")

# Ensure user_id is defined or fallback to a default value
if not user_id:
    st.warning("Please provide an experiment ID to start the chat.")
else:
    # Initialize chat history in session state
    if "messages" not in st.session_state:
        st.session_state.messages = []

    retrival = []
    response = {}

    if user_input := st.chat_input("Start chat:"):
        st.session_state.messages.append({"role": "user", "content": user_input})
        data = ChatRequestClient(
            user_id=user_id,
            user_input=user_input,
            numberOfQuestions=1000,
            welcomeMessage="",
            llm1=llm1,
            tokens1=tokens1,
            temperature1=temp1,
            persona1SystemMessage=persona1SystemMessage,
            persona2SystemMessage="",
            userMessage2="",
            llm2="GPT3.5",
            tokens2=1000,
            temperature2=0.2
        
        )

        response, retrival = call_chat_api(data, k)
        agent_message = response.get("content", "No response received from the agent.")
        elapsed_time = response.get("elapsed_time", 0)
        st.session_state.messages.append({"role": "assistant", "content": agent_message})

    col1, col2 = st.columns(2)

    with col1:
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])

        if response:
            st.chat_message("assistant").markdown(response.get("content", "No response"))
            st.caption(f"##### Time taken: {format_elapsed_time(response.get('elapsed_time', 0))} seconds")

    with col2:
        for entry in retrival:
            with st.container():
                st.write(f"**Title:** {entry['Title']}")
                st.write(f"**Score** {entry['score']}")
                st.write(f"**Page Number:** {entry['PageNumber']}")
                st.write("Grounding Text", entry['ReconstructedText'], height=150)