monoai.models

Models are the core of MonoAI. They are responsible for executing prompts and returning responses.

 1"""
 2Models are the core of MonoAI. They are responsible for executing prompts and returning responses.
 3"""
 4
 5from .model import Model
 6from .multi_model import MultiModel
 7from .collaborative_model import CollaborativeModel
 8from .image_model import ImageModel
 9
10__all__ = ['Model', 'MultiModel', 'CollaborativeModel', 'ImageModel'] 
class Model(monoai.models._base_model.BaseModel, monoai.models._response_processor.ResponseProcessorMixin, monoai.models._prompt_executor.PromptExecutorMixin):
 13class Model(BaseModel, ResponseProcessorMixin, PromptExecutorMixin):
 14    """
 15    Model class for interacting with AI language models.
 16
 17    This module provides the Model class which serves as the primary interface for interacting
 18    with various AI language models (like GPT-4, Claude-3, etc.).
 19
 20    Examples
 21    --------
 22    Basic usage:
 23    ```
 24    model = Model(provider="openai", model="gpt-4")
 25    response = model.ask("What is the capital of France?")
 26    ```
 27
 28    With prompt:
 29    ```
 30    model = Model(
 31        provider="anthropic",
 32        model="claude-3",
 33    )
 34    prompt = Prompt(
 35        prompt="What is the capital of {country}?",
 36        prompt_data={"country": "France"},
 37        response_type=str
 38    )
 39    response = model.ask(prompt)
 40    ```
 41    """
 42
 43    def __init__(
 44        self, 
 45        provider: str | None = None, 
 46        model: str | None = None, 
 47        system_prompt: str | Sequence[str] = (),
 48        count_tokens: bool = False, 
 49        count_cost: bool = False,
 50        max_tokens: int = None
 51    ):
 52        """
 53        Initialize a new Model instance.
 54
 55        Parameters
 56        ----------
 57        provider : str
 58            Name of the provider (e.g., 'openai', 'anthropic')
 59        model : str
 60            Name of the model (e.g., 'gpt-4', 'claude-3')
 61        system_prompt : str | Sequence[str], optional
 62            System prompt or sequence of prompts
 63        count_tokens : bool, optional
 64            Whether to count tokens for each request
 65        count_cost : bool, optional
 66            Whether to calculate costs for each request
 67        max_tokens : int, optional
 68            Maximum number of tokens for each request
 69        """
 70        super().__init__(count_tokens, count_cost, max_tokens)
 71        
 72        if provider is None:
 73            provider = Conf()["base_model"]["provider"]
 74        if model is None:
 75            model = Conf()["base_model"]["model"]
 76
 77        load_key(provider)
 78
 79        self.provider = provider
 80        self.model = model
 81
 82    async def _ask_async(self, prompt: Union[str, Prompt, PromptChain]) -> Dict:
 83        """
 84        Ask the model asynchronously.
 85
 86        Parameters
 87        ----------
 88        prompt : Union[str, Prompt]
 89            The prompt to process
 90
 91        Returns
 92        -------
 93        Dict
 94            Dictionary containing:
 95            - response: The model's response
 96            - prompt: The original prompt
 97            - model: Dictionary with provider and model name
 98            - tokens: Token counts (if enabled)
 99            - cost: Cost calculation (if enabled)
100
101        """
102        response = await self._execute_async(prompt)
103        return self._process_response(
104            prompt,
105            response,
106        )
107
108    def ask(self, prompt: Union[str, Prompt, PromptChain]) -> Dict:
109        """
110        Ask the model.
111
112        Parameters
113        ----------
114        prompt : Union[str, Prompt]
115            The prompt to process
116
117        Returns
118        -------
119        Dict
120            Dictionary containing:
121            - response: The model's response
122            - prompt: The original prompt
123            - model: Dictionary with provider and model name
124            - tokens: Token counts (if enabled)
125            - cost: Cost calculation (if enabled)
126
127        """
128
129        response = self._execute(prompt)
130        return self._process_response(
131            prompt,
132            response
133        )
134
135    def _post_process_response(self, question: str, answer: str) -> Dict:
136        """
137        Process the response and add optional token and cost information.
138
139        Parameters
140        ----------
141        question : str
142            The input question
143        answer : str
144            The model's answer
145
146        Returns
147        -------
148        Dict
149            Dictionary containing:
150            - input: The original question
151            - output: The model's answer
152            - model: Dictionary with provider and model name
153            - tokens: Token counts (if enabled)
154            - cost: Cost calculation (if enabled)
155        """
156        response = {
157            "input": question, 
158            "output": answer, 
159            "model": {
160                "provider": self.provider, 
161                "name": self.model
162            }
163        }
164        
165        if self._count_tokens or self._count_cost:
166            tokens = None
167            if self._count_tokens:
168                tokens = TokenCounter().count(self.model, question, answer)
169                response["tokens"] = tokens
170                
171            if self._count_cost and tokens:
172                cost = TokenCost().compute(
173                    self.provider, 
174                    self.model, 
175                    tokens["input_tokens"], 
176                    tokens["output_tokens"]
177                )
178                response["cost"] = cost
179                
180        return response

