11import argparse
2- import http .server
3- import json
42
53from gptcache import cache
64from gptcache .adapter .api import (
97 init_similar_cache ,
108 init_similar_cache_from_config ,
119)
10+ from gptcache .utils import import_fastapi , import_pydantic
1211
12+ import_fastapi ()
13+ import_pydantic ()
1314
14- class GPTCacheHandler (http .server .BaseHTTPRequestHandler ):
15- """
16- HTTPServer handler for GPTCache Service.
17- """
15+ from fastapi import FastAPI
16+ import uvicorn
17+ from pydantic import BaseModel
1818
19- # curl -X GET "http://localhost:8000?prompt=hello"
20- def do_GET (self ):
21- params = self .path .split ("?" )[1 ]
22- prompt = params .split ("=" )[1 ]
2319
24- result = get ( prompt )
20+ app = FastAPI ( )
2521
26- response = json .dumps (result )
2722
28- self .send_response (200 )
29- self .send_header ("Content-type" , "application/json" )
30- self .end_headers ()
31- self .wfile .write (bytes (response , "utf-8" ))
23+ class CacheData (BaseModel ):
24+ prompt : str
25+ answer : str = ""
3226
33- # curl -X PUT -d "receive a hello message" "http://localhost:8000?prompt=hello"
34- def do_PUT (self ):
35- params = self .path .split ("?" )[1 ]
36- prompt = params .split ("=" )[1 ]
37- content_length = int (self .headers .get ("Content-Length" , "0" ))
38- data = self .rfile .read (content_length ).decode ("utf-8" )
3927
40- put (prompt , data )
28+ @app .get ("/" )
29+ async def hello ():
30+ return "hello gptcache server"
4131
42- self .send_response (200 )
43- self .end_headers ()
44- self .wfile .write (bytes ("successfully update the cache" , "utf-8" ))
4532
46- # curl -X POST "http://localhost:8000?flush=true"
47- def do_POST (self ):
48- params = self .path .split ("?" )[1 ]
49- flush = params .split ("=" )[1 ]
50- back_message = "currently only be used to flush the cache, like: example.com?flush=true"
51- if flush == "true" :
52- cache .flush ()
53- self .send_response (200 )
54- back_message = "successfully flush the cache"
55- else :
56- self .send_response (404 )
57- self .end_headers ()
58- self .wfile .write (bytes (back_message , "utf-8" ))
33+ @app .post ("/put" )
34+ async def put_cache (cache_data : CacheData ) -> str :
35+ put (cache_data .prompt , cache_data .answer )
36+ return "successfully update the cache"
5937
6038
61- def start_server (host : str , port : int ):
62- httpd = http .server .HTTPServer ((host , port ), GPTCacheHandler )
63- print (f"Starting server at { host } :{ port } " )
64- httpd .serve_forever ()
39+ @app .post ("/get" )
40+ async def get_cache (cache_data : CacheData ) -> CacheData :
41+ result = get (cache_data .prompt )
42+ return CacheData (prompt = cache_data .prompt , answer = result )
43+
44+
45+ @app .post ("/flush" )
46+ async def get_cache () -> str :
47+ cache .flush ()
48+ return "successfully flush the cache"
6549
6650
6751def main ():
@@ -86,7 +70,7 @@ def main():
8670 else :
8771 init_similar_cache (args .cache_dir )
8872
89- start_server ( args .host , args .port )
73+ uvicorn . run ( app , host = args .host , port = args .port )
9074
9175
9276if __name__ == "__main__" :
0 commit comments