configuration.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import os
  2. from pydantic import BaseModel, Field
  3. from typing import Any, Optional
  4. from langchain_core.runnables import RunnableConfig
  5. class Configuration(BaseModel):
  6. """The configuration for the agent."""
  7. query_generator_model: str = Field(
  8. default="gemini-2.0-flash",
  9. metadata={
  10. "description": "The name of the language model to use for the agent's query generation."
  11. },
  12. )
  13. reflection_model: str = Field(
  14. default="gemini-2.5-flash",
  15. metadata={
  16. "description": "The name of the language model to use for the agent's reflection."
  17. },
  18. )
  19. answer_model: str = Field(
  20. default="gemini-2.5-pro",
  21. metadata={
  22. "description": "The name of the language model to use for the agent's answer."
  23. },
  24. )
  25. number_of_initial_queries: int = Field(
  26. default=3,
  27. metadata={"description": "The number of initial search queries to generate."},
  28. )
  29. max_research_loops: int = Field(
  30. default=2,
  31. metadata={"description": "The maximum number of research loops to perform."},
  32. )
  33. @classmethod
  34. def from_runnable_config(
  35. cls, config: Optional[RunnableConfig] = None
  36. ) -> "Configuration":
  37. """Create a Configuration instance from a RunnableConfig."""
  38. configurable = (
  39. config["configurable"] if config and "configurable" in config else {}
  40. )
  41. # Get raw values from environment or config
  42. raw_values: dict[str, Any] = {
  43. name: os.environ.get(name.upper(), configurable.get(name))
  44. for name in cls.model_fields.keys()
  45. }
  46. # Filter out None values
  47. values = {k: v for k, v in raw_values.items() if v is not None}
  48. return cls(**values)