Skip to content

Commit b38908c

Browse files
committed
Get custom server changes up to source control
1 parent 87a6e57 commit b38908c

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

llama_cpp/server/app.py

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import json
5+
import boto3
56

67
from threading import Lock
78
from functools import partial
@@ -45,6 +46,49 @@
4546
from llama_cpp.server.errors import RouteErrorHandler
4647

4748

49+
title_message = os.getenv('TITLEMESSAGE', "🦙 llama.cpp Python API")
50+
apitable = os.getenv('APITABLE')
51+
52+
def check_and_update_api_key(api_key, invocation_type, credit_cost=1):
53+
# Initialize a boto3 DynamoDB resource
54+
dynamodb = boto3.resource('dynamodb')
55+
table = dynamodb.Table(apitable) # Replace with your DynamoDB table name
56+
57+
# Try to get the item for the given API key
58+
response = table.get_item(Key={'ApiKey': api_key})
59+
item = response.get('Item')
60+
61+
if not item or not item.get('Authorized') or item.get('Credits', 0) < credit_cost:
62+
# API key not found, not authorized, or not enough credits
63+
return False
64+
65+
# Deduct credit_cost from the Credits and prepare TotalInvocations update
66+
new_credits = item['Credits'] - credit_cost
67+
invocations_update = {
68+
':cost': credit_cost,
69+
':newval': 1,
70+
':inv_type': {invocation_type: 0}
71+
}
72+
73+
# Update the item in DynamoDB for the given API key
74+
try:
75+
table.update_item(
76+
Key={'ApiKey': api_key},
77+
UpdateExpression="SET Credits = Credits - :cost ADD TotalInvocations.#type :newval",
78+
ExpressionAttributeNames={
79+
'#type': invocation_type
80+
},
81+
ExpressionAttributeValues=invocations_update,
82+
ConditionExpression="attribute_exists(ApiKey) AND Credits >= :cost",
83+
ReturnValues="UPDATED_NEW"
84+
)
85+
return True
86+
except Exception as e:
87+
print(f"Error updating item: {e}")
88+
return False
89+
90+
91+
4892
router = APIRouter(route_class=RouteErrorHandler)
4993

5094
_server_settings: Optional[ServerSettings] = None
@@ -117,7 +161,8 @@ def create_app(
117161
middleware = [Middleware(RawContextMiddleware, plugins=(RequestIdPlugin(),))]
118162
app = FastAPI(
119163
middleware=middleware,
120-
title="🦙 llama.cpp Python API",
164+
###WORKHERE Make a modification so this reads in from OS on the specific endpoint for the end customer
165+
title=title_message,
121166
version=llama_cpp.__version__,
122167
)
123168
app.add_middleware(
@@ -175,7 +220,7 @@ def _logit_bias_tokens_to_input_ids(
175220
# Setup Bearer authentication scheme
176221
bearer_scheme = HTTPBearer(auto_error=False)
177222

178-
223+
#so here is where I can put in my custom API authentication system. ###WORKHERE
179224
async def authenticate(
180225
settings: Settings = Depends(get_server_settings),
181226
authorization: Optional[str] = Depends(bearer_scheme),
@@ -185,14 +230,15 @@ async def authenticate(
185230
return True
186231

187232
# check bearer credentials against the api_key
188-
if authorization and authorization.credentials == settings.api_key:
233+
if authorization: # and authorization.credentials == settings.api_key:
234+
if check_and_update_api_key(api_key=authorization.credentials,invocation_type="text"):
189235
# api key is valid
190-
return authorization.credentials
236+
return authorization.credentials
191237

192238
# raise http error 401
193239
raise HTTPException(
194240
status_code=status.HTTP_401_UNAUTHORIZED,
195-
detail="Invalid API key",
241+
detail="Invalid API key. Check API key and credits.",
196242
)
197243

198244

0 commit comments

Comments
 (0)