first successful run of photo llm #11
@@ -1,9 +1,8 @@
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from openai import OpenAI
|
|
||||||
import openai
|
import openai
|
||||||
from openai.types.chat import ChatCompletion
|
from openai import OpenAI
|
||||||
|
|
||||||
from photools3.models import (
|
from photools3.models import (
|
||||||
LlmMessageContent,
|
LlmMessageContent,
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from photools3.llm_session import LlmSession
|
from photools3.llm_session import LlmSession
|
||||||
from photools3.photo import Photo
|
from photools3.photo import Photo
|
||||||
from test.test_photo import PATHS_DNG_EXAMPLES
|
from test.test_photo import PATHS_DNG_EXAMPLES
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def test_photo():
|
def test_photo():
|
||||||
return Photo(PATHS_DNG_EXAMPLES[0])
|
return Photo(PATHS_DNG_EXAMPLES[0])
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def llm_session():
|
def llm_session():
|
||||||
return LlmSession(
|
return LlmSession(
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from photools3.llm_session import LlmSession
|
|
||||||
from photools3.photo import Photo
|
|
||||||
|
|
||||||
@pytest.mark.skipif(not LlmSession(system_prompt="").is_api_reachable(), reason="API not available")
|
from photools3.llm_session import LlmSession
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(
|
||||||
|
not LlmSession(system_prompt="").is_api_reachable(), reason="API not available"
|
||||||
|
)
|
||||||
def test_llm_session(llm_session, test_photo):
|
def test_llm_session(llm_session, test_photo):
|
||||||
result = llm_session.call(
|
result = llm_session.call(
|
||||||
prompt="keywords for the attached picture", photos=[test_photo]
|
prompt="keywords for the attached picture", photos=[test_photo]
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import pytest
|
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
from photools3.llm_session import LlmSession
|
from photools3.llm_session import LlmSession
|
||||||
from photools3.photo import Photo
|
|
||||||
from test.test_photo import PATHS_DNG_EXAMPLES
|
|
||||||
|
|
||||||
def test_llm_session_mocked(test_photo):
|
def test_llm_session_mocked(test_photo):
|
||||||
# We want to mock OpenAI client's chat.completions.create
|
# We want to mock OpenAI client's chat.completions.create
|
||||||
@@ -10,22 +9,24 @@ def test_llm_session_mocked(test_photo):
|
|||||||
# Setup the mock response structure
|
# Setup the mock response structure
|
||||||
mock_client = MagicMock()
|
mock_client = MagicMock()
|
||||||
mocked_openai.return_value = mock_client
|
mocked_openai.return_value = mock_client
|
||||||
|
|
||||||
# Mock the models list to return a proper object with an ID string
|
# Mock the models list to return a proper object with an ID string
|
||||||
# This prevents Pydantic ValidationError when LlmSession tries to use model_id
|
# This prevents Pydantic ValidationError when LlmSession tries to use model_id
|
||||||
mock_model = MagicMock()
|
mock_model = MagicMock()
|
||||||
mock_model.id = "gpt-4o"
|
mock_model.id = "gpt-4o"
|
||||||
mock_client.models.list.return_value.data = [mock_model]
|
mock_client.models.list.return_value.data = [mock_model]
|
||||||
|
|
||||||
# Create a response object: response.choices[0].message.content
|
# Create a response object: response.choices[0].message.content
|
||||||
mock_response = MagicMock()
|
mock_response = MagicMock()
|
||||||
mock_choice = MagicMock()
|
mock_choice = MagicMock()
|
||||||
mock_message = MagicMock()
|
mock_message = MagicMock()
|
||||||
|
|
||||||
mock_message.content = '{"caption": "A beautiful sunset", "tags": ["sunset", "nature"]}'
|
mock_message.content = (
|
||||||
|
'{"caption": "A beautiful sunset", "tags": ["sunset", "nature"]}'
|
||||||
|
)
|
||||||
mock_choice.message = mock_message
|
mock_choice.message = mock_message
|
||||||
mock_response.choices = [mock_choice]
|
mock_response.choices = [mock_choice]
|
||||||
|
|
||||||
# Configure the client to return our mock response
|
# Configure the client to return our mock response
|
||||||
mock_client.chat.completions.create.return_value = mock_response
|
mock_client.chat.completions.create.return_value = mock_response
|
||||||
|
|
||||||
@@ -41,6 +42,6 @@ def test_llm_session_mocked(test_photo):
|
|||||||
assert result["caption"] == "A beautiful sunset"
|
assert result["caption"] == "A beautiful sunset"
|
||||||
assert "sunset" in result["tags"]
|
assert "sunset" in result["tags"]
|
||||||
assert "nature" in result["tags"]
|
assert "nature" in result["tags"]
|
||||||
|
|
||||||
# Verify that the mock was called
|
# Verify that the mock was called
|
||||||
assert mock_client.chat.completions.create.called
|
assert mock_client.chat.completions.create.called
|
||||||
|
|||||||
Reference in New Issue
Block a user