From c80add335627c3af3cad44f1ca3f3561192fd4bb Mon Sep 17 00:00:00 2001 From: Evan Doyle Date: Wed, 17 Apr 2024 23:55:02 -0700 Subject: [PATCH 1/4] Version 0.1.11, add publish GHA, add some fixes and clean up error output when missing API key --- .github/workflows/publish.yml | 31 +++++++++++++++++++++++++++++++ chatpdb/__main__.py | 3 +++ chatpdb/chat/llm/openai.py | 20 ++++++++++++-------- chatpdb/chat/prompts/util.py | 12 ++++-------- pyproject.toml | 2 +- 5 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..30c9120 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,31 @@ +# This workflow will upload a Python Package using Twine when a release is created +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries + +name: Publish chatpdb + +on: + release: + types: [published] + +permissions: + contents: read + +jobs: + publish: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.x' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install build + - name: Build package + run: python -m build + - name: Publish package + uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 + with: + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/chatpdb/__main__.py b/chatpdb/__main__.py index c84d9c3..bcef64a 100644 --- a/chatpdb/__main__.py +++ b/chatpdb/__main__.py @@ -49,6 +49,9 @@ def _get_debugger_cls(): # Let IPython decide about which debugger class to use # This is especially important for tools that fiddle with stdout + if getattr(type(shell), "__module__", "").startswith("google.colab"): + # Google Colab has its own debugger, ChatPdb seems to work though + return ChatPdb return ChatPdb if shell.simple_prompt else TerminalChatPdb diff --git a/chatpdb/chat/llm/openai.py b/chatpdb/chat/llm/openai.py index 6f3f482..1f4979a 100644 --- a/chatpdb/chat/llm/openai.py +++ b/chatpdb/chat/llm/openai.py @@ -5,12 +5,16 @@ from pydantic import BaseModel -client = OpenAI( - api_key=os.environ.get("CHAT_PDB_OPENAI_API_KEY") - or os.environ.get("OPENAI_API_KEY"), - organization=os.environ.get("CHAT_PDB_OPENAI_ORG_ID") - or os.environ.get("OPENAI_ORG_ID"), -) +def get_client() -> OpenAI: + api_key = os.environ.get("CHAT_PDB_OPENAI_API_KEY") or os.environ.get( + "OPENAI_API_KEY" + ) + organization = os.environ.get("CHAT_PDB_OPENAI_ORG_ID") or os.environ.get( + "OPENAI_ORG_ID" + ) + if not api_key: + raise ValueError("OpenAI API key not set") + return OpenAI(api_key=api_key, organization=organization) def get_model() -> str: @@ -35,7 +39,7 @@ def user_message(cls, content: str) -> "OpenAIMessage": def prompt(messages: List[OpenAIMessage]) -> str: if not messages: raise ValueError("messages must not be empty for OpenAI prompt") - response = client.chat.completions.create( + response = get_client().chat.completions.create( messages=[message.model_dump() for message in messages], # type: ignore model=get_model(), ) @@ -45,7 +49,7 @@ def prompt(messages: List[OpenAIMessage]) -> str: def prompt_streaming(messages: List[OpenAIMessage]) -> Iterable[str]: if not messages: raise ValueError("messages must not be empty for OpenAI prompt") - completion_stream = client.chat.completions.create( # type: ignore + completion_stream = get_client().chat.completions.create( # type: ignore messages=[message.model_dump() for message in messages], # type: ignore model=get_model(), stream=True, diff --git a/chatpdb/chat/prompts/util.py b/chatpdb/chat/prompts/util.py index 52c2ea6..bcba351 100644 --- a/chatpdb/chat/prompts/util.py +++ b/chatpdb/chat/prompts/util.py @@ -29,14 +29,10 @@ def format_stack_trace(stack_trace: List[traceback.FrameSummary]) -> str: else: content.append(f" {i + 1} | {line}") file_contents[filename] = "".join(content) - except FileNotFoundError: - console.print( - f"Warning: File found in traceback ('{filename}') does not exist." - ) - except IOError: - console.print( - f"Warning: Could not read file found in traceback ('{filename}')." - ) + except (FileNotFoundError, IOError): + # Just continue if we can't read the file + # Later we can show a warning in verbose mode or similar + pass files = [] for frame in stack_trace: diff --git a/pyproject.toml b/pyproject.toml index 4f0d5bc..8a283d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "chatpdb" -version = "0.1.10" +version = "0.1.11" authors = [ { name="Caelean Barnes", email="caeleanb@gmail.com" }, { name="Evan Doyle", email="evanmdoyle@gmail.com" }, From c25536f3fb01de55b1452712f98e8c5dd057192a Mon Sep 17 00:00:00 2001 From: Evan Doyle Date: Thu, 18 Apr 2024 00:06:38 -0700 Subject: [PATCH 2/4] Fix build --- pyproject.toml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8a283d8..cda57f0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,5 +52,7 @@ reportMissingTypeStubs = false requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" -[tool.setuptools] -packages = ["chatpdb", "chatpdb.chat", "chatpdb.parsing"] +[tool.setuptools.packages.find] +where = ["chatpdb"] +include = ["*"] +namespaces = false From 4f15643d24c985f651950002af0769212ad22a98 Mon Sep 17 00:00:00 2001 From: Evan Doyle Date: Thu, 18 Apr 2024 00:07:22 -0700 Subject: [PATCH 3/4] 0.1.12 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index cda57f0..5363fac 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "chatpdb" -version = "0.1.11" +version = "0.1.12" authors = [ { name="Caelean Barnes", email="caeleanb@gmail.com" }, { name="Evan Doyle", email="evanmdoyle@gmail.com" }, From 772f531a0b695525558bf9dc120517e834f57121 Mon Sep 17 00:00:00 2001 From: Evan Doyle Date: Thu, 18 Apr 2024 00:10:23 -0700 Subject: [PATCH 4/4] 0.1.13, Restore top level package to build --- pyproject.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5363fac..71cd2a1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "chatpdb" -version = "0.1.12" +version = "0.1.13" authors = [ { name="Caelean Barnes", email="caeleanb@gmail.com" }, { name="Evan Doyle", email="evanmdoyle@gmail.com" }, @@ -53,6 +53,6 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [tool.setuptools.packages.find] -where = ["chatpdb"] -include = ["*"] +where = ["."] +include = ["chatpdb*"] namespaces = false