Skip to content

Commit

Permalink
fix: fixed lazy import of openai/anthropic to speed up start times
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Dec 3, 2024
1 parent 4ed70f1 commit 11b74f1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,6 @@ cloc-total:
cloc ${SRCFILES} --by-file

bench-importtime:
time poetry run python -X importtime -m gptme --model openrouter --non-interactive 2>&1 | grep "import time" | cut -d'|' -f 2- | sort -n
time poetry run python -X importtime -m gptme --model openai --non-interactive 2>&1 | grep "import time" | cut -d'|' -f 2- | sort -n
@#time poetry run python -X importtime -m gptme --model openrouter --non-interactive 2>&1 | grep "import time" | cut -d'|' -f 2- | sort -n
@#time poetry run python -X importtime -m gptme --model anthropic --non-interactive 2>&1 | grep "import time" | cut -d'|' -f 2- | sort -n
15 changes: 11 additions & 4 deletions gptme/llm/llm_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
cast,
)

import anthropic.types
from anthropic import NOT_GIVEN
from anthropic.types.beta.prompt_caching import PromptCachingBetaToolParam
from typing_extensions import Required

from ..constants import TEMPERATURE, TOP_P
Expand All @@ -23,6 +20,9 @@


if TYPE_CHECKING:
# noreorder
import anthropic # fmt: skip
import anthropic.types.beta.prompt_caching # fmt: skip
from anthropic import Anthropic # fmt: skip

_anthropic: "Anthropic | None" = None
Expand Down Expand Up @@ -59,6 +59,8 @@ class MessagePart(TypedDict, total=False):


def chat(messages: list[Message], model: str, tools: list[ToolSpec] | None) -> str:
from anthropic import NOT_GIVEN # fmt: skip

assert _anthropic, "LLM not initialized"
messages, system_messages = _transform_system_messages(messages)

Expand All @@ -84,6 +86,9 @@ def chat(messages: list[Message], model: str, tools: list[ToolSpec] | None) -> s
def stream(
messages: list[Message], model: str, tools: list[ToolSpec] | None
) -> Generator[str, None, None]:
import anthropic.types # fmt: skip
from anthropic import NOT_GIVEN # fmt: skip

assert _anthropic, "LLM not initialized"
messages, system_messages = _transform_system_messages(messages)

Expand Down Expand Up @@ -260,7 +265,9 @@ def parameters2dict(parameters: list[Parameter]) -> dict[str, object]:
}


def _spec2tool(spec: ToolSpec) -> "PromptCachingBetaToolParam":
def _spec2tool(
spec: ToolSpec,
) -> "anthropic.types.beta.prompt_caching.PromptCachingBetaToolParam":
name = spec.name
if spec.block_types:
name = spec.block_types[0]
Expand Down
22 changes: 13 additions & 9 deletions gptme/llm/llm_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
from collections.abc import Generator
from pathlib import Path
from typing import TYPE_CHECKING, Any, cast
from openai.types.chat import ChatCompletionChunk, ChatCompletionToolParam
from openai.types.chat.chat_completion_chunk import (
ChoiceDeltaToolCall,
ChoiceDeltaToolCallFunction,
)

from ..tools.base import ToolSpec, Parameter

from ..config import Config
from ..constants import TEMPERATURE, TOP_P
from ..message import Message, msgs2dicts
from ..tools.base import Parameter, ToolSpec
from .models import Provider, get_model

if TYPE_CHECKING:
from openai import OpenAI
# noreorder
from openai import OpenAI # fmt: skip
from openai.types.chat import ( # fmt: skip
ChatCompletionToolParam,
)
from openai.types.chat.chat_completion_chunk import ( # fmt: skip
ChoiceDeltaToolCall,
ChoiceDeltaToolCallFunction,
)

openai: "OpenAI | None" = None
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -174,6 +176,8 @@ def stream(
),
):
# Cast the chunk to the correct type
from openai.types.chat import ChatCompletionChunk # fmt: skip

chunk = cast(ChatCompletionChunk, chunk_raw)

if not chunk.choices:
Expand Down Expand Up @@ -293,7 +297,7 @@ def parameters2dict(parameters: list[Parameter]) -> dict[str, object]:
}


def _spec2tool(spec: ToolSpec) -> ChatCompletionToolParam:
def _spec2tool(spec: ToolSpec) -> "ChatCompletionToolParam":
name = spec.name
if spec.block_types:
name = spec.block_types[0]
Expand Down

0 comments on commit 11b74f1

Please sign in to comment.