mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-19 21:33:31 -04:00
068142fce2
# Add caching to BaseChatModel Fixes #1644 (Sidenote: While testing, I noticed we have multiple implementations of Fake LLMs, used for testing. I consolidated them.) ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: Models - @hwchase17 - @agola11 Twitter: [@UmerHAdil](https://twitter.com/@UmerHAdil) | Discord: RicChilligerDude#7589 --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
34 lines
951 B
Python
34 lines
951 B
Python
"""Fake ChatModel for testing purposes."""
|
|
from typing import Any, List, Mapping, Optional
|
|
|
|
from langchain.callbacks.manager import CallbackManagerForLLMRun
|
|
from langchain.chat_models.base import SimpleChatModel
|
|
from langchain.schema import BaseMessage
|
|
|
|
|
|
class FakeListChatModel(SimpleChatModel):
|
|
"""Fake ChatModel for testing purposes."""
|
|
|
|
responses: List
|
|
i: int = 0
|
|
|
|
@property
|
|
def _llm_type(self) -> str:
|
|
return "fake-list-chat-model"
|
|
|
|
def _call(
|
|
self,
|
|
messages: List[BaseMessage],
|
|
stop: Optional[List[str]] = None,
|
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
**kwargs: Any,
|
|
) -> str:
|
|
"""First try to lookup in queries, else return 'foo' or 'bar'."""
|
|
response = self.responses[self.i]
|
|
self.i += 1
|
|
return response
|
|
|
|
@property
|
|
def _identifying_params(self) -> Mapping[str, Any]:
|
|
return {"responses": self.responses}
|