File size: 9,646 Bytes
411adbd
5fed436
411adbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61bb151
411adbd
 
 
 
 
5fed436
 
 
411adbd
5fed436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61bb151
 
 
 
 
 
5fed436
61bb151
411adbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import generationhelper
import json

def evaluate_response_with_prompt(templete, query, documents, answer, eval_model="llama-3.3-70b-specdec"):

    formatted_documents = ""
    for doc_idx, doc_text in enumerate(documents["document"]):
      if isinstance(doc_text, list):
          doc_text = " ".join(doc_text)  # Convert list to a single string
      sentences = doc_text.split('. ')
      formatted_documents += "\n".join([f"{doc_idx}{chr(97 + i)}. {sent}" for i, sent in enumerate(sentences)]) + "\n"

    # Format response with unique keys (a, b, c)
    formatted_answer = "\n".join([f"{chr(97 + i)}. {sent}" for i, sent in enumerate(answer.split('. '))])

    prompt = templete.format(documents=formatted_documents, question=query, answer=formatted_answer)

    # Call the LLM API (Llama 3.3-70B)
    completion = generationhelper.groq_client.chat.completions.create(
        model=eval_model,
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7,
        max_tokens=2048,
        top_p=1
    )

    print("\nGenerated Response:\n", completion)

    return completion

def FormatAndScores(query, documents, answer, eval_model):
   templete= get_templet_to_calculatescores()

   completion_results = evaluate_response_with_prompt(templete, query,documents, answer, eval_model)

   print(completion_results)
   completion_results_response = completion_results.choices[0].message.content
   completion_results_response = completion_results_response.strip().strip('```')
   print(completion_results_response)

   # Check if response_content is empty
   if not completion_results_response.strip():
      raise ValueError("Empty response content")
   
   # Decode if it's a byte string
   if isinstance(completion_results_response, bytes):
      completion_results_response = completion_results_response.decode('utf-8')

   # Try to parse JSON
   try:
        data_json = json.loads(completion_results_response)
        print("JSON parsed successfully:")
        print(data_json)
   except json.JSONDecodeError as e:
        print(f"Failed to parse JSON: {e}")
        print(f"Response content: {completion_results_response}")

   relavance_explanation = data_json['relevance_explanation']
   relevant_sentence_keys = data_json['all_relevant_sentence_keys']
   overall_supported_explanation = data_json['overall_supported_explanation']
   overall_supported = data_json['overall_supported']
   sentence_support_information = data_json['sentence_support_information']
   all_utilized_sentence_keys = data_json['all_utilized_sentence_keys']

   support_keys = []
   support_level = []
   for sentence_support in sentence_support_information:
     support_keys += sentence_support['supporting_sentence_keys']
     support_level.append(sentence_support['fully_supported'])

   print(relavance_explanation)
   print(relevant_sentence_keys)
   print(overall_supported_explanation)
   print(overall_supported)
   print(sentence_support_information)
   print(all_utilized_sentence_keys)

   return completion_results_response,relevant_sentence_keys,all_utilized_sentence_keys,support_keys,support_level



