Skip to content

Commit

Permalink
Version 0.1.11, add publish GHA, add some fixes and clean up error ou…
Browse files Browse the repository at this point in the history
…tput when missing API key
  • Loading branch information
emdoyle committed Apr 18, 2024
1 parent 9b050ed commit c80add3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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 }}
3 changes: 3 additions & 0 deletions chatpdb/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
20 changes: 12 additions & 8 deletions chatpdb/chat/llm/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(),
)
Expand All @@ -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,
Expand Down
12 changes: 4 additions & 8 deletions chatpdb/chat/prompts/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "chatpdb"
version = "0.1.10"
version = "0.1.11"
authors = [
{ name="Caelean Barnes", email="[email protected]" },
{ name="Evan Doyle", email="[email protected]" },
Expand Down

0 comments on commit c80add3

Please sign in to comment.