Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing logging system message in anthropic #208

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions log10/_httpx_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ async def _try_post_request_async(url: str, payload: dict = {}) -> httpx.Respons


def format_anthropic_request(request_content) -> str:
system_message = request_content.get("system", "")
if system_message:
kxtran marked this conversation as resolved.
Show resolved Hide resolved
request_content["messages"].insert(0, {"role": "system", "content": system_message})

for message in request_content.get("messages", []):
new_content = []
message_content = message.get("content")
Expand Down
10 changes: 7 additions & 3 deletions tests/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ def test_completions_create(session, anthropic_legacy_model):
@pytest.mark.chat
def test_messages_create(session, anthropic_model):
client = Anthropic()

system_message = "Respond only in Yoda-speak."
message = client.messages.create(
model=anthropic_model,
max_tokens=1000,
temperature=0.0,
system="Respond only in Yoda-speak.",
system=system_message,
messages=[{"role": "user", "content": "How are you today?"}],
)

text = message.content[0].text
assert isinstance(text, str)
_LogAssertion(completion_id=session.last_completion_id(), message_content=text).assert_chat_response()
log_assertion = _LogAssertion(
completion_id=session.last_completion_id(), message_content=text, system_message=system_message
)
log_assertion.assert_chat_response()
log_assertion.assert_system_message_request()


@pytest.mark.chat
Expand Down
18 changes: 18 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, *args, **kwargs):
self._message_content = kwargs.get("message_content", "")
self._text = kwargs.get("text", "")
self._function_args = kwargs.get("function_args", [])
self._system_message = kwargs.get("system_message", "")

assert self._completion_id, "No completion id provided."
assert is_valid_uuid(self._completion_id), "Completion ID should be found and valid uuid."
Expand Down Expand Up @@ -52,6 +53,23 @@ def assert_text_response(self):
self._text == text
), f"Text does not match the generated completion for completion {self._completion_id}."

def assert_system_message_request(self):
if not self._system_message:
return

self.get_completion()
kxtran marked this conversation as resolved.
Show resolved Hide resolved
assert self.data.get("request", {}), f"No request logged for completion {self._completion_id}."
request = self.data["request"]
assert request.get("messages", ""), f"No request message logged for completion {self._completion_id}."
system_message = request["messages"][0]
assert system_message.get(
"content", ""
), f"No system message content logged for completion {self._completion_id}."
content = system_message["content"]
assert (
self._system_message == content
), f"System message content does not match the generated completion for completion {self._completion_id}."

def assert_chat_response(self):
assert self._message_content, "No output generated from the model."
self.get_completion()
Expand Down
Loading