2023-06-20 15:25:55 +00:00
|
|
|
"""Test OpenAI Chat API wrapper."""
|
|
|
|
import json
|
2023-07-27 19:39:39 +00:00
|
|
|
from typing import Any
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
import pytest
|
2023-11-21 16:35:29 +00:00
|
|
|
from langchain_core.messages import (
|
2023-07-28 20:00:54 +00:00
|
|
|
AIMessage,
|
|
|
|
FunctionMessage,
|
|
|
|
HumanMessage,
|
|
|
|
SystemMessage,
|
|
|
|
)
|
2023-06-20 15:25:55 +00:00
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.adapters.openai import convert_dict_to_message
|
|
|
|
from langchain_community.chat_models.openai import ChatOpenAI
|
2023-11-20 21:09:30 +00:00
|
|
|
|
2023-06-20 15:25:55 +00:00
|
|
|
|
2023-08-24 01:23:21 +00:00
|
|
|
@pytest.mark.requires("openai")
|
|
|
|
def test_openai_model_param() -> None:
|
2024-02-08 03:08:26 +00:00
|
|
|
llm = ChatOpenAI(model="foo", openai_api_key="foo")
|
2023-08-24 01:23:21 +00:00
|
|
|
assert llm.model_name == "foo"
|
2024-02-08 03:08:26 +00:00
|
|
|
llm = ChatOpenAI(model_name="foo", openai_api_key="foo")
|
2023-08-24 01:23:21 +00:00
|
|
|
assert llm.model_name == "foo"
|
|
|
|
|
|
|
|
|
2023-06-20 15:25:55 +00:00
|
|
|
def test_function_message_dict_to_function_message() -> None:
|
|
|
|
content = json.dumps({"result": "Example #1"})
|
|
|
|
name = "test_function"
|
2023-08-10 23:08:50 +00:00
|
|
|
result = convert_dict_to_message(
|
2023-06-20 15:25:55 +00:00
|
|
|
{
|
|
|
|
"role": "function",
|
|
|
|
"name": name,
|
|
|
|
"content": content,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert isinstance(result, FunctionMessage)
|
|
|
|
assert result.name == name
|
|
|
|
assert result.content == content
|
2023-07-27 19:39:39 +00:00
|
|
|
|
|
|
|
|
2023-07-28 20:00:54 +00:00
|
|
|
def test__convert_dict_to_message_human() -> None:
|
|
|
|
message = {"role": "user", "content": "foo"}
|
2023-08-10 23:08:50 +00:00
|
|
|
result = convert_dict_to_message(message)
|
2023-07-28 20:00:54 +00:00
|
|
|
expected_output = HumanMessage(content="foo")
|
|
|
|
assert result == expected_output
|
|
|
|
|
|
|
|
|
|
|
|
def test__convert_dict_to_message_ai() -> None:
|
|
|
|
message = {"role": "assistant", "content": "foo"}
|
2023-08-10 23:08:50 +00:00
|
|
|
result = convert_dict_to_message(message)
|
2023-07-28 20:00:54 +00:00
|
|
|
expected_output = AIMessage(content="foo")
|
|
|
|
assert result == expected_output
|
|
|
|
|
|
|
|
|
|
|
|
def test__convert_dict_to_message_system() -> None:
|
|
|
|
message = {"role": "system", "content": "foo"}
|
2023-08-10 23:08:50 +00:00
|
|
|
result = convert_dict_to_message(message)
|
2023-07-28 20:00:54 +00:00
|
|
|
expected_output = SystemMessage(content="foo")
|
|
|
|
assert result == expected_output
|
|
|
|
|
|
|
|
|
2023-07-27 19:39:39 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_completion() -> dict:
|
|
|
|
return {
|
|
|
|
"id": "chatcmpl-7fcZavknQda3SQ",
|
|
|
|
"object": "chat.completion",
|
|
|
|
"created": 1689989000,
|
|
|
|
"model": "gpt-3.5-turbo-0613",
|
|
|
|
"choices": [
|
|
|
|
{
|
|
|
|
"index": 0,
|
|
|
|
"message": {
|
|
|
|
"role": "assistant",
|
|
|
|
"content": "Bar Baz",
|
|
|
|
},
|
|
|
|
"finish_reason": "stop",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("openai")
|
|
|
|
def test_openai_predict(mock_completion: dict) -> None:
|
2024-02-08 03:08:26 +00:00
|
|
|
llm = ChatOpenAI(openai_api_key="foo")
|
2023-07-27 19:39:39 +00:00
|
|
|
mock_client = MagicMock()
|
|
|
|
completed = False
|
|
|
|
|
|
|
|
def mock_create(*args: Any, **kwargs: Any) -> Any:
|
|
|
|
nonlocal completed
|
|
|
|
completed = True
|
|
|
|
return mock_completion
|
|
|
|
|
|
|
|
mock_client.create = mock_create
|
|
|
|
with patch.object(
|
|
|
|
llm,
|
|
|
|
"client",
|
|
|
|
mock_client,
|
|
|
|
):
|
2024-04-12 21:28:23 +00:00
|
|
|
res = llm.invoke("bar")
|
|
|
|
assert res.content == "Bar Baz"
|
2023-07-27 19:39:39 +00:00
|
|
|
assert completed
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("openai")
|
|
|
|
async def test_openai_apredict(mock_completion: dict) -> None:
|
2024-02-08 03:08:26 +00:00
|
|
|
llm = ChatOpenAI(openai_api_key="foo")
|
2023-07-27 19:39:39 +00:00
|
|
|
mock_client = MagicMock()
|
|
|
|
completed = False
|
|
|
|
|
2024-02-08 03:08:26 +00:00
|
|
|
async def mock_create(*args: Any, **kwargs: Any) -> Any:
|
2023-07-27 19:39:39 +00:00
|
|
|
nonlocal completed
|
|
|
|
completed = True
|
|
|
|
return mock_completion
|
|
|
|
|
|
|
|
mock_client.create = mock_create
|
|
|
|
with patch.object(
|
|
|
|
llm,
|
2024-02-08 03:08:26 +00:00
|
|
|
"async_client",
|
2023-07-27 19:39:39 +00:00
|
|
|
mock_client,
|
|
|
|
):
|
2024-02-08 03:08:26 +00:00
|
|
|
res = await llm.apredict("bar")
|
2023-07-27 19:39:39 +00:00
|
|
|
assert res == "Bar Baz"
|
|
|
|
assert completed
|