|
| 1 | +from typing import Dict, List, Optional, Tuple |
| 2 | +from game_sdk.game.custom_types import Argument, Function, FunctionResultStatus |
| 3 | +import requests |
| 4 | +import os |
| 5 | + |
| 6 | +DEFAULT_BASE_API_URL = "https://api.together.xyz/v1/images/generations" |
| 7 | + |
| 8 | + |
| 9 | +class ImageGenPlugin: |
| 10 | + """ |
| 11 | + AI Image Generation plugin using Together.ai API. |
| 12 | + |
| 13 | + Requires: |
| 14 | + - Together.ai API key |
| 15 | + |
| 16 | + Example: |
| 17 | + client = ImageGenPlugin( |
| 18 | + api_key="your-together-api-key", |
| 19 | + api_url="https://api.together.xyz/v1/images/generations", |
| 20 | + ) |
| 21 | +
|
| 22 | + generate_image_fn = client.get_function("generate_image") |
| 23 | + """ |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + api_key: Optional[str] = os.environ.get("TOGETHER_API_KEY"), |
| 27 | + api_url: Optional[str] = DEFAULT_BASE_API_URL, |
| 28 | + ): |
| 29 | + self.api_key = api_key |
| 30 | + self.api_url = api_url |
| 31 | + |
| 32 | + # Available client functions |
| 33 | + self._functions: Dict[str, Function] = { |
| 34 | + "generate_image": Function( |
| 35 | + fn_name="generate_image", |
| 36 | + fn_description="Generates AI generated image based on prompt.", |
| 37 | + args=[ |
| 38 | + Argument( |
| 39 | + name="prompt", |
| 40 | + description="The prompt for image generation model. Example: A dog in the park", |
| 41 | + type="string", |
| 42 | + ), |
| 43 | + Argument( |
| 44 | + name="width", |
| 45 | + description="Width of generated image, up to 1440 px. Default should be 1024 unless other sizes specifically needed.", |
| 46 | + type="int", |
| 47 | + ), |
| 48 | + Argument( |
| 49 | + name="height", |
| 50 | + description="Height of generated image, up to 1440 px. Default should be 1024 unless other sizes specifically needed.", |
| 51 | + type="int", |
| 52 | + ), |
| 53 | + ], |
| 54 | + hint="This function is used to generate an AI image based on prompt", |
| 55 | + executable=self.generate_image, |
| 56 | + ), |
| 57 | + } |
| 58 | + |
| 59 | + @property |
| 60 | + def available_functions(self) -> List[str]: |
| 61 | + """Get list of available function names.""" |
| 62 | + return list(self._functions.keys()) |
| 63 | + |
| 64 | + def get_function(self, fn_name: str) -> Function: |
| 65 | + """ |
| 66 | + Get a specific function by name. |
| 67 | +
|
| 68 | + Args: |
| 69 | + fn_name: Name of the function to retrieve |
| 70 | +
|
| 71 | + Raises: |
| 72 | + ValueError: If function name is not found |
| 73 | +
|
| 74 | + Returns: |
| 75 | + Function object |
| 76 | + """ |
| 77 | + if fn_name not in self._functions: |
| 78 | + raise ValueError( |
| 79 | + f"Function '{fn_name}' not found. Available functions: {', '.join(self.available_functions)}" |
| 80 | + ) |
| 81 | + return self._functions[fn_name] |
| 82 | + |
| 83 | + def generate_image(self, prompt: str, width: int = 1024, height: int = 1024, **kwargs) -> str: |
| 84 | + """Generate image based on prompt. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + str URL of image (need to save since temporal) |
| 88 | + """ |
| 89 | + # API endpoint for image generation |
| 90 | + url = DEFAULT_BASE_API_URL |
| 91 | + |
| 92 | + # Prepare headers for the request |
| 93 | + headers = { |
| 94 | + "Authorization": f"Bearer {self.api_key}", |
| 95 | + "Content-Type": "application/json", |
| 96 | + } |
| 97 | + |
| 98 | + # Prepare request payload |
| 99 | + payload = { |
| 100 | + "model": "black-forest-labs/FLUX.1-schnell-Free", |
| 101 | + "prompt": prompt, |
| 102 | + "width": width, |
| 103 | + "height": height, |
| 104 | + "steps": 1, |
| 105 | + "n": 1, |
| 106 | + "response_format": "url", |
| 107 | + } |
| 108 | + |
| 109 | + try: |
| 110 | + # Make the API request |
| 111 | + response = requests.post(self.api_url, headers=headers, json=payload) |
| 112 | + response.raise_for_status() |
| 113 | + |
| 114 | + # Extract the image URL from the response |
| 115 | + response_data = response.json() |
| 116 | + image_url = response_data["data"][0]["url"] |
| 117 | + |
| 118 | + return ( |
| 119 | + FunctionResultStatus.DONE, |
| 120 | + f"The generated image is: {image_url}", |
| 121 | + { |
| 122 | + "prompt": prompt, |
| 123 | + "image_url": image_url, |
| 124 | + }, |
| 125 | + ) |
| 126 | + except Exception as e: |
| 127 | + print(f"An error occurred while generating image: {str(e)}") |
| 128 | + return ( |
| 129 | + FunctionResultStatus.FAILED, |
| 130 | + f"An error occurred while while generating image: {str(e)}", |
| 131 | + { |
| 132 | + "prompt": prompt, |
| 133 | + }, |
| 134 | + ) |
0 commit comments