Source code for llms.llm

from abc import abstractmethod
from typing import Any

from pydantic import BaseModel


[docs] class BaseLLM(BaseModel): """ **Description:** This class serves as a base class for developing new LLM interfaces connecting to online or local LLMs. Each new LLM should implement the **generate**, **parse_response**, and **prepare_prompt** method. The generate method is what is called in high level and the implementation should get the input `query` and send it to the desired LLM and return the result. It is important to note that these LLM classes should not implement any extra logic rather than just sending the query over and return the generated response by the LLM. Look at :ref:`openai` for sample implementation. """ class Config: """Configuration for this pydantic object.""" arbitrary_types_allowed = True
[docs] @abstractmethod def _parse_response(self, response) -> str: """ This is an abstract method that should be implemented by subclasses. Parses the response object generated by the LLM and returns the generated completion text. For example, in openai, the response is a json containing choices and each choice has a message with content so to return only the generated text, we need to call something like this: `response.choices[0].message.content` Args: response (object): The response object. Return: str: The generated completion text. """
[docs] @abstractmethod def _prepare_prompt(self, prompt) -> Any: """ This is an abstract method that should be implemented by subclasses. Prepare the prompt by combining the human and AI prompts with the input prompt that you send to the LLM. For example, for openai, it accepts the message in the following format: `[{"role": "system", "content": prompt}]` Args: prompt (str): The input prompt. Return: Any: The prepared prompt. """
[docs] @abstractmethod def generate(self, query: str, **kwargs: Any) -> str: """ This is an abstract method that should be implemented by subclasses. It should call the selected LLM and generate a response based on the provided query and any additional keyword arguments. The specific implementation may vary depending on the subclass. Args: self (object): The instance of the class. query (str): The query for generating the response. **kwargs (Any): Additional keyword arguments that may be required by subclasses. Return: str: The generated response. """