-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (42 loc) · 1.96 KB
/
utils.py
File metadata and controls
51 lines (42 loc) · 1.96 KB
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
from typing import Dict, List, ClassVar
from llama_index.core.instrumentation.events import BaseEvent
from llama_index.core.instrumentation.event_handlers import BaseEventHandler
from llama_index.core.instrumentation import get_dispatcher
from llama_index.core.instrumentation.events.llm import LLMCompletionEndEvent
class GetTrustworthinessScoreAndReasoning(BaseEventHandler):
events: ClassVar[List[BaseEvent]] = []
trustworthiness_score: float = 0.0
reasoning: str = ""
@classmethod
def class_name(cls) -> str:
"""Class name."""
return "GetTrustworthinessScoreAndReasoning"
def handle(self, event: BaseEvent) -> Dict:
if isinstance(event, LLMCompletionEndEvent):
self.trustworthiness_score = event.response.additional_kwargs[
"trustworthiness_score"
]
self.reasoning = event.response.additional_kwargs[
"explanation"
]
self.events.append(event)
def setup_trustworthiness_handler():
"""Initialize and register the trustworthiness event handler."""
root_dispatcher = get_dispatcher()
event_handler = GetTrustworthinessScoreAndReasoning()
root_dispatcher.add_event_handler(event_handler)
return event_handler
def display_response(response, event_handler):
"""Display the response along with trustworthiness information."""
response_str = response.response
trustworthiness_score = event_handler.trustworthiness_score
reasoning = event_handler.reasoning
print(f"Response: {response_str}")
print(f"Trustworthiness score: {round(trustworthiness_score, 2)}")
print(f"Reasoning: {reasoning}")
def outputs_with_trustworthiness(response, event_handler):
"""Display the response along with trustworthiness information."""
response_str = response.response
trustworthiness_score = event_handler.trustworthiness_score
reasoning = event_handler.reasoning
return response_str, trustworthiness_score, reasoning