> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lyzr.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Model Calling

1. **OpenAI**

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

```python theme={null}
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**

<ParamField path="parameters" type="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.
</ParamField>

<ParamField path="api_key" type="str">
  The api\_key parameter is the user's OpenAI API key, required for authentication to access the chat completion service.
</ParamField>

**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.

```python theme={null}
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",
    },
)
```

<ParamField path="parameters" type="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.
</ParamField>

<ParamField path="api_key" type="str">
  The api\_key parameter is the user's OpenAI API key, required for authentication to access the image generation service.
</ParamField>

2. **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.

```python theme={null}
from lyzr_automata.ai_models.perplexity import PerplexityModel

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

**Parameters**

<ParamField path="parameters" type="dict">
  A dictionary supporting all existing parameters accepted by the Perplexity chat completion model for customizable requests.
</ParamField>

<ParamField path="api_key" type="str">
  The user's Perplexity API key required for authentication to access the chat completion service.
</ParamField>

3. **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.

<Steps>
  <Step title="Import">
    Import AIModel class
  </Step>

  <Step title="Params">
    You may provide initial parameters, including the API key and any model-specific parameters.
  </Step>

  <Step title="Methods">
    Crucially, create two methods: **generate\_text** and **generate\_image**, as stipulated by the abstract class design of AIModel.
  </Step>
</Steps>

**How to bring your own model to Lyzr Automata?** 👇

```python theme={null}

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.

<ParamField path="task_id" type="Optional[str]">
  An optional identifier for the task. Used to specify or reference a particular task instance.
</ParamField>

<ParamField path="system_persona" type="Optional[str]">
  An optional string that defines the persona or role of the system in the conversation.
</ParamField>

<ParamField path="prompt" type="Optional[str]">
  An optional string containing the initial prompt or question posed by the user to the AI system.
</ParamField>

<ParamField path="messages" type="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.
</ParamField>

**`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.

<ParamField path="task_id" type="str">
  A required identifier for the task, used to specify a unique instance for image generation and storage.
</ParamField>

<ParamField path="prompt" type="str">
  The prompt based on which the image will be generated. This string should describe the desired outcome for the image.
</ParamField>

<ParamField path="resource_box" type="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.
</ParamField>
