forked from squarecat/chatgpt-retrieval-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
463 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
scripts/ | ||
tests/ | ||
examples/ | ||
local-server/ | ||
*.md | ||
*.pyc | ||
.dockerignore | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"schema_version": "v1", | ||
"name_for_model": "retrieval", | ||
"name_for_human": "Retrieval Plugin", | ||
"description_for_model": "Plugin for searching through the user's documents (such as files, emails, and more) to find answers to questions and retrieve relevant information. Use it whenever a user asks something that might be found in their personal information.", | ||
"description_for_human": "Search through your documents.", | ||
"auth": { | ||
"type": "none" | ||
}, | ||
"api": { | ||
"type": "openapi", | ||
"url": "http://localhost:3333/.well-known/openapi.yaml" | ||
}, | ||
"logo_url": "http://localhost:3333/.well-known/logo.png", | ||
"contact_email": "[email protected]", | ||
"legal_info_url": "[email protected]" | ||
} | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
# This is a version of the main.py file found in ../../../server/main.py for testing the plugin locally. | ||
# Use the command `poetry run dev` to run this. | ||
from typing import Optional | ||
import uvicorn | ||
from fastapi import FastAPI, File, Form, HTTPException, Body, UploadFile | ||
|
||
from models.api import ( | ||
DeleteRequest, | ||
DeleteResponse, | ||
QueryRequest, | ||
QueryResponse, | ||
UpsertRequest, | ||
UpsertResponse, | ||
) | ||
from datastore.factory import get_datastore | ||
from services.file import get_document_from_file | ||
|
||
from starlette.responses import FileResponse | ||
|
||
from models.models import DocumentMetadata, Source | ||
from fastapi.middleware.cors import CORSMiddleware | ||
|
||
|
||
app = FastAPI() | ||
|
||
PORT = 3333 | ||
|
||
origins = [ | ||
f"http://localhost:{PORT}", | ||
"https://chat.openai.com", | ||
] | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=origins, | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
|
||
@app.route("/.well-known/ai-plugin.json") | ||
async def get_manifest(request): | ||
file_path = "./local-server/ai-plugin.json" | ||
return FileResponse(file_path, media_type="text/json") | ||
|
||
|
||
@app.route("/.well-known/logo.png") | ||
async def get_logo(request): | ||
file_path = "./local-server/logo.png" | ||
return FileResponse(file_path, media_type="text/json") | ||
|
||
|
||
@app.route("/.well-known/openapi.yaml") | ||
async def get_openapi(request): | ||
file_path = "./local-server/openapi.yaml" | ||
return FileResponse(file_path, media_type="text/json") | ||
|
||
|
||
@app.post( | ||
"/upsert-file", | ||
response_model=UpsertResponse, | ||
) | ||
async def upsert_file( | ||
file: UploadFile = File(...), | ||
metadata: Optional[str] = Form(None), | ||
): | ||
try: | ||
metadata_obj = ( | ||
DocumentMetadata.parse_raw(metadata) | ||
if metadata | ||
else DocumentMetadata(source=Source.file) | ||
) | ||
except: | ||
metadata_obj = DocumentMetadata(source=Source.file) | ||
|
||
document = await get_document_from_file(file, metadata_obj) | ||
|
||
try: | ||
ids = await datastore.upsert([document]) | ||
return UpsertResponse(ids=ids) | ||
except Exception as e: | ||
print("Error:", e) | ||
raise HTTPException(status_code=500, detail=f"str({e})") | ||
|
||
|
||
@app.post( | ||
"/upsert", | ||
response_model=UpsertResponse, | ||
) | ||
async def upsert( | ||
request: UpsertRequest = Body(...), | ||
): | ||
try: | ||
ids = await datastore.upsert(request.documents) | ||
return UpsertResponse(ids=ids) | ||
except Exception as e: | ||
print("Error:", e) | ||
raise HTTPException(status_code=500, detail="Internal Service Error") | ||
|
||
|
||
@app.post("/query", response_model=QueryResponse) | ||
async def query_main(request: QueryRequest = Body(...)): | ||
try: | ||
results = await datastore.query( | ||
request.queries, | ||
) | ||
return QueryResponse(results=results) | ||
except Exception as e: | ||
print("Error:", e) | ||
raise HTTPException(status_code=500, detail="Internal Service Error") | ||
|
||
|
||
@app.delete( | ||
"/delete", | ||
response_model=DeleteResponse, | ||
) | ||
async def delete( | ||
request: DeleteRequest = Body(...), | ||
): | ||
if not (request.ids or request.filter or request.delete_all): | ||
raise HTTPException( | ||
status_code=400, | ||
detail="One of ids, filter, or delete_all is required", | ||
) | ||
try: | ||
success = await datastore.delete( | ||
ids=request.ids, | ||
filter=request.filter, | ||
delete_all=request.delete_all, | ||
) | ||
return DeleteResponse(success=success) | ||
except Exception as e: | ||
print("Error:", e) | ||
raise HTTPException(status_code=500, detail="Internal Service Error") | ||
|
||
|
||
@app.on_event("startup") | ||
async def startup(): | ||
global datastore | ||
datastore = await get_datastore() | ||
|
||
|
||
def start(): | ||
uvicorn.run("local-server.main:app", host="localhost", port=PORT, reload=True) |
Oops, something went wrong.