Model class for interacting with AI language models.

This module provides the Model class which serves as the primary interface for interacting with various AI language models (like GPT-4, Claude-3, etc.).

Examples

Basic usage:

model = Model(provider="openai", model="gpt-4")
response = model.ask("What is the capital of France?")

With prompt:

model = Model(
    provider="anthropic",
    model="claude-3",
)
prompt = Prompt(
    prompt="What is the capital of {country}?",
    prompt_data={"country": "France"},
    response_type=str
)
response = model.ask(prompt)
Model( provider: str | None = None, model: str | None = None, system_prompt: Union[str, Sequence[str]] = (), count_tokens: bool = False, count_cost: bool = False, max_tokens: int = None)
43    def __init__(
44        self, 
45        provider: str | None = None, 
46        model: str | None = None, 
47        system_prompt: str | Sequence[str] = (),
48        count_tokens: bool = False, 
49        count_cost: bool = False,
50        max_tokens: int = None
51    ):
52        """
53        Initialize a new Model instance.
54
55        Parameters
56        ----------
57        provider : str
58            Name of the provider (e.g., 'openai', 'anthropic')
59        model : str
60            Name of the model (e.g., 'gpt-4', 'claude-3')
61        system_prompt : str | Sequence[str], optional
62            System prompt or sequence of prompts
63        count_tokens : bool, optional
64            Whether to count tokens for each request
65        count_cost : bool, optional
66            Whether to calculate costs for each request
67        max_tokens : int, optional
68            Maximum number of tokens for each request
69        """
70        super().__init__(count_tokens, count_cost, max_tokens)
71        
72        if provider is None:
73            provider = Conf()["base_model"]["provider"]
74        if model is None:
75            model = Conf()["base_model"]["model"]
76
77        load_key(provider)
78
79        self.provider = provider
80        self.model = model

Initialize a new Model instance.

Parameters
  • provider (str): Name of the provider (e.g., 'openai', 'anthropic')
  • model (str): Name of the model (e.g., 'gpt-4', 'claude-3')
  • system_prompt (str | Sequence[str], optional): System prompt or sequence of prompts
  • count_tokens (bool, optional): Whether to count tokens for each request
  • count_cost (bool, optional): Whether to calculate costs for each request
  • max_tokens (int, optional): Maximum number of tokens for each request
provider
model
def ask( self, prompt: Union[str, monoai.prompts.Prompt, monoai.prompts.PromptChain]) -> Dict:
108    def ask(self, prompt: Union[str, Prompt, PromptChain]) -> Dict:
109        """
110        Ask the model.
111
112        Parameters
113        ----------
114        prompt : Union[str, Prompt]
115            The prompt to process
116
117        Returns
118        -------
119        Dict
120            Dictionary containing:
121            - response: The model's response
122            - prompt: The original prompt
123            - model: Dictionary with provider and model name
124            - tokens: Token counts (if enabled)
125            - cost: Cost calculation (if enabled)
126
127        """
128
129        response = self._execute(prompt)
130        return self._process_response(
131            prompt,
132            response
133        )

Ask the model.

Parameters
  • prompt (Union[str, Prompt]): The prompt to process
Returns
  • Dict: Dictionary containing:
    • response: The model's response
    • prompt: The original prompt
    • model: Dictionary with provider and model name
    • tokens: Token counts (if enabled)
    • cost: Cost calculation (if enabled)
