Skip to content

Commit

Permalink
Merge pull request #223 from Mirascope/fix-extractor-temp-call
Browse files Browse the repository at this point in the history
fix: issue with not properly setting messages from extractor in temp call
  • Loading branch information
willbakst committed May 9, 2024
2 parents b01356b + 42bfdb6 commit 81bfc47
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 3 additions & 1 deletion mirascope/base/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,9 @@ class TempCall(call_type): # type: ignore

properties = getmembers(self)
for name, value in properties:
if not hasattr(TempCall, name):
if not hasattr(TempCall, name) or (
name == "messages" and "messages" in self.__class__.__dict__
):
setattr(TempCall, name, value)

return TempCall
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mirascope"
version = "0.12.0"
version = "0.12.1"
description = "LLM toolkit for lightning-fast, high-quality development"
license = "MIT"
authors = [
Expand Down
32 changes: 32 additions & 0 deletions tests/openai/test_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,38 @@ def test_prop(self) -> str:
)


@patch("openai.resources.chat.completions.Completions.create", new_callable=MagicMock)
def test_openai_extractor_extract_with_custom_messages(
mock_call: MagicMock,
fixture_chat_completion_with_tools: ChatCompletion,
fixture_my_openai_tool: Type[OpenAITool],
fixture_my_openai_tool_schema: Type[BaseModel],
) -> None:
"""Tests that writing custom messages with an OpenAI extractor works."""
mock_call.return_value = fixture_chat_completion_with_tools

messages = [{"role": "user", "content": "ensure this is the message"}]

class TempExtractor(OpenAIExtractor[BaseModel]):
prompt_template = "{test_prop}"
api_key = "test"

extract_schema: Type[BaseModel] = fixture_my_openai_tool_schema

call_params = OpenAICallParams(model="gpt-4")

def messages(self):
return messages

TempExtractor().extract()
mock_call.assert_called_once_with(
model="gpt-4",
stream=False,
messages=messages,
tools=[fixture_my_openai_tool.tool_schema()],
)


@patch("mirascope.openai.calls.OpenAICall.call_async", new_callable=AsyncMock)
@pytest.mark.asyncio
async def test_openai_extractor_extract_async(
Expand Down

0 comments on commit 81bfc47

Please sign in to comment.