def get_templet_to_calculatescores():
  return """
      You asked someone to answer a question based on one or more documents.
      Your task is to review their response and assess whether or not each sentence
      in that response is supported by text in the documents. And if so, which
      sentences in the documents provide that support. You will also tell me which
      of the documents contain useful information for answering the question, and
      which of the documents the answer was sourced from.
      Here are the documents, each of which is split into sentences. Alongside each
      sentence is an associated key, such as '0a.' or '0b.' that you can use to refer
      to it:
      β€˜β€™β€™
      {documents}
      β€˜β€™β€™
      The question was:
      β€˜β€™β€™
      {question}
      β€˜β€™β€™
      Here is their response, split into sentences. Alongside each sentence is
      an associated key, such as 'a.' or 'b.' that you can use to refer to it. Note
      that these keys are unique to the response, and are not related to the keys
      in the documents:
      β€˜β€™β€™
      {answer}
      β€˜β€™β€™
      You must respond with a JSON object matching this schema:
      β€˜β€™β€™
      {{
      "relevance_explanation": string,
      "all_relevant_sentence_keys": [string],
      "overall_supported_explanation": string,
      "overall_supported": boolean,
      "sentence_support_information": [
      {{
      "response_sentence_key": string,
      "explanation": string,
      "supporting_sentence_keys": [string],
      "fully_supported": boolean
      }},
      ],
      "all_utilized_sentence_keys": [string]
      }}
      β€˜β€™β€™
      The relevance_explanation field is a string explaining which documents
      contain useful information for answering the question. Provide a step-by-step
      breakdown of information provided in the documents and how it is useful for
      answering the question.
      The all_relevant_sentence_keys field is a list of all document sentences keys
      (e.g. '0a') that are relevant to the question. Include every sentence that is
      useful and relevant to the question, even if it was not used in the response,
      or if only parts of the sentence are useful. Ignore the provided response when
      making this judgment and base your judgment solely on the provided documents
      and question. Omit sentences that, if removed from the document, would not
      impact someone's ability to answer the question.
      The overall_supported_explanation field is a string explaining why the response
      *as a whole* is or is not supported by the documents. In this field, provide a
      step-by-step breakdown of the claims made in the response and the support (or
      lack thereof) for those claims in the documents. Begin by assessing each claim
      separately, one by one; don't make any remarks about the response as a whole
      until you have assessed all the claims in isolation.
      The overall_supported field is a boolean indicating whether the response as a
      whole is supported by the documents. This value should reflect the conclusion
      you drew at the end of your step-by-step breakdown in overall_supported_explanation.
      In the sentence_support_information field, provide information about the support
      *for each sentence* in the response.
      The sentence_support_information field is a list of objects, one for each sentence
      in the response. Each object MUST have the following fields:
      - response_sentence_key: a string identifying the sentence in the response.
      This key is the same as the one used in the response above.
      - explanation: a string explaining why the sentence is or is not supported by the
      documents.
      - supporting_sentence_keys: keys (e.g. '0a') of sentences from the documents that
      support the response sentence. If the sentence is not supported, this list MUST
      be empty. If the sentence is supported, this list MUST contain one or more keys.
      In special cases where the sentence is supported, but not by any specific sentence,
      you can use the string "supported_without_sentence" to indicate that the sentence
      is generally supported by the documents. Consider cases where the sentence is
      expressing inability to answer the question due to lack of relevant information in
      the provided context as "supported_without_sentence". In cases where the
      sentence is making a general statement (e.g. outlining the steps to produce an answer, or
      summarizing previously stated sentences, or a transition sentence), use the
      string "general". In cases where the sentence is correctly stating a well-known fact,
      like a mathematical formula, use the string "well_known_fact". In cases where the
      sentence is performing numerical reasoning (e.g. addition, multiplication), use the
      string "numerical_reasoning".
      - fully_supported: a boolean indicating whether the sentence is fully supported by
      the documents.
      - This value should reflect the conclusion you drew at the end of your step-by-step
      breakdown in explanation.
      - If supporting_sentence_keys is an empty list, then fully_supported must be false.
      - Otherwise, use fully_supported to clarify whether everything in the response
      sentence is fully supported by the document text indicated in supporting_sentence_keys
      (fully_supported = true), or whether the sentence is only partially or incompletely
      supported by that document text (fully_supported = false).
      The all_utilized_sentence_keys field is a list of all sentences keys (e.g. '0a') that
      were used to construct the answer. Include every sentence that either directly supported
      the answer, or was implicitly used to construct the answer, even if it was not used
      in its entirety. Omit sentences that were not used and could have been removed from
      the documents without affecting the answer.
      You must respond with a valid JSON string. Use escapes for quotes, e.g. '\\\\"', and
      newlines, e.g. '\\\\n'. Do not write anything before or after the JSON string. Do not
      wrap the JSON string in backticks like '\\`' or '\\`json.
      As a reminder: your task is to review the response and assess which documents contain
      useful information pertaining to the question, and how each sentence in the response
      is supported by the text in the documents.
      """