Skip to content

Commit

Permalink
format: fixed formatting and typing
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Sep 20, 2024
1 parent baf9a25 commit 68c2552
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 12 deletions.
5 changes: 2 additions & 3 deletions gptme/eval/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import concurrent
import concurrent.futures
import inspect
import io
Expand All @@ -11,7 +10,7 @@
from collections import defaultdict
from concurrent.futures import Future, ProcessPoolExecutor, as_completed
from multiprocessing import Manager, Process
from typing import TypedDict, Union
from typing import TypedDict

from tqdm import tqdm

Expand Down Expand Up @@ -44,7 +43,7 @@ class ProcessError(TypedDict):
duration: float


ProcessResult = Union[ProcessSuccess, ProcessError]
ProcessResult = ProcessSuccess | ProcessError


class SyncedDict(TypedDict):
Expand Down
1 change: 1 addition & 0 deletions gptme/llm_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def init(config):
global anthropic
api_key = config.get_env_required("ANTHROPIC_API_KEY")
from anthropic import Anthropic # fmt: skip

anthropic = Anthropic(
api_key=api_key,
max_retries=5,
Expand Down
1 change: 1 addition & 0 deletions gptme/server/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ def main(verbose: bool, model: str | None): # pragma: no cover

# noreorder
from gptme.server.api import main as server_main # fmt: skip

server_main()
11 changes: 8 additions & 3 deletions gptme/tools/_browser_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import urllib.parse
from dataclasses import dataclass

from playwright.sync_api import ElementHandle, Page, sync_playwright

_p = None
from playwright.sync_api import (
ElementHandle,
Page,
Playwright,
sync_playwright,
)

_p: Playwright | None = None
logger = logging.getLogger(__name__)


Expand Down
1 change: 1 addition & 0 deletions gptme/tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def execute(self, ask: bool) -> Generator[Message, None, None]:
def is_runnable(self) -> bool:
# noreorder
from . import get_tool # fmt: skip

tool = get_tool(self.tool)
return bool(tool.execute) if tool else False

Expand Down
18 changes: 15 additions & 3 deletions gptme/tools/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@
import types
from collections.abc import Callable, Generator
from logging import getLogger
from typing import Literal, TypeVar, get_origin
from typing import (
TYPE_CHECKING,
Literal,
TypeVar,
get_origin,
)

from ..message import Message
from ..util import ask_execute, print_preview
from .base import ToolSpec, ToolUse

if TYPE_CHECKING:
from IPython.terminal.embed import InteractiveShellEmbed # fmt: skip


logger = getLogger(__name__)

# TODO: launch the IPython session in the current venv, if any, instead of the pipx-managed gptme-python venv (for example) in which gptme itself runs
# would let us use libraries installed with `pip install` in the current venv
# https://github.com/ErikBjare/gptme/issues/29

# IPython instance
_ipython = None
_ipython: "InteractiveShellEmbed | None" = None


registered_functions: dict[str, Callable] = {}
Expand Down Expand Up @@ -72,6 +81,7 @@ def get_functions_prompt() -> str:
def _get_ipython():
global _ipython
from IPython.terminal.embed import InteractiveShellEmbed # fmt: skip

if _ipython is None:
_ipython = InteractiveShellEmbed()
_ipython.push(registered_functions)
Expand All @@ -98,6 +108,7 @@ def execute_python(code: str, ask: bool, args=None) -> Generator[Message, None,

# Capture the standard output and error streams
from IPython.utils.capture import capture_output # fmt: skip

with capture_output() as captured:
# Execute the code
result = _ipython.run_cell(code, silent=False, store_history=False)
Expand All @@ -118,7 +129,8 @@ def execute_python(code: str, ask: bool, args=None) -> Generator[Message, None,
tb = result.error_in_exec.__traceback__
while tb.tb_next: # type: ignore
tb = tb.tb_next # type: ignore
output += f"Exception during execution on line {tb.tb_lineno}:\n {result.error_in_exec.__class__.__name__}: {result.error_in_exec}" # type: ignore
# type: ignore
output += f"Exception during execution on line {tb.tb_lineno}:\n {result.error_in_exec.__class__.__name__}: {result.error_in_exec}"
if result.result is not None:
output += f"Result:\n```\n{result.result}\n```\n\n"

Expand Down
2 changes: 1 addition & 1 deletion gptme/tools/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def restart(self):
self._init()


_shell = None
_shell: ShellSession | None = None


def get_shell() -> ShellSession:
Expand Down
3 changes: 2 additions & 1 deletion gptme/tools/subagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
# noreorder
from ..logmanager import LogManager # fmt: skip


logger = logging.getLogger(__name__)

Status = Literal["running", "success", "failure"]

_subagents = []
_subagents: list["Subagent"] = []


@dataclass
Expand Down
1 change: 1 addition & 0 deletions gptme/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def _is_sphinx_build() -> bool:
try:
# noreorder
import sphinx # fmt: skip

is_sphinx = hasattr(sphinx, "application")
except ImportError:
is_sphinx = False
Expand Down
2 changes: 1 addition & 1 deletion train/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def collect(model: str):
(name, convs)
for name, convs in zip(names, convs)
if quality_check_convo(convs, name)
]
],
)
logger.info("Got %d conversations after filtering", len(convs))

Expand Down

0 comments on commit 68c2552

Please sign in to comment.