class MultiModel(monoai.models._base_model.BaseModel, monoai.models._prompt_executor.PromptExecutorMixin, monoai.models._response_processor.ResponseProcessorMixin):
 11class MultiModel(BaseModel, PromptExecutorMixin, ResponseProcessorMixin):
 12    """
 13    A class to execute prompts across multiple AI models in parallel.
 14    
 15    MultiModel manages a collection of AI models and enables parallel execution of prompts
 16    across all models. It's particularly useful for comparing model responses or
 17    implementing ensemble approaches.
 18
 19    Examples
 20    --------
 21    Basic comparison of models:
 22    ```
 23    models = [
 24        {"provider": "openai", "model": "gpt-4"},
 25        {"provider": "anthropic", "model": "claude-3"}
 26    ]
 27    multi_model = MultiModel(models=models)
 28    prompt = Prompt(
 29        prompt="What is 2+2?",
 30        response_type=int
 31    )
 32    responses = multi_model.ask(prompt)
 33    for resp in responses:
 34        print(f"{resp['model']['name']}: {resp['response']}")
 35    ```
 36    """
 37
 38    def __init__(
 39        self, 
 40        models: List[Dict[str, str]], 
 41        count_tokens: bool = False, 
 42        count_cost: bool = False
 43    ):
 44        """
 45        Initialize a new MultiModel instance.
 46
 47        Parameters
 48        ----------
 49        models : List[Dict[str, str]]
 50            List of dictionaries with provider and model information
 51        count_tokens : bool, optional
 52            Whether to count tokens for each request
 53        count_cost : bool, optional
 54            Whether to calculate costs for each request
 55        """
 56        super().__init__(count_tokens, count_cost)
 57        self._models = [
 58            Model(
 59                model['provider'],
 60                model['model'],
 61                count_tokens=count_tokens,
 62                count_cost=count_cost
 63            ) for model in models
 64        ]
 65
 66    async def _task(self, model: Model, prompt: Union[str, Prompt, PromptChain]) -> Dict:
 67        """
 68        Execute a single model task asynchronously.
 69
 70        Parameters
 71        ----------
 72        model : Model
 73            The model instance to use
 74        prompt : Union[str, Prompt, PromptChain]
 75            The prompt to process
 76
 77        Returns
 78        -------
 79        Dict
 80            Dictionary containing:
 81            - response: The model's response
 82            - prompt: The original prompt
 83            - model: Dictionary with provider and model name
 84            - tokens: Token counts (if enabled)
 85            - cost: Cost calculation (if enabled)
 86        """
 87        response = await self._execute_async(prompt, model._agent)
 88        return self._process_response(
 89            prompt,
 90            response,
 91            model.provider,
 92            model.model,
 93            self._count_tokens,
 94            self._count_cost
 95        )
 96
 97    async def _ask_async(self, prompt: Union[str, Prompt, PromptChain]) -> List[Dict]:
 98        """
 99        Ask all models asynchronously.
100
101        Parameters
102        ----------
103        prompt : Union[str, Prompt, PromptChain]
104            The prompt to process across all models
105
106        Returns
107        -------
108        List[Dict]
109            List of response dictionaries, one per model, each containing:
110            - response: The model's response
111            - prompt: The original prompt
112            - model: Dictionary with provider and model name
113            - tokens: Token counts (if enabled)
114            - cost: Cost calculation (if enabled)
115
116        Examples
117        --------
118        Using async/await:
119            >>> responses = await multi_model.ask_async("What is 2+2?")
120            >>> for resp in responses:
121            ...     print(f"{resp['model']['name']}: {resp['response']}")
122        """
123        tasks = [self._task(model, prompt) for model in self._models]
124        return await asyncio.gather(*tasks)
125
126    def ask(self, prompt: Union[str, Prompt]) -> List[Dict]:
127        """
128        Ask all models.
129
130        Parameters
131        ----------
132        prompt : Union[str, Prompt]
133            The prompt to process across all models
134
135        Returns
136        -------
137        List[Dict]
138            List of response dictionaries, one per model, each containing:
139            - response: The model's response
140            - prompt: The original prompt
141            - model: Dictionary with provider and model name
142            - tokens: Token counts (if enabled)
143            - cost: Cost calculation (if enabled)
144
145        """
146        return asyncio.run(self.ask_async(prompt))

