mirror of
https://github.com/lightbroker/llmsecops-research.git
synced 2026-08-11 22:00:23 +02:00
support JSON request payload passed to LLM service layer
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import cgi
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
# Add the parent folder (or any relative path)
|
||||
sys.path.append(os.path.abspath('./../llm'))
|
||||
|
||||
from phi3_language_model import Phi3LanguageModel
|
||||
|
||||
|
||||
class PathDispatcher:
|
||||
class ApiController:
|
||||
def __init__(self):
|
||||
self.routes = {}
|
||||
|
||||
@@ -11,6 +17,10 @@ class PathDispatcher:
|
||||
start_response('415 Unsupported Media Type', self.response_headers)
|
||||
return [json.dumps({'error': 'Unsupported Content-Type'}).encode('utf-8')]
|
||||
|
||||
def get_service_response(self, prompt):
|
||||
service = Phi3LanguageModel()
|
||||
response = service.get_response(prompt_input=prompt)
|
||||
return response
|
||||
|
||||
def __http_200_ok(self, env, start_response):
|
||||
try:
|
||||
@@ -19,10 +29,14 @@ class PathDispatcher:
|
||||
request_body_size = 0
|
||||
|
||||
request_body = env['wsgi.input'].read(request_body_size)
|
||||
request_body = request_body.decode('utf-8')
|
||||
request_json = json.loads(request_body.decode('utf-8'))
|
||||
prompt = request_json.get('prompt')
|
||||
|
||||
# for now, just reading request and echoing back in response
|
||||
data = json.loads(request_body)
|
||||
# data = json.loads(prompt)
|
||||
# response_body = json.dumps(data).encode('utf-8')
|
||||
|
||||
data = self.get_service_response(prompt)
|
||||
response_body = json.dumps(data).encode('utf-8')
|
||||
|
||||
response_headers = [('Content-Type', 'application/json'), ('Content-Length', str(len(response_body)))]
|
||||
@@ -34,6 +48,8 @@ class PathDispatcher:
|
||||
method = env.get('REQUEST_METHOD').upper()
|
||||
path = env.get('PATH_INFO')
|
||||
|
||||
# TODO: register route for POST /api/conversations
|
||||
|
||||
if not method == 'POST':
|
||||
self.__http_415_notsupported(env, start_response)
|
||||
|
||||
@@ -44,7 +60,3 @@ class PathDispatcher:
|
||||
start_response('400 Bad Request', self.response_headers)
|
||||
return [json.dumps({'error': 'Invalid JSON'}).encode('utf-8')]
|
||||
|
||||
|
||||
def register(self, method, path, function):
|
||||
self.routes[method.lower(), path] = function
|
||||
return function
|
||||
+3
-4
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
|
||||
from PathDispatcher import PathDispatcher
|
||||
from controller import ApiController
|
||||
from wsgiref.simple_server import make_server
|
||||
|
||||
|
||||
@@ -14,9 +14,8 @@ class RestApiServer:
|
||||
|
||||
def listen(self):
|
||||
port = 9999
|
||||
dispatcher = PathDispatcher()
|
||||
dispatcher.register('POST', '/', self.post_response)
|
||||
with make_server('', port, dispatcher) as wsgi_srv:
|
||||
controller = ApiController()
|
||||
with make_server('', port, controller) as wsgi_srv:
|
||||
print(f'listening on port {port}...')
|
||||
wsgi_srv.serve_forever()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user