__init__.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from __future__ import annotations
  2. from ..typing import Union, Messages
  3. from ..locals.provider import LocalProvider
  4. from ..locals.models import get_models
  5. from ..client.client import iter_response, filter_none
  6. from ..client.types import IterResponse
  7. class LocalClient():
  8. def __init__(self, **kwargs) -> None:
  9. self.chat: Chat = Chat(self)
  10. @staticmethod
  11. def list_models():
  12. return list(get_models())
  13. class Completions():
  14. def __init__(self, client: LocalClient):
  15. self.client: LocalClient = client
  16. def create(
  17. self,
  18. messages: Messages,
  19. model: str,
  20. stream: bool = False,
  21. response_format: dict = None,
  22. max_tokens: int = None,
  23. stop: Union[list[str], str] = None,
  24. **kwargs
  25. ) -> IterResponse:
  26. stop = [stop] if isinstance(stop, str) else stop
  27. response = LocalProvider.create_completion(
  28. model, messages, stream,
  29. **filter_none(
  30. max_tokens=max_tokens,
  31. stop=stop,
  32. ),
  33. **kwargs
  34. )
  35. response = iter_response(response, stream, response_format, max_tokens, stop)
  36. return response if stream else next(response)
  37. class Chat():
  38. completions: Completions
  39. def __init__(self, client: LocalClient):
  40. self.completions = Completions(client)