forked from Eloquent-Algorithmics/GPT_ALL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DALL-E image generation function
- Loading branch information
ExplorerGT92
authored and
ExplorerGT92
committed
Jan 4, 2024
1 parent
fa5a019
commit feccaba
Showing
4 changed files
with
96 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
# !/usr/bin/env python | ||
# coding: utf-8 | ||
# Filename: openai_dalle_tools.py | ||
# Path: utils/openai_dalle_tools.py | ||
|
||
""" | ||
This file contains the OpenAi Dall-e tools for the AI Assistant. | ||
""" | ||
|
||
from openai import OpenAI, AsyncOpenAI | ||
from config import ( | ||
OPENAI_API_KEY, | ||
OPENAI_ORG_ID | ||
) | ||
|
||
api_key = OPENAI_API_KEY | ||
openai_org_id = OPENAI_ORG_ID | ||
|
||
# Create an OpenAI client instance using keyword arguments | ||
client = OpenAI(api_key=api_key, organization=openai_org_id, timeout=10) | ||
|
||
# Create an AsyncOpenAI client instance using keyword arguments | ||
client_async = AsyncOpenAI(api_key=api_key, organization=openai_org_id, timeout=60) | ||
|
||
|
||
async def generate_an_image_with_dalle3(**kwargs) -> str: | ||
""" | ||
Generate an image with DALL-E 3. | ||
""" | ||
prompt = kwargs.get("prompt", "") | ||
n = kwargs.get("n", 1) | ||
size = kwargs.get("size", "1024x1024") | ||
quality = kwargs.get("quality", "hd") | ||
style = kwargs.get("style", "natural") | ||
response_format = kwargs.get("response_format", "url") | ||
|
||
response = await client_async.images.generate( | ||
model="dall-e-3", # The model identifier | ||
prompt=prompt, # Prompt required for image generation 4000 characters max | ||
n=n, # Must be between 1 and 10 | ||
size=size, # 1024x1024, 1792x1024, or 1024x1792 dall-e-3 model | ||
quality=quality, # hd or standard | ||
style=style, # natural or vivid | ||
response_format=response_format, # b64_json or url | ||
) | ||
await client_async.close() | ||
return response.data[0].url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters