rknl commited on
Commit
a1be166
·
verified ·
1 Parent(s): 66d1830

added telcom core

Browse files
Files changed (1) hide show
  1. telcom_core.py +207 -0
telcom_core.py ADDED
@@ -0,0 +1,207 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from llama_index.core import PromptTemplate
3
+ from llama_index.llms.openai import OpenAI
4
+
5
+ teamplate_prompt_upsell = '''You are a virtual assistant for a telecom company, designed to assist users with their queries and potentially upsell services. Your task is to analyze the customer's data from context, their query, and offer the most appropriate assistance.
6
+
7
+ First, you will be given the customer's data context. This information will help you understand the customer's current plan and usage patterns:
8
+
9
+ When interacting with a customer, you will receive a query with their details like name or phone number.
10
+ <query>
11
+ {QUERY}
12
+ </query>
13
+
14
+ Analyze the query to determine the type of assistance required. Categorize it into one of the following:
15
+ 1. Technical Support
16
+ 2. Billing Inquiry
17
+ 3. Plan Information
18
+ 4. Service Upgrade
19
+ 5. General Inquiry
20
+
21
+ Based on the query type and customer data, provide an appropriate response. Your response should:
22
+ 1. Address the customer's immediate concern
23
+ 2. Be clear and concise
24
+ 3. Use a friendly and causal tone
25
+ 4. Make sure to provide facts and relations for each response
26
+ 5. Use Emojis to engage the customer in conversation
27
+
28
+ If the query presents an opportunity for upselling, consider recommending relevant services or upgrades based on the customer's current plan and usage patterns. However, ensure that your primary focus remains on resolving the customer's initial query.
29
+
30
+ Format your response as follows:
31
+
32
+ <response>
33
+ <query_type>[Categorized query type]</query_type>
34
+ <answer>[Your detailed response addressing the customer's query]</answer>
35
+ <reference>[Provide the reference documents used for generating the response]</reference>
36
+ <facts>[Provide the facts used for generating the response]</facts>
37
+ <upsell_opportunity>[If applicable, provide a brief upsell recommendation]</upsell_opportunity>
38
+ </response>
39
+
40
+ Remember to always prioritize customer satisfaction and only suggest upsells when they genuinely benefit the customer.
41
+ '''
42
+
43
+
44
+ llm_eval_prompt = """You are an AI tasked with evaluating the performance of a language model (LLM) based on a given query and response. Your role is to assess the LLM's output using four specific metrics and provide scores for each.
45
+
46
+ Here are the metrics you will use to evaluate the LLM's performance:
47
+
48
+ 1. Comprehensiveness: How thoroughly and completely the response addresses all aspects of the query.
49
+ 2. Diversity: The variety of perspectives, examples, or approaches included in the response.
50
+ 3. Empowerment: How well the response enables the user to understand or act on the information provided.
51
+ 4. Directness: The clarity and conciseness of the response in addressing the query.
52
+
53
+ To perform your evaluation, carefully analyze the following query and response:
54
+
55
+ <query>
56
+ {QUERY}
57
+ </query>
58
+
59
+ <response>
60
+ {RESPONSE}
61
+ </response>
62
+
63
+ For each metric, consider the following:
64
+
65
+ 1. Comprehensiveness: Does the response cover all aspects of the query? Are there any missing or underdeveloped points?
66
+ 2. Diversity: Does the response offer multiple viewpoints or examples? Is there a good range of information or approaches presented?
67
+ 3. Empowerment: Does the response provide actionable information or insights? Does it enhance the user's understanding or ability to address the query?
68
+ 4. Directness: Is the response clear and to the point? Does it avoid unnecessary information or tangents?
69
+
70
+ Score each metric on a scale from 0 to 5, where 0 is the lowest (poor performance) and 5 is the highest (excellent performance).
71
+
72
+ For each metric, provide a brief justification for your score before stating the score itself. Your justification should reference specific aspects of the query and response that influenced your decision.
73
+
74
+ Present your evaluation in the following format:
75
+
76
+ <evaluation>
77
+ <metric name="Comprehensiveness">
78
+ <justification>
79
+ [Your justification for the Comprehensiveness score]
80
+ </justification>
81
+ <score>[Your score from 0-5]</score>
82
+ </metric>
83
+
84
+ <metric name="Diversity">
85
+ <justification>
86
+ [Your justification for the Diversity score]
87
+ </justification>
88
+ <score>[Your score from 0-5]</score>
89
+ </metric>
90
+
91
+ <metric name="Empowerment">
92
+ <justification>
93
+ [Your justification for the Empowerment score]
94
+ </justification>
95
+ <score>[Your score from 0-5]</score>
96
+ </metric>
97
+
98
+ <metric name="Directness">
99
+ <justification>
100
+ [Your justification for the Directness score]
101
+ </justification>
102
+ <score>[Your score from 0-5]</score>
103
+ </metric>
104
+ </evaluation>
105
+
106
+ Ensure that your evaluation is fair, objective, and based solely on the provided query and response. Do not make assumptions about information not present in the given text.
107
+ """
108
+
109
+ def extract_pattern_triplet(text):
110
+ # Define the regex pattern to match the desired format
111
+ pattern = re.compile(r'\b\w+\b\s*->\s*\b\w+\b\s*->\s*\b\w+\b')
112
+ # Find all matches in the text
113
+ matches = pattern.findall(text)
114
+ return "\n <br> ".join(matches)
115
+
116
+ def query_rag_qa(rag_index,query,search_level):
117
+ """
118
+ A function to query the RAG QA with a given query and search level.
119
+ It returns the response, nodes, and response metadata.
120
+ Parameters:
121
+ - query: The query to search for
122
+ - search_level: The level of similarity to search for
123
+ Return:
124
+ - response: The query response
125
+ - nodes: The retrieved nodes
126
+ - metadata: The metadata of the response
127
+ """
128
+ myretriever = rag_index.as_retriever(
129
+ include_text=True, # include source text, default True
130
+ similarity_top_k=search_level,
131
+ )
132
+ query_engine = rag_index.as_query_engine(
133
+ sub_retrievers=[
134
+ myretriever,
135
+ ],
136
+ include_text=True,
137
+ similarity_top_k=search_level,
138
+ )
139
+ response = query_engine.query(query)
140
+ nodes = myretriever.retrieve(query)
141
+
142
+ return response, nodes, response.metadata
143
+
144
+
145
+ def query_graph_rag_qa(graph_rag_index,query,search_level):
146
+ """
147
+ A function to query the RAG QA with a given query and search level.
148
+ It returns the response, reference, and reference text.
149
+ Parameters:
150
+ - query: The query to search for
151
+ - search_level: The level of similarity to search for
152
+ Return:
153
+ - response: The query response
154
+ - reference: The extracted patterns
155
+ - reference_text: The text of the extracted patterns
156
+ """
157
+ myretriever = graph_rag_index.as_retriever(
158
+ include_text=True, # include source text, default True
159
+ similarity_top_k=search_level,
160
+ )
161
+ query_engine = graph_rag_index.as_query_engine(
162
+ sub_retrievers=[
163
+ myretriever,
164
+ ],
165
+ include_text=True,
166
+ similarity_top_k=search_level,
167
+ )
168
+
169
+ data = {'QUERY': query}
170
+
171
+ # prompt = PromptTemplate(template=teamplate_prompt_upsell, input_variables=["QUERY"])
172
+ # prompt = PromptTemplate(teamplate_prompt_upsell) #, input_variables=["QUERY"])
173
+ # query_ready = prompt.format(**data)
174
+ response = query_engine.query(query)
175
+ nodes = myretriever.retrieve(query)
176
+ # parsed_resp = parse_response_with_regex(str(response))
177
+
178
+ reference = []
179
+ reference_text = []
180
+ for node in nodes:
181
+ reference.append(extract_pattern_triplet(node.text))
182
+ reference_text.append(node.text)
183
+
184
+ return response, reference , reference_text
185
+
186
+ def eval_llm(query,rag_response,grag_response):
187
+ data = {'QUERY': query,
188
+ 'RESPONSE': rag_response
189
+ }
190
+ prompt = PromptTemplate(llm_eval_prompt)
191
+ query_ready = prompt.format(**data)
192
+ rag_eval = OpenAI().complete(query_ready)
193
+
194
+ data = {'QUERY': query,
195
+ 'RESPONSE': grag_response
196
+ }
197
+ prompt = PromptTemplate(llm_eval_prompt)
198
+ query_ready = prompt.format(**data)
199
+ grag_eval = OpenAI().complete(query_ready)
200
+ return grag_eval,rag_eval
201
+
202
+
203
+ def plot_full_kg(kg_plot_path):
204
+ """Plot the full knowledge graph and return the HTML representation."""
205
+ # return HTML(filename=kg_plot_path)
206
+ with open(kg_plot_path, "r") as file:
207
+ return file.read()