File size: 2,072 Bytes
2a735cc |
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 52 53 54 55 56 57 58 59 |
#!/usr/bin/env python
"""
Update deprecated langchain imports to langchain_community in project files
"""
import os
import re
def update_imports(file_path):
"""Update imports in a single file"""
print(f"Processing {file_path}")
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Define import replacements
replacements = [
(r'from langchain\.vectorstores import (.*)', r'from langchain_community.vectorstores import \1'),
(r'from langchain\.llms import (.*)', r'from langchain_community.llms import \1'),
(r'from langchain\.document_loaders import (.*)', r'from langchain_community.document_loaders import \1'),
(r'from langchain\.embeddings import (.*)', r'from langchain_community.embeddings import \1'),
(r'import langchain\.vectorstores', r'import langchain_community.vectorstores'),
(r'import langchain\.llms', r'import langchain_community.llms'),
(r'import langchain\.document_loaders', r'import langchain_community.document_loaders'),
(r'import langchain\.embeddings', r'import langchain_community.embeddings'),
]
# Apply all replacements
for pattern, replacement in replacements:
content = re.sub(pattern, replacement, content)
# Write updated content back to the file
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
return True
def main():
"""Main function to update all files"""
# Files to update
files_to_update = [
'app/core/memory.py',
'app/core/llm.py',
'app/core/ingestion.py',
'app/core/agent.py'
]
updated_count = 0
for file_path in files_to_update:
if os.path.exists(file_path):
success = update_imports(file_path)
if success:
updated_count += 1
else:
print(f"File not found: {file_path}")
print(f"\nCompleted! Updated {updated_count}/{len(files_to_update)} files.")
if __name__ == "__main__":
main() |