A class to execute prompts across multiple AI models in parallel.

MultiModel manages a collection of AI models and enables parallel execution of prompts across all models. It's particularly useful for comparing model responses or implementing ensemble approaches.

Examples

Basic comparison of models:

models = [
    {"provider": "openai", "model": "gpt-4"},
    {"provider": "anthropic", "model": "claude-3"}
]
multi_model = MultiModel(models=models)
prompt = Prompt(
    prompt="What is 2+2?",
    response_type=int
)
responses = multi_model.ask(prompt)
for resp in responses:
    print(f"{resp['model']['name']}: {resp['response']}")
MultiModel( models: List[Dict[str, str]], count_tokens: bool = False, count_cost: bool = False)
38    def __init__(
39        self, 
40        models: List[Dict[str, str]], 
41        count_tokens: bool = False, 
42        count_cost: bool = False
43    ):
44        """
45        Initialize a new MultiModel instance.
46
47        Parameters
48        ----------
49        models : List[Dict[str, str]]
50            List of dictionaries with provider and model information
51        count_tokens : bool, optional
52            Whether to count tokens for each request
53        count_cost : bool, optional
54            Whether to calculate costs for each request
55        """
56        super().__init__(count_tokens, count_cost)
57        self._models = [
58            Model(
59                model['provider'],
60                model['model'],
61                count_tokens=count_tokens,
62                count_cost=count_cost
63            ) for model in models
64        ]

Initialize a new MultiModel instance.

Parameters
  • models (List[Dict[str, str]]): List of dictionaries with provider and model information
  • count_tokens (bool, optional): Whether to count tokens for each request
  • count_cost (bool, optional): Whether to calculate costs for each request
def ask(self, prompt: Union[str, monoai.prompts.Prompt]) -> List[Dict]:
126    def ask(self, prompt: Union[str, Prompt]) -> List[Dict]:
127        """
128        Ask all models.
129
130        Parameters
131        ----------
132        prompt : Union[str, Prompt]
133            The prompt to process across all models
134
135        Returns
136        -------
137        List[Dict]
138            List of response dictionaries, one per model, each containing:
139            - response: The model's response
140            - prompt: The original prompt
141            - model: Dictionary with provider and model name
142            - tokens: Token counts (if enabled)
143            - cost: Cost calculation (if enabled)
144
145        """
146        return asyncio.run(self.ask_async(prompt))

Ask all models.

Parameters
  • prompt (Union[str, Prompt]): The prompt to process across all models
Returns
  • List[Dict]: List of response dictionaries, one per model, each containing:
    • response: The model's response
    • prompt: The original prompt
    • model: Dictionary with provider and model name
    • tokens: Token counts (if enabled)
    • cost: Cost calculation (if enabled)
