mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-21 14:43:07 +00:00
v1
This commit is contained in:
1012
libs/partners/openrouter/langchain_openrouter/chat_models.py
Normal file
1012
libs/partners/openrouter/langchain_openrouter/chat_models.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,69 @@
|
||||
"""Integration tests for `ChatOpenRouter` chat model."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from langchain_core.messages import AIMessageChunk, BaseMessageChunk
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from langchain_openrouter.chat_models import ChatOpenRouter
|
||||
|
||||
|
||||
def test_basic_invoke() -> None:
|
||||
"""Test basic invocation."""
|
||||
model = ChatOpenRouter(model="openai/gpt-4o-mini", temperature=0)
|
||||
response = model.invoke("Say 'hello' and nothing else.")
|
||||
assert response.content
|
||||
assert response.response_metadata.get("model_provider") == "openrouter"
|
||||
|
||||
|
||||
def test_streaming() -> None:
|
||||
"""Test streaming."""
|
||||
model = ChatOpenRouter(model="openai/gpt-4o-mini", temperature=0)
|
||||
full: BaseMessageChunk | None = None
|
||||
for chunk in model.stream("Say 'hello' and nothing else."):
|
||||
full = chunk if full is None else full + chunk
|
||||
assert isinstance(full, AIMessageChunk)
|
||||
assert full.content
|
||||
|
||||
|
||||
def test_tool_calling() -> None:
|
||||
"""Test tool calling via OpenRouter."""
|
||||
|
||||
class GetWeather(BaseModel):
|
||||
"""Get the current weather in a given location."""
|
||||
|
||||
location: str = Field(description="The city and state")
|
||||
|
||||
model = ChatOpenRouter(model="openai/gpt-4o-mini", temperature=0)
|
||||
model_with_tools = model.bind_tools([GetWeather])
|
||||
response = model_with_tools.invoke("What's the weather in San Francisco?")
|
||||
assert response.tool_calls
|
||||
|
||||
|
||||
def test_structured_output() -> None:
|
||||
"""Test structured output via OpenRouter."""
|
||||
|
||||
class Joke(BaseModel):
|
||||
"""A joke."""
|
||||
|
||||
setup: str = Field(description="The setup of the joke")
|
||||
punchline: str = Field(description="The punchline of the joke")
|
||||
|
||||
model = ChatOpenRouter(model="openai/gpt-4o-mini", temperature=0)
|
||||
structured = model.with_structured_output(Joke)
|
||||
result = structured.invoke("Tell me a joke about programming")
|
||||
assert isinstance(result, Joke)
|
||||
assert result.setup
|
||||
assert result.punchline
|
||||
|
||||
|
||||
@pytest.mark.xfail(reason="Depends on reasoning model availability on OpenRouter.")
|
||||
def test_reasoning_content() -> None:
|
||||
"""Test reasoning content from a reasoning model."""
|
||||
model = ChatOpenRouter(
|
||||
model="openai/o3-mini",
|
||||
openrouter_reasoning={"effort": "low"},
|
||||
)
|
||||
response = model.invoke("What is 2 + 2?")
|
||||
assert response.content
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Standard integration tests for `ChatOpenRouter`."""
|
||||
|
||||
from langchain_tests.integration_tests import ChatModelIntegrationTests
|
||||
|
||||
from langchain_openrouter.chat_models import ChatOpenRouter
|
||||
|
||||
MODEL_NAME = "openai/gpt-4o-mini"
|
||||
|
||||
|
||||
class TestChatOpenRouter(ChatModelIntegrationTests):
|
||||
"""Test `ChatOpenRouter` chat model."""
|
||||
|
||||
@property
|
||||
def chat_model_class(self) -> type[ChatOpenRouter]:
|
||||
"""Return class of chat model being tested."""
|
||||
return ChatOpenRouter
|
||||
|
||||
@property
|
||||
def chat_model_params(self) -> dict:
|
||||
"""Parameters to create chat model instance for testing."""
|
||||
return {
|
||||
"model": MODEL_NAME,
|
||||
"temperature": 0,
|
||||
}
|
||||
|
||||
@property
|
||||
def supports_json_mode(self) -> bool:
|
||||
"""Whether the chat model supports JSON mode."""
|
||||
return True
|
||||
|
||||
@property
|
||||
def supports_image_inputs(self) -> bool:
|
||||
"""Whether the chat model supports image inputs."""
|
||||
return True
|
||||
|
||||
@property
|
||||
def supports_image_urls(self) -> bool:
|
||||
"""Whether the chat model supports image inputs from URLs."""
|
||||
return True
|
||||
1309
libs/partners/openrouter/tests/unit_tests/test_chat_models.py
Normal file
1309
libs/partners/openrouter/tests/unit_tests/test_chat_models.py
Normal file
File diff suppressed because it is too large
Load Diff
12
libs/partners/openrouter/tests/unit_tests/test_imports.py
Normal file
12
libs/partners/openrouter/tests/unit_tests/test_imports.py
Normal file
@@ -0,0 +1,12 @@
|
||||
"""Test `langchain_openrouter` public API surface."""
|
||||
|
||||
from langchain_openrouter import __all__
|
||||
|
||||
EXPECTED_ALL = [
|
||||
"ChatOpenRouter",
|
||||
]
|
||||
|
||||
|
||||
def test_all_imports() -> None:
|
||||
"""Verify that __all__ exports match the expected public API."""
|
||||
assert sorted(EXPECTED_ALL) == sorted(__all__)
|
||||
39
libs/partners/openrouter/tests/unit_tests/test_standard.py
Normal file
39
libs/partners/openrouter/tests/unit_tests/test_standard.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""Standard unit tests for `ChatOpenRouter`."""
|
||||
|
||||
from langchain_tests.unit_tests import ChatModelUnitTests
|
||||
|
||||
from langchain_openrouter.chat_models import ChatOpenRouter
|
||||
|
||||
MODEL_NAME = "openai/gpt-4o-mini"
|
||||
|
||||
|
||||
class TestChatOpenRouterUnit(ChatModelUnitTests):
|
||||
"""Standard unit tests for `ChatOpenRouter` chat model."""
|
||||
|
||||
@property
|
||||
def chat_model_class(self) -> type[ChatOpenRouter]:
|
||||
"""Chat model class being tested."""
|
||||
return ChatOpenRouter
|
||||
|
||||
@property
|
||||
def init_from_env_params(self) -> tuple[dict, dict, dict]:
|
||||
"""Parameters to initialize from environment variables."""
|
||||
return (
|
||||
{
|
||||
"OPENROUTER_API_KEY": "api_key",
|
||||
},
|
||||
{
|
||||
"model": MODEL_NAME,
|
||||
},
|
||||
{
|
||||
"openrouter_api_key": "api_key",
|
||||
},
|
||||
)
|
||||
|
||||
@property
|
||||
def chat_model_params(self) -> dict:
|
||||
"""Parameters to create chat model instance for testing."""
|
||||
return {
|
||||
"model": MODEL_NAME,
|
||||
"api_key": "test-api-key",
|
||||
}
|
||||
Reference in New Issue
Block a user