Skip to content

Commit

Permalink
add state check for openai
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceunx committed May 15, 2024
1 parent 01d4465 commit 68703f8
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions gpt_server/routers/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

url = os.environ['OPENAI_URL']

subscription_url = os.environ.get("OPENAI_SUBSCRIPTION_URL", None)

headers = {
"Authorization": f"Bearer {os.environ['OPENAI_KEY']}",
"Content-Type": "application/json",
Expand Down Expand Up @@ -39,3 +41,14 @@ async def chat(request: Request, prompt: Prompt):
less_messages = prompt.messages[1:]
data["messages"] = [dict(msg) for msg in less_messages]
return EventSourceResponse(get_content(data))


@router.post("/state", status_code=200)
async def state(request: Request, prompt: Prompt):
async with aiohttp.ClientSession(headers=headers) as sess:
if subscription_url:
async with sess.get(subscription_url) as res:
data = await res.json()
value = data["remain_quota"]
return {"remain_token": value}
return {"remain_token": 0}

0 comments on commit 68703f8

Please sign in to comment.