mirror of
https://github.com/katanemo/plano.git
synced 2026-06-17 15:25:17 +02:00
rename
This commit is contained in:
parent
13429e4465
commit
fb476d37d5
5 changed files with 76 additions and 67 deletions
|
|
@ -13,6 +13,6 @@ FROM python:3.10-slim AS output
|
|||
COPY --from=builder /runtime /usr/local
|
||||
|
||||
WORKDIR /app
|
||||
COPY /run_stream.py .
|
||||
COPY *.py .
|
||||
|
||||
CMD ["python", "run_stream.py"]
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
import json
|
||||
|
||||
|
||||
ARCH_STATE_HEADER = "x-arch-state"
|
||||
|
||||
|
||||
def get_arch_messages(response_json):
|
||||
arch_messages = []
|
||||
if response_json and "metadata" in response_json:
|
||||
# load arch_state from metadata
|
||||
arch_state_str = response_json.get("metadata", {}).get(ARCH_STATE_HEADER, "{}")
|
||||
# parse arch_state into json object
|
||||
arch_state = json.loads(arch_state_str)
|
||||
# load messages from arch_state
|
||||
arch_messages_str = arch_state.get("messages", "[]")
|
||||
# parse messages into json object
|
||||
arch_messages = json.loads(arch_messages_str)
|
||||
# append messages from arch gateway to history
|
||||
return arch_messages
|
||||
return []
|
||||
72
chatbot_ui/common.py
Normal file
72
chatbot_ui/common.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import json
|
||||
import logging
|
||||
import yaml
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s - %(levelname)s - %(message)s",
|
||||
)
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
ARCH_STATE_HEADER = "x-arch-state"
|
||||
|
||||
|
||||
def get_arch_messages(response_json):
|
||||
arch_messages = []
|
||||
if response_json and "metadata" in response_json:
|
||||
# load arch_state from metadata
|
||||
arch_state_str = response_json.get("metadata", {}).get(ARCH_STATE_HEADER, "{}")
|
||||
# parse arch_state into json object
|
||||
arch_state = json.loads(arch_state_str)
|
||||
# load messages from arch_state
|
||||
arch_messages_str = arch_state.get("messages", "[]")
|
||||
# parse messages into json object
|
||||
arch_messages = json.loads(arch_messages_str)
|
||||
# append messages from arch gateway to history
|
||||
return arch_messages
|
||||
return []
|
||||
|
||||
|
||||
def convert_prompt_target_to_openai_format(target):
|
||||
tool = {
|
||||
"description": target["description"],
|
||||
"parameters": {"type": "object", "properties": {}, "required": []},
|
||||
}
|
||||
|
||||
if "parameters" in target:
|
||||
for param_info in target["parameters"]:
|
||||
parameter = {
|
||||
"type": param_info["type"],
|
||||
"description": param_info["description"],
|
||||
}
|
||||
|
||||
for key in ["default", "format", "enum", "items", "minimum", "maximum"]:
|
||||
if key in param_info:
|
||||
parameter[key] = param_info[key]
|
||||
|
||||
tool["parameters"]["properties"][param_info["name"]] = parameter
|
||||
|
||||
required = param_info.get("required", False)
|
||||
if required:
|
||||
tool["parameters"]["required"].append(param_info["name"])
|
||||
|
||||
return {"name": target["name"], "info": tool}
|
||||
|
||||
|
||||
def get_prompt_targets():
|
||||
try:
|
||||
with open(os.getenv("ARCH_CONFIG", "arch_config.yaml"), "r") as file:
|
||||
config = yaml.safe_load(file)
|
||||
|
||||
available_tools = []
|
||||
for target in config["prompt_targets"]:
|
||||
if not target.get("default", False):
|
||||
available_tools.append(
|
||||
convert_prompt_target_to_openai_format(target)
|
||||
)
|
||||
|
||||
return {tool["name"]: tool["info"] for tool in available_tools}
|
||||
except Exception as e:
|
||||
log.info(e)
|
||||
return None
|
||||
|
|
@ -2,7 +2,7 @@ import json
|
|||
import os
|
||||
import logging
|
||||
import yaml
|
||||
from arch_util import get_arch_messages
|
||||
from common import get_arch_messages
|
||||
import gradio as gr
|
||||
|
||||
from typing import List, Optional, Tuple
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ from typing import List, Optional, Tuple
|
|||
from openai import OpenAI
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from common import get_prompt_targets
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
|
|
@ -40,50 +42,6 @@ client = OpenAI(
|
|||
)
|
||||
|
||||
|
||||
def convert_prompt_target_to_openai_format(target):
|
||||
tool = {
|
||||
"description": target["description"],
|
||||
"parameters": {"type": "object", "properties": {}, "required": []},
|
||||
}
|
||||
|
||||
if "parameters" in target:
|
||||
for param_info in target["parameters"]:
|
||||
parameter = {
|
||||
"type": param_info["type"],
|
||||
"description": param_info["description"],
|
||||
}
|
||||
|
||||
for key in ["default", "format", "enum", "items", "minimum", "maximum"]:
|
||||
if key in param_info:
|
||||
parameter[key] = param_info[key]
|
||||
|
||||
tool["parameters"]["properties"][param_info["name"]] = parameter
|
||||
|
||||
required = param_info.get("required", False)
|
||||
if required:
|
||||
tool["parameters"]["required"].append(param_info["name"])
|
||||
|
||||
return {"name": target["name"], "info": tool}
|
||||
|
||||
|
||||
def get_prompt_targets():
|
||||
try:
|
||||
with open(os.getenv("ARCH_CONFIG", "arch_config.yaml"), "r") as file:
|
||||
config = yaml.safe_load(file)
|
||||
|
||||
available_tools = []
|
||||
for target in config["prompt_targets"]:
|
||||
if not target.get("default", False):
|
||||
available_tools.append(
|
||||
convert_prompt_target_to_openai_format(target)
|
||||
)
|
||||
|
||||
return {tool["name"]: tool["info"] for tool in available_tools}
|
||||
except Exception as e:
|
||||
log.info(e)
|
||||
return None
|
||||
|
||||
|
||||
def chat(query: Optional[str], conversation: Optional[List[Tuple[str, str]]], state):
|
||||
if "history" not in state:
|
||||
state["history"] = []
|
||||
|
|
@ -105,7 +63,6 @@ def chat(query: Optional[str], conversation: Optional[List[Tuple[str, str]]], st
|
|||
log.info("Error calling gateway API: {}".format(e))
|
||||
raise gr.Error("Error calling gateway API: {}".format(e))
|
||||
|
||||
history.append({"role": "assistant", "content": "", "model": ""})
|
||||
conversation.append((query, ""))
|
||||
|
||||
for chunk in response:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue