1
1
import argparse
2
- import http .server
3
- import json
4
2
5
3
from gptcache import cache
6
4
from gptcache .adapter .api import (
9
7
init_similar_cache ,
10
8
init_similar_cache_from_config ,
11
9
)
10
+ from gptcache .utils import import_fastapi , import_pydantic
12
11
12
+ import_fastapi ()
13
+ import_pydantic ()
13
14
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
18
18
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 ]
23
19
24
- result = get ( prompt )
20
+ app = FastAPI ( )
25
21
26
- response = json .dumps (result )
27
22
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 = ""
32
26
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" )
39
27
40
- put (prompt , data )
28
+ @app .get ("/" )
29
+ async def hello ():
30
+ return "hello gptcache server"
41
31
42
- self .send_response (200 )
43
- self .end_headers ()
44
- self .wfile .write (bytes ("successfully update the cache" , "utf-8" ))
45
32
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"
59
37
60
38
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"
65
49
66
50
67
51
def main ():
@@ -86,7 +70,7 @@ def main():
86
70
else :
87
71
init_similar_cache (args .cache_dir )
88
72
89
- start_server ( args .host , args .port )
73
+ uvicorn . run ( app , host = args .host , port = args .port )
90
74
91
75
92
76
if __name__ == "__main__" :
0 commit comments