mirror of
https://github.com/ParisNeo/lollms.git
synced 2025-03-01 04:06:07 +00:00
Update personality.py
This commit is contained in:
parent
12ca2678c7
commit
f995c304a0
@ -4257,10 +4257,11 @@ transition-all duration-300 ease-in-out">
|
|||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_code(original_content: str, original_code: str, new_code: str) -> str:
|
def update_code_with_best_match(original_content: str, original_code: str, new_code: str) -> str:
|
||||||
"""
|
"""
|
||||||
Updates the original content by replacing the original code snippet with the new code.
|
Updates the original content by replacing the best-matching code snippet with the new code.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
original_content (str): The complete source code content
|
original_content (str): The complete source code content
|
||||||
@ -4271,45 +4272,53 @@ transition-all duration-300 ease-in-out">
|
|||||||
str: Updated content with the replacement made
|
str: Updated content with the replacement made
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
ValueError: If the original code snippet is not found in the content
|
ValueError: If no sufficiently close match is found
|
||||||
or if multiple matches are found
|
|
||||||
"""
|
"""
|
||||||
# Normalize line endings and strip whitespace from search pattern
|
if not pm.is_installed("difflib"):
|
||||||
|
pm.install("difflib")
|
||||||
|
|
||||||
|
import difflib
|
||||||
|
# Normalize line endings and strip whitespace from inputs
|
||||||
original_code = original_code.strip()
|
original_code = original_code.strip()
|
||||||
new_code = new_code.strip()
|
new_code = new_code.strip()
|
||||||
|
|
||||||
# If any of the inputs are empty, raise an error
|
# If any of the inputs are empty, raise an error
|
||||||
if not original_content or not original_code:
|
if not original_content or not original_code:
|
||||||
raise ValueError("Original content and code snippet cannot be empty")
|
raise ValueError("Original content and code snippet cannot be empty")
|
||||||
|
|
||||||
# Find all occurrences of the original code
|
|
||||||
# Using splitlines() and join to normalize line endings
|
|
||||||
normalized_content = '\n'.join(original_content.splitlines())
|
|
||||||
normalized_code = '\n'.join(original_code.splitlines())
|
|
||||||
|
|
||||||
# Split content into lines for more precise matching
|
# Split content into lines for better matching
|
||||||
content_lines = normalized_content.splitlines()
|
content_lines = original_content.splitlines()
|
||||||
code_lines = normalized_code.splitlines()
|
code_lines = original_code.splitlines()
|
||||||
|
|
||||||
|
# Join lines to create a normalized version of the original code
|
||||||
|
normalized_code = '\n'.join(code_lines)
|
||||||
|
|
||||||
|
# Find the best match using difflib
|
||||||
|
best_match = None
|
||||||
|
best_match_ratio = 0
|
||||||
|
best_match_start = None
|
||||||
|
best_match_end = None
|
||||||
|
|
||||||
# Find matches
|
|
||||||
matches = []
|
|
||||||
for i in range(len(content_lines) - len(code_lines) + 1):
|
for i in range(len(content_lines) - len(code_lines) + 1):
|
||||||
|
# Extract a slice of the content to compare
|
||||||
content_slice = '\n'.join(content_lines[i:i + len(code_lines)])
|
content_slice = '\n'.join(content_lines[i:i + len(code_lines)])
|
||||||
if content_slice.strip() == normalized_code.strip():
|
|
||||||
matches.append(i)
|
# Calculate similarity ratio
|
||||||
|
match_ratio = difflib.SequenceMatcher(None, content_slice.strip(), normalized_code.strip()).ratio()
|
||||||
|
|
||||||
|
# Update the best match if this one is better
|
||||||
|
if match_ratio > best_match_ratio:
|
||||||
|
best_match = content_slice
|
||||||
|
best_match_ratio = match_ratio
|
||||||
|
best_match_start = i
|
||||||
|
best_match_end = i + len(code_lines)
|
||||||
|
|
||||||
# Check number of matches
|
# Check if a sufficiently close match was found
|
||||||
if not matches:
|
if best_match is None or best_match_ratio < 0.8: # Threshold for similarity
|
||||||
raise ValueError("Original code snippet not found in content")
|
raise ValueError("No sufficiently close match found for the original code snippet")
|
||||||
if len(matches) > 1:
|
|
||||||
raise ValueError("Multiple matches found for the original code snippet")
|
|
||||||
|
|
||||||
# Perform the replacement
|
|
||||||
start_line = matches[0]
|
|
||||||
end_line = start_line + len(code_lines)
|
|
||||||
|
|
||||||
# Preserve original indentation
|
# Preserve original indentation
|
||||||
first_line = content_lines[start_line]
|
first_line = content_lines[best_match_start]
|
||||||
indentation = ''
|
indentation = ''
|
||||||
for char in first_line:
|
for char in first_line:
|
||||||
if char in (' ', '\t'):
|
if char in (' ', '\t'):
|
||||||
@ -4323,14 +4332,15 @@ transition-all duration-300 ease-in-out">
|
|||||||
|
|
||||||
# Combine everything together
|
# Combine everything together
|
||||||
updated_lines = (
|
updated_lines = (
|
||||||
content_lines[:start_line] +
|
content_lines[:best_match_start] +
|
||||||
new_code_lines +
|
new_code_lines +
|
||||||
content_lines[end_line:]
|
content_lines[best_match_end:]
|
||||||
)
|
)
|
||||||
|
|
||||||
return '\n'.join(updated_lines)
|
return '\n'.join(updated_lines)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def make_title(self, prompt, max_title_length: int = 50):
|
def make_title(self, prompt, max_title_length: int = 50):
|
||||||
"""
|
"""
|
||||||
Generates a title for a given prompt.
|
Generates a title for a given prompt.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user