forked from praneetdhoolia/retrieval-agent-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiguration.py
178 lines (146 loc) · 5.78 KB
/
configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""Define the configurable parameters for the agent."""
from __future__ import annotations
from dataclasses import dataclass, field, fields
from typing import Annotated, Any, Literal, Optional, Type, TypeVar
from langchain_core.runnables import RunnableConfig, ensure_config
from retrieval_graph import prompts
@dataclass(kw_only=True)
class CommonConfiguration:
"""Configuration class for indexing and retrieval operations.
This class defines the parameters needed for both configuring the index and
retrieval processes, including tenant identification, embedding model selection,
retriever provider choice, and search parameters.
"""
user_id: str = field(metadata={"description": "Unique identifier for the user."})
embedding_model: Annotated[
str,
{"__template_metadata__": {"kind": "embeddings"}},
] = field(
default="openai/text-embedding-3-large",
metadata={
"description": "Name of the embedding model to use. Must be a valid embedding model name."
},
)
retriever_provider: Annotated[
Literal["elastic", "elastic-local", "pinecone", "mongodb", "milvus"],
{"__template_metadata__": {"kind": "retriever"}},
] = field(
default="milvus",
metadata={
"description": "The vector store provider to use for retrieval. Options are 'elastic', 'pinecone', 'mongodb', or, 'milvus'."
},
)
search_kwargs: dict[str, Any] = field(
default_factory=lambda: {"k": 10},
metadata={
"description": "Additional keyword arguments to pass to the search function of the retriever."
},
)
@classmethod
def from_runnable_config(
cls: Type[T], config: Optional[RunnableConfig] = None
) -> T:
"""Create a CommonConfiguration instance from a RunnableConfig object.
Args:
cls (Type[T]): The class itself.
config (Optional[RunnableConfig]): The configuration object to use.
Returns:
T: An instance of CommonConfiguration with the specified configuration.
"""
config = ensure_config(config)
configurable = config.get("configurable") or {}
_fields = {f.name for f in fields(cls) if f.init}
return cls(**{k: v for k, v in configurable.items() if k in _fields})
T = TypeVar("T", bound=CommonConfiguration)
@dataclass(kw_only=True)
class IndexConfiguration(CommonConfiguration):
"""Crawler configuration class for indexing operations."""
starter_urls: str = field(
default="",
metadata={
"description": "Comma-separated string of starter URLs to crawl for indexing web pages."
},
)
hops: int = field(
default=2,
metadata={
"description": "Maximum number of hops to traverse pages linked to the starter URLs."
},
)
batch_size: int = field(
default=400,
metadata={
"description": "Number of documents to index in a single batch."
},
)
apify_dataset_id: str = field(
default="",
metadata={
"description": "The Apify dataset ID to use if already crawled and stored on Apify."
},
)
def parse_starter_urls(self) -> list[str]:
"""Parse the starter URLs into a list.
Returns:
list[str]: A list of URLs parsed from the comma-separated string.
"""
return [url.strip() for url in self.starter_urls.split(",") if url.strip()]
@dataclass(kw_only=True)
class Configuration(CommonConfiguration):
"""The configuration for the agent."""
# optional: index location
alternate_milvus_uri: Optional[str] = field(
default="",
metadata={
"description": "If you want to use one of the already available indexes, provide the file location here."
},
)
response_system_prompt: str = field(
default=prompts.RESPONSE_SYSTEM_PROMPT,
metadata={"description": "The system prompt used for generating responses."},
)
response_model: Annotated[str, {"__template_metadata__": {"kind": "llm"}}] = field(
default="openai/gpt-4o",
metadata={
"description": "The language model used for generating responses. Should be in the form: provider/model-name."
},
)
query_system_prompt: str = field(
default=prompts.QUERY_SYSTEM_PROMPT,
metadata={
"description": "The system prompt used for processing and refining queries."
},
)
query_model: Annotated[str, {"__template_metadata__": {"kind": "llm"}}] = field(
default="openai/gpt-4o",
metadata={
"description": "The language model used for processing and refining queries. Should be in the form: provider/model-name."
},
)
embedding_model: Annotated[
str,
{"__template_metadata__": {"kind": "embeddings"}},
] = field(
default="openai/text-embedding-3-large",
metadata={
"description": "Name of the embedding model to use. Must be a valid embedding model name."
},
)
retriever_provider: Annotated[
Literal["elastic", "elastic-local", "pinecone", "mongodb", "milvus"],
{"__template_metadata__": {"kind": "retriever"}},
] = field(
default="milvus",
metadata={
"description": "The vector store provider to use for retrieval. Options are 'elastic', 'pinecone', 'mongodb', or, 'milvus'."
},
)
search_kwargs: dict[str, Any] = field(
default_factory=dict,
metadata={
"description": "Additional keyword arguments to pass to the search function of the retriever."
},
)
def __post_init__(self):
# Always ensure "k"=10 if not already set
self.search_kwargs.setdefault("k", 10)