mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-31 18:38:48 +00:00
community[minor]: Integrating GPTRouter (#14900)
**Description:** Adding a langchain integration for [GPTRouter](https://gpt-router.writesonic.com/) 🚀 , **Tag maintainer:** @Gupta-Anubhav12 @samanyougarg @sirjan-ws-ext **Twitter handle:** [@SamanyouGarg](https://twitter.com/SamanyouGarg) Integration Tests Passing: <img width="1137" alt="Screenshot 2023-12-19 at 5 45 31 PM" src="https://github.com/Writesonic/langchain/assets/151817113/4a59df9a-ee30-47aa-9df9-b8c4eeb9dc76">
This commit is contained in:
committed by
GitHub
parent
1069a93d18
commit
44cb899a93
@@ -0,0 +1,84 @@
|
||||
"""Test GPTRouter API wrapper."""
|
||||
from typing import List
|
||||
|
||||
import pytest
|
||||
from langchain_core.callbacks import (
|
||||
CallbackManager,
|
||||
)
|
||||
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
|
||||
from langchain_core.outputs import ChatGeneration, LLMResult
|
||||
|
||||
from langchain_community.chat_models.gpt_router import ChatGPTRouter, GPTRouterModel
|
||||
from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler
|
||||
|
||||
|
||||
def test_gpt_router_call() -> None:
|
||||
"""Test valid call to GPTRouter."""
|
||||
anthropic_claude = GPTRouterModel(
|
||||
name="claude-instant-1.2", provider_name="anthropic"
|
||||
)
|
||||
chat = ChatGPTRouter(models_priority_list=[anthropic_claude])
|
||||
message = HumanMessage(content="Hello World")
|
||||
response = chat([message])
|
||||
assert isinstance(response, AIMessage)
|
||||
assert isinstance(response.content, str)
|
||||
|
||||
|
||||
def test_gpt_router_call_incorrect_model() -> None:
|
||||
"""Test invalid modelName"""
|
||||
anthropic_claude = GPTRouterModel(
|
||||
name="model_does_not_exist", provider_name="anthropic"
|
||||
)
|
||||
chat = ChatGPTRouter(models_priority_list=[anthropic_claude])
|
||||
message = HumanMessage(content="Hello World")
|
||||
with pytest.raises(Exception):
|
||||
chat([message])
|
||||
|
||||
|
||||
def test_gpt_router_generate() -> None:
|
||||
"""Test generate method of GPTRouter."""
|
||||
anthropic_claude = GPTRouterModel(
|
||||
name="claude-instant-1.2", provider_name="anthropic"
|
||||
)
|
||||
chat = ChatGPTRouter(models_priority_list=[anthropic_claude])
|
||||
chat_messages: List[List[BaseMessage]] = [
|
||||
[HumanMessage(content="If (5 + x = 18), what is x?")]
|
||||
]
|
||||
messages_copy = [messages.copy() for messages in chat_messages]
|
||||
result: LLMResult = chat.generate(chat_messages)
|
||||
assert isinstance(result, LLMResult)
|
||||
for response in result.generations[0]:
|
||||
assert isinstance(response, ChatGeneration)
|
||||
assert isinstance(response.text, str)
|
||||
assert response.text == response.message.content
|
||||
assert chat_messages == messages_copy
|
||||
|
||||
|
||||
def test_gpt_router_streaming() -> None:
|
||||
"""Test streaming tokens from GPTRouter."""
|
||||
anthropic_claude = GPTRouterModel(
|
||||
name="claude-instant-1.2", provider_name="anthropic"
|
||||
)
|
||||
chat = ChatGPTRouter(models_priority_list=[anthropic_claude], streaming=True)
|
||||
message = HumanMessage(content="Hello")
|
||||
response = chat([message])
|
||||
assert isinstance(response, AIMessage)
|
||||
assert isinstance(response.content, str)
|
||||
|
||||
|
||||
def test_gpt_router_streaming_callback() -> None:
|
||||
"""Test that streaming correctly invokes on_llm_new_token callback."""
|
||||
callback_handler = FakeCallbackHandler()
|
||||
callback_manager = CallbackManager([callback_handler])
|
||||
anthropic_claude = GPTRouterModel(
|
||||
name="claude-instant-1.2", provider_name="anthropic"
|
||||
)
|
||||
chat = ChatGPTRouter(
|
||||
models_priority_list=[anthropic_claude],
|
||||
streaming=True,
|
||||
callback_manager=callback_manager,
|
||||
verbose=True,
|
||||
)
|
||||
message = HumanMessage(content="Write me a 5 line poem.")
|
||||
chat([message])
|
||||
assert callback_handler.llm_streams > 1
|
Reference in New Issue
Block a user