ollama.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import logging
  2. from collections.abc import Iterable
  3. from typing import Optional, Union
  4. from langchain.callbacks.manager import CallbackManager
  5. from langchain.callbacks.stdout import StdOutCallbackHandler
  6. from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
  7. from langchain_community.llms.ollama import Ollama
  8. from ollama import Client
  9. from embedchain.config import BaseLlmConfig
  10. from embedchain.helpers.json_serializable import register_deserializable
  11. from embedchain.llm.base import BaseLlm
  12. logger = logging.getLogger(__name__)
  13. @register_deserializable
  14. class OllamaLlm(BaseLlm):
  15. def __init__(self, config: Optional[BaseLlmConfig] = None):
  16. super().__init__(config=config)
  17. if self.config.model is None:
  18. self.config.model = "llama2"
  19. client = Client(host=config.base_url)
  20. local_models = client.list()["models"]
  21. if not any(model.get("name") == self.config.model for model in local_models):
  22. logger.info(f"Pulling {self.config.model} from Ollama!")
  23. client.pull(self.config.model)
  24. def get_llm_model_answer(self, prompt):
  25. return self._get_answer(prompt=prompt, config=self.config)
  26. @staticmethod
  27. def _get_answer(prompt: str, config: BaseLlmConfig) -> Union[str, Iterable]:
  28. callback_manager = [StreamingStdOutCallbackHandler()] if config.stream else [StdOutCallbackHandler()]
  29. llm = Ollama(
  30. model=config.model,
  31. system=config.system_prompt,
  32. temperature=config.temperature,
  33. top_p=config.top_p,
  34. callback_manager=CallbackManager(callback_manager),
  35. base_url=config.base_url,
  36. )
  37. return llm.invoke(prompt)