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
Comments
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)
Sandbox Execution ✓Here are the sandbox execution logs prior to making any changes: Sandbox logs for
|
] | |
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) |
sweep/sweepai/agents/assistant_wrapper.py
Lines 143 to 156 in 338fc3c
) | |
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, | |
): |
sweep/sweepai/agents/assistant_wrapper.py
Lines 287 to 302 in 338fc3c
) | |
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, |
sweep/sweepai/core/context_pruning.py
Lines 1 to 20 in 338fc3c
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 | |
sweep/sweepai/core/context_pruning.py
Lines 200 to 210 in 338fc3c
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, | |
): |
sweep/sweepai/core/context_pruning.py
Lines 403 to 469 in 338fc3c
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"}), | |
) |
sweep/sweepai/core/context_pruning.py
Lines 210 to 216 in 338fc3c
): | |
modify_iterations: int = 2 | |
model = ( | |
"gpt-3.5-turbo-1106" | |
if (chat_logger and chat_logger.use_faster_model()) | |
else "gpt-4-1106-preview" | |
) |
sweep/sweepai/utils/chat_logger.py
Lines 67 to 111 in 338fc3c
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.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.py
✓ Edit
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
we want to update the chat_logger logs after the entire run is complete
Checklist
sweepai/core/context_pruning.py
✓ d3a5011 Editsweepai/core/context_pruning.py
✓ EditThe text was updated successfully, but these errors were encountered: