forked from zilliztech/deep-searcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
210 lines (179 loc) · 6.27 KB
/
main.py
File metadata and controls
210 lines (179 loc) · 6.27 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import argparse
from typing import Dict, List, Union
import uvicorn
from fastapi import Body, FastAPI, HTTPException, Query
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from deepsearcher.configuration import Configuration, init_config
from deepsearcher.offline_loading import load_from_local_files, load_from_website
from deepsearcher.online_query import query
app = FastAPI()
config = Configuration()
init_config(config)
class ProviderConfigRequest(BaseModel):
"""
Request model for setting provider configuration.
Attributes:
feature (str): The feature to configure (e.g., 'embedding', 'llm').
provider (str): The provider name (e.g., 'openai', 'azure').
config (Dict): Configuration parameters for the provider.
"""
feature: str
provider: str
config: Dict
@app.post("/set-provider-config/")
def set_provider_config(request: ProviderConfigRequest):
"""
Set configuration for a specific provider.
Args:
request (ProviderConfigRequest): The request containing provider configuration.
Returns:
dict: A dictionary containing a success message and the updated configuration.
Raises:
HTTPException: If setting the provider config fails.
"""
try:
config.set_provider_config(request.feature, request.provider, request.config)
init_config(config)
return {
"message": "Provider config set successfully",
"provider": request.provider,
"config": request.config,
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to set provider config: {str(e)}")
@app.post("/load-files/")
def load_files(
paths: Union[str, List[str]] = Body(
...,
description="A list of file paths to be loaded.",
examples=["/path/to/file1", "/path/to/file2", "/path/to/dir1"],
),
collection_name: str = Body(
None,
description="Optional name for the collection.",
examples=["my_collection"],
),
collection_description: str = Body(
None,
description="Optional description for the collection.",
examples=["This is a test collection."],
),
batch_size: int = Body(
None,
description="Optional batch size for the collection.",
examples=[256],
),
):
"""
Load files into the vector database.
Args:
paths (Union[str, List[str]]): File paths or directories to load.
collection_name (str, optional): Name for the collection. Defaults to None.
collection_description (str, optional): Description for the collection. Defaults to None.
batch_size (int, optional): Batch size for processing. Defaults to None.
Returns:
dict: A dictionary containing a success message.
Raises:
HTTPException: If loading files fails.
"""
try:
load_from_local_files(
paths_or_directory=paths,
collection_name=collection_name,
collection_description=collection_description,
batch_size=batch_size,
)
return {"message": "Files loaded successfully."}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/load-website/")
def load_website(
urls: Union[str, List[str]] = Body(
...,
description="A list of URLs of websites to be loaded.",
examples=["https://milvus.io/docs/overview.md"],
),
collection_name: str = Body(
None,
description="Optional name for the collection.",
examples=["my_collection"],
),
collection_description: str = Body(
None,
description="Optional description for the collection.",
examples=["This is a test collection."],
),
batch_size: int = Body(
None,
description="Optional batch size for the collection.",
examples=[256],
),
):
"""
Load website content into the vector database.
Args:
urls (Union[str, List[str]]): URLs of websites to load.
collection_name (str, optional): Name for the collection. Defaults to None.
collection_description (str, optional): Description for the collection. Defaults to None.
batch_size (int, optional): Batch size for processing. Defaults to None.
Returns:
dict: A dictionary containing a success message.
Raises:
HTTPException: If loading website content fails.
"""
try:
load_from_website(
urls=urls,
collection_name=collection_name,
collection_description=collection_description,
batch_size=batch_size,
)
return {"message": "Website loaded successfully."}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/query/")
def perform_query(
original_query: str = Query(
...,
description="Your question here.",
examples=["Write a report about Milvus."],
),
max_iter: int = Query(
3,
description="The maximum number of iterations for reflection.",
ge=1,
examples=[3],
),
):
"""
Perform a query against the loaded data.
Args:
original_query (str): The user's question or query.
max_iter (int, optional): Maximum number of iterations for reflection. Defaults to 3.
Returns:
dict: A dictionary containing the query result and token consumption.
Raises:
HTTPException: If the query fails.
"""
try:
result_text, _, consume_token = query(original_query, max_iter)
return {"result": result_text, "consume_token": consume_token}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="FastAPI Server")
parser.add_argument("--enable-cors", type=bool, default=False, help="Enable CORS support")
args = parser.parse_args()
if args.enable_cors:
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
print("CORS is enabled.")
else:
print("CORS is disabled.")
uvicorn.run(app, host="0.0.0.0", port=8000)