Ollama.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from __future__ import annotations
  2. import requests
  3. import os
  4. from ..needs_auth.OpenaiAPI import OpenaiAPI
  5. from ...typing import AsyncResult, Messages
  6. class Ollama(OpenaiAPI):
  7. label = "Ollama"
  8. url = "https://ollama.com"
  9. login_url = None
  10. needs_auth = False
  11. working = True
  12. @classmethod
  13. def get_models(cls, api_base: str = None, **kwargs):
  14. if not cls.models:
  15. if api_base is None:
  16. host = os.getenv("OLLAMA_HOST", "127.0.0.1")
  17. port = os.getenv("OLLAMA_PORT", "11434")
  18. url = f"http://{host}:{port}/api/tags"
  19. else:
  20. url = api_base.replace("/v1", "/api/tags")
  21. models = requests.get(url).json()["models"]
  22. cls.models = [model["name"] for model in models]
  23. cls.default_model = cls.models[0]
  24. return cls.models
  25. @classmethod
  26. def create_async_generator(
  27. cls,
  28. model: str,
  29. messages: Messages,
  30. api_base: str = None,
  31. **kwargs
  32. ) -> AsyncResult:
  33. if api_base is None:
  34. host = os.getenv("OLLAMA_HOST", "localhost")
  35. port = os.getenv("OLLAMA_PORT", "11434")
  36. api_base: str = f"http://{host}:{port}/v1"
  37. return super().create_async_generator(
  38. model, messages, api_base=api_base, **kwargs
  39. )