Skip to content

Commit d8272a1

Browse files
fix/chat history case (#76)
1 parent 2766ee4 commit d8272a1

File tree

5 files changed

+12
-14
lines changed

5 files changed

+12
-14
lines changed

nebuly/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
from .init import init
33

44
__all__ = ["init", "new_interaction"]
5-
__version__ = "0.3.37"
5+
__version__ = "0.3.38"

nebuly/providers/langchain.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ def _parse_langchain_data( # pylint: disable=too-many-return-statements
153153
data["messages"][-1], (HumanMessage, AIMessage, ToolMessage)
154154
):
155155
return str(data["messages"][-1].content)
156+
if "chat_history" in data:
157+
messages: list[HumanMessage | AIMessage] = data["chat_history"]
158+
human_messages = [m for m in messages if m.type == "human"]
159+
return str(human_messages[-1].content)
156160
return "\n".join([f"{key}: {value}" for key, value in data.items()])
157161
if isinstance(data, list):
158162
data = data[-1]

nebuly/requests.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def post_message(
9090
if watched.api_key:
9191
api_key = watched.api_key
9292
post_json_data(url, message, api_key)
93-
9493

9594

9695
def post_json_data(url: str, json_data: str, api_key: str) -> Any:
@@ -145,10 +144,7 @@ def post_json_data(url: str, json_data: str, api_key: str) -> Any:
145144
return None
146145

147146

148-
def _crop_spans(
149-
message: str,
150-
max_size: float = 0.5
151-
) -> str:
147+
def _crop_spans(message: str, max_size: float = 0.5) -> str:
152148
message_size = len(str(message).encode("utf-8")) / 1_000_000
153149
if message_size < max_size:
154150
return message
@@ -157,12 +153,10 @@ def _crop_spans(
157153
_remove_long_text(message_dict["body"]["spans"])
158154
return json.dumps(message_dict)
159155
except (json.JSONDecodeError, KeyError, AttributeError) as e:
160-
logger.warning(
161-
"Failed to crop spans. Error: %s",
162-
str(e)
163-
)
156+
logger.warning("Failed to crop spans. Error: %s", str(e))
164157
return message
165-
158+
159+
166160
def _remove_long_text(
167161
item: dict[str, Any] | list[Any] | Any, max_words: int = 20
168162
) -> None:
@@ -177,4 +171,4 @@ def _remove_long_text(
177171
item[k] = item[k][:50] + "..."
178172
elif isinstance(item, list):
179173
for v in item:
180-
_remove_long_text(v)
174+
_remove_long_text(v)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "nebuly"
3-
version = "0.3.37"
3+
version = "0.3.38"
44
description = "The SDK for instrumenting applications for tracking AI costs."
55
authors = ["Nebuly"]
66
readme = "README.md"

tests/test_crop_spans.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_crop_spans__is_working() -> None:
2828
_ = InteractionWatch(**message["body"])
2929
except (TypeError, ValueError, KeyError) as e:
3030
pytest.fail(f"Creating InteractionWatch raised an error: {e}")
31-
31+
3232

3333
def test_crop_spans__is_doing_nothing_if_size_is_small() -> None:
3434
with open("./tests/huge_message.json", encoding="utf-8") as f:

0 commit comments

Comments
 (0)