How to control your 🚁 drone using #Azure OpenAI or OpenAI APIs and Semantic Kernel
Tags: englishpost, azureopenai, chatgpt, codesample
Published: 2023-08-03 20:55:37

---
title: How to control your 🚁 drone using #Azure OpenAI or OpenAI APIs and Semantic Kernel
published: true
date: 2023-07-31 13:00:00 UTC
tags: EnglishPost,AzureOpenAI,ChatGPT,CodeSample
canonical_url: 
---

## Coding4Fun Drone 🚁 posts

1. [Introduction to DJI Tello](https://elbruno.com/2020/02/03/coding4fun-how-to-control-your-drone-with-20-lines-of-code-1-n/)
2. [Analyzing Python samples code from the official SDK](https://elbruno.com/2020/02/04/coding4fun-how-to-control-your-drone-with-20-lines-of-code-2-n/)
3. [Drone Hello World ! Takeoff and land](https://elbruno.com/2020/02/10/coding4fun-how-to-control-your-drone-with-20-lines-of-code-3-n/)
4. [Tips to connect to Drone WiFi in Windows 10](https://elbruno.com/2020/02/11/coding4fun-how-to-control-your-drone-with-20-lines-of-code-4-n/)
5. [Reading data from the Drone, Get battery level](https://elbruno.com/?p=30977)
6. [Sample for real time data read, Get Accelerometer data](https://elbruno.com/?p=30984)
7. [How the drone camera video feed works, using FFMPEG to display the feed](https://elbruno.com/2020/02/17/coding4fun-how-to-control-your-drone-with-20-lines-of-code-7-n/)
8. [Open the drone camera video feed using OpenCV](https://elbruno.com/?p=31026)
9. [Performance and OpenCV, measuring FPS](https://elbruno.com/?p=31037)
10. [Detect faces using the drone camera](https://elbruno.com/?p=31119)
11. [Detect a banana and land!](https://elbruno.com/?p=31148)
12. [Flip when a face is detected!](https://elbruno.com/?p=31168)
13. [How to connect to Internet and to the drone at the same time](https://elbruno.com/?p=31305)
14. [Video with real time demo using the drone, Python and Visual Studio Code](https://elbruno.com/2020/04/10/coding4fun-how-to-control-your-drone-with-20-lines-of-code-14-n-globalaicommunity-virtual-tour-recorded-session/)
15. [Using custom vision to analyze drone camera images](https://elbruno.com/2020/04/24/coding4fun-how-to-control-your-drone-with-20-lines-of-code-15-n/)
16. [Drawing frames for detected objects in real-time in the drone camera feed](https://elbruno.com/?p=31380)
17. [Save detected objects to local files, images and JSON results](https://elbruno.com/?p=31396)
18. [Save the Drone camera feed into a local video file](https://elbruno.com/?p=31430)
19. [Overlay images into the Drone camera feed using OpenCV](https://elbruno.com/2020/05/07/coding4fun-how-to-control-your-drone-with-20-lines-of-code-19-n/)
20. [Instance Segmentation from the Drone Camera using OpenCV, TensorFlow and PixelLib](https://elbruno.com/2020/05/21/coding4fun-how-to-control-your-drone-with-20-lines-of-code-20-n/)
21. [Create a 3×3 grid on the camera frame to detect objects and calculate positions in the grid](https://elbruno.com/2020/06/05/coding4fun-how-to-control-your-drone-with-20-lines-of-code-21-n/)
22. [Create an Azure IoT Central Device Template to work with drone information](https://elbruno.com/2021/01/18/coding4fun-how-to-control-your-drone-%e2%9c%88-with-20-lines-of-code-22-n-azureiot/)
23. [Create a Drone Device for Azure IoT Central](https://elbruno.com/2021/01/19/coding4fun-how-to-control-your-drone-%e2%9c%88-with-20-lines-of-code-23-n-azureiot/)
24. [Send drone information to Azure IoT Central](https://elbruno.com/2021/01/20/coding4fun-how-to-control-your-drone-%e2%9c%88-with-20-lines-of-code-24-n-azureiot/)
25. [Using GPT models to generate code to control the drone. Using ChatGPT](https://elbruno.com/?p=36467)
26. [Generate code to control the 🚁 drone using Azure OpenAI Services or OpenAI APIs, and Semantic Kernel](https://elbruno.com/?p=36480)

Hi!

In my previous post, I wrote about how we can use ChatGPT to generate Python code to control a drone.

Today, we are going to use Azure OpenAI Services or OpenAI APIs to generate the same code using GPT models.

We are going to use the same prompt that we had for ChatGPT.

```

Use this python code as reference 

[CODE START]
# display battery level
sendReadCommand('battery?')
print(f'Battery: {battery} %')

# take off
sendCommand("takeoff") 

# flip the drone left
sendCommand("flip l") 

# move drone up 5 cms
sendCommand("up 5") 

# move drone left 5 cms
sendCommand("left 5") 

# rotate drone clock wise 90 degrees
sendCommand("cw 90") 

# land the drone
sendCommand("land") 

[CODE END]

Generate code to takeoff the drone, flip right, move down 30 centimeters and land

```

Running this prompt in Azure OpenAI Studio generates the correct Python code:

![Running this prompt in Azure OpenAI Studio generates the correct Python code:](https://brunocapuano.files.wordpress.com/2023/07/image-11.png?w=1024)

Both Azure OpenAI Services and OpenAI APIs have amazing SDKs, however, I’ll use semantic kernel to have a single code base that can use both.

_ **Note:** This is the best way to learn semantic kernel for NET and Python:_

#### [Start learning how to use Semantic Kernel.](https://learn.microsoft.com/en-us/semantic-kernel/get-started/quick-start-guide/)

Now, let’s create a simple function that:

- 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

Here is the function code:

{% gist https://gist.github.com/elbruno/913104ce3690bf4d803c469e96fa11a2/raw %}

And, I need to have a Skill directory with my DroneAI skill. Something like this:

![folder structure for a semantic kernel skill](https://brunocapuano.files.wordpress.com/2023/07/image-12.png?w=338)

Here is the config.json content:

```

{
  "schema": 1,
  "description": "Generate commands to control the drone",
  "type": "completion",
  "completion": {
    "max_tokens": 1000,
    "temperature": 0.0,
    "top_p": 1.0,
    "presence_penalty": 0.0,
    "frequency_penalty": 0.0
  },
  "input": {
    "parameters": [
      {
        "name": "input",
        "description": "commands for the drone",
        "defaultValue": ""
      }
    ]
  }
}

```

And the skprompt.txt:

```

Use this python code as reference 

[CODE START]
# display battery level
sendReadCommand('battery?')
print(f'Battery: {battery} %')

# take off
sendCommand("takeoff") 

# flip the drone left
sendCommand("flip l") 

# move drone up 5 cms
sendCommand("up 5") 

# move drone left 5 cms
sendCommand("left 5") 

# rotate drone clock wise 90 degrees
sendCommand("cw 90") 

# land the drone
sendCommand("land")
[CODE END]

Generate python code only to follow these orders
{{$input}}
+++++

```

And that’s it! We can test this function with 2 unit tests for for platforms:

```

import unittest
from DroneGenCommandsWithAI import generate_drone_commands

class TestGenerateDroneCommands(unittest.TestCase):

    test_command = "takeoff the drone, flip right, move down 30 centimeters and land the drone"

    def test_generate_drone_commandsusing_openAI(self):
        print('###############################################')
        print(' Test using OpenAI APIs')
        commands = generate_drone_commands(self.test_command)

        # validate if the generated string contains the drone commands
        self.assertIn('takeoff', commands)
        self.assertIn('flip r', commands)
        self.assertIn('down 30', commands)
        self.assertIn('land', commands)
        print('###############################################')

    def test_generate_drone_commandsusing_azureOpenAI(self):
        print('###############################################')
        print(' Test using AZURE OpenAI Services')
        commands = generate_drone_commands(self.test_command, True)

        # validate if the generated string contains the drone commands
        self.assertIn('takeoff', commands)
        self.assertIn('flip r', commands)
        self.assertIn('down 30', commands)
        self.assertIn('land', commands)

if __name__ == ' __main__':
    unittest.main()

```

And, they both works!

![test successfully run](https://brunocapuano.files.wordpress.com/2023/07/image-13.png?w=852)

In the next post we will review some options and configurations necessary for the correct code generation.

Happy coding!

Greetings

El Bruno

More posts in my blog [ElBruno.com](https://www.elbruno.com).

More info in [https://beacons.ai/elbruno](https://beacons.ai/elbruno)

* * *