alfraser commited on
Commit
3af3634
·
1 Parent(s): 7a2c982

Added ability to include a message in the trace from the component when it triggers an early exit in the pipeline.

Browse files
Files changed (1) hide show
  1. src/architectures.py +15 -10
src/architectures.py CHANGED
@@ -11,7 +11,7 @@ import traceback
11
  from abc import ABC, abstractmethod
12
  from enum import Enum
13
  from time import time
14
- from typing import List, Optional
15
 
16
  from src.common import config_dir, data_dir, hf_api_token
17
  from src.models import HFLlamaChatModel
@@ -24,9 +24,10 @@ class ArchitectureRequest:
24
  is a stack which can be modified through life.
25
  """
26
  def __init__(self, query: str):
27
- self._request = [query] # Stack for the request text as it evolves down the pipeline
28
- self._response = [] # Stack for the response text as it evolves down the pipeline
29
- self.early_exit = False
 
30
 
31
  @property
32
  def request(self):
@@ -79,14 +80,15 @@ class ArchitectureTraceStep:
79
  self.start_ms = int(time() * 1000)
80
  self.end_ms = None
81
  self.outcome = ArchitectureTraceOutcome.NONE
82
- self._exception = None
 
83
 
84
  def end(self, outcome: ArchitectureTraceOutcome):
85
  self.end_ms = int(time() * 1000)
86
  self.outcome = outcome
87
 
88
  @property
89
- def exception(self):
90
  return self._exception
91
 
92
  @exception.setter
@@ -105,7 +107,7 @@ class ArchitectureTraceStep:
105
  if self.outcome == ArchitectureTraceOutcome.SUCCESS:
106
  outcome = "Success"
107
  elif self.outcome == ArchitectureTraceOutcome.EARLY_EXIT:
108
- outcome = "Early Exit"
109
  elif self.outcome == ArchitectureTraceOutcome.EXCEPTION:
110
  outcome = f"Exception ({self._exception})"
111
  md += f" - **Outcome**: {outcome}"
@@ -124,10 +126,12 @@ class ArchitectureTrace:
124
  def start_trace(self, name: str):
125
  self.steps.append(ArchitectureTraceStep(name=name))
126
 
127
- def end_trace(self, outcome):
128
  assert len(self.steps) > 0
129
  assert self.steps[-1].outcome == ArchitectureTraceOutcome.NONE
130
  self.steps[-1].end(outcome=outcome)
 
 
131
 
132
  def as_markdown(self) -> str:
133
  """
@@ -238,7 +242,8 @@ class Architecture:
238
  try:
239
  component.process_request(request)
240
  if request.early_exit:
241
- trace.end_trace(outcome=ArchitectureTraceOutcome.EARLY_EXIT)
 
242
  break
243
  else:
244
  trace.end_trace(outcome=ArchitectureTraceOutcome.SUCCESS)
@@ -264,6 +269,7 @@ class InputRequestScreener(ArchitectureComponent):
264
  if response[0:2].lower() != 'no': # Lean cautious even if the model fails to return yes/no
265
  request.response = "Sorry - I cannot answer this question. Please try and rephrase it."
266
  request.early_exit = True
 
267
 
268
 
269
  class OutputResponseScreener(ArchitectureComponent):
@@ -276,7 +282,6 @@ class OutputResponseScreener(ArchitectureComponent):
276
  raise ValueError(f'Screener model "meta-llama/Llama-2-7b-chat-hf" not set up')
277
  response = llm(request.response, system_prompt=system_prompt)
278
  if response[0:2].lower() != 'no': # Lean cautious even if the model fails to return yes/no
279
- print(f'OutputResponseScreener responded {response}')
280
  request.response = "Sorry - I cannot answer this question. Please try and rephrase it."
281
  request.early_exit = True
282
 
 
11
  from abc import ABC, abstractmethod
12
  from enum import Enum
13
  from time import time
14
+ from typing import List
15
 
16
  from src.common import config_dir, data_dir, hf_api_token
17
  from src.models import HFLlamaChatModel
 
24
  is a stack which can be modified through life.
25
  """
26
  def __init__(self, query: str):
27
+ self._request: List[str] = [query] # Stack for the request text as it evolves down the pipeline
28
+ self._response: List[str] = [] # Stack for the response text as it evolves down the pipeline
29
+ self.early_exit: bool = False
30
+ self.early_exit_message: str = None
31
 
32
  @property
33
  def request(self):
 
80
  self.start_ms = int(time() * 1000)
81
  self.end_ms = None
82
  self.outcome = ArchitectureTraceOutcome.NONE
83
+ self._exception: str = None
84
+ self.early_exit_message: str = None
85
 
86
  def end(self, outcome: ArchitectureTraceOutcome):
87
  self.end_ms = int(time() * 1000)
88
  self.outcome = outcome
89
 
90
  @property
91
+ def exception(self) -> str:
92
  return self._exception
93
 
94
  @exception.setter
 
107
  if self.outcome == ArchitectureTraceOutcome.SUCCESS:
108
  outcome = "Success"
109
  elif self.outcome == ArchitectureTraceOutcome.EARLY_EXIT:
110
+ outcome = f"Early Exit ({self.early_exit_message})"
111
  elif self.outcome == ArchitectureTraceOutcome.EXCEPTION:
112
  outcome = f"Exception ({self._exception})"
113
  md += f" - **Outcome**: {outcome}"
 
126
  def start_trace(self, name: str):
127
  self.steps.append(ArchitectureTraceStep(name=name))
128
 
129
+ def end_trace(self, outcome: ArchitectureTraceOutcome, early_exit_message: str = None):
130
  assert len(self.steps) > 0
131
  assert self.steps[-1].outcome == ArchitectureTraceOutcome.NONE
132
  self.steps[-1].end(outcome=outcome)
133
+ if early_exit_message is not None:
134
+ self.steps[-1].early_exit_message = early_exit_message
135
 
136
  def as_markdown(self) -> str:
137
  """
 
242
  try:
243
  component.process_request(request)
244
  if request.early_exit:
245
+ trace.end_trace(outcome=ArchitectureTraceOutcome.EARLY_EXIT,
246
+ early_exit_message=request.early_exit_message)
247
  break
248
  else:
249
  trace.end_trace(outcome=ArchitectureTraceOutcome.SUCCESS)
 
269
  if response[0:2].lower() != 'no': # Lean cautious even if the model fails to return yes/no
270
  request.response = "Sorry - I cannot answer this question. Please try and rephrase it."
271
  request.early_exit = True
272
+ request.early_exit_message = response
273
 
274
 
275
  class OutputResponseScreener(ArchitectureComponent):
 
282
  raise ValueError(f'Screener model "meta-llama/Llama-2-7b-chat-hf" not set up')
283
  response = llm(request.response, system_prompt=system_prompt)
284
  if response[0:2].lower() != 'no': # Lean cautious even if the model fails to return yes/no
 
285
  request.response = "Sorry - I cannot answer this question. Please try and rephrase it."
286
  request.early_exit = True
287