Skip to content

Commit

Permalink
Add a simple GPTCache server (#244)
Browse files Browse the repository at this point in the history
Signed-off-by: SimFG <[email protected]>
  • Loading branch information
SimFG authored Apr 19, 2023
1 parent 5ecdbbc commit 252de14
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
40 changes: 40 additions & 0 deletions docs/release_note.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,46 @@ To read the following content, you need to understand the basic use of GPTCache,
- [Readme doc](https://github.com/zilliztech/GPTCache)
- [Usage doc](https://github.com/zilliztech/GPTCache/blob/main/docs/usage.md)

## v0.1.16 (2023.4.18)

1. Add StableDiffusion adapter (**experimental**)

```python
import torch

from gptcache.adapter.diffusers import StableDiffusionPipeline
from gptcache.processor.pre import get_prompt
from gptcache import cache

cache.init(
pre_embedding_func=get_prompt,
)
model_id = "stabilityai/stable-diffusion-2-1"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)

prompt = "a photo of an astronaut riding a horse on mars"
pipe(prompt=prompt).images[0]
```

2. Add speech to text bootcamp, [link](https://github.com/zilliztech/GPTCache/tree/main/docs/bootcamp/openai/speech_to_text.ipynb)

3. More convenient management of cache files

```python
from gptcache.manager.factory import manager_factory

data_manager = manager_factory('sqlite,faiss', data_dir="test_cache", vector_params={"dimension": 5})
```

4. Add a simple GPTCache server (**experimental**)

After starting this server, you can:

- put the data to cache, like: `curl -X PUT -d "receive a hello message" "http://localhost:8000?prompt=hello"`
- get the data from cache, like: `curl -X GET "http://localhost:8000?prompt=hello"`

Currently the service is just a map cache, more functions are still under development.

## v0.1.15 (2023.4.17)

1. Add GPTCache api, makes it easier to access other different llm models and applications
Expand Down
1 change: 1 addition & 0 deletions gptcache_server/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from gptcache_server.server import start_server
52 changes: 52 additions & 0 deletions gptcache_server/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import argparse
import http.server
import json

from gptcache import cache
from gptcache.adapter.api import get, put
from gptcache.processor.pre import get_prompt


class GPTCacheHandler(http.server.BaseHTTPRequestHandler):
# curl -X GET "http://localhost:8000?prompt=hello"
def do_GET(self):
params = self.path.split("?")[1]
prompt = params.split("=")[1]

result = get(prompt)

response = json.dumps(result)

self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(bytes(response, "utf-8"))

# curl -X PUT -d "receive a hello message" "http://localhost:8000?prompt=hello"
def do_PUT(self):
params = self.path.split("?")[1]
prompt = params.split("=")[1]
content_length = int(self.headers.get("Content-Length", "0"))
data = self.rfile.read(content_length).decode("utf-8")

put(prompt, data)

self.send_response(200)
self.end_headers()


def start_server(host: str, port: int):
httpd = http.server.HTTPServer((host, port), GPTCacheHandler)
print(f"Starting server at {host}:{port}")
httpd.serve_forever()


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--host", default="localhost", help="the hostname to listen on")
parser.add_argument("--port", type=int, default=8000, help="the port to listen on")
args = parser.parse_args()

cache.init(pre_embedding_func=get_prompt)

start_server(args.host, args.port)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def parse_requirements(file_name: str) -> List[str]:
setuptools.setup(
name="gptcache",
packages=find_packages(),
version="0.1.15",
version="0.1.16",
author="SimFG",
author_email="[email protected]",
description="GPTCache, a powerful caching library that can be used to speed up and lower the cost of chat "
Expand Down

0 comments on commit 252de14

Please sign in to comment.