2023-09-14 15:22:38 +08:00
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
import requests
|
2023-09-17 13:16:21 +00:00
|
|
|
from typing import Optional, List
|
2023-09-14 15:22:38 +08:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
from .compute_task import ComputeTask, ComputeTaskState, ComputeTaskType
|
|
|
|
|
from .queue_compute_node import Queue_ComputeNode
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
This is a custom implementation, it should be redesigned.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
class LocalLlama_ComputeNode(Queue_ComputeNode):
|
|
|
|
|
async def execute_task(self, task: ComputeTask) -> {
|
|
|
|
|
"content": str,
|
|
|
|
|
"message": str,
|
|
|
|
|
"state": ComputeTaskState,
|
|
|
|
|
"error": {
|
|
|
|
|
"code": int,
|
|
|
|
|
"message": str,
|
|
|
|
|
}
|
|
|
|
|
}:
|
|
|
|
|
class GenerateResponse(BaseModel):
|
|
|
|
|
error: Optional[int]
|
|
|
|
|
msg: Optional[str]
|
2023-09-17 13:16:21 +00:00
|
|
|
results: Optional[List[str]]
|
2023-09-14 15:22:38 +08:00
|
|
|
|
2023-09-17 13:16:21 +00:00
|
|
|
try:
|
|
|
|
|
prompt_msgs = []
|
|
|
|
|
for prompt in task.params["prompts"]:
|
|
|
|
|
prompt_msgs.append(prompt["content"])
|
|
|
|
|
|
2023-09-14 15:22:38 +08:00
|
|
|
body = {
|
2023-09-17 13:16:21 +00:00
|
|
|
"prompts": prompt_msgs
|
2023-09-14 15:22:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-09-17 13:16:21 +00:00
|
|
|
response = requests.post("http://aigc:7880/generate", json = body, verify=False, headers={"Content-Type": "application/json"})
|
2023-09-14 15:22:38 +08:00
|
|
|
response.close()
|
|
|
|
|
|
|
|
|
|
logger.info(f"LocalLlama_ComputeNode task responsed, request: {body}, status-code: {response.status_code}, headers: {response.headers}, content: {response.content}")
|
|
|
|
|
|
|
|
|
|
if response.status_code != 200:
|
|
|
|
|
return {
|
|
|
|
|
"state": ComputeTaskState.ERROR,
|
|
|
|
|
"error": {
|
|
|
|
|
"code": response.status_code,
|
2023-09-20 07:46:42 +00:00
|
|
|
"message": "http request failed: " + str(response.status_code)
|
2023-09-14 15:22:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else:
|
2023-09-17 13:16:21 +00:00
|
|
|
resp = response.json()
|
|
|
|
|
if "error" in resp:
|
2023-09-14 15:22:38 +08:00
|
|
|
return {
|
|
|
|
|
"state": ComputeTaskState.ERROR,
|
|
|
|
|
"error": {
|
2023-09-17 13:16:21 +00:00
|
|
|
"code": resp["error"],
|
|
|
|
|
"message": "local llama failed:" + resp["msg"]
|
2023-09-14 15:22:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
return {
|
2023-09-17 13:16:21 +00:00
|
|
|
"state": ComputeTaskState.DONE,
|
|
|
|
|
"content": str(resp["results"]),
|
|
|
|
|
"message": str(resp["results"])
|
2023-09-14 15:22:38 +08:00
|
|
|
}
|
|
|
|
|
except Exception as err:
|
|
|
|
|
import traceback
|
|
|
|
|
logger.error(f"{traceback.format_exc()}, error: {err}")
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"state": ComputeTaskState.ERROR,
|
|
|
|
|
"error": {
|
|
|
|
|
"code": -1,
|
|
|
|
|
"message": "unknown exception: " + str(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-26 01:19:33 -07:00
|
|
|
async def initial(self) -> bool:
|
|
|
|
|
return True
|
|
|
|
|
|
2023-09-14 15:22:38 +08:00
|
|
|
def display(self) -> str:
|
|
|
|
|
return f"LocalLlama_ComputeNode: {self.node_id}"
|
|
|
|
|
|
|
|
|
|
def get_capacity(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def is_support(self, task: ComputeTask) -> bool:
|
|
|
|
|
return task.task_type == ComputeTaskType.LLM_COMPLETION and (not task.params["model_name"] or task.params["model_name"] == "llama")
|
|
|
|
|
|
|
|
|
|
def is_local(self) -> bool:
|
|
|
|
|
return True
|