MCPcopy Create free account
hub / github.com/abetlen/llama-cpp-python / transform

Method transform

examples/server/server.py:377–503  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

375 return f'"{text}"' if is_literal else text
376
377 def transform() -> Tuple[str, bool]:
378 nonlocal index
379 start = index
380 sequence: List[Tuple[str, bool]] = []
381
382 def get_dot() -> str:
383 rule = self.DOTALL if self._dotall else self.DOT
384 return self._add_rule("dot", rule)
385
386 def join_sequence() -> Tuple[str, bool]:
387 if len(sequence) == 1:
388 return sequence[0]
389 return (" ".join(to_rule(item) for item in sequence), False)
390
391 while index < length:
392 char = pattern[index]
393 if char == ".":
394 sequence.append((get_dot(), False))
395 index += 1
396 elif char == "(":
397 index += 1
398 if index < length:
399 assert pattern[index] != "?", (
400 f'Unsupported pattern syntax "{pattern[index]}" at index {index} of /{pattern}/'
401 )
402 sequence.append((f"({to_rule(transform())})", False))
403 elif char == ")":
404 index += 1
405 assert start > 0 and pattern[start - 1] == "(", (
406 f"Unbalanced parentheses; start = {start}, index = {index}, pattern = {pattern}"
407 )
408 return join_sequence()
409 elif char == "[":
410 square_brackets = char
411 index += 1
412 while index < length and pattern[index] != "]":
413 if pattern[index] == "\\":
414 square_brackets += pattern[index : index + 2]
415 index += 2
416 else:
417 square_brackets += pattern[index]
418 index += 1
419 assert index < length, (
420 f"Unbalanced square brackets; start = {start}, index = {index}, pattern = {pattern}"
421 )
422 square_brackets += "]"
423 index += 1
424 sequence.append((square_brackets, False))
425 elif char == "|":
426 sequence.append(("|", False))
427 index += 1
428 elif char in ("*", "+", "?"):
429 sequence[-1] = (to_rule(sequence[-1]) + char, False)
430 index += 1
431 elif char == "{":
432 curly_brackets = char
433 index += 1
434 while index < length and pattern[index] != "}":

Callers

nothing calls this directly

Calls 3

_add_ruleMethod · 0.95
_build_repetitionMethod · 0.95
appendMethod · 0.80

Tested by

no test coverage detected