|
1 | 1 | from unittest.mock import patch |
2 | 2 |
|
3 | | -from langchain_core.language_models.chat_models import BaseChatModel |
4 | 3 | from langchain_core.messages import HumanMessage, AIMessage, SystemMessage |
5 | 4 | from langchain_core.tools import tool |
| 5 | +from langchain_gigachat import GigaChat |
| 6 | +from langchain_ollama import ChatOllama |
| 7 | +from langchain_openai import ChatOpenAI |
6 | 8 | from pydantic import BaseModel, Field |
7 | 9 | import pytest |
8 | 10 |
|
@@ -307,16 +309,53 @@ def test_structured_output_dict_out_of_the_box(custom_chat_openai_with_fc_and_so |
307 | 309 | assert result["age"] == 30 |
308 | 310 |
|
309 | 311 |
|
310 | | -@pytest.mark.parametrize( |
311 | | - "model_url", |
312 | | - [ |
313 | | - "https://api.vsegpt.ru/v1;openai/gpt-4o-mini", |
314 | | - "https://gigachat.devices.sberbank.ru/api/v1/chat/completions;GigaChat", |
315 | | - "test_model", |
316 | | - "https://example.com/v1;test/example_model" |
317 | | - ] |
318 | | -) |
319 | | -def test_connector_creator(model_url): |
320 | | - with pytest.raises(Exception): |
321 | | - connector = create_llm_connector(model_url) |
322 | | - assert issubclass(connector, BaseChatModel) |
| 312 | +def test_vsegpt_connector(monkeypatch): |
| 313 | + model_url = "https://api.vsegpt.ru/v1;meta-llama/llama-3.1-70b-instruct" |
| 314 | + test_api_key = "test_vsegpt_key" |
| 315 | + monkeypatch.setenv("VSE_GPT_KEY", test_api_key) |
| 316 | + connector = create_llm_connector(model_url) |
| 317 | + assert isinstance(connector, CustomChatOpenAI) |
| 318 | + |
| 319 | + |
| 320 | +@patch("protollm.connectors.connector_creator.get_access_token", return_value="test_gigachat_token") |
| 321 | +def test_gigachat_connector(mock_get_token): |
| 322 | + model_url = "https://gigachat.devices.sberbank.ru/api/v1;Gigachat" |
| 323 | + connector = create_llm_connector(model_url) |
| 324 | + assert isinstance(connector, GigaChat) |
| 325 | + |
| 326 | + |
| 327 | +def test_openai_connector(monkeypatch): |
| 328 | + model_url = "https://api.openai.com/v1;gpt-4o" |
| 329 | + test_api_key = "test_openai_key" |
| 330 | + monkeypatch.setenv("OPENAI_KEY", test_api_key) |
| 331 | + connector = create_llm_connector(model_url) |
| 332 | + assert isinstance(connector, ChatOpenAI) |
| 333 | + |
| 334 | + |
| 335 | +def test_ollama_connector(): |
| 336 | + model_url = "ollama;http://localhost:11434;llama3.2" |
| 337 | + connector = create_llm_connector(model_url) |
| 338 | + assert isinstance(connector, ChatOllama) |
| 339 | + |
| 340 | + |
| 341 | +def test_test_model_connector(): |
| 342 | + model_url = "test_model" |
| 343 | + connector = create_llm_connector(model_url) |
| 344 | + assert isinstance(connector, CustomChatOpenAI) |
| 345 | + |
| 346 | + |
| 347 | +def test_unsupported_provider(): |
| 348 | + model_url = "https://unknown.provider/v1;some-model" |
| 349 | + with pytest.raises(ValueError) as exc_info: |
| 350 | + create_llm_connector(model_url) |
| 351 | + assert "Unsupported provider URL" in str(exc_info.value) |
| 352 | + |
| 353 | + |
| 354 | +@pytest.mark.parametrize("invalid_url", [ |
| 355 | + "invalid_url_without_semicolon", |
| 356 | + "https://api.vsegpt.ru/v1", |
| 357 | + ";;", |
| 358 | +]) |
| 359 | +def test_invalid_url_format(invalid_url): |
| 360 | + with pytest.raises(ValueError): |
| 361 | + create_llm_connector(invalid_url) |
0 commit comments