File size: 5,788 Bytes
78402dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
"""
Quick Fix for Common Requirements Issues
This script automatically fixes common version problems in requirements.txt
"""

import subprocess
import sys
import re

# Known problematic packages and their fixes
KNOWN_FIXES = {
    "llama-index-readers-file==0.5.0": "llama-index-readers-file==0.4.9",
    "duckduckgo-search==3.9.0": "duckduckgo-search==3.9.3",
    "langchain-core==0.1.0": "langchain-core==0.3.60",
    "langchain-openai==0.0.5": "langchain-openai==0.3.20",
    "langchain-community==0.0.10": "langchain-community==0.3.20",
    "langchain-experimental==0.0.49": "langchain-experimental==0.3.3",
    "langchain-groq==0.0.1": "langchain-groq==0.3.0",
    "langchain-tavily==0.0.1": "langchain-tavily==0.3.0",
    "llama-index-embeddings-openai==0.1.0": "llama-index-embeddings-openai==0.4.1",
    "llama-index-vector-stores-supabase==0.1.0": "llama-index-vector-stores-supabase==0.4.0",
}

def get_latest_version(package_name):
    """Get the latest version of a package from PyPI."""
    try:
        result = subprocess.run(
            [sys.executable, "-m", "pip", "index", "versions", package_name],
            capture_output=True,
            text=True,
            timeout=10
        )
        
        if result.returncode == 0:
            # Parse the output to find available versions
            lines = result.stdout.strip().split('\n')
            for line in lines:
                if 'Available versions:' in line:
                    versions = line.split(':', 1)[1].strip().split(', ')
                    # Filter out pre-release versions
                    stable_versions = [v for v in versions if not any(x in v for x in ['a', 'b', 'rc', 'dev'])]
                    if stable_versions:
                        return stable_versions[0]
                    elif versions:
                        return versions[0]
        return None
    except Exception:
        return None

def fix_requirements_file(filename):
    """Fix common issues in requirements.txt file."""
    print(f"Fixing requirements in {filename}...")
    
    try:
        with open(filename, 'r') as f:
            lines = f.readlines()
    except FileNotFoundError:
        print(f"Error: File '{filename}' not found")
        return False
    
    # Backup original file
    backup_filename = filename + '.backup'
    with open(backup_filename, 'w') as f:
        f.writelines(lines)
    print(f"Created backup: {backup_filename}")
    
    fixed_lines = []
    changes_made = 0
    
    for line in lines:
        original_line = line
        line_stripped = line.strip()
        
        # Skip comments and empty lines
        if not line_stripped or line_stripped.startswith('#'):
            fixed_lines.append(original_line)
            continue
        
        # Check if line matches any known problematic package
        fixed = False
        for problem, fix in KNOWN_FIXES.items():
            if line_stripped == problem:
                fixed_lines.append(fix + '\n')
                print(f"  Fixed: {problem}{fix}")
                changes_made += 1
                fixed = True
                break
        
        if not fixed:
            # Check for LangChain 0.1.x to 0.3.x migration
            if 'langchain' in line_stripped and '==0.0.' in line_stripped:
                package_match = re.match(r'^(langchain[a-zA-Z0-9-_]*)==0\.0\.\d+', line_stripped)
                if package_match:
                    package_name = package_match.group(1)
                    # Try to get latest version
                    latest = get_latest_version(package_name)
                    if latest and latest.startswith('0.3'):
                        new_line = f"{package_name}=={latest}\n"
                        fixed_lines.append(new_line)
                        print(f"  Updated: {line_stripped}{package_name}=={latest}")
                        changes_made += 1
                        fixed = True
            
            # Check for llama-index version consistency
            elif line_stripped.startswith('llama-index=='):
                fixed_lines.append('llama-index==0.12.42\n')
                if line_stripped != 'llama-index==0.12.42':
                    print(f"  Updated: {line_stripped} → llama-index==0.12.42")
                    changes_made += 1
                fixed = True
            elif line_stripped.startswith('llama-index-core=='):
                fixed_lines.append('llama-index-core==0.12.42\n')
                if line_stripped != 'llama-index-core==0.12.42':
                    print(f"  Updated: {line_stripped} → llama-index-core==0.12.42")
                    changes_made += 1
                fixed = True
        
        if not fixed:
            fixed_lines.append(original_line)
    
    # Write fixed file
    with open(filename, 'w') as f:
        f.writelines(fixed_lines)
    
    print(f"\nTotal changes made: {changes_made}")
    
    if changes_made > 0:
        print(f"\n✅ Fixed {filename} successfully!")
        print(f"Backup saved as {backup_filename}")
        return True
    else:
        print("\n✅ No issues found in requirements.txt")
        return True

def main():
    """Main function."""
    filename = 'requirements.txt'
    if len(sys.argv) > 1:
        filename = sys.argv[1]
    
    print("Requirements Quick Fix Tool")
    print("==========================")
    print("This tool fixes common version issues in requirements.txt\n")
    
    if fix_requirements_file(filename):
        print("\nNext steps:")
        print("1. Review the changes")
        print("2. Test installation: pip install -r requirements.txt")
        print("3. If issues persist, use the verify script to check all versions")

if __name__ == "__main__":
    main()