Skip to content

Commit

Permalink
Merge pull request #27 from plasma-umass/upgrade-openai
Browse files Browse the repository at this point in the history
Fix issues due to OpenAI module breaking backwards-compatibility
  • Loading branch information
khlevin authored Jan 9, 2024
2 parents 53e72c3 + 6e2cade commit f630e12
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
16 changes: 6 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "ChatDBG"
version = "0.2.2"
authors = [
{ name="Emery Berger", email="[email protected]" },
]
dependencies = ["llm_utils==0.2.2", "openai>=0.27.0"]
description = "ChatDBG."
dependencies = ["llm_utils==0.2.2", "openai>=1.6.1"]
description = "AI-assisted debugging. Uses AI to answer 'why'."
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: MIT License",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
]

Expand All @@ -27,4 +23,4 @@ chatdbg = "chatdbg.__main__:main"

[project.urls]
"Homepage" = "https://github.com/plasma-umass/ChatDBG"
"Bug Tracker" = "https://github.com/plasma-umass/ChatDBG"
"Bug Tracker" = "https://github.com/plasma-umass/ChatDBG/issues"
39 changes: 26 additions & 13 deletions src/chatdbg/chatdbg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,31 @@ def explain(source_code: str, traceback: str, exception: str, really_run=True) -
return

try:
completion = openai.ChatCompletion.create(
model=model,
request_timeout=30,
messages=[{"role": "user", "content": user_prompt}],
)
text = completion.choices[0].message.content
input_tokens = completion.usage.prompt_tokens
output_tokens = completion.usage.completion_tokens
cost = llm_utils.calculate_cost(input_tokens, output_tokens, model)
text += f"\n(Total cost: approximately ${cost:.2f} USD.)"
print(llm_utils.word_wrap_except_code_blocks(text))
except openai.error.AuthenticationError:
print("You need a valid OpenAI key to use ChatDBG.")
client = openai.OpenAI(timeout=30)
except openai.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.")
return

try:
completion = client.chat.completions.create(
model=model, messages=[{"role": "user", "content": user_prompt}]
)
except openai.NotFoundError:
print(f"'{model}' either does not exist or you do not have access to it.")
return
except openai.RateLimitError:
print("You have exceeded a rate limit or have no remaining funds.")
return
except openai.APITimeoutError:
print("The OpenAI API timed out.")
return

text = completion.choices[0].message.content
print(llm_utils.word_wrap_except_code_blocks(text))

input_tokens = completion.usage.prompt_tokens
output_tokens = completion.usage.completion_tokens
cost = llm_utils.calculate_cost(input_tokens, output_tokens, model)
print(f"\n(Total cost: approximately ${cost:.2f} USD.)")
33 changes: 22 additions & 11 deletions src/chatdbg/chatdbg_why.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,26 @@ def why(self, arg):
return

try:
completion = openai.ChatCompletion.create(
model=model,
request_timeout=30,
messages=[{"role": "user", "content": user_prompt}],
)
text = completion.choices[0].message.content
print(llm_utils.word_wrap_except_code_blocks(text))
except openai.error.AuthenticationError:
print(
"You need a valid OpenAI key to use ChatDBG. You can get a key here: https://openai.com/api/"
)
client = openai.OpenAI(timeout=30)
except openai.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.")
return

try:
completion = client.chat.completions.create(
model=model, messages=[{"role": "user", "content": user_prompt}]
)
except openai.NotFoundError:
print(f"'{model}' either does not exist or you do not have access to it.")
return
except openai.RateLimitError:
print("You have exceeded a rate limit or have no remaining funds.")
return
except openai.APITimeoutError:
print("The OpenAI API timed out.")
return

text = completion.choices[0].message.content
print(llm_utils.word_wrap_except_code_blocks(text))

0 comments on commit f630e12

Please sign in to comment.