-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from mnemonica-ai/development
Release 0.0.6
- Loading branch information
Showing
13 changed files
with
187 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from pydantic import BaseModel | ||
from typing import Optional, List, Literal | ||
from datetime import datetime | ||
|
||
|
||
class ChatMessage(BaseModel): | ||
role: Literal["user", "assistant", "system"] | ||
content: str | ||
|
||
# TODO add support for images | ||
# images: NotRequired[Sequence[Any]] | ||
|
||
|
||
class ChatRequestPayload(BaseModel): | ||
model: str | ||
messages: Optional[List[ChatMessage]] = None | ||
format: Optional[str] = "" | ||
options: Optional[dict] = {} | ||
stream: Optional[bool] = False | ||
keep_alive: Optional[str] = None | ||
|
||
|
||
class ChatRequest(BaseModel): | ||
type: str = "chat" | ||
payload: ChatRequestPayload | ||
|
||
|
||
class ChatResponse(BaseModel): | ||
model: str | ||
created_at: datetime | ||
message: ChatMessage | ||
done: bool | ||
total_duration: int | ||
load_duration: int | ||
prompt_eval_count: Optional[int] = None | ||
prompt_eval_duration: int | ||
eval_count: int | ||
eval_duration: int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Generate a chat completion | ||
API implementation of `POST /api/chat` endpoint, handling completion orchestration, as replica of the same Ollama server endpoint. | ||
Ollama endpoint reference: https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion | ||
""" | ||
|
||
import time | ||
from flask import Blueprint, request | ||
from oshepherd.api.utils import streamify_json | ||
from oshepherd.api.chat.models import ChatRequest | ||
|
||
chat_blueprint = Blueprint("chat", __name__, url_prefix="/api/chat") | ||
|
||
|
||
@chat_blueprint.route("/", methods=["POST"]) | ||
def chat(): | ||
from oshepherd.worker.tasks import exec_completion | ||
|
||
print(f" # request.json {request.json}") | ||
chat_request = ChatRequest(**{"payload": request.json}) | ||
|
||
# req as json string ready to be sent through broker | ||
chat_request_json_str = chat_request.model_dump_json() | ||
print(f" # chat request {chat_request_json_str}") | ||
|
||
# queue request to remote ollama api server | ||
task = exec_completion.delay(chat_request_json_str) | ||
while not task.ready(): | ||
print(" > waiting for response...") | ||
time.sleep(1) | ||
ollama_res = task.get(timeout=1) | ||
|
||
status = 200 | ||
if ollama_res.get("error"): | ||
ollama_res = { | ||
"error": "Internal Server Error", | ||
"message": f"error executing completion: {ollama_res['error']['message']}", | ||
} | ||
status = 500 | ||
|
||
print(f" $ ollama response {status}: {ollama_res}") | ||
|
||
return streamify_json(ollama_res, status) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters