-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
63 lines (49 loc) · 1.3 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import json
from utils import chat_completion, config, get_cache, set_cache
SYSTEM_PROMPT = """
Reply only with the element that comes by combining two elements using the logic on the examples below, along with it's corresponding emoji on the next line.
Examples:
Earth + Water
Plant
🌱
Earth + Lava
Stone
🪨
Earth + Island
Continent
🌎
Water + Water
Lake
🌊
Fire + Fire
Volcano
🌋
"""
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def pair(first: str, second: str):
cached_value = get_cache((first, second))
if cached_value:
cached_value["isNew"] = False
return cached_value
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"{first} + {second}"},
]
completion = await chat_completion(messages)
print(completion)
lines = completion.strip().split("\n")
result = {"result": lines[0], "emoji": lines[1]}
set_cache((first, second), result)
if "discovery_enabled" in config and config["discovery_enabled"]:
result["isNew"] = True
return result