blazickjp / GPT-CodeApp

This project is everything Chat-GPT should be for developers! An advanced AI-driven coding companion tailored for developers. Seamlessly bridging the gap between traditional coding and AI capabilities, we offer real-time chat interactions, on-demand agent functions, and intuitive code management. Feedback welcome!
MIT License
33 stars 12 forks source link

Sweep: Auto Summarize Working Context #23

Open blazickjp opened 10 months ago

blazickjp commented 10 months ago

Details

Add a feature to the working context that automatically summarizes the working context after every 5 turns of the conversation.

Checklist - [X] Modify `backend/memory/memory_manager.py` ✓ https://github.com/blazickjp/GPT-CodeApp/commit/b11cd11459272b993c369412c13f17cf0877806a [Edit](https://github.com/blazickjp/GPT-CodeApp/edit/sweep/auto_summarize_working_context/backend/memory/memory_manager.py#L16-L54) - [X] Running GitHub Actions for `backend/memory/memory_manager.py` ✓ [Edit](https://github.com/blazickjp/GPT-CodeApp/edit/sweep/auto_summarize_working_context/backend/memory/memory_manager.py#L16-L54) - [X] Modify `backend/tests/test_memory_manager.py` ✓ https://github.com/blazickjp/GPT-CodeApp/commit/459d4cf4b6896fb17bc275efaa5e919603af55ad [Edit](https://github.com/blazickjp/GPT-CodeApp/edit/sweep/auto_summarize_working_context/backend/tests/test_memory_manager.py#L85-L118) - [X] Running GitHub Actions for `backend/tests/test_memory_manager.py` ✓ [Edit](https://github.com/blazickjp/GPT-CodeApp/edit/sweep/auto_summarize_working_context/backend/tests/test_memory_manager.py#L85-L118) - [X] Modify `backend/agent/coding_agent.py` ✓ https://github.com/blazickjp/GPT-CodeApp/commit/1e0a34a20233a2c8b5293f950db716e9dea2a755 [Edit](https://github.com/blazickjp/GPT-CodeApp/edit/sweep/auto_summarize_working_context/backend/agent/coding_agent.py#L59-L120) - [X] Running GitHub Actions for `backend/agent/coding_agent.py` ✓ [Edit](https://github.com/blazickjp/GPT-CodeApp/edit/sweep/auto_summarize_working_context/backend/agent/coding_agent.py#L59-L120)
sweep-ai[bot] commented 10 months ago

🚀 Here's the PR! #24

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: 1ba266c898)

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

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


Actions (click)

Sandbox Execution ✓

Here are the sandbox execution logs prior to making any changes:

Sandbox logs for 08b3592
Checking backend/memory/memory_manager.py for syntax errors... ✅ backend/memory/memory_manager.py has no syntax errors! 1/1 ✓
Checking backend/memory/memory_manager.py for syntax errors...
✅ backend/memory/memory_manager.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/blazickjp/GPT-CodeApp/blob/08b3592ad4397aaf2dfb6a3c5cab8d9e63069466/backend/memory/memory_manager.py#L321-L366 https://github.com/blazickjp/GPT-CodeApp/blob/08b3592ad4397aaf2dfb6a3c5cab8d9e63069466/backend/memory/memory_manager.py#L15-L54 https://github.com/blazickjp/GPT-CodeApp/blob/08b3592ad4397aaf2dfb6a3c5cab8d9e63069466/backend/tests/test_memory_manager.py#L85-L118 https://github.com/blazickjp/GPT-CodeApp/blob/08b3592ad4397aaf2dfb6a3c5cab8d9e63069466/backend/agent/coding_agent.py#L59-L120

Step 2: ⌨️ Coding

--- 
+++ 
@@ -9,6 +9,7 @@
 from instructor import OpenAISchema
 from openai import OpenAI, AsyncOpenAI
 import logging
+from gensim.summarize import summarize

 CLIENT = instructor.patch(AsyncOpenAI())

@@ -34,6 +35,7 @@
         self.cur = self.conn.cursor()
         self.client = CLIENT
         self.project_directory = project_directory
+        self.turn_counter = 0
         self.create_tables()

     def create_tables(self) -> None:
@@ -92,6 +94,9 @@
             (context, self.project_directory),
         )
         self.conn.commit()
+
+    def summarize_context(self):
+        return summarize(self.context)

     def __str__(self) -> str:
         return self.context
@@ -321,6 +326,10 @@

     async def update_context(self):
         ctx = self.working_context.get_context()
+        self.turn_counter += 1
+        if self.turn_counter == 5:
+            self.context = self.summarize_context()
+            self.turn_counter = 0
         print("Working Context: ", ctx)
         prompt = f"""
 You are monitoring a conversation between an engineer and their AI Assistant.

Ran GitHub Actions for b11cd11459272b993c369412c13f17cf0877806a:

--- 
+++ 
@@ -68,6 +68,24 @@
         self.cursor.fetchall.return_value = [("test_context",)]
         self.memory_manager = MemoryManager(db_connection=self.conn)

+    def test_summarize_context_shortens_long_context(self):
+        # Arrange: Create a context longer than what summarize would return
+        long_context = 'A very long repetitive context ' * 30  # assuming summarization would shorten this
+        self.memory_manager.working_context.context = long_context
+        # Act: Call summarize_context method
+        summary = self.memory_manager.working_context.summarize_context()
+        # Assert: Check that the summary is shorter than the original context
+        assert len(summary) < len(long_context), 'Summarize did not shorten the context'
+
+    def test_turn_counter_reset_after_5th_update(self):
+        # Arrange: Reset the turn counter to 0
+        self.memory_manager.working_context.turn_counter = 0
+        # Act: Call the update_context method 5 times
+        for _ in range(5):
+            asyncio.run(self.memory_manager.update_context())
+        # Assert: Check whether the turn_counter is reset to 0
+        assert self.memory_manager.working_context.turn_counter == 0, 'Turn counter was not reset after 5th update'
+
     def test_add_message(self):
         # Arrange: Prepare the message to be added
         role = "user"

Ran GitHub Actions for 459d4cf4b6896fb17bc275efaa5e919603af55ad:

--- 
+++ 
@@ -114,6 +114,9 @@
         """
         print(f"Input Text: {input}\nCommand: {command}")
         self.memory_manager.add_message("user", input)
+
+        if self.memory_manager.working_context.turn_counter == 0:
+            print("Summarized Context: ", self.memory_manager.working_context.context)

         message_history = [
             {"role": i["role"], "content": i["content"]}

Ran GitHub Actions for 1e0a34a20233a2c8b5293f950db716e9dea2a755:


Step 3: 🔁 Code Review

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


🎉 Latest improvements to Sweep:


💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request. Join Our Discord