2
2
3
3
import os
4
4
import json
5
+ import boto3
5
6
6
7
from threading import Lock
7
8
from functools import partial
45
46
from llama_cpp .server .errors import RouteErrorHandler
46
47
47
48
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
+
48
92
router = APIRouter (route_class = RouteErrorHandler )
49
93
50
94
_server_settings : Optional [ServerSettings ] = None
@@ -117,7 +161,8 @@ def create_app(
117
161
middleware = [Middleware (RawContextMiddleware , plugins = (RequestIdPlugin (),))]
118
162
app = FastAPI (
119
163
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 ,
121
166
version = llama_cpp .__version__ ,
122
167
)
123
168
app .add_middleware (
@@ -175,7 +220,7 @@ def _logit_bias_tokens_to_input_ids(
175
220
# Setup Bearer authentication scheme
176
221
bearer_scheme = HTTPBearer (auto_error = False )
177
222
178
-
223
+ #so here is where I can put in my custom API authentication system. ###WORKHERE
179
224
async def authenticate (
180
225
settings : Settings = Depends (get_server_settings ),
181
226
authorization : Optional [str ] = Depends (bearer_scheme ),
@@ -185,14 +230,15 @@ async def authenticate(
185
230
return True
186
231
187
232
# 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" ):
189
235
# api key is valid
190
- return authorization .credentials
236
+ return authorization .credentials
191
237
192
238
# raise http error 401
193
239
raise HTTPException (
194
240
status_code = status .HTTP_401_UNAUTHORIZED ,
195
- detail = "Invalid API key" ,
241
+ detail = "Invalid API key. Check API key and credits. " ,
196
242
)
197
243
198
244
0 commit comments