Skip to content

Commit

Permalink
add LLM model & manager, get-all, get and completion API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbedard committed Oct 17, 2024
1 parent 09a4140 commit 11ad86d
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 0 deletions.
123 changes: 123 additions & 0 deletions apis/paios/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ paths:
- $ref: '#/components/parameters/id'
/shares:
get:
security:
- jwt: []
tags:
- Share Management
summary: Retrieve all share links
Expand All @@ -721,6 +723,8 @@ paths:
X-Total-Count:
$ref: '#/components/headers/X-Total-Count'
post:
security:
- jwt: []
summary: Create new share link
tags:
- Share Management
Expand All @@ -737,6 +741,8 @@ paths:
$ref: '#/components/schemas/ShareCreate'
'/shares/{id}':
get:
security:
- jwt: []
tags:
- Share Management
summary: Retrieve share link by id
Expand All @@ -751,6 +757,8 @@ paths:
schema:
$ref: '#/components/schemas/Share'
put:
security:
- jwt: []
tags:
- Share Management
summary: Update share link by id
Expand All @@ -766,6 +774,8 @@ paths:
'200':
description: OK
delete:
security:
- jwt: []
tags:
- Share Management
summary: Delete share link by id
Expand All @@ -777,6 +787,74 @@ paths:
description: No Content
'404':
description: Not Found
/llms:
get:
tags:
- LLM Management
summary: Retrieve all available LLMs
description: Get all installed / available LLMs.
parameters:
- $ref: '#/components/parameters/sort'
- $ref: '#/components/parameters/range'
- $ref: '#/components/parameters/filter'
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Llm'
headers:
X-Total-Count:
$ref: '#/components/headers/X-Total-Count'
'/llms/{id}':
get:
tags:
- LLM Management
summary: Retrieve LLM by id
description: Retrieve the LLM with the specified id.
parameters:
- $ref: '#/components/parameters/kebab-dot_id'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Llm'
'404':
description: LLM not found
'/llms/{id}/completion':
post:
tags:
- LLM Management
summary: Invoke Completion on LLM
description: Invoke Completion on the LLM with the specified id.
operationId: backend.api.LlmsView.completion
parameters:
- $ref: '#/components/parameters/kebab-dot_id'
requestBody:
description: Messages to input to Completion function.
content:
application/json:
schema:
type: object
properties:
messages:
$ref: '#/components/schemas/messagesList'
responses:
'200':
description: Completion succeeded
content:
application/json:
schema:
type: object
'400':
description: Completion failed
'404':
description: LLM not found
/auth/webauthn/register-options:
post:
summary: Generate WebAuthn registration options
Expand Down Expand Up @@ -936,6 +1014,13 @@ components:
required: true
schema:
$ref: '#/components/schemas/kebab-snake_id'
kebab-dot_id:
name: id
in: path
description: id of the object
required: true
schema:
$ref: '#/components/schemas/kebab-dot_id'
key:
name: key
in: path
Expand Down Expand Up @@ -1003,6 +1088,12 @@ components:
maxLength: 100
example: langchain-core
pattern: ^[a-z0-9]+([_-][a-z0-9]+)*$
kebab-dot_id:
type: string
minLength: 2
maxLength: 100
example: ollama-llama3.2
pattern: ^[a-z0-9]+([.-][a-z0-9]+)*$
semVer:
type: string
example: '1.1.0'
Expand Down Expand Up @@ -1068,6 +1159,16 @@ components:
readOnly: true
example: abcd-efgh-ijkl
pattern: ^[a-z]{4}-[a-z]{4}-[a-z]{4}$
messagesList:
type: array
example: [{"role": "user", "content": "What is Kwaai.ai?"}]
items:
type: object
properties:
role:
type: string
content:
type: string
download:
type: object
properties:
Expand Down Expand Up @@ -1390,6 +1491,28 @@ components:
example: false
required:
- resource_id
Llm:
type: object
title: Llm
properties:
id:
type: string
name:
type: string
llm_name:
type: string
provider:
type: string
api_base:
type: string
nullable: true
is_active:
type: boolean
required:
- id
- name
- provider
- is_active
RegistrationOptions:
type: object
properties:
Expand Down
43 changes: 43 additions & 0 deletions backend/api/LlmsView.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from starlette.responses import JSONResponse
from backend.managers.LlmsManager import LlmsManager
from backend.pagination import parse_pagination_params

class LlmsView:
def __init__(self):
self.llmm = LlmsManager()

async def get(self, id: str):
llm = await self.llmm.get_llm(id)
if llm is None:
return JSONResponse(headers={"error": "LLM not found"}, status_code=404)
return JSONResponse(llm.model_dump(), status_code=200)

async def search(self, filter: str = None, range: str = None, sort: str = None):
result = parse_pagination_params(filter, range, sort)
if isinstance(result, JSONResponse):
return result

offset, limit, sort_by, sort_order, filters = result

llms, total_count = await self.llmm.retrieve_llms(limit=limit, offset=offset, sort_by=sort_by, sort_order=sort_order, filters=filters)
headers = {
'X-Total-Count': str(total_count),
'Content-Range': f'shares {offset}-{offset + len(llms) - 1}/{total_count}'
}
return JSONResponse([llm.model_dump() for llm in llms], status_code=200, headers=headers)

async def completion(self, id: str, body: dict):
print("completion. body: {}".format(body))
messages = []
if 'messages' in body and body['messages']:
messages = body['messages']
llm = await self.llmm.get_llm(id)
if llm:
print("Selected LLM is {}".format(llm.llm_name))
response = self.llmm.completion(llm, messages)
if response:
return JSONResponse(response.model_dump(), status_code=200)
else:
return JSONResponse(status_code=400, content={"message": "Completion failed"})
else:
return JSONResponse(status_code=404, content={"message": "LLM not found"})
1 change: 1 addition & 0 deletions backend/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .PersonasView import PersonasView
from .SharesView import SharesView
from .AuthView import AuthView
from .LlmsView import LlmsView
Loading

0 comments on commit 11ad86d

Please sign in to comment.