beekeeper-core 1.0.9__py3-none-any.whl → 1.0.11__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- beekeeper/core/llms/base.py +15 -10
- beekeeper/core/llms/decorators.py +1 -80
- beekeeper/core/llms/types.py +24 -0
- {beekeeper_core-1.0.9.dist-info → beekeeper_core-1.0.11.dist-info}/METADATA +2 -2
- {beekeeper_core-1.0.9.dist-info → beekeeper_core-1.0.11.dist-info}/RECORD +6 -6
- {beekeeper_core-1.0.9.dist-info → beekeeper_core-1.0.11.dist-info}/WHEEL +0 -0
beekeeper/core/llms/base.py
CHANGED
|
@@ -1,35 +1,40 @@
|
|
|
1
1
|
from abc import ABC, abstractmethod
|
|
2
|
-
from typing import Any,
|
|
2
|
+
from typing import Any, Optional
|
|
3
3
|
|
|
4
4
|
from beekeeper.core.llms.types import ChatMessage, ChatResponse, GenerateResponse
|
|
5
5
|
from beekeeper.core.monitors import BaseMonitor
|
|
6
6
|
from pydantic import BaseModel
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
class BaseLLM(
|
|
9
|
+
class BaseLLM(BaseModel, ABC):
|
|
10
10
|
"""Abstract base class defining the interface for LLMs."""
|
|
11
11
|
|
|
12
12
|
model_config = {"arbitrary_types_allowed": True}
|
|
13
|
+
model: str
|
|
13
14
|
callback_manager: Optional[BaseMonitor] = None
|
|
14
15
|
|
|
15
16
|
@classmethod
|
|
16
17
|
def class_name(cls) -> str:
|
|
17
18
|
return "BaseLLM"
|
|
18
19
|
|
|
19
|
-
def
|
|
20
|
-
"""
|
|
21
|
-
|
|
20
|
+
def text_completion(self, prompt: str, **kwargs: Any) -> str:
|
|
21
|
+
"""
|
|
22
|
+
Generates a chat completion for LLM. Using OpenAI's standard endpoint (/completions).
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
prompt (str): The input prompt to generate a completion for.
|
|
26
|
+
**kwargs (Any): Additional keyword arguments to customize the LLM completion request.
|
|
27
|
+
"""
|
|
28
|
+
response = self.completion(prompt=prompt, **kwargs)
|
|
29
|
+
|
|
30
|
+
return response.text
|
|
22
31
|
|
|
23
32
|
@abstractmethod
|
|
24
33
|
def completion(self, prompt: str, **kwargs: Any) -> GenerateResponse:
|
|
25
34
|
"""Generates a completion for LLM."""
|
|
26
35
|
|
|
27
|
-
@abstractmethod
|
|
28
|
-
def text_completion(self, prompt: str, **kwargs: Any) -> str:
|
|
29
|
-
"""Generates a text completion for LLM."""
|
|
30
|
-
|
|
31
36
|
@abstractmethod
|
|
32
37
|
def chat_completion(
|
|
33
|
-
self, messages:
|
|
38
|
+
self, messages: list[ChatMessage | dict], **kwargs: Any
|
|
34
39
|
) -> ChatResponse:
|
|
35
40
|
"""Generates a chat completion for LLM."""
|
|
@@ -6,89 +6,10 @@ from typing import Callable
|
|
|
6
6
|
|
|
7
7
|
from beekeeper.core.llms.types import ChatMessage
|
|
8
8
|
from beekeeper.core.monitors.types import PayloadRecord
|
|
9
|
-
from deprecated import deprecated
|
|
10
9
|
|
|
11
10
|
logger = getLogger(__name__)
|
|
12
11
|
|
|
13
12
|
|
|
14
|
-
@deprecated(
|
|
15
|
-
reason="'llm_chat_observer()' is deprecated and will be removed in a future version. Use 'llm_chat_monitor()'.",
|
|
16
|
-
version="1.0.8",
|
|
17
|
-
action="always",
|
|
18
|
-
)
|
|
19
|
-
def llm_chat_observer() -> Callable:
|
|
20
|
-
"""
|
|
21
|
-
Decorator to wrap a method with llm handler logic.
|
|
22
|
-
Looks for observability instances in `self.callback_manager`.
|
|
23
|
-
"""
|
|
24
|
-
|
|
25
|
-
def decorator(f: Callable) -> Callable:
|
|
26
|
-
def async_wrapper(self, *args, **kwargs):
|
|
27
|
-
callback_manager_fns = getattr(self, "callback_manager", None)
|
|
28
|
-
|
|
29
|
-
start_time = time.time()
|
|
30
|
-
llm_return_val = f(self, *args, **kwargs)
|
|
31
|
-
response_time = int((time.time() - start_time) * 1000)
|
|
32
|
-
|
|
33
|
-
if callback_manager_fns:
|
|
34
|
-
|
|
35
|
-
def async_callback_thread():
|
|
36
|
-
try:
|
|
37
|
-
# Extract input messages
|
|
38
|
-
if len(args) > 0 and isinstance(args[0], ChatMessage):
|
|
39
|
-
input_chat_messages = args[0]
|
|
40
|
-
elif "messages" in kwargs:
|
|
41
|
-
input_chat_messages = kwargs["messages"]
|
|
42
|
-
else:
|
|
43
|
-
raise ValueError(
|
|
44
|
-
"No messages provided in positional or keyword arguments"
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
# Get the user's latest message after each interaction to chat observability.
|
|
48
|
-
user_messages = [
|
|
49
|
-
msg for msg in input_chat_messages if msg.role == "user"
|
|
50
|
-
]
|
|
51
|
-
last_user_message = (
|
|
52
|
-
user_messages[-1].content if user_messages else None
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
# Get the system/instruct (first) message to chat observability.
|
|
56
|
-
system_messages = [
|
|
57
|
-
msg for msg in input_chat_messages if msg.role == "system"
|
|
58
|
-
]
|
|
59
|
-
system_message = (
|
|
60
|
-
system_messages[0].content if system_messages else None
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
callback = callback_manager_fns(
|
|
64
|
-
payload=PayloadRecord(
|
|
65
|
-
input_text=(system_message or "") + last_user_message,
|
|
66
|
-
generated_text=llm_return_val.message.content,
|
|
67
|
-
generated_token_count=llm_return_val.raw["usage"][
|
|
68
|
-
"completion_tokens"
|
|
69
|
-
],
|
|
70
|
-
input_token_count=llm_return_val.raw["usage"][
|
|
71
|
-
"prompt_tokens"
|
|
72
|
-
],
|
|
73
|
-
response_time=response_time,
|
|
74
|
-
)
|
|
75
|
-
)
|
|
76
|
-
|
|
77
|
-
if asyncio.iscoroutine(callback):
|
|
78
|
-
asyncio.run(callback)
|
|
79
|
-
|
|
80
|
-
except Exception as e:
|
|
81
|
-
logger.error(f"Observability callback error: {e}")
|
|
82
|
-
|
|
83
|
-
threading.Thread(target=async_callback_thread).start()
|
|
84
|
-
|
|
85
|
-
return llm_return_val
|
|
86
|
-
|
|
87
|
-
return async_wrapper
|
|
88
|
-
|
|
89
|
-
return decorator
|
|
90
|
-
|
|
91
|
-
|
|
92
13
|
def llm_chat_monitor() -> Callable:
|
|
93
14
|
"""
|
|
94
15
|
Decorator to wrap a method with llm handler logic.
|
|
@@ -151,7 +72,7 @@ def llm_chat_monitor() -> Callable:
|
|
|
151
72
|
asyncio.run(callback)
|
|
152
73
|
|
|
153
74
|
except Exception as e:
|
|
154
|
-
logger.error(f"Observability callback
|
|
75
|
+
logger.error(f"Observability callback: {e}")
|
|
155
76
|
|
|
156
77
|
threading.Thread(target=async_callback_thread).start()
|
|
157
78
|
|
beekeeper/core/llms/types.py
CHANGED
|
@@ -18,6 +18,30 @@ class ChatMessage(BaseModel):
|
|
|
18
18
|
role: MessageRole = Field(default=MessageRole.USER)
|
|
19
19
|
content: Optional[str] = Field(default=None)
|
|
20
20
|
|
|
21
|
+
def to_dict(self) -> dict:
|
|
22
|
+
"""Convert ChatMessage to dict."""
|
|
23
|
+
return self.model_dump(exclude_none=True)
|
|
24
|
+
|
|
25
|
+
@classmethod
|
|
26
|
+
def from_value(cls, value: dict) -> "ChatMessage":
|
|
27
|
+
if value is None:
|
|
28
|
+
raise ValueError("Invalid 'ChatMessage', cannot be None")
|
|
29
|
+
|
|
30
|
+
if isinstance(value, cls):
|
|
31
|
+
return value
|
|
32
|
+
|
|
33
|
+
if isinstance(value, dict):
|
|
34
|
+
try:
|
|
35
|
+
return cls.model_validate(value)
|
|
36
|
+
except Exception as e:
|
|
37
|
+
raise ValueError(
|
|
38
|
+
"Invalid 'ChatMessage' dict. Received: '{}'.".format(e)
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
raise TypeError(
|
|
42
|
+
f"Invalid 'ChatMessage' type. Expected dict or ChatMessage, but received {type(value).__name__}."
|
|
43
|
+
)
|
|
44
|
+
|
|
21
45
|
|
|
22
46
|
class GenerateResponse(BaseModel):
|
|
23
47
|
"""Generate response."""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: beekeeper-core
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.11
|
|
4
4
|
Summary: Load any data in one line of code and connect with AI applications
|
|
5
5
|
Project-URL: Repository, https://github.com/beekeeper-ai/beekeeper
|
|
6
6
|
Author-email: Leonardo Furnielis <leonardofurnielis@outlook.com>
|
|
@@ -9,7 +9,7 @@ Keywords: AI,LLM,QA,RAG,data,observability,retrieval,semantic-search
|
|
|
9
9
|
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
10
10
|
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
11
11
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
12
|
-
Requires-Python: <
|
|
12
|
+
Requires-Python: <3.14,>=3.11
|
|
13
13
|
Requires-Dist: deprecated<2.0.0,>=1.3.1
|
|
14
14
|
Requires-Dist: nltk<4.0.0,>=3.9.2
|
|
15
15
|
Requires-Dist: numpy<1.27.0,>=1.26.4
|
|
@@ -12,9 +12,9 @@ beekeeper/core/guardrails/__init__.py,sha256=onznwYWAyOaxOVeYZle7oDtj3QJODQvJHcf
|
|
|
12
12
|
beekeeper/core/guardrails/base.py,sha256=T-Ywr80iTL0EFYarCymFEEI3QkMsrw27JVh_0407sEU,427
|
|
13
13
|
beekeeper/core/guardrails/types.py,sha256=7sgw1S5BZY0OqO-n04pHXPU7sG-NEZJlQyIeb2Fsq9Q,359
|
|
14
14
|
beekeeper/core/llms/__init__.py,sha256=PN-5Y_Km_l2vO8v9d7iJ6_5xPCZJBh8UzwqRvQZlmTo,250
|
|
15
|
-
beekeeper/core/llms/base.py,sha256=
|
|
16
|
-
beekeeper/core/llms/decorators.py,sha256=
|
|
17
|
-
beekeeper/core/llms/types.py,sha256=
|
|
15
|
+
beekeeper/core/llms/base.py,sha256=fxjOJJR3pNJxtvkICr0Nq9A9wnqf3SMM4cm4sAhA9-o,1308
|
|
16
|
+
beekeeper/core/llms/decorators.py,sha256=5vrYwShFgdtkrKOeEjWNVW1CfkOnIJOYpEkDJx1xVLA,3276
|
|
17
|
+
beekeeper/core/llms/types.py,sha256=ttPCZ3VYtY6tx-E6VIOTicflYMJ3m0aw1znZeYAEVu8,1489
|
|
18
18
|
beekeeper/core/monitors/__init__.py,sha256=TvoiIUJtWRO_4zqCICsFaGl_v4Tpvft1M542Bi13pOI,112
|
|
19
19
|
beekeeper/core/monitors/base.py,sha256=3ooSfgVpWoRLe2TqizHMRK_bI5C-sla57aYJ47FmIXM,980
|
|
20
20
|
beekeeper/core/monitors/types.py,sha256=s-4tB8OdeaCUIRvi6FLuib2u4Yl9evqQdCundNREXQY,217
|
|
@@ -38,6 +38,6 @@ beekeeper/core/tools/base.py,sha256=A6TXn7g3DAZMREYAobfVlyOBuJn_8mIeCByc5412L9Y,
|
|
|
38
38
|
beekeeper/core/utils/pairwise.py,sha256=cpi8GItPFSYP4sjB5zgTFHi6JfBVWsMnNu8koA9VYQU,536
|
|
39
39
|
beekeeper/core/vector_stores/__init__.py,sha256=R5SRG3YpOZqRwIfBLB8KVV6FALWqhIzIhCjRGj-bwPc,93
|
|
40
40
|
beekeeper/core/vector_stores/base.py,sha256=YFW1ioZbFEcJovAh0ZCpHnj0eiXtZvqy_pj2lxPS92k,1652
|
|
41
|
-
beekeeper_core-1.0.
|
|
42
|
-
beekeeper_core-1.0.
|
|
43
|
-
beekeeper_core-1.0.
|
|
41
|
+
beekeeper_core-1.0.11.dist-info/METADATA,sha256=ZJcrMqUeLOri7mgulUQc8DXpxsxZLaSIC5DByjHwj5w,1331
|
|
42
|
+
beekeeper_core-1.0.11.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
43
|
+
beekeeper_core-1.0.11.dist-info/RECORD,,
|
|
File without changes
|