gpt4all.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from typing import Iterable, Optional, Union
  2. from langchain.callbacks.stdout import StdOutCallbackHandler
  3. from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
  4. from embedchain.config import BaseLlmConfig
  5. from embedchain.helper.json_serializable import register_deserializable
  6. from embedchain.llm.base import BaseLlm
  7. @register_deserializable
  8. class GPT4ALLLlm(BaseLlm):
  9. def __init__(self, config: Optional[BaseLlmConfig] = None):
  10. super().__init__(config=config)
  11. if self.config.model is None:
  12. self.config.model = "orca-mini-3b-gguf2-q4_0.gguf"
  13. self.instance = GPT4ALLLlm._get_instance(self.config.model)
  14. self.instance.streaming = self.config.stream
  15. def get_llm_model_answer(self, prompt):
  16. return self._get_answer(prompt=prompt, config=self.config)
  17. @staticmethod
  18. def _get_instance(model):
  19. try:
  20. from langchain.llms.gpt4all import GPT4All as LangchainGPT4All
  21. except ModuleNotFoundError:
  22. raise ModuleNotFoundError(
  23. "The GPT4All python package is not installed. Please install it with `pip install --upgrade embedchain[opensource]`" # noqa E501
  24. ) from None
  25. return LangchainGPT4All(model=model, allow_download=True)
  26. def _get_answer(self, prompt: str, config: BaseLlmConfig) -> Union[str, Iterable]:
  27. if config.model and config.model != self.config.model:
  28. raise RuntimeError(
  29. "GPT4ALLLlm does not support switching models at runtime. Please create a new app instance."
  30. )
  31. messages = []
  32. if config.system_prompt:
  33. messages.append(config.system_prompt)
  34. messages.append(prompt)
  35. kwargs = {
  36. "temp": config.temperature,
  37. "max_tokens": config.max_tokens,
  38. }
  39. if config.top_p:
  40. kwargs["top_p"] = config.top_p
  41. callbacks = [StreamingStdOutCallbackHandler()] if config.stream else [StdOutCallbackHandler()]
  42. response = self.instance.generate(prompts=messages, callbacks=callbacks, **kwargs)
  43. answer = ""
  44. for generations in response.generations:
  45. answer += " ".join(map(lambda generation: generation.text, generations))
  46. return answer