1. OpenAI

Example for creating an OpenAI model with parameters for chat completion.

from lyzr_automata.ai_models.openai import OpenAIModel

# Creating an instance of OpenAIModel with specific configuration for chat completion

open_ai_text_completion_model = OpenAIModel(
    api_key="YOUR_OPEN_AI_KEY",
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.2,
        "max_tokens": 1500,
    },
)

Parameters

parameters
dict

All existing parameters supported by OpenAI’s chat completion model are included by default for customizable requests. Users can pass these using the parameters dictionary.

api_key
str

The api_key parameter is the user’s OpenAI API key, required for authentication to access the chat completion service.

Example for creating an OpenAI model with parameters for image generation

All existing parameters supported by OpenAI’s image generation model are supported by default.

from lyzr_automata.ai_models.openai import OpenAIModel

# Creating an instance of OpenAIModel with specific configuration for image generation

open_ai_model_image = OpenAIModel(
    api_key="YOUR_OPEN_AI_KEY",
    parameters={
        "n": 1,
        "model": "dall-e-3",
    },
)
parameters
dict

All existing parameters supported by OpenAI’s image generation model are included by default for customizable requests. Users can pass these using the parameters dictionary.

api_key
str

The api_key parameter is the user’s OpenAI API key, required for authentication to access the image generation service.

  1. Perplexity

Example of creating a perplexity model with parameters for chat completion.

All existing params supported by the Perplexity chat completion model are supported by default.

from lyzr_automata.ai_models.perplexity import PerplexityModel

perplexity_model_text = PerplexityModel(
    api_key="YOUR_PERPLEXITY_KEY",
    parameters={
        "model": "pplx-7b-online",
    },
)

Parameters

parameters
dict

A dictionary supporting all existing parameters accepted by the Perplexity chat completion model for customizable requests.

api_key
str

The user’s Perplexity API key required for authentication to access the chat completion service.

  1. Connect your own model to the framework

You can bring in your own model and connect it to our framework by extending our base AIModel class.

1

Import

Import AIModel class

2

Params

You may provide initial parameters, including the API key and any model-specific parameters.

3

Methods

Crucially, create two methods: generate_text and generate_image, as stipulated by the abstract class design of AIModel.

How to bring your own model to Lyzr Automata? 👇


from typing import Any, Dict, List
from openai import OpenAI
from lyzr_automata.ai_models.model_base import AIModel #import the AIModel base class
from lyzr_automata.data_models import FileResponse
from lyzr_automata.utils.resource_handler import ResourceBox


class MyModel(AIModel):
    def __init__(self, api_key, parameters: Dict[str, Any]):
        self.parameters = parameters
        self.client = OpenAI(api_key=api_key)
        self.api_key = api_key

    def generate_text(
        self,
        task_id: str=None,
        system_persona: str=None,
        prompt: str=None,
        messages: List[dict] = None,
    ) -> str:
        if messages is None:
            messages = [
                {"role": "system", "content": system_persona},
                {"role": "user", "content": prompt},
            ]

        response = self.client.chat.completions.create(
            **self.parameters, messages=messages
        )
        return response.choices[0].message.content

    def generate_image(
        self, task_id: str, prompt: str, resource_box: ResourceBox
    ) -> FileResponse:
        response = self.client.images.generate(**self.parameters, prompt=prompt)
        return resource_box.save_from_url(url=response.data[0].url, subfolder=task_id)

Methods

generate_text Method

Generates a text response using an AI chat model based on the provided prompt and optional parameters. This method is intended for creating conversational AI responses, leveraging the client’s chat completion capabilities.

task_id
Optional[str]

An optional identifier for the task. Used to specify or reference a particular task instance.

system_persona
Optional[str]

An optional string that defines the persona or role of the system in the conversation.

prompt
Optional[str]

An optional string containing the initial prompt or question posed by the user to the AI system.

messages
Optional[List[dict]]

An optional list of dictionaries, each representing a message in the conversation history. The default value creates a conversation starting with the system persona’s message followed by the user’s prompt.

generate_image Method

Generates an image based on a given prompt, utilizing the client’s image generation capabilities. The generated image is saved using a ResourceBox, which likely handles file management and storage.

task_id
str

A required identifier for the task, used to specify a unique instance for image generation and storage.

prompt
str

The prompt based on which the image will be generated. This string should describe the desired outcome for the image.

resource_box
ResourceBox

An instance of ResourceBox, which is used for managing the storage of the generated image, including saving the file from a URL and determining the subfolder based on task_id.