class CollaborativeModel(monoai.models._base_model.BaseModel, monoai.models._prompt_executor.PromptExecutorMixin, monoai.models._response_processor.ResponseProcessorMixin):
 12class CollaborativeModel(BaseModel, PromptExecutorMixin, ResponseProcessorMixin):
 13    """
 14    A class to implement collaborative decision making across multiple AI models.
 15    
 16    CollaborativeModel manages a collection of AI models and an aggregator model.
 17    It executes prompts across all models in parallel and then uses the aggregator
 18    to synthesize a final response based on all individual responses.
 19
 20    Examples
 21    --------
 22    Basic collaborative analysis:
 23    ```
 24    models = [
 25        {"provider": "openai", "model": "gpt-4"},
 26        {"provider": "anthropic", "model": "claude-3"}
 27    ]
 28    aggregator = {"provider": "openai", "model": "gpt-4"}
 29    collab = CollaborativeModel(models=models, aggregator=aggregator)
 30    response = collab.ask("Explain quantum computing")
 31    print(response["response"])  # Aggregated response
 32    for ind_resp in response["individual_responses"]:
 33        print(f"{ind_resp['model']['name']}: {ind_resp['response']}")
 34    ```
 35    """
 36
 37    def __init__(
 38        self,
 39        models: List[Dict[str, str]],
 40        aggregator: Dict[str, str],
 41        count_tokens: bool = False,
 42        count_cost: bool = False
 43    ):
 44        """
 45        Initialize a new CollaborativeModel instance.
 46
 47        Parameters
 48        ----------
 49        models : List[Dict[str, str]]
 50            List of dictionaries with provider and model information
 51        aggregator : Dict[str, str]
 52            Dictionary with provider and model information for the aggregator
 53        count_tokens : bool, optional
 54            Whether to count tokens for each request
 55        count_cost : bool, optional
 56            Whether to calculate costs for each request
 57        """
 58        super().__init__(count_tokens, count_cost)
 59
 60        self._multi_model = MultiModel(
 61            models=models,
 62            count_tokens=count_tokens,
 63            count_cost=count_cost
 64        )
 65
 66        self._aggregator = Model(
 67            aggregator['provider'],
 68            aggregator['model'],
 69            count_tokens=count_tokens,
 70            count_cost=count_cost
 71        )
 72
 73    def _format_aggregator_prompt(self, prompt: Union[str, Prompt, PromptChain], responses: List[Dict]) -> str:
 74        """
 75        Format the prompt for the aggregator model.
 76
 77        Parameters
 78        ----------
 79        prompt : Union[str, Prompt, PromptChain]
 80            The original prompt
 81        responses : List[Dict]
 82            List of responses from individual models
 83
 84        Returns
 85        -------
 86        str
 87            Formatted prompt for the aggregator including original question
 88            and all model responses
 89        """
 90        prompt_text = str(prompt)
 91        model_responses = "\n\n".join([
 92            f"Model {i+1} ({response['model']['provider']} - {response['model']['name']}):\n{response['response']}"
 93            for i, response in enumerate(responses)
 94        ])
 95        
 96        return f"""Please analyze the following responses from different models and provide a comprehensive answer:
 97                    Original Question: {prompt_text}
 98                    Model Responses:
 99                    {model_responses}
100                    Please provide a well-reasoned response that takes into account all the information above."""
101
102    async def _ask_async(self, prompt: Union[str, Prompt, PromptChain]) -> Dict:
103        """
104        Ask all models and aggregate their responses asynchronously.
105
106        Parameters
107        ----------
108        prompt : Union[str, Prompt, PromptChain]
109            The prompt to process across all models
110
111        Returns
112        -------
113        Dict
114            Dictionary containing:
115            - response: The aggregated response
116            - prompt: The original prompt
117            - model: Dictionary with aggregator's provider and model name
118            - tokens: Token counts (if enabled)
119            - cost: Cost calculation (if enabled)
120            - individual_responses: List of responses from individual models
121
122        Examples
123        --------
124        Using async/await:
125            >>> response = await collab.ask_async("What is consciousness?")
126            >>> print(response["response"])  # Aggregated response
127            >>> for resp in response["individual_responses"]:
128            ...     print(f"{resp['model']['name']}: {resp['response']}")
129        """
130        # Get responses from all models
131        model_responses = await self._multi_model.ask_async(prompt)
132        
133        # Get aggregator response
134        aggregator_prompt = self._format_aggregator_prompt(prompt, model_responses)
135        aggregator_response = await self._execute_async(aggregator_prompt, self._aggregator._agent)
136        
137        # Process aggregator response
138        processed_aggregator = self._process_response(
139            aggregator_prompt,
140            aggregator_response,
141            self._aggregator.provider,
142            self._aggregator.model,
143            self._count_tokens,
144            self._count_cost
145        )
146
147        processed_aggregator["individual_responses"] = model_responses
148        return processed_aggregator
149
150    def ask(self, prompt: Union[str, Prompt, PromptChain]) -> Dict:
151        """
152        Ask all models and aggregate their responses synchronously.
153
154        Parameters
155        ----------
156        prompt : Union[str, Prompt, PromptChain]
157            The prompt to process across all models
158
159        Returns
160        -------
161        Dict
162            Dictionary containing:
163            - response: The aggregated response
164            - prompt: The original prompt
165            - model: Dictionary with aggregator's provider and model name
166            - tokens: Token counts (if enabled)
167            - cost: Cost calculation (if enabled)
168            - individual_responses: List of responses from individual models
169
170        """
171        return asyncio.run(self.ask_async(prompt))

A class to implement collaborative decision making across multiple AI models.

