Skip to content

Instantly share code, notes, and snippets.

Created March 8, 2023 16:51
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tito/230863f714ab43fc92ad55510de2ca4e to your computer and use it in GitHub Desktop.
Using OpenAI/ChatGPT as refactoring tool
Refactoring tool using ChatGPT from Vue 2 to Vue 3
$ export OPENAPI_APIKEY=sk.........
$ python MyView.vue
import os
import re
import sys
import openai
You are an assistant design to help developper for migrating their code from Vue 2 to Vue 3 using Typescript with Composition API. Here is a set of rules you must absolutely follow:
1. Rewrite the <script lang="ts"> to <script setup lang="ts">
2. The content of the script tag must be a valid Typescript code
3. The component must be flattened into the script setup
4. Remove any "export default".
5. Use the `onMounted` hook instead of the `created` lifecycle hook if necessary
6. Use the `useRoute` approach instead of $route. Same for $router.
7. Store is not using vuex but pinia.
8. Auth related function is accessible in stores/auth.ts, using useAuthStore.
9. Do not use Ref is the type can be infered from the value pass into ref()
10. Do not put all the methods and properties into a global const object
11. Prefer using global "const router = useRouter()" instead of live instanciation when needed
RE_SCRIPT = re.compile(r"(<script lang=\"ts\">.*</script>)", re.DOTALL)
def refactor(filename, model):
with open(filename, "r", encoding="utf8") as f:
content =
# extract the script tag from <script lang="ts"> to </script>
match =
if not match:
print("ERR: No script tag found")
spanstart, spanend = match.span()
# print the start
# ask for refactoring
response = openai.ChatCompletion.create(
{"role": "system", "content": REFACTOR_PROMPT},
{"role": "user", "content": content[spanstart:spanend]},
# get the refactored script
for entry in response:
choice = entry["choices"][0]
if choice["finish_reason"] == "stop":
if choice["finish_reason"] is not None:
print("ERR: Unexpected finish_reason", choice["finish_reason"])
delta_content = choice["delta"].get("content")
if delta_content is not None:
print(delta_content, end="")
# replace the script tag
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=str, default="gpt-3.5-turbo")
parser.add_argument("file", type=str)
args = parser.parse_args()
openai.api_key = os.environ["OPENAI_APIKEY"]
refactor(args.file, args.model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment