local_packages.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. """Resolve the server at setup; retain verified native embedding packages."""
  2. from __future__ import annotations
  3. import hashlib
  4. import os
  5. import platform
  6. import sys
  7. import tempfile
  8. from pathlib import Path
  9. from urllib.parse import parse_qs, urldefrag, urlparse
  10. import httpx
  11. from packaging.requirements import Requirement
  12. from packaging.specifiers import SpecifierSet
  13. from packaging.tags import sys_tags
  14. from packaging.utils import InvalidWheelFilename, parse_wheel_filename
  15. from packaging.version import InvalidVersion, Version
  16. _CPP = "https://github.com/abetlen/llama-cpp-python/releases/download/"
  17. # macOS uses the working Metal archive. The same release's CPU macOS archive
  18. # fails CRC validation and must not be substituted here.
  19. _PACKAGES = {
  20. ("Darwin", "arm64"): (
  21. "v0.3.30-metal/llama_cpp_python-0.3.30-py3-none-macosx_11_0_arm64.whl",
  22. "4f5b385f31dbda502d7118aff0c07c9e98961fa0402c025b89ff3250db843eb3",
  23. ),
  24. ("Linux", "x86_64"): (
  25. "v0.3.30/llama_cpp_python-0.3.30-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl",
  26. "591337ea31b6fea25f02da53db136041899a530eda1b2c2611f0bebe7c560a15",
  27. ),
  28. ("Linux", "aarch64"): (
  29. "v0.3.30/llama_cpp_python-0.3.30-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl",
  30. "0720e1d122a4d1ad46607813bd34018ff46b0f50cd0a38fb93fad650b3b98ade",
  31. ),
  32. ("Windows", "x86_64"): (
  33. "v0.3.36/llama_cpp_python-0.3.36-py3-none-win_amd64.whl",
  34. "ae5e88a2cf464e5a2dde7c8898c2d5c71cc24d03cac6396bf7509456bde0b9fb",
  35. ),
  36. }
  37. def install_requirements(*, allow_source_build=False):
  38. from .quick_local import OPENVIKING_REQUIREMENT, SourceBuildRequired
  39. system = platform.system()
  40. machine = platform.machine().lower()
  41. machine = {"amd64": "x86_64", "arm64": "arm64" if system == "Darwin" else "aarch64"}.get(
  42. machine, machine
  43. )
  44. packages = _PACKAGES.get((system, machine))
  45. compatible = sys.maxsize > 2**32
  46. if system == "Darwin":
  47. compatible = compatible and Version(platform.mac_ver()[0] or "0") >= Version("14")
  48. elif system == "Linux":
  49. libc, version = platform.libc_ver()
  50. compatible = compatible and libc == "glibc" and Version(version or "0") >= Version("2.31")
  51. if not packages or not compatible:
  52. if not allow_source_build:
  53. raise SourceBuildRequired(
  54. "No reviewed Quick Local binary is available for this platform. "
  55. "Use a separate OpenViking server, or explicitly allow a source build "
  56. "in setup. A build needs native development tools and can take several minutes."
  57. )
  58. requirements = [OPENVIKING_REQUIREMENT, "llama-cpp-python==0.3.30"]
  59. else:
  60. cpp_path, cpp_hash = packages
  61. requirements = [
  62. OPENVIKING_REQUIREMENT,
  63. f"llama-cpp-python @ {_CPP}{cpp_path}#sha256={cpp_hash}",
  64. ]
  65. # The PM resolver otherwise chooses a LiteLLM release that excludes Python
  66. # 3.14. This constraint belongs only to OpenViking's private environment.
  67. return [*requirements, 'litellm==1.83.7; python_version >= "3.14"']
  68. def resolve_server_requirement(*, allow_source_build=False):
  69. """Select a stable PyPI release only during explicit Quick Local setup."""
  70. from .quick_local import QuickLocalSetupError
  71. try:
  72. response = httpx.get("https://pypi.org/pypi/openviking/json", timeout=30)
  73. response.raise_for_status()
  74. return _select_server_requirement(
  75. response.json()["releases"],
  76. python_version=platform.python_version(),
  77. tags=list(sys_tags()),
  78. allow_source_build=allow_source_build,
  79. )
  80. except (httpx.HTTPError, KeyError, TypeError, ValueError) as exc:
  81. raise QuickLocalSetupError(
  82. "Could not check OpenViking releases on PyPI. Check network access and retry setup."
  83. ) from exc
  84. def _select_server_requirement(releases, *, python_version, tags, allow_source_build=False):
  85. from .quick_local import OPENVIKING_REQUIREMENT, QuickLocalSetupError
  86. specifier = Requirement(OPENVIKING_REQUIREMENT).specifier
  87. ranked_tags = {tag: rank for rank, tag in enumerate(tags)}
  88. versions = []
  89. for raw_version, files in releases.items():
  90. try:
  91. version = Version(raw_version)
  92. except InvalidVersion:
  93. continue
  94. if version in specifier and not version.is_prerelease and not version.is_devrelease:
  95. versions.append((version, files))
  96. for version, files in sorted(versions, key=lambda item: item[0], reverse=True):
  97. wheels = []
  98. source_available = False
  99. for file in files:
  100. if file.get("yanked") or python_version not in SpecifierSet(
  101. file.get("requires_python") or ""
  102. ):
  103. continue
  104. if file.get("packagetype") == "sdist":
  105. source_available = True
  106. continue
  107. try:
  108. name, wheel_version, _build, wheel_tags = parse_wheel_filename(file["filename"])
  109. except (InvalidWheelFilename, KeyError):
  110. continue
  111. matches = wheel_tags.intersection(ranked_tags)
  112. if name == "openviking" and wheel_version == version and matches:
  113. wheels.append((min(ranked_tags[tag] for tag in matches), file))
  114. if wheels:
  115. file = min(wheels, key=lambda item: item[0])[1]
  116. url = file["url"]
  117. if (
  118. urlparse(url).scheme != "https"
  119. or urlparse(url).hostname != "files.pythonhosted.org"
  120. ):
  121. raise QuickLocalSetupError("OpenViking's release download must come from PyPI.")
  122. digest = file["digests"]["sha256"]
  123. return f"openviking[local-embed] @ {url}#sha256={digest}"
  124. if allow_source_build and source_available:
  125. return f"openviking[local-embed]=={version}"
  126. raise QuickLocalSetupError(
  127. "No compatible OpenViking release is available for this Python and platform. "
  128. "Use a separate server or explicitly allow a source build in setup."
  129. )
  130. def verified_requirements(requirements: list[str], cache: Path) -> list[str]:
  131. """Give installers local wheels whose bytes match the reviewed pins.
  132. PM's project resolver does not enforce hashes in URL fragments. Checking
  133. before installation also protects the pre-PM path and cache reuse.
  134. """
  135. from .quick_local import QuickLocalSetupError, _prepare_private_directory
  136. verified = []
  137. for value in requirements:
  138. requirement = Requirement(value)
  139. if not requirement.url:
  140. verified.append(value)
  141. continue
  142. url, fragment = urldefrag(requirement.url)
  143. digest = parse_qs(fragment).get("sha256", [""])[0]
  144. if len(digest) != 64 or any(c not in "0123456789abcdef" for c in digest):
  145. raise QuickLocalSetupError("Quick Local's native package needs a reviewed SHA-256 pin.")
  146. _prepare_private_directory(cache)
  147. directory = cache / digest
  148. _prepare_private_directory(directory)
  149. wheel = directory / Path(urlparse(url).path).name
  150. if not wheel.name.endswith(".whl") or wheel.is_symlink():
  151. raise QuickLocalSetupError("Quick Local's native package must be a private wheel file.")
  152. if not wheel.is_file() or _sha256(wheel) != digest:
  153. fd, temporary = tempfile.mkstemp(dir=directory, suffix=".download")
  154. try:
  155. checksum = hashlib.sha256()
  156. with os.fdopen(fd, "wb") as output:
  157. with httpx.stream("GET", url, follow_redirects=True, timeout=60) as response:
  158. response.raise_for_status()
  159. for chunk in response.iter_bytes(1024 * 1024):
  160. checksum.update(chunk)
  161. output.write(chunk)
  162. if checksum.hexdigest() != digest:
  163. raise QuickLocalSetupError(
  164. "Quick Local's native package failed SHA-256 verification. Installation was cancelled."
  165. )
  166. os.replace(temporary, wheel)
  167. finally:
  168. Path(temporary).unlink(missing_ok=True)
  169. extras = f"[{','.join(sorted(requirement.extras))}]" if requirement.extras else ""
  170. marker = f" ; {requirement.marker}" if requirement.marker else ""
  171. verified.append(f"{requirement.name}{extras} @ {wheel.absolute().as_uri()}{marker}")
  172. return verified
  173. def _sha256(path: Path) -> str:
  174. with path.open("rb") as file:
  175. return hashlib.file_digest(file, "sha256").hexdigest()