CollaborativeModel manages a collection of AI models and an aggregator model. It executes prompts across all models in parallel and then uses the aggregator to synthesize a final response based on all individual responses.

Examples

Basic collaborative analysis:

models = [
    {"provider": "openai", "model": "gpt-4"},
    {"provider": "anthropic", "model": "claude-3"}
]
aggregator = {"provider": "openai", "model": "gpt-4"}
collab = CollaborativeModel(models=models, aggregator=aggregator)
response = collab.ask("Explain quantum computing")
print(response["response"])  # Aggregated response
for ind_resp in response["individual_responses"]:
    print(f"{ind_resp['model']['name']}: {ind_resp['response']}")
CollaborativeModel( models: List[Dict[str, str]], aggregator: Dict[str, str], count_tokens: bool = False, count_cost: bool = False)
37    def __init__(
38        self,
39        models: List[Dict[str, str]],
40        aggregator: Dict[str, str],
41        count_tokens: bool = False,
42        count_cost: bool = False
43    ):
44        """
45        Initialize a new CollaborativeModel instance.
46
47        Parameters
48        ----------
49        models : List[Dict[str, str]]
50            List of dictionaries with provider and model information
51        aggregator : Dict[str, str]
52            Dictionary with provider and model information for the aggregator
53        count_tokens : bool, optional
54            Whether to count tokens for each request
55        count_cost : bool, optional
56            Whether to calculate costs for each request
57        """
58        super().__init__(count_tokens, count_cost)
59
60        self._multi_model = MultiModel(
61            models=models,
62            count_tokens=count_tokens,
63            count_cost=count_cost
64        )
65
66        self._aggregator = Model(
67            aggregator['provider'],
68            aggregator['model'],
69            count_tokens=count_tokens,
70            count_cost=count_cost
71        )

Initialize a new CollaborativeModel instance.

Parameters
  • models (List[Dict[str, str]]): List of dictionaries with provider and model information
  • aggregator (Dict[str, str]): Dictionary with provider and model information for the aggregator
  • count_tokens (bool, optional): Whether to count tokens for each request
  • count_cost (bool, optional): Whether to calculate costs for each request
def ask( self, prompt: Union[str, monoai.prompts.Prompt, monoai.prompts.PromptChain]) -> Dict:
150    def ask(self, prompt: Union[str, Prompt, PromptChain]) -> Dict:
151        """
152        Ask all models and aggregate their responses synchronously.
153
154        Parameters
155        ----------
156        prompt : Union[str, Prompt, PromptChain]
157            The prompt to process across all models
158
159        Returns
160        -------
161        Dict
162            Dictionary containing:
163            - response: The aggregated response
164            - prompt: The original prompt
165            - model: Dictionary with aggregator's provider and model name
166            - tokens: Token counts (if enabled)
167            - cost: Cost calculation (if enabled)
168            - individual_responses: List of responses from individual models
169
170        """
171        return asyncio.run(self.ask_async(prompt))

Ask all models and aggregate their responses synchronously.

Parameters
  • prompt (Union[str, Prompt, PromptChain]): The prompt to process across all models
Returns
  • Dict: Dictionary containing:
    • response: The aggregated response
    • prompt: The original prompt
    • model: Dictionary with aggregator's provider and model name
    • tokens: Token counts (if enabled)
    • cost: Cost calculation (if enabled)
    • individual_responses: List of responses from individual models
