forked from ViperX7/Alpaca-Turbo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
88 lines (58 loc) · 2.55 KB
/
api.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# import asyncio
# from async_generator import async_generator, yield_
from typing import List, Optional, Union
from pydantic import BaseModel
from fastapi.responses import StreamingResponse, HTMLResponse
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from alpaca_turbo import Assistant
assistant = Assistant()
assistant.prep_model()
templates = Jinja2Templates(directory="templates")
app = FastAPI()
class Attachment(BaseModel):
image: bytes
# TODO support attachments: Communication = Union[str, Attachment]
Communication = str
class Message(BaseModel):
role: str
content: Communication
class CompletionRequest(BaseModel):
model: Optional[str]
prompt: Union[Communication, List[Message]]
max_tokens: Optional[int]
temperature: Optional[float]
stop: Optional[Union[str, List[str]]]
stream: Optional[bool]
class CompletionResponseChoice(BaseModel):
text: Communication
finish_reason: Optional[str]
class CompletionResponse(BaseModel):
choices: List[CompletionResponseChoice]
class Model:
def get_completion(self, model, prompt, max_tokens, temperature, stop) -> str:
res = ""
for char in assistant.ask_bot(prompt):
res += char
return res
def get_completion_tokens(self, model, prompt, max_tokens, temperature, stop):
for char in assistant.ask_bot(prompt):
yield char
model = Model()
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.post("/completions", response_model=CompletionResponse)
async def completions(request: CompletionRequest):
if request.stream:
return StreamingResponse(completion_token_generator(request), media_type="text/event-stream")
text = model.get_completion(
request.model, request.prompt, request.max_tokens, request.temperature, request.stop)
return CompletionResponse(choices=[CompletionResponseChoice(text=text)])
def completion_token_generator(request: CompletionRequest):
for token in model.get_completion_tokens(request.model, request.prompt, request.max_tokens, request.temperature, request.stop):
completion_response = CompletionResponse(
choices=[CompletionResponseChoice(text=token)])
yield f"data: {completion_response.json()}\n\n"
# Run using uvicorn api:app --reload
# This will start a local development server at http://127.0.0.1:8000. You can access the auto-generated documentation for your API at http://127.0.0.1:8000/docs.