From 69a1e17a738ccc8f6ddae8e944969401d553794d Mon Sep 17 00:00:00 2001 From: Benjoyo Date: Tue, 19 Mar 2024 16:15:27 +0100 Subject: [PATCH] optimize imports --- .../tools/prompt_constructors.py | 3 - .../llm/anthropic_chat/tools/tool_user.py | 8 +- bpm-ai-core/bpm_ai_core/llm/openai_chat.py | 127 ------------------ bpm-ai-core/bpm_ai_core/prompt/filters.py | 1 + .../speech_recognition/faster_whisper.py | 1 - .../speech_recognition/openai_whisper.py | 3 +- bpm-ai-core/bpm_ai_core/tracing/tracing.py | 1 - .../bpm_ai_core/translation/easy_nmt/util.py | 4 +- 8 files changed, 9 insertions(+), 139 deletions(-) delete mode 100644 bpm-ai-core/bpm_ai_core/llm/openai_chat.py diff --git a/bpm-ai-core/bpm_ai_core/llm/anthropic_chat/tools/prompt_constructors.py b/bpm-ai-core/bpm_ai_core/llm/anthropic_chat/tools/prompt_constructors.py index d7b5d47..3b991cd 100644 --- a/bpm-ai-core/bpm_ai_core/llm/anthropic_chat/tools/prompt_constructors.py +++ b/bpm-ai-core/bpm_ai_core/llm/anthropic_chat/tools/prompt_constructors.py @@ -1,6 +1,3 @@ -import typing - - # This file contains prompt constructors for various pieces of code. Used primarily to keep other code legible. def construct_tool_use_system_prompt(tools): tool_use_system_prompt = ( diff --git a/bpm-ai-core/bpm_ai_core/llm/anthropic_chat/tools/tool_user.py b/bpm-ai-core/bpm_ai_core/llm/anthropic_chat/tools/tool_user.py index de28f11..f9cb975 100644 --- a/bpm-ai-core/bpm_ai_core/llm/anthropic_chat/tools/tool_user.py +++ b/bpm-ai-core/bpm_ai_core/llm/anthropic_chat/tools/tool_user.py @@ -1,11 +1,11 @@ -import re -import builtins import ast +import builtins +import re -from .prompt_constructors import construct_use_tools_prompt, construct_successful_function_run_injection_prompt, \ - construct_error_function_run_injection_prompt, construct_prompt_from_messages from .messages_api_converters import convert_completion_to_messages, \ convert_messages_completion_object_to_completions_completion_object +from .prompt_constructors import construct_use_tools_prompt, construct_successful_function_run_injection_prompt, \ + construct_error_function_run_injection_prompt, construct_prompt_from_messages from .. import get_anthropic_client from .._constants import DEFAULT_MODEL diff --git a/bpm-ai-core/bpm_ai_core/llm/openai_chat.py b/bpm-ai-core/bpm_ai_core/llm/openai_chat.py deleted file mode 100644 index 0cdcabb..0000000 --- a/bpm-ai-core/bpm_ai_core/llm/openai_chat.py +++ /dev/null @@ -1,127 +0,0 @@ -import json -from typing import Dict, Any, Optional, List -import logging - -from bpm_ai_core.llm.common.llm import LLM -from bpm_ai_core.llm.common.message import ChatMessage, ToolCallsMessage, SingleToolCallMessage -from bpm_ai_core.llm.common.tool import Tool -from bpm_ai_core.util.openai import messages_to_openai_dicts, json_schema_to_openai_function - -logger = logging.getLogger(__name__) - -try: - from openai import AsyncOpenAI, APIConnectionError, InternalServerError, RateLimitError, OpenAIError - from openai.types.chat import ChatCompletionMessage, ChatCompletion - import httpx - - has_openai = True - try: - client = AsyncOpenAI( - http_client=httpx.AsyncClient( - limits=httpx.Limits(max_connections=1000, max_keepalive_connections=100) - ), - max_retries=0 # we use own retry logic - ) - except OpenAIError as e: - logger.error(e) -except ImportError: - has_openai = False - - -class ChatOpenAI(LLM): - """ - `OpenAI` Chat large language models API. - - To use, you should have the ``openai`` python package installed, and the - environment variable ``OPENAI_API_KEY`` set with your API key. - """ - - def __init__( - self, - model: str = "gpt-3.5-turbo-0125", - temperature: float = 0.0, - seed: Optional[int] = None, - max_retries: int = 8 - ): - if not has_openai: - raise ImportError('openai is not installed') - super().__init__( - model=model, - temperature=temperature, - max_retries=max_retries, - retryable_exceptions=[ - RateLimitError, InternalServerError, APIConnectionError - ] - ) - self.seed = seed - - async def _predict( - self, - messages: List[ChatMessage], - output_schema: Optional[Dict[str, Any]] = None, - tools: Optional[List[Tool]] = None - ) -> ChatMessage: - openai_tools = [] - if output_schema: - tools = [Tool.from_callable(name="store_result", description="Stores your result", args_schema=output_schema)] - if tools: - openai_tools = [json_schema_to_openai_function(f.name, f.description, f.args_schema) for f in tools] - completion = await self._run_completion(messages, openai_tools) - - message = completion.choices[0].message - if message.tool_calls: - if output_schema: - return ChatMessage(role=message.role, content=self._load_tool_call_json(message)) - else: - return self._openai_tool_calls_to_tool_message(message, tools) - else: - return ChatMessage(role=message.role, content=message.content) - - async def _run_completion(self, messages: List[ChatMessage], functions: List[dict]) -> ChatCompletion: - args = { - "model": self.model, - "temperature": self.temperature, - **({"seed": self.seed} if self.seed else {}), - "messages": messages_to_openai_dicts(messages), - **({ - "tool_choice": { - "type": "function", - "function": {"name": functions[0]["function"]["name"]} - } if (len(functions) == 1) else ("auto" if functions else "none"), - "tools": functions - } if functions else {}) - } - return await client.chat.completions.create(**args) - - @staticmethod - def _openai_tool_calls_to_tool_message(message: ChatCompletionMessage, tools: List[Tool]) -> ToolCallsMessage: - return ToolCallsMessage( - name=", ".join([t.function.name for t in message.tool_calls]), - content=message.content, - tool_calls=[ - SingleToolCallMessage( - id=t.id, - name=t.function.name, - payload=t.function.arguments, - tool=next((item for item in tools if item.name == t.function.name), None) - ) - for t in message.tool_calls - ] - ) - - @staticmethod - def _load_tool_call_json(message: ChatCompletionMessage): - try: - json_object = json.loads(message.tool_calls[0].function.arguments) - except ValueError as e: - json_object = None - return json_object - - def supports_images(self) -> bool: - return "vision" in self.model - - def supports_audio(self) -> bool: - return False - - def name(self) -> str: - return "openai" diff --git a/bpm-ai-core/bpm_ai_core/prompt/filters.py b/bpm-ai-core/bpm_ai_core/prompt/filters.py index 5809c44..772c653 100644 --- a/bpm-ai-core/bpm_ai_core/prompt/filters.py +++ b/bpm-ai-core/bpm_ai_core/prompt/filters.py @@ -1,4 +1,5 @@ import json + from bpm_ai_core.util.markdown import dict_to_md from bpm_ai_core.util.xml import dict_to_xml as _dict_to_xml diff --git a/bpm-ai-core/bpm_ai_core/speech_recognition/faster_whisper.py b/bpm-ai-core/bpm_ai_core/speech_recognition/faster_whisper.py index a11d39c..39ec9d8 100644 --- a/bpm-ai-core/bpm_ai_core/speech_recognition/faster_whisper.py +++ b/bpm-ai-core/bpm_ai_core/speech_recognition/faster_whisper.py @@ -1,7 +1,6 @@ import io from bpm_ai_core.speech_recognition.asr import ASRModel -from bpm_ai_core.util.audio import load_audio try: from faster_whisper import WhisperModel diff --git a/bpm-ai-core/bpm_ai_core/speech_recognition/openai_whisper.py b/bpm-ai-core/bpm_ai_core/speech_recognition/openai_whisper.py index 7d47887..4617182 100644 --- a/bpm-ai-core/bpm_ai_core/speech_recognition/openai_whisper.py +++ b/bpm-ai-core/bpm_ai_core/speech_recognition/openai_whisper.py @@ -1,9 +1,8 @@ import io import logging -from typing import Optional, Union +from typing import Optional from bpm_ai_core.speech_recognition.asr import ASRModel -from bpm_ai_core.util.audio import load_audio logger = logging.getLogger(__name__) diff --git a/bpm-ai-core/bpm_ai_core/tracing/tracing.py b/bpm-ai-core/bpm_ai_core/tracing/tracing.py index 8d1ba2d..e614670 100644 --- a/bpm-ai-core/bpm_ai_core/tracing/tracing.py +++ b/bpm-ai-core/bpm_ai_core/tracing/tracing.py @@ -2,7 +2,6 @@ from contextvars import ContextVar from bpm_ai_core.tracing.delegate import DelegateTracer - from bpm_ai_core.tracing.tracer import Tracer _tracers: ContextVar[list[Tracer] | None] = ContextVar('tracers', default=None) diff --git a/bpm-ai-core/bpm_ai_core/translation/easy_nmt/util.py b/bpm-ai-core/bpm_ai_core/translation/easy_nmt/util.py index d697b0b..5b74400 100644 --- a/bpm-ai-core/bpm_ai_core/translation/easy_nmt/util.py +++ b/bpm-ai-core/bpm_ai_core/translation/easy_nmt/util.py @@ -1,7 +1,9 @@ import os -import requests import sys +import requests + + def http_get(url, path): """ Downloads a URL to a given path on disc