class ImageModel:
 5class ImageModel:
 6    """
 7    A class to interact with AI image generation models.
 8    
 9    ImageModel provides an interface for generating images from text prompts using
10    AI models. Currently supports OpenAI's DALL-E 3, with potential for expansion
11    to other providers and models in the future.
12
13    Examples
14    --------
15    Basic image generation:
16    ```
17    model = ImageModel(provider="openai", model="dall-e-3")
18    response = model.generate("A beautiful garden with flowers")
19    ```
20    """
21
22    def __init__(self, provider: str, model: str):
23        """
24        Initialize a new ImageModel instance.
25
26        Parameters
27        ----------
28        provider : str
29            Name of the provider (currently only "openai" is supported)
30        model : str
31            Name of the model (currently only "dall-e-3" is supported)
32
33        Raises
34        ------
35        ValueError
36            If an unsupported provider or model is specified
37        """
38        self.provider = provider
39        self.model = model
40
41        if provider.lower() != "openai":
42            raise ValueError(f"Provider {provider} not supported")
43        if model.lower() != "dall-e-3":
44            raise ValueError(f"Model {model} not supported")
45        
46        load_key(provider)
47        self._client = OpenAI()
48
49    def generate(self, 
50                prompt: str, 
51                size: str = "1024x1024", 
52                quality: str = "standard", 
53                n: int = 1) -> dict:
54        """
55        Generate images from a text prompt.
56
57        Parameters
58        ----------
59        prompt : str
60            The text description of the image to generate
61        size : str, optional
62            The size of the generated image(s). Options:
63            - "1024x1024" (default)
64            - "1792x1024"
65            - "1024x1792"
66        quality : str, optional
67            The quality of the generated image(s). Options:
68            - "standard" (default)
69            - "hd"
70        n : int, optional
71            Number of images to generate (default: 1)
72
73        Returns
74        -------
75        dict
76            OpenAI image generation response containing:
77            - created: timestamp
78            - data: list of generated images with URLs and other metadata
79        """
80        response = self._client.images.generate(
81            model=self.model,
82            prompt=prompt,
83            size=size,
84            quality=quality,
85            n=n,
86        )
87        return response

A class to interact with AI image generation models.

ImageModel provides an interface for generating images from text prompts using AI models. Currently supports OpenAI's DALL-E 3, with potential for expansion to other providers and models in the future.

Examples

Basic image generation:

model = ImageModel(provider="openai", model="dall-e-3")
response = model.generate("A beautiful garden with flowers")
ImageModel(provider: str, model: str)
22    def __init__(self, provider: str, model: str):
23        """
24        Initialize a new ImageModel instance.
25
26        Parameters
27        ----------
28        provider : str
29            Name of the provider (currently only "openai" is supported)
30        model : str
31            Name of the model (currently only "dall-e-3" is supported)
32
33        Raises
34        ------
35        ValueError
36            If an unsupported provider or model is specified
37        """
38        self.provider = provider
39        self.model = model
40
41        if provider.lower() != "openai":
42            raise ValueError(f"Provider {provider} not supported")
43        if model.lower() != "dall-e-3":
44            raise ValueError(f"Model {model} not supported")
45        
46        load_key(provider)
47        self._client = OpenAI()

Initialize a new ImageModel instance.

Parameters
  • provider (str): Name of the provider (currently only "openai" is supported)
  • model (str): Name of the model (currently only "dall-e-3" is supported)
Raises
  • ValueError: If an unsupported provider or model is specified
provider
model
def generate( self, prompt: str, size: str = '1024x1024', quality: str = 'standard', n: int = 1) -> dict:
49    def generate(self, 
50                prompt: str, 
51                size: str = "1024x1024", 
52                quality: str = "standard", 
53                n: int = 1) -> dict:
54        """
55        Generate images from a text prompt.
56
57        Parameters
58        ----------
59        prompt : str
60            The text description of the image to generate
61        size : str, optional
62            The size of the generated image(s). Options:
63            - "1024x1024" (default)
64            - "1792x1024"
65            - "1024x1792"
66        quality : str, optional
67            The quality of the generated image(s). Options:
68            - "standard" (default)
69            - "hd"
70        n : int, optional
71            Number of images to generate (default: 1)
72
73        Returns
74        -------
75        dict
76            OpenAI image generation response containing:
77            - created: timestamp
78            - data: list of generated images with URLs and other metadata
79        """
80        response = self._client.images.generate(
81            model=self.model,
82            prompt=prompt,
83            size=size,
84            quality=quality,
85            n=n,
86        )
87        return response

Generate images from a text prompt.

Parameters
  • prompt (str): The text description of the image to generate
  • size (str, optional): The size of the generated image(s). Options:
    • "1024x1024" (default)
    • "1792x1024"
    • "1024x1792"
  • quality (str, optional): The quality of the generated image(s). Options:
    • "standard" (default)
    • "hd"
  • n (int, optional): Number of images to generate (default: 1)
Returns
  • dict: OpenAI image generation response containing:
    • created: timestamp
    • data: list of generated images with URLs and other metadata