MCPcopy Create free account
hub / github.com/OpenBMB/BMTools / __log_probs

Method __log_probs

bmtools/tools/database/utils/llm.py:191–238  ·  view source on GitHub ↗

Returns the log probs of the text.

(self, text, log_prob_range=None)

Source from the content-addressed store, hash-verified

189 return response['choices']
190
191 def __log_probs(self, text, log_prob_range=None):
192 """Returns the log probs of the text."""
193 if not isinstance(text, list):
194 text = [text]
195 if log_prob_range is not None:
196 for i in range(len(text)):
197 lower_index, upper_index = log_prob_range[i]
198 assert lower_index < upper_index
199 assert lower_index >= 0
200 assert upper_index - 1 < len(text[i])
201 config = self.config['gpt_config'].copy()
202 config['logprobs'] = 1
203 config['echo'] = True
204 config['max_tokens'] = 0
205 if isinstance(text, list):
206 text = [f'\n{text[i]}' for i in range(len(text))]
207 else:
208 text = f'\n{text}'
209 response = None
210 while response is None:
211 try:
212 response = openai.Completion.create(
213 **config, prompt=text)
214 except Exception as e:
215 print(e)
216 print('Retrying...')
217 time.sleep(5)
218 log_probs = [response['choices'][i]['logprobs']['token_logprobs'][1:]
219 for i in range(len(response['choices']))]
220 tokens = [response['choices'][i]['logprobs']['tokens'][1:]
221 for i in range(len(response['choices']))]
222 offsets = [response['choices'][i]['logprobs']['text_offset'][1:]
223 for i in range(len(response['choices']))]
224
225 # Subtract 1 from the offsets to account for the newline
226 for i in range(len(offsets)):
227 offsets[i] = [offset - 1 for offset in offsets[i]]
228
229 if log_prob_range is not None:
230 # First, we need to find the indices of the tokens in the log probs
231 # that correspond to the tokens in the log_prob_range
232 for i in range(len(log_probs)):
233 lower_index, upper_index = self.get_token_indices(
234 offsets[i], log_prob_range[i])
235 log_probs[i] = log_probs[i][lower_index:upper_index]
236 tokens[i] = tokens[i][lower_index:upper_index]
237
238 return log_probs, tokens
239
240 def get_token_indices(self, offsets, log_prob_range):
241 """Returns the indices of the tokens in the log probs that correspond to the tokens in the log_prob_range."""

Callers 1

log_probsMethod · 0.95

Calls 1

get_token_indicesMethod · 0.95

Tested by

no test coverage detected