Home / Class/ SimpleFakeChat Class — langchain Architecture

SimpleFakeChat Class — langchain Architecture

Architecture documentation for the SimpleFakeChat class in test_cache.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  869ed915_e1a9_5e24_b91e_9a20f6bfff96["SimpleFakeChat"]
  e24b5be2_a888_4700_a118_b05d34a03395["Generation"]
  869ed915_e1a9_5e24_b91e_9a20f6bfff96 -->|extends| e24b5be2_a888_4700_a118_b05d34a03395
  fb3554e0_291b_93d2_d325_51461432ed8a["ChatGeneration"]
  869ed915_e1a9_5e24_b91e_9a20f6bfff96 -->|extends| fb3554e0_291b_93d2_d325_51461432ed8a
  51f634bf_713d_3f19_d694_5c6ef3e59c57["test_cache.py"]
  869ed915_e1a9_5e24_b91e_9a20f6bfff96 -->|defined in| 51f634bf_713d_3f19_d694_5c6ef3e59c57
  eef51bae_1b1b_fcc0_9f29_b0a21ba0c12d["__init__()"]
  869ed915_e1a9_5e24_b91e_9a20f6bfff96 -->|method| eef51bae_1b1b_fcc0_9f29_b0a21ba0c12d
  873a0144_ec7d_5873_8bb8_65ef4d23a42b["_get_llm_string()"]
  869ed915_e1a9_5e24_b91e_9a20f6bfff96 -->|method| 873a0144_ec7d_5873_8bb8_65ef4d23a42b
  2ef2fddf_1cd3_0353_dd94_9d062db35943["generate_response()"]
  869ed915_e1a9_5e24_b91e_9a20f6bfff96 -->|method| 2ef2fddf_1cd3_0353_dd94_9d062db35943

Relationship Graph

Source Code

libs/core/tests/unit_tests/language_models/chat_models/test_cache.py lines 322–364

    class SimpleFakeChat:
        """Simple fake chat model for testing."""

        def __init__(self, cache: BaseCache) -> None:
            self.cache = cache
            self.response = "hello"

        def _get_llm_string(self) -> str:
            return "test_llm_string"

        def generate_response(self, prompt: str) -> ChatResult:
            """Simulate the cache lookup and generation logic."""
            llm_string = self._get_llm_string()
            prompt_str = dumps([prompt])

            # Check cache first
            cache_val = self.cache.lookup(prompt_str, llm_string)
            if cache_val:
                # This is where our fix should work
                converted_generations = []
                for gen in cache_val:
                    if isinstance(gen, Generation) and not isinstance(
                        gen, ChatGeneration
                    ):
                        # Convert Generation to ChatGeneration by creating an AIMessage
                        chat_gen = ChatGeneration(
                            message=AIMessage(content=gen.text),
                            generation_info=gen.generation_info,
                        )
                        converted_generations.append(chat_gen)
                    else:
                        converted_generations.append(gen)
                return ChatResult(generations=converted_generations)

            # Generate new response
            chat_gen = ChatGeneration(
                message=AIMessage(content=self.response), generation_info={}
            )
            result = ChatResult(generations=[chat_gen])

            # Store in cache
            self.cache.update(prompt_str, llm_string, result.generations)
            return result

Frequently Asked Questions

What is the SimpleFakeChat class?
SimpleFakeChat is a class in the langchain codebase, defined in libs/core/tests/unit_tests/language_models/chat_models/test_cache.py.
Where is SimpleFakeChat defined?
SimpleFakeChat is defined in libs/core/tests/unit_tests/language_models/chat_models/test_cache.py at line 322.
What does SimpleFakeChat extend?
SimpleFakeChat extends Generation, ChatGeneration.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free