LLMs#

class llms.llm.BaseLLM[source]#

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 Open AI for sample implementation.

abstract _parse_response(response) str[source]#

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

Parameters:

response (object) – The response object.

Returns:

The generated completion text.

Return type:

str

abstract _prepare_prompt(prompt) Any[source]#

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}]

Parameters:

prompt (str) – The input prompt.

Returns:

The prepared prompt.

Return type:

Any

abstract generate(query: str, **kwargs: Any) str[source]#

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.

Parameters:
  • 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.

Returns:

The generated response.

Return type:

str