Ollama.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. needs_auth = False
  10. working = True
  11. @classmethod
  12. def get_models(cls):
  13. if not cls.models:
  14. host = os.getenv("OLLAMA_HOST", "127.0.0.1")
  15. port = os.getenv("OLLAMA_PORT", "11434")
  16. url = f"http://{host}:{port}/api/tags"
  17. models = requests.get(url).json()["models"]
  18. cls.models = [model["name"] for model in models]
  19. cls.default_model = cls.models[0]
  20. return cls.models
  21. @classmethod
  22. def create_async_generator(
  23. cls,
  24. model: str,
  25. messages: Messages,
  26. api_base: str = None,
  27. **kwargs
  28. ) -> AsyncResult:
  29. if not api_base:
  30. host = os.getenv("OLLAMA_HOST", "localhost")
  31. port = os.getenv("OLLAMA_PORT", "11434")
  32. api_base: str = f"http://{host}:{port}/v1"
  33. return super().create_async_generator(
  34. model, messages, api_base=api_base, **kwargs
  35. )