repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import os |
| #2 | from collections.abc import Iterable |
| #3 | from pathlib import Path |
| #4 | from typing import Optional, Union |
| #5 | |
| #6 | from langchain.callbacks.stdout import StdOutCallbackHandler |
| #7 | from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler |
| #8 | |
| #9 | from embedchain.config import BaseLlmConfig |
| #10 | from embedchain.helpers.json_serializable import register_deserializable |
| #11 | from embedchain.llm.base import BaseLlm |
| #12 | |
| #13 | |
| #14 | @register_deserializable |
| #15 | class GPT4ALLLlm(BaseLlm): |
| #16 | def __init__(self, config: Optional[BaseLlmConfig] = None): |
| #17 | super().__init__(config=config) |
| #18 | if self.config.model is None: |
| #19 | self.config.model = "orca-mini-3b-gguf2-q4_0.gguf" |
| #20 | self.instance = GPT4ALLLlm._get_instance(self.config.model) |
| #21 | self.instance.streaming = self.config.stream |
| #22 | |
| #23 | def get_llm_model_answer(self, prompt): |
| #24 | return self._get_answer(prompt=prompt, config=self.config) |
| #25 | |
| #26 | @staticmethod |
| #27 | def _get_instance(model): |
| #28 | try: |
| #29 | from langchain_community.llms.gpt4all import GPT4All as LangchainGPT4All |
| #30 | except ModuleNotFoundError: |
| #31 | raise ModuleNotFoundError( |
| #32 | "The GPT4All python package is not installed. Please install it with `pip install --upgrade embedchain[opensource]`" # noqa E501 |
| #33 | ) from None |
| #34 | |
| #35 | model_path = Path(model).expanduser() |
| #36 | if os.path.isabs(model_path): |
| #37 | if os.path.exists(model_path): |
| #38 | return LangchainGPT4All(model=str(model_path)) |
| #39 | else: |
| #40 | raise ValueError(f"Model does not exist at {model_path=}") |
| #41 | else: |
| #42 | return LangchainGPT4All(model=model, allow_download=True) |
| #43 | |
| #44 | def _get_answer(self, prompt: str, config: BaseLlmConfig) -> Union[str, Iterable]: |
| #45 | if config.model and config.model != self.config.model: |
| #46 | raise RuntimeError( |
| #47 | "GPT4ALLLlm does not support switching models at runtime. Please create a new app instance." |
| #48 | ) |
| #49 | |
| #50 | messages = [] |
| #51 | if config.system_prompt: |
| #52 | messages.append(config.system_prompt) |
| #53 | messages.append(prompt) |
| #54 | kwargs = { |
| #55 | "temp": config.temperature, |
| #56 | "max_tokens": config.max_tokens, |
| #57 | } |
| #58 | if config.top_p: |
| #59 | kwargs["top_p"] = config.top_p |
| #60 | |
| #61 | callbacks = [StreamingStdOutCallbackHandler()] if config.stream else [StdOutCallbackHandler()] |
| #62 | |
| #63 | response = self.instance.generate(prompts=messages, callbacks=callbacks, **kwargs) |
| #64 | answer = "" |
| #65 | for generations in response.generations: |
| #66 | answer += " ".join(map(lambda generation: generation.text, generations)) |
| #67 | return answer |
| #68 |