Skip to content

Instantly share code, notes, and snippets.

@elbruno
Created July 28, 2023 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbruno/913104ce3690bf4d803c469e96fa11a2 to your computer and use it in GitHub Desktop.
Save elbruno/913104ce3690bf4d803c469e96fa11a2 to your computer and use it in GitHub Desktop.
DroneGenCommandsWithAI.py
# Copyright (c) 2023
# Author : Bruno Capuano
# Change Log :
# - Use semantic kernel to generate drone commands
# - Use Azure OpenAI Services or OpenAI APIs to generate drone commands
# - Use the semantic skill "DroneAI" to generate drone commands
# - Return the generated drone commands as a string
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion, OpenAITextCompletion
def generate_drone_commands(user_input, use_azure_openAI=False):
print(f'-----------------------------------------------')
# check for empty command and add a default value
if user_input == "":
print(f'Empty user input, using default user input')
user_input = "takeoff the drone, flip right, move down 30 centimeters and land"
print(f'Input user: {user_input}')
print(f'-----------------------------------------------')
# create a text completion service
kernel = sk.Kernel()
if use_azure_openAI:
# using Azure OpenAI Services
print(f'>> Using Azure OpenAI Services')
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion
deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
kernel.add_text_completion_service("dv", AzureTextCompletion(deployment, endpoint, api_key))
else:
# using OpenAI Services
print(f'>> Using OpenAI Services')
from semantic_kernel.connectors.ai.open_ai import OpenAITextCompletion
api_key, org_id = sk.openai_settings_from_dot_env()
kernel.add_text_completion_service("dv", OpenAITextCompletion("text-davinci-003", api_key, org_id))
# import the semantic skill from the directory
skill = kernel.import_semantic_skill_from_directory("skills", "DroneAI")
drone_function = skill["Drone"]
# generate the drone commands
drone_command = drone_function(user_input)
print(f'Generated Python code to control the drone')
print(f'-----------------------------------------------')
print(f'{drone_command}')
print(f'-----------------------------------------------')
return str(drone_command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment