Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sweep: Add chat_logger logs to context_pruning.py similarly to how assistant_wrapper.py handles it #2722

Open
2 tasks done
wwzeng1 opened this issue Dec 7, 2023 · 1 comment · May be fixed by #2724
Open
2 tasks done
Labels
sweep Assigns Sweep to an issue or pull request.

Comments

@wwzeng1
Copy link
Contributor

wwzeng1 commented Dec 7, 2023

we want to update the chat_logger logs after the entire run is complete

Checklist
  • Modify sweepai/core/context_pruning.pyd3a5011 Edit
  • Running GitHub Actions for sweepai/core/context_pruning.pyEdit

Flowchart

@wwzeng1 wwzeng1 added the sweep Assigns Sweep to an issue or pull request. label Dec 7, 2023
@wwzeng1 wwzeng1 changed the title Sweep: Add chat_logger logs to context_pruning.oy similarly to how assistant_wrapper.py handles it Sweep: Add chat_logger logs to context_pruning.py similarly to how assistant_wrapper.py handles it Dec 7, 2023
Copy link
Contributor

sweep-nightly bot commented Dec 7, 2023

Here's the PR! #2724. See Sweep's process at dashboard.

💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: 276666f22c)

Actions (click)

  • ↻ Restart Sweep

Sandbox Execution ✓

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

Sandbox logs for 338fc3c
Checking sweepai/core/context_pruning.py for syntax errors... ✅ sweepai/core/context_pruning.py has no syntax errors! 1/1 ✓
Checking sweepai/core/context_pruning.py for syntax errors...
✅ sweepai/core/context_pruning.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.

]
if message_strings != current_message_strings and current_message_strings:
logger.info(run.status)
logger.info(current_message_strings[0])
message_strings = current_message_strings
json_messages = get_json_messages(
thread_id=thread_id,
run_id=run_id,
assistant_id=assistant_id,
)
if chat_logger is not None:
chat_logger.add_chat(
{
"model": model,
"messages": json_messages,
"output": message_strings[0],
"thread_id": thread_id,
"run_id": run_id,
"max_tokens": 1000,
"temperature": 0,
}
)
else:
if i % 5 == 0:
logger.info(run.status)

)
return messages_json
def run_until_complete(
thread_id: str,
run_id: str,
assistant_id: str,
model: str = "gpt-4-1106-preview",
chat_logger: ChatLogger | None = None,
sleep_time: int = 3,
max_iterations: int = 200,
save_ticket_progress: save_ticket_progress_type | None = None,
):

)
run_until_complete(
thread_id=thread.id,
run_id=run.id,
model=model,
chat_logger=chat_logger,
assistant_id=assistant.id,
sleep_time=sleep_time,
save_ticket_progress=save_ticket_progress,
)
for file_id in file_ids:
client.files.delete(file_id=file_id)
return (
assistant.id,
run.id,
thread.id,

import json
import re
import time
from copy import deepcopy
from attr import dataclass
from loguru import logger
from openai.types.beta.thread import Thread
from openai.types.beta.threads.run import Run
from sweepai.agents.assistant_wrapper import client, openai_retry_with_timeout
from sweepai.core.entities import Snippet
from sweepai.logn.cache import file_cache
from sweepai.utils.chat_logger import ChatLogger
from sweepai.utils.github_utils import ClonedRepo
from sweepai.utils.progress import AssistantConversation, TicketProgress
from sweepai.utils.tree_utils import DirectoryTree
ASSISTANT_MAX_CHARS = 4096 * 4 * 0.95 # ~95% of 4k tokens

if can_add_snippet(snippet, self.current_top_snippets):
self.current_top_snippets.append(snippet)
@file_cache(ignore_params=["repo_context_manager", "ticket_progress", "chat_logger"])
def get_relevant_context(
query: str,
repo_context_manager: RepoContextManager,
ticket_progress: TicketProgress | None = None,
chat_logger: ChatLogger = None,
):

tool_outputs=tool_outputs,
)
else:
logger.warning(
f"Context pruning iteration taking too long. Status: {run.status}"
)
assistant_conversation = AssistantConversation.from_ids(
assistant_id=run.assistant_id,
run_id=run.id,
thread_id=thread.id,
)
if ticket_progress:
if assistant_conversation:
ticket_progress.search_progress.pruning_conversation = (
assistant_conversation
)
ticket_progress.save()
logger.info(
f"Context Management End:\npaths_to_keep: {paths_to_keep}\npaths_to_add: {paths_to_add}\ndirectories_to_expand: {directories_to_expand}"
)
if paths_to_keep or paths_to_add:
repo_context_manager.remove_all_non_kept_paths(paths_to_keep + paths_to_add)
if directories_to_expand:
repo_context_manager.expand_all_directories(directories_to_expand)
logger.info(
f"Context Management End:\ncurrent snippet paths: {repo_context_manager.top_snippet_paths}"
)
paths_changed = set(initial_file_paths) != set(
repo_context_manager.top_snippet_paths
)
# if the paths have not changed or all tools were empty, we are done
return not (
paths_changed and (paths_to_keep or directories_to_expand or paths_to_add)
)
if __name__ == "__main__":
import os
from sweepai.utils.ticket_utils import prep_snippets
installation_id = os.environ["INSTALLATION_ID"]
cloned_repo = ClonedRepo("sweepai/sweep", installation_id, "main")
query = "create a new search query filtering agent that will be used in ticket_utils.py. The agent should filter unnecessary terms out of the search query to be sent into lexical search. Use a prompt to do this, using name_agent.py as a reference."
ticket_progress = TicketProgress(
tracking_id="test",
)
import linecache
import sys
def trace_lines(frame, event, arg):
if event == "line":
filename = frame.f_code.co_filename
if "context_pruning" in filename:
lineno = frame.f_lineno
line = linecache.getline(filename, lineno)
print(f"Executing {filename}:line {lineno}:{line.rstrip()}")
return trace_lines
sys.settrace(trace_lines)
repo_context_manager = prep_snippets(cloned_repo, query, ticket_progress)
rcm = get_relevant_context(
query,
repo_context_manager,
ticket_progress,
chat_logger=ChatLogger({"username": "wwzeng1"}),
)

):
modify_iterations: int = 2
model = (
"gpt-3.5-turbo-1106"
if (chat_logger and chat_logger.use_faster_model())
else "gpt-4-1106-preview"
)

