Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitca2f353

Browse files
authored
FastAPI example using pgml python SDK (#738)
1 parent399b534 commitca2f353

File tree

6 files changed

+1996
-106
lines changed

6 files changed

+1996
-106
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#Documentation for FastAPI Example
2+
3+
This document provides an overview of the available API endpoints`/ingest` and`/search`, how to use them, and how to run the server.
4+
5+
##Run the Server
6+
7+
To run the server, use the following command:
8+
9+
```bash
10+
poetry run python3 examples/fastapi-server/app/main.py
11+
```
12+
13+
14+
##API Endpoints
15+
16+
###`/ingest` Endpoint
17+
18+
This endpoint allows you to ingest documents for processing. It expects a POST request with a JSON body that contains the details of the documents to be processed.
19+
20+
####Request
21+
22+
-**URL:**`http://0.0.0.0:8888/ingest`
23+
-**Method:**`POST`
24+
-**Content-Type:**`application/json`
25+
-**Body:**
26+
27+
```json
28+
{
29+
"collection_name":"<name_of_collection>",
30+
"document_path":"<path_to_document>"
31+
}
32+
```
33+
34+
####Example
35+
36+
You can use the following`curl` command as an example:
37+
38+
```bash
39+
curl --location'http://0.0.0.0:8888/ingest' \
40+
--header'Content-Type: application/json' \
41+
--data'{
42+
"collection_name": "test_collection",
43+
"document_path": "~/path/to/pdf"
44+
}'
45+
```
46+
47+
###`/search` Endpoint
48+
49+
This endpoint allows you to search within a given collection. It expects a POST request with a JSON body that contains the details of the search request.
50+
51+
52+
####Request
53+
54+
-**URL:**`http://0.0.0.0:8888/search`
55+
-**Method:**`POST`
56+
-**Content-Type:**`application/json`
57+
-**Body:**
58+
59+
```json
60+
{
61+
"collection_name":"<name_of_collection>",
62+
"question":"<search_query>",
63+
"k":"<number_of_results>",
64+
"metadata_filter":"<metadata_filter>"
65+
}
66+
```
67+
68+
Note: The`k` and`metadata_filter` fields are optional. The`k` field is used to limit the number of search results, and the metadata_filter field is used to add additional filters on the metadata of the documents.
69+
70+
###Example Request
71+
You can use the following`curl` command as an example:
72+
73+
```bash
74+
curl --location'http://0.0.0.0:8888/search' \
75+
--header'Content-Type: application/json' \
76+
--data'{
77+
"collection_name": "testing",
78+
"question": "What people did he met?"
79+
}'
80+
```
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Types
2+
fromtypingimportList
3+
frompydanticimportBaseModel
4+
5+
fromfastapiimportFastAPI,HTTPException
6+
fromfastapi.middleware.corsimportCORSMiddleware
7+
8+
fromlangchain.document_loaders.unstructuredimportUnstructuredFileLoader
9+
fromlangchain.docstore.documentimportDocument
10+
11+
importos
12+
importpgml
13+
14+
15+
app=FastAPI()
16+
17+
18+
@app.on_event("startup")
19+
asyncdefstartup():
20+
local_pgml="postgres://postgres@127.0.0.1:5433/pgml_development"
21+
conninfo=os.environ.get("PGML_CONNECTION",local_pgml)
22+
app.state.db:pgml.Database=pgml.Database(conninfo,min_connections=4)
23+
24+
25+
classIngestionBody(BaseModel):
26+
collection_name:str
27+
document_path:str
28+
29+
classSearchBody(BaseModel):
30+
question:str
31+
collection_name:str
32+
k:int=5
33+
metadata_filter :dict= {}
34+
35+
@app.post("/ingest")
36+
asyncdefinsert_documents(body:IngestionBody):
37+
"""
38+
Example:
39+
curl --location 'http://0.0.0.0:8888/ingest'\
40+
--header 'Content-Type: application/json'\
41+
--data '{
42+
"collection_name": "test_collection",
43+
"document_path": "~/path/to/pdf"
44+
}'
45+
"""
46+
47+
try:
48+
49+
# # Get Db connection from pgml
50+
db:pgml.Database=app.state.db
51+
collection:pgml.Collection=db.create_or_get_collection(body.collection_name)
52+
53+
54+
# get documents, using Langchain Unstructored loader.
55+
print("Loading Document")
56+
loader:List[Document]=UnstructuredFileLoader(body.document_path).load()
57+
58+
# Converting from Langchain Document to regular dict for pgml to process
59+
documents:List[dict]= [
60+
{
61+
"text":x.page_content,
62+
**x.metadata
63+
}
64+
forxinloader
65+
]
66+
67+
# fun stuff
68+
collection.upsert_documents(documents)
69+
collection.generate_chunks()
70+
collection.generate_embeddings()
71+
72+
returndocuments
73+
74+
exceptExceptionase:
75+
raiseHTTPException(status_code=404,detail=str(e))
76+
77+
78+
@app.post("/search")
79+
asyncdefsearch_documents(body:SearchBody):
80+
"""
81+
Example:
82+
curl --location 'http://0.0.0.0:8888/search'\
83+
--header 'Content-Type: application/json'\
84+
--data '{
85+
"collection_name": "testing",
86+
"question": "What people did he met?"
87+
}'
88+
"""
89+
90+
try:
91+
92+
# # Get Db connection from pgml
93+
db:pgml.Database=app.state.db
94+
collection:pgml.Collection=db.create_or_get_collection(body.collection_name)
95+
returncollection.vector_search(body.question,top_k=body.k,metadata_filter=body.metadata_filter)
96+
97+
exceptExceptionase:
98+
raiseHTTPException(status_code=404,detail=str(e))
99+
100+
101+
if__name__=="__main__":
102+
importuvicorn
103+
uvicorn.run(app,host="0.0.0.0",port=8888)
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp