-
Notifications
You must be signed in to change notification settings - Fork 46
/
claude.py
64 lines (48 loc) · 1.68 KB
/
claude.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
from os import getenv
from typing import Union
from fastapi import FastAPI, Depends, Header, HTTPException, status
from pydantic import BaseModel
from fastapi.responses import StreamingResponse
from sse_starlette.sse import EventSourceResponse
from slack import client
app = FastAPI()
server_token = getenv("SERVER_TOKEN")
async def must_token(x_token: Union[str, None] = Header(None)):
if server_token and x_token != server_token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail={
"msg": "must token",
}
)
class ClaudeChatPrompt(BaseModel):
prompt: str
@app.post("/claude/chat", dependencies=[Depends(must_token)])
async def chat(body: ClaudeChatPrompt):
await client.open_channel()
await client.chat(body.prompt)
return {
"claude": await client.get_reply()
}
# add --no-buffer to see the effect of streaming
# curl -X 'POST' --no-buffer \
# 'http://127.0.0.1:8088/claude/stream_chat' \
# -H 'accept: text/plain' \
# -H 'Content-Type: application/json' \
# -d '{
# "prompt": "今天天气很不错吧"}'
@app.post("/claude/stream_chat", dependencies=[Depends(must_token)])
async def chat(body: ClaudeChatPrompt):
await client.open_channel()
await client.chat(body.prompt)
return EventSourceResponse(client.get_stream_reply(), ping=100)
@app.post("/claude/reset", dependencies=[Depends(must_token)])
async def chat():
await client.open_channel()
await client.chat("请忘记上面的会话内容")
return {
"claude": await client.get_reply()
}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8088)