Skip to content

Commit

Permalink
fix test warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjoyo committed Jan 2, 2024
1 parent a074ed2 commit 4df1fc4
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
1 change: 1 addition & 0 deletions bpm-ai-core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ MANIFEST.in

.venv
.env
.env.test
/releases/*
pip-wheel-metadata
/poetry.toml
8 changes: 3 additions & 5 deletions bpm-ai-core/bpm_ai_core/llm/common/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bpm_ai_core.llm.common.message import ChatMessage
from bpm_ai_core.llm.common.tool import Tool
from bpm_ai_core.prompt.prompt import Prompt
from bpm_ai_core.tracing.tracing import LangsmithTracer
from bpm_ai_core.tracing.config import tracer


class LLM(ABC):
Expand All @@ -19,8 +19,6 @@ class LLM(ABC):
max_retries: int = 0
retryable_exceptions: List[Type[BaseException]] = [Exception]

tracer = LangsmithTracer()

@retry(
wait=wait_exponential(multiplier=1.5, min=2, max=60),
stop=stop_after_attempt(max_retries),
Expand All @@ -37,9 +35,9 @@ def predict(

messages = prompt.format(llm_name=self.name())

self.tracer.start_llm_trace(self, messages, self.predict.retry.statistics['attempt_number'], tools)
tracer.start_llm_trace(self, messages, self.predict.retry.statistics['attempt_number'], tools)
completion = self._predict(messages, output_schema, tools)
self.tracer.end_llm_trace(completion)
tracer.end_llm_trace(completion)

return completion

Expand Down
10 changes: 4 additions & 6 deletions bpm-ai-core/bpm_ai_core/llm/common/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from typing import Optional, Literal, Any, Union, List

from PIL import Image
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, ConfigDict

from bpm_ai_core.llm.common.tool import Tool
from bpm_ai_core.tracing.config import tracer


class ChatMessage(BaseModel):
content: Optional[Union[str, dict, List[Union[str, Image]]]] = None
content: Optional[Union[str, dict, List[Union[str, Image.Image]]]] = None
"""
The contents of the message.
Either a string for normal completions,
Expand All @@ -29,8 +30,7 @@ class ChatMessage(BaseModel):
The name of the author of this message.
"""

class Config:
arbitrary_types_allowed = True
model_config = ConfigDict(arbitrary_types_allowed=True)


class SingleToolCallMessage(BaseModel):
Expand Down Expand Up @@ -65,8 +65,6 @@ def invoke(self):
async def ainvoke(self) -> Any:
_callable = self.tool.callable
inputs = self.payload_dict()
from bpm_ai_core.tracing.tracing import LangsmithTracer
tracer = LangsmithTracer() # todo
tracer.start_function_trace(self.tool, inputs)
if inspect.iscoroutinefunction(_callable):
result = await _callable(**inputs)
Expand Down
3 changes: 3 additions & 0 deletions bpm-ai-core/bpm_ai_core/tracing/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from bpm_ai_core.tracing.tracing import LangsmithTracer

tracer = LangsmithTracer()
14 changes: 6 additions & 8 deletions bpm-ai-core/bpm_ai_core/tracing/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
from langsmith.run_helpers import LangSmithExtra, _collect_extra, _TraceableContainer, _PROJECT_NAME, \
_PARENT_RUN_TREE, _TAGS, _METADATA

from bpm_ai_core.llm.common.message import ChatMessage, ToolCallsMessage
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


class LangsmithTracer:

Expand All @@ -27,10 +23,11 @@ def __init__(self):
def start_llm_trace(
self,
llm: Any,
messages: List[ChatMessage],
messages: List,
current_try: int,
tools: Optional[List[Tool]] = None
tools: Optional[List] = None
):
from bpm_ai_core.util.openai import messages_to_openai_dicts, json_schema_to_openai_function
inputs = {
"messages": messages_to_openai_dicts(messages),
"model": llm.model,
Expand All @@ -45,7 +42,8 @@ def start_llm_trace(
inputs=inputs
)

def end_llm_trace(self, completion: Optional[ChatMessage] = None, error_msg: Optional[str] = None):
def end_llm_trace(self, completion = None, error_msg: Optional[str] = None):
from bpm_ai_core.llm.common.message import ToolCallsMessage
choices = {
"choices": [{
"message": {
Expand All @@ -64,7 +62,7 @@ def end_llm_trace(self, completion: Optional[ChatMessage] = None, error_msg: Opt

def start_function_trace(
self,
function: Tool,
function,
inputs: dict,
):
self.start_trace(
Expand Down
2 changes: 1 addition & 1 deletion bpm-ai-core/tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
env_files = .env
env_files = .env.test

0 comments on commit 4df1fc4

Please sign in to comment.