Skip to content

Commit

Permalink
Merge pull request #37 from stephenfreund/main
Browse files Browse the repository at this point in the history
Rewrite of ChatDBG for python
  • Loading branch information
stephenfreund authored Mar 1, 2024
2 parents 1fa6500 + c59e5b3 commit 0c8819f
Show file tree
Hide file tree
Showing 34 changed files with 1,201 additions and 85,142 deletions.
1 change: 0 additions & 1 deletion .github/workflows/sanity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ jobs:
- name: Check calling executables directly
run: |
chatdbg --help
ichatdbg --help
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,7 @@ This will install ChatDBG as a GDB extension.

### Debugging Python

To use ChatDBG to debug Python programs, simply run your Python script with the `-m` flag:

```bash
python3 -m chatdbg -c continue yourscript.py
```

or just
To use ChatDBG to debug Python programs, simply run your Python script as follows:

```bash
chatdbg -c continue yourscript.py
Expand All @@ -121,6 +115,30 @@ enter post mortem debugging mode.
Unlike other debuggers, you can then use the `why` command to ask
ChatDBG why your program failed and get a suggested fix.

#### IPython and Jupyter Support

To use ChatDBG as the default debugger for IPython or inside Jupyter Notebooks,
create a IPython profile and then add the necessary exensions on startup. (Modify
these lines as necessary if you already have a customized profile file.)

```bash
ipython profile create
echo "c.InteractiveShellApp.extensions = ['chatdbg.chatdbg_pdb', 'ipyflow']" >> ~/.ipython/profile_default/ipython_config.py
```

On the command line, you can then run:

```bash
ipython --pdb yourscript.py
```

Inside Jupyter, run your notebook with the [ipyflow kernel](https://github.com/ipyflow/ipyflow) and include this line magic at the top of the file.

```
%pdb
```


### Debugging native code (C, C++, or Rust with <TT>lldb</TT> / <TT>gdb</TT>)

To use ChatDBG with `lldb` or `gdb`, just run native code (compiled with `-g` for debugging symbols) with your choice of debugger; when it crashes, ask `why`. This also works for post mortem debugging (when you load a core with the `-c` option).
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ authors = [
{ name="Emery Berger", email="[email protected]" },
{ name="Kyla Levin", email="[email protected]" },
{ name="Nicolas van Kempen", email="[email protected]" },
{ name="Stephen Freund", email="[email protected]" },
{ name="Stephen Freund", email="[email protected]" }
]
dependencies = [
"llm-utils>=0.2.6",
Expand All @@ -20,6 +20,8 @@ dependencies = [
"ipdb>=0.13.13",
"ipython>=8.21.0",
"litellm>=1.26.6",
"PyYAML>=6.0.1",
"ipyflow>=0.0.130"
]
description = "AI-assisted debugging. Uses AI to answer 'why'."
readme = "README.md"
Expand All @@ -32,7 +34,6 @@ classifiers = [

[project.scripts]
chatdbg = "chatdbg.__main__:main"
ichatdbg = "chatdbg.__imain__:main"

[project.urls]
"Homepage" = "https://github.com/plasma-umass/ChatDBG"
Expand Down
9 changes: 0 additions & 9 deletions src/chatdbg/__imain__.py

This file was deleted.

7 changes: 5 additions & 2 deletions src/chatdbg/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from . import chatdbg
from .chatdbg_pdb import *
import ipdb

chatdbg.main()
def main():
ipdb.__main__._get_debugger_cls = lambda : ChatDBG
ipdb.__main__.main()
22 changes: 16 additions & 6 deletions src/chatdbg/assistant/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ def __init__(
try:
self.client = OpenAI(timeout=timeout)
except OpenAIError:
print("You need an OpenAI key to use this tool.")
print("You can get a key here: https://platform.openai.com/api-keys")
print("Set the environment variable OPENAI_API_KEY to your key value.")
sys.exit(1)
print(textwrap.dedent("""\
You need an OpenAI key to use this tool.
You can get a key here: https://platform.openai.com/api-keys
Set the environment variable OPENAI_API_KEY to your key value.
"""))
sys.exit(-1)


self.assistants = self.client.beta.assistants
self.threads = self.client.beta.threads
Expand All @@ -64,6 +67,8 @@ def _delete_assistant(self):
response = self.assistants.delete(id)
self._log(response)
assert response.deleted
except OSError:
raise
except Exception as e:
print(f"Assistant {id} was not deleted ({e}).")
print("You can do so at https://platform.openai.com/assistants.")
Expand All @@ -88,6 +93,7 @@ def add_function(self, function):
self._log(assistant)
except OpenAIError as e:
print(f"*** OpenAI Error: {e}")
sys.exit(-1)

def _make_call(self, tool_call):
name = tool_call.function.name
Expand All @@ -99,6 +105,8 @@ def _make_call(self, tool_call):
args = json.loads(args)
function = self.functions[name]
result = function(**args)
except OSError:
raise
except Exception as e:
result = f"Ill-formed function call ({e})\n"

Expand Down Expand Up @@ -177,6 +185,8 @@ def run(self, prompt, client_print=print):
thread_id=self.thread.id, run_id=run.id, tool_outputs=outputs
)
self._log(run)
except OSError:
raise
except Exception as e:
self._log(run, f"FAILED to submit tool call results: {e}")

Expand All @@ -186,7 +196,7 @@ def run(self, prompt, client_print=print):
if run.status == "failed":
message = f"\n**Internal Failure ({run.last_error.code}):** {run.last_error.message}"
client_print(message)
return 0, 0, 0
sys.exit(-1)

messages = self.threads.messages.list(
thread_id=self.thread.id, after=last_printed_message_id, order="asc"
Expand All @@ -206,7 +216,7 @@ def run(self, prompt, client_print=print):
return run.usage.total_tokens, cost, elapsed_time
except OpenAIError as e:
client_print(f"*** OpenAI Error: {e}")
return 0, 0, 0
sys.exit(-1)

def _log(self, obj, title=""):
if self.json != None:
Expand Down
Loading

0 comments on commit 0c8819f

Please sign in to comment.