Skeyelab / MoneyPrinter

Automate Creation of YouTube Shorts using MoviePy.
MIT License
0 stars 0 forks source link

Sweep: look for ways to refactor the gpt.py file #1

Open Skeyelab opened 9 months ago

Skeyelab commented 9 months ago
Checklist - [X] Modify `Backend/utils.py` ✓ https://github.com/Skeyelab/MoneyPrinter/commit/81d1a92a3de6d164c562abd9e8175b47c47bb100 [Edit](https://github.com/Skeyelab/MoneyPrinter/edit/sweep/look_for_ways_to_refactor_the_gptpy_file/Backend/utils.py) - [X] Running GitHub Actions for `Backend/utils.py` ✓ [Edit](https://github.com/Skeyelab/MoneyPrinter/edit/sweep/look_for_ways_to_refactor_the_gptpy_file/Backend/utils.py) - [X] Modify `Backend/gpt.py` ✓ https://github.com/Skeyelab/MoneyPrinter/commit/be3712e916ea1f102477cc200a9fd8c5d5b72ffc [Edit](https://github.com/Skeyelab/MoneyPrinter/edit/sweep/look_for_ways_to_refactor_the_gptpy_file/Backend/gpt.py#L38-L57) - [X] Running GitHub Actions for `Backend/gpt.py` ✓ [Edit](https://github.com/Skeyelab/MoneyPrinter/edit/sweep/look_for_ways_to_refactor_the_gptpy_file/Backend/gpt.py#L38-L57) - [X] Modify `Backend/gpt.py` ✓ https://github.com/Skeyelab/MoneyPrinter/commit/b5d7db16b1cb5f12ca8e76f248249b95d9279dc5 [Edit](https://github.com/Skeyelab/MoneyPrinter/edit/sweep/look_for_ways_to_refactor_the_gptpy_file/Backend/gpt.py#L100-L120) - [X] Running GitHub Actions for `Backend/gpt.py` ✓ [Edit](https://github.com/Skeyelab/MoneyPrinter/edit/sweep/look_for_ways_to_refactor_the_gptpy_file/Backend/gpt.py#L100-L120)
sweep-ai[bot] commented 9 months ago

🚀 Here's the PR! #4

See Sweep's progress at the progress dashboard!
Sweep Basic Tier: I'm using GPT-4. You have 5 GPT-4 tickets left for the month and 3 for the day. (tracking ID: 71d036d8d0)

For more GPT-4 tickets, visit our payment portal. For a one week free trial, try Sweep Pro (unlimited GPT-4 tickets).
Install Sweep Configs: Pull Request

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)

GitHub Actions✓

Here are the GitHub Actions logs prior to making any changes:

Sandbox logs for fe6f875
Checking Backend/gpt.py for syntax errors... ✅ Backend/gpt.py has no syntax errors! 1/1 ✓
Checking Backend/gpt.py for syntax errors...
✅ Backend/gpt.py has no syntax errors!

Sandbox passed on the latest main, so sandbox checks will be enabled for this issue.


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/Skeyelab/MoneyPrinter/blob/fe6f875bd37edf59ac0ba333263d0903bca4d3ad/Backend/gpt.py#L7-L58 https://github.com/Skeyelab/MoneyPrinter/blob/fe6f875bd37edf59ac0ba333263d0903bca4d3ad/Backend/gpt.py#L61-L124

Step 2: ⌨️ Coding

--- 
+++ 
@@ -1,6 +1,9 @@
 import os
 import logging
+import g4f
+import json
 from termcolor import colored
+from utils import send_prompt_to_gpt

 # Configure logging
 logging.basicConfig(level=logging.INFO)

Ran GitHub Actions for 81d1a92a3de6d164c562abd9e8175b47c47bb100:

--- 
+++ 
@@ -1,6 +1,7 @@
 import re
 import json
 import g4f
+from utils import send_prompt_to_gpt
 from typing import Tuple, List  
 from termcolor import colored

@@ -36,12 +37,12 @@
     """

     # Generate script
-    response = g4f.ChatCompletion.create(
-        model=g4f.models.gpt_35_turbo_16k_0613,
-        messages=[{"role": "user", "content": prompt}],
+    response = send_prompt_to_gpt(
+        model='gpt_35_turbo_16k_0613',
+        prompt=prompt
     )

-    print(colored(response, "cyan"))
+    

     # Return the generated script
     if response:
@@ -55,7 +56,7 @@
         response = re.sub(r'\(.*\)', '', response)

         return f"{response} "
-    print(colored("[-] GPT returned an empty response.", "red"))
+    
     return None

Ran GitHub Actions for be3712e916ea1f102477cc200a9fd8c5d5b72ffc:

--- 
+++ 
@@ -1,6 +1,7 @@
 import re
 import json
 import g4f
+from utils import send_prompt_to_gpt
 from typing import Tuple, List  
 from termcolor import colored

@@ -36,12 +37,12 @@
     """

     # Generate script
-    response = g4f.ChatCompletion.create(
-        model=g4f.models.gpt_35_turbo_16k_0613,
-        messages=[{"role": "user", "content": prompt}],
+    response = send_prompt_to_gpt(
+        model='gpt_35_turbo_16k_0613',
+        prompt=prompt
     )

-    print(colored(response, "cyan"))
+    

     # Return the generated script
     if response:
@@ -55,7 +56,7 @@
         response = re.sub(r'\(.*\)', '', response)

         return f"{response} "
-    print(colored("[-] GPT returned an empty response.", "red"))
+    
     return None

@@ -98,25 +99,24 @@
     """

     # Generate search terms
-    response = g4f.ChatCompletion.create(
-        model=g4f.models.gpt_35_turbo_16k_0613,
-        messages=[{"role": "user", "content": prompt}],
+    response = send_prompt_to_gpt(
+        model='gpt_35_turbo_16k_0613',
+        prompt=prompt
     )

-    # Load response into JSON-Array
+    # Try to parse the response as JSON
     try:
         search_terms = json.loads(response)
-    except Exception:
-        print(colored("[*] GPT returned an unformatted response. Attempting to clean...", "yellow"))
-
-        # Use Regex to extract the array from the markdown
-        search_terms = re.findall(r'\[.*\]', str(response))
-
-        if not search_terms:
-            print(colored("[-] Could not parse response.", "red"))
-
-        # Load the array into a JSON-Array
-        search_terms = json.loads(search_terms)
+    except json.JSONDecodeError as e:
+        print(colored(f"[*] GPT returned an unformatted response: {str(e)}", "yellow"))
+        # Attempt to extract and process JSON array from response as fallback
+        found_array = re.findall(r'\[.*\]', str(response))
+        if found_array:
+            try:
+                search_terms = json.loads(found_array[0])
+            except json.JSONDecodeError:
+                print(colored("[-] Failed to parse the extracted JSON array.", "red"))
+                search_terms = None

     # Let user know
     print(colored(f"\nGenerated {amount} search terms: {', '.join(search_terms)}", "cyan"))

Ran GitHub Actions for b5d7db16b1cb5f12ca8e76f248249b95d9279dc5:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/look_for_ways_to_refactor_the_gptpy_file.


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.Something wrong? Let us know.

This is an automated message generated by Sweep AI.

Skeyelab commented 9 months ago

Sweep, this is good, but lets keep these new functions in the gpt.py file