Open blazickjp opened 10 months ago
1ba266c898
)[!TIP] I can email you next time I complete a pull request if you set up your email here!
Here are the sandbox execution logs prior to making any changes:
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.
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
backend/memory/memory_manager.py
✓ https://github.com/blazickjp/GPT-CodeApp/commit/b11cd11459272b993c369412c13f17cf0877806a Edit
Modify backend/memory/memory_manager.py with contents:
• Import the summarize method from gensim at the top of the file: `from gensim.summarize import summarize`.
• Add a new attribute to the WorkingContext class to keep track of the turns. Initialize it to 0 in the __init__ method: `self.turn_counter = 0`.
• Add a new method to the WorkingContext class called summarize_context. This method will call the gensim summarize method on the context attribute and return the result: ``` def summarize_context(self): return summarize(self.context) ```
• In the update_context method, increment the turn_counter by 1 each time the method is called: `self.turn_counter += 1`.
• Still in the update_context method, check if the turn_counter is equal to 5. If it is, call the summarize_context method, assign the result to the context attribute, and reset the turn_counter to 0: ``` if self.turn_counter == 5: self.context = self.summarize_context() self.turn_counter = 0 ```
--- +++ @@ -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.
backend/memory/memory_manager.py
✓ Edit
Check backend/memory/memory_manager.py with contents:
Ran GitHub Actions for b11cd11459272b993c369412c13f17cf0877806a:
backend/tests/test_memory_manager.py
✓ https://github.com/blazickjp/GPT-CodeApp/commit/459d4cf4b6896fb17bc275efaa5e919603af55ad Edit
Modify backend/tests/test_memory_manager.py with contents:
• Add a new test method to test the summarize_context method. This method will create a long context, call the summarize_context method, and assert that the returned summary is shorter than the original context.
• Add a new test method to test the turn_counter attribute. This method will call the update_context method 5 times and assert that the turn_counter is reset to 0 after the 5th call.
--- +++ @@ -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"
backend/tests/test_memory_manager.py
✓ Edit
Check backend/tests/test_memory_manager.py with contents:
Ran GitHub Actions for 459d4cf4b6896fb17bc275efaa5e919603af55ad:
backend/agent/coding_agent.py
✓ https://github.com/blazickjp/GPT-CodeApp/commit/1e0a34a20233a2c8b5293f950db716e9dea2a755 Edit
Modify backend/agent/coding_agent.py with contents:
• In the query method, after the line where the add_message method is called, check if the turn_counter of the memory_manager's working_context is 0. If it is, print the summarized context: ``` if self.memory_manager.working_context.turn_counter == 0: print("Summarized Context: ", self.memory_manager.working_context.context) ```
--- +++ @@ -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"]}
backend/agent/coding_agent.py
✓ Edit
Check backend/agent/coding_agent.py with contents:
Ran GitHub Actions for 1e0a34a20233a2c8b5293f950db716e9dea2a755:
I have finished reviewing the code for completeness. I did not find errors for sweep/auto_summarize_working_context
.
💡 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
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)