|
| 1 | +from superagi.config.config import get_config |
| 2 | +from superagi.lib.logger import logger |
| 3 | +from superagi.llms.base_llm import BaseLlm |
| 4 | +from superagi.helper.llm_loader import LLMLoader |
| 5 | + |
| 6 | + |
| 7 | +class LocalLLM(BaseLlm): |
| 8 | + def __init__(self, temperature=0.6, max_tokens=get_config("MAX_MODEL_TOKEN_LIMIT"), top_p=1, |
| 9 | + frequency_penalty=0, |
| 10 | + presence_penalty=0, number_of_results=1, model=None, api_key='EMPTY', context_length=4096): |
| 11 | + """ |
| 12 | + Args: |
| 13 | + model (str): The model. |
| 14 | + temperature (float): The temperature. |
| 15 | + max_tokens (int): The maximum number of tokens. |
| 16 | + top_p (float): The top p. |
| 17 | + frequency_penalty (float): The frequency penalty. |
| 18 | + presence_penalty (float): The presence penalty. |
| 19 | + number_of_results (int): The number of results. |
| 20 | + """ |
| 21 | + self.model = model |
| 22 | + self.api_key = api_key |
| 23 | + self.temperature = temperature |
| 24 | + self.max_tokens = max_tokens |
| 25 | + self.top_p = top_p |
| 26 | + self.frequency_penalty = frequency_penalty |
| 27 | + self.presence_penalty = presence_penalty |
| 28 | + self.number_of_results = number_of_results |
| 29 | + self.context_length = context_length |
| 30 | + |
| 31 | + llm_loader = LLMLoader(self.context_length) |
| 32 | + self.llm_model = llm_loader.model |
| 33 | + self.llm_grammar = llm_loader.grammar |
| 34 | + |
| 35 | + def chat_completion(self, messages, max_tokens=get_config("MAX_MODEL_TOKEN_LIMIT")): |
| 36 | + """ |
| 37 | + Call the chat completion. |
| 38 | +
|
| 39 | + Args: |
| 40 | + messages (list): The messages. |
| 41 | + max_tokens (int): The maximum number of tokens. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + dict: The response. |
| 45 | + """ |
| 46 | + try: |
| 47 | + if self.llm_model is None or self.llm_grammar is None: |
| 48 | + logger.error("Model not found.") |
| 49 | + return {"error": "Model loading error", "message": "Model not found. Please check your model path and try again."} |
| 50 | + else: |
| 51 | + response = self.llm_model.create_chat_completion(messages=messages, functions=None, function_call=None, temperature=self.temperature, top_p=self.top_p, |
| 52 | + max_tokens=int(max_tokens), presence_penalty=self.presence_penalty, frequency_penalty=self.frequency_penalty, grammar=self.llm_grammar) |
| 53 | + content = response["choices"][0]["message"]["content"] |
| 54 | + logger.info(content) |
| 55 | + return {"response": response, "content": content} |
| 56 | + |
| 57 | + except Exception as exception: |
| 58 | + logger.info("Exception:", exception) |
| 59 | + return {"error": "ERROR", "message": "Error: "+str(exception)} |
| 60 | + |
| 61 | + def get_source(self): |
| 62 | + """ |
| 63 | + Get the source. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + str: The source. |
| 67 | + """ |
| 68 | + return "Local LLM" |
| 69 | + |
| 70 | + def get_api_key(self): |
| 71 | + """ |
| 72 | + Returns: |
| 73 | + str: The API key. |
| 74 | + """ |
| 75 | + return self.api_key |
| 76 | + |
| 77 | + def get_model(self): |
| 78 | + """ |
| 79 | + Returns: |
| 80 | + str: The model. |
| 81 | + """ |
| 82 | + return self.model |
| 83 | + |
| 84 | + def get_models(self): |
| 85 | + """ |
| 86 | + Returns: |
| 87 | + list: The models. |
| 88 | + """ |
| 89 | + return self.model |
| 90 | + |
| 91 | + def verify_access_key(self, api_key): |
| 92 | + return True |
0 commit comments