def add_chat(self, additional_data):
if self.chat_collection is None:
logger.error("Chat collection is not initialized")
return
document = {
**self.data,
**additional_data,
"expiration": self.expiration,
"index": self.index,
}
self.index += 1
self.chat_collection.insert_one(document)
def add_successful_ticket(self, gpt3=False):
if self.ticket_collection is None:
logger.error("Ticket Collection Does Not Exist")
return
username = self.data.get("assignee", self.data["username"])
update_fields = {self.current_month: 1, self.current_date: 1}
if gpt3:
key = f"{self.current_month}_gpt3"
update_fields = {key: 1}
self.ticket_collection.update_one(
{"username": username}, {"$inc": update_fields}, upsert=True
)
ticket_count = self.get_ticket_count()
should_decrement = (self.is_paying_user() and ticket_count >= 500) or (
self.is_consumer_tier() and ticket_count >= 20
)
if should_decrement:
self.ticket_collection.update_one(
{"username": username}, {"$inc": {"purchased_tickets": -1}}, upsert=True
)
logger.info(f"Added Successful Ticket for {username}")
def _cache_key(self, username, field, metadata=""):
return f"{username}_{field}_{metadata}"


Step 2: ⌨️ Coding

  • Modify sweepai/core/context_pruning.pyd3a5011 Edit
Modify sweepai/core/context_pruning.py with contents:
• At the end of the get_relevant_context function, after the context pruning process is complete, add a call to the add_chat method of the chat_logger object.
• The add_chat method should be called with a dictionary that contains the relevant data. This data might include the query, the repo_context_manager object, the ticket_progress object, and any other data that is relevant to the context pruning process.
• For example, the code might look like this: ```python chat_logger.add_chat( { "query": query, "repo_context_manager": repo_context_manager, "ticket_progress": ticket_progress, # add any other relevant data here } ) ```
• Make sure to handle the case where the chat_logger object is None. In this case, the add_chat method should not be called. You can do this by adding a condition before the call to the add_chat method, like this: ```python if chat_logger is not None: chat_logger.add_chat( { "query": query, "repo_context_manager": repo_context_manager, "ticket_progress": ticket_progress, # add any other relevant data here } ) ```

+++
@@ -1,3 +1,4 @@
+from sweepai.api import chat_logger
import json
import re
import time
@@ -431,9 +432,18 @@
repo_context_manager.top_snippet_paths
)
# if the paths have not changed or all tools were empty, we are done

  • return not (
  •    paths_changed and (paths_to_keep or directories_to_expand or paths_to_add)
    
  • )
  • finished = not (paths_changed and (paths_to_keep or directories_to_expand or paths_to_add))
  • if chat_logger is not None:
  •    chat_logger.add_chat(
    
  •        {
    
  •            "query": query,
    
  •            "repo_context_manager": repo_context_manager,
    
  •            "ticket_progress": ticket_progress,
    
  •            # add any other relevant data here
    
  •        }
    
  •    )
    
  • return finished

if name == "main":

  • Running GitHub Actions for sweepai/core/context_pruning.pyEdit
Check sweepai/core/context_pruning.py with contents:

Ran GitHub Actions for d3a50112db4d73a8c62906d62f51f86dad9afba3:
• black:
• Vercel Preview Comments:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/add-chat-logger-to-context-pruning.


🎉 Latest improvements to Sweep:

  • We just released a dashboard to track Sweep's progress on your issue in real-time, showing every stage of the process – from search to planning and coding.
  • Sweep uses OpenAI's latest Assistant API to plan code changes and modify code! This is 3x faster and significantly more reliable as it allows Sweep to edit code and validate the changes in tight iterations, the same way as a human would.

💡 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sweep Assigns Sweep to an issue or pull request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant