Skip to content

Commit

Permalink
Add DALL-E image generation function
Browse files Browse the repository at this point in the history
  • Loading branch information
ExplorerGT92 authored and ExplorerGT92 committed Jan 4, 2024
1 parent fa5a019 commit feccaba
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 15 deletions.
53 changes: 46 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
ask_chat_gpt_4_0613_synchronous,
ask_chat_gpt_4_0613_asynchronous,
)
from utils.openai_dalle_tools import generate_an_image_with_dalle3
from utils.core_tools import get_current_date_time, display_help
from output_methods.audio_pyttsx3 import tts_output
from plugins.plugins_enabled import enable_plugins
Expand Down Expand Up @@ -380,6 +381,7 @@ async def main():
"ask_chat_gpt_4_0314_asynchronous": ask_chat_gpt_4_0314_asynchronous,
"ask_chat_gpt_4_0613_synchronous": ask_chat_gpt_4_0613_synchronous,
"ask_chat_gpt_4_0613_asynchronous": ask_chat_gpt_4_0613_asynchronous,
"generate_an_image_with_dalle3": generate_an_image_with_dalle3,
# Add more core functions here
}
logging.info('Initialized available functions line 385')
Expand Down Expand Up @@ -493,27 +495,64 @@ async def main():
},
},
},
{
"type": "function",
"function": {
"name": "generate_an_image_with_dalle3",
"description": "This function allows you to generate an image with DALL-E 3.",
"parameters": {
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "The prompt to use for image generation. 4000 characters max.",
},
"n": {
"type": "integer",
"description": "The number of images to generate. 10 max.",
},
"size": {
"type": "string",
"description": "The size of the image to generate. 1024x1024, 1792x1024, or 1024x1792.",
},
"quality": {
"type": "string",
"description": "The quality of the image to generate. standard or hd.",
},
"style": {
"type": "string",
"description": "The style of the image to generate. natural or vivid.",
},
"response_format": {
"type": "string",
"description": "The format of the response. b64_json or url.",
},
},
"required": ["prompt"],
},
},
},
]
logging.info('Defined available core tools line 497')
logging.info('Defined available core tools line 532')

# Use the load_plugins_and_get_tools function to conditionally add tools
available_functions, tools = await enable_plugins(
available_functions,
tools
)
logging.info('Enabled plugins line 504')
logging.info('Enabled plugins line 539')

# Initialize the conversation memory
memory = []
logging.info('Initialized conversation memory line 508')
logging.info('Initialized conversation memory line 543')

# Main Loop
while True:
# Ask the user for input
user_input = Prompt.ask(
"\nHow can I be of assistance? ([yellow]/tools[/yellow] or [bold yellow]quit[/bold yellow])",
)
logging.info('Line 516 received user input: %s', user_input)
logging.info('Line 551 received user input: %s', user_input)

# Check if the user wants to exit the program
if user_input.lower() == "quit":
Expand Down Expand Up @@ -572,16 +611,16 @@ async def main():
console.print("\n" + final_text, style="green")
else:
# Print an error message if the model did not return a response
logging.warning('Model did not return a response line 575')
logging.warning('Model did not return a response line 610')
console.print("\nI'm not sure how to help with that.", style="red")
else:
# Print an error message if the model did not return a response
logging.warning('Model did not return a response line 579')
logging.warning('Model did not return a response line 614')
console.print("\nI'm not sure how to help with that.", style="red")

# Remove tools from the tools list after processing
tools[:] = [tool for tool in tools if not tool.get("function", {}).get("name", "").lower() in user_input.lower()]
logging.info('Removed used tools from the tools list line 584')
logging.info('Removed used tools from the tools list line 619')


# Run the main function
Expand Down
6 changes: 0 additions & 6 deletions plugins/_news_plugin/newsapi_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,6 @@ async def get_top_headlines_newsapi(api_key=TOOL_API_KEY, **kwargs) -> List:
"parameters": {
"type": "object",
"properties": {
"country": {
"type": "string",
"description": "The 2-letter ISO 3166-1 code of the country you want to get headlines for. Possible options: ae ar at au be bg br ca ch cn co cu cz de eg fr gb gr hk hu id ie il in it jp kr lt lv ma mx my ng nl no nz ph pl pt ro rs ru sa se sg si sk th tr tw ua us ve za.",
"enum": ["ae", "ar", "at", "au", "be", "bg", "br", "ca", "ch", "cn", "co", "cu", "cz", "de", "eg", "fr", "gb", "gr", "hk", "hu", "id", "ie", "il", "in", "it", "jp", "kr", "lt", "lv", "ma", "mx", "my", "ng", "nl", "no", "nz", "ph", "pl", "pt", "ro", "rs", "ru", "sa", "se", "sg", "si", "sk", "th", "tr", "tw", "ua", "us", "ve", "za"],
"default": "us",
},
"category": {
"type": "string",
"description": "The category you want to get headlines for. Possible options: business entertainment general health science sports technology.",
Expand Down
48 changes: 48 additions & 0 deletions utils/openai_dalle_tools.py
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
4 changes: 2 additions & 2 deletions utils/openai_model_tools.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

# !/usr/bin/env python
# coding: utf-8
# Filename: core_tools.py
# Path: utils/core_tools.py
# Filename: openai_model_tools.py
# Path: utils/openai_model_tools.py

"""
Core tools.
Expand Down

0 comments on commit feccaba

Please sign in to comment.