2022-10-24 21:51:15 +00:00
|
|
|
"""Fake LLM wrapper for testing purposes."""
|
2023-04-14 05:07:58 +00:00
|
|
|
from typing import Any, List, Mapping, Optional, cast
|
|
|
|
|
|
|
|
from pydantic import validator
|
2022-10-24 21:51:15 +00:00
|
|
|
|
2023-04-30 18:14:09 +00:00
|
|
|
from langchain.callbacks.manager import CallbackManagerForLLMRun
|
2022-10-24 21:51:15 +00:00
|
|
|
from langchain.llms.base import LLM
|
|
|
|
|
|
|
|
|
2023-04-06 19:45:16 +00:00
|
|
|
class FakeLLM(LLM):
|
2022-10-24 21:51:15 +00:00
|
|
|
"""Fake LLM wrapper for testing purposes."""
|
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
queries: Optional[Mapping] = None
|
2023-04-14 05:07:58 +00:00
|
|
|
sequential_responses: Optional[bool] = False
|
|
|
|
response_index: int = 0
|
|
|
|
|
|
|
|
@validator("queries", always=True)
|
|
|
|
def check_queries_required(
|
|
|
|
cls, queries: Optional[Mapping], values: Mapping[str, Any]
|
|
|
|
) -> Optional[Mapping]:
|
|
|
|
if values.get("sequential_response") and not queries:
|
|
|
|
raise ValueError(
|
|
|
|
"queries is required when sequential_response is set to True"
|
|
|
|
)
|
|
|
|
return queries
|
2022-12-13 14:46:01 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _llm_type(self) -> str:
|
|
|
|
"""Return type of llm."""
|
|
|
|
return "fake"
|
2022-10-24 21:51:15 +00:00
|
|
|
|
2023-04-30 18:14:09 +00:00
|
|
|
def _call(
|
|
|
|
self,
|
|
|
|
prompt: str,
|
|
|
|
stop: Optional[List[str]] = None,
|
|
|
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
|
|
) -> str:
|
2023-04-14 05:07:58 +00:00
|
|
|
if self.sequential_responses:
|
|
|
|
return self._get_next_response_in_sequence
|
|
|
|
|
2022-12-13 14:46:01 +00:00
|
|
|
if self.queries is not None:
|
|
|
|
return self.queries[prompt]
|
2022-10-24 21:51:15 +00:00
|
|
|
if stop is None:
|
|
|
|
return "foo"
|
|
|
|
else:
|
|
|
|
return "bar"
|
2022-11-09 06:17:10 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _identifying_params(self) -> Mapping[str, Any]:
|
|
|
|
return {}
|
2023-04-14 05:07:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _get_next_response_in_sequence(self) -> str:
|
|
|
|
queries = cast(Mapping, self.queries)
|
|
|
|
response = queries[list(queries.keys())[self.response_index]]
|
|
|
|
self.response_index = self.response_index + 1
|
|
|
|
return response
|