2024-10-05 19:25:16 -07:00
|
|
|
import json
|
2024-07-30 16:23:23 -07:00
|
|
|
import os
|
2024-10-18 13:25:39 -07:00
|
|
|
import logging
|
2024-10-24 14:11:53 -07:00
|
|
|
import yaml
|
2024-10-29 00:10:11 -07:00
|
|
|
from common import get_arch_messages, get_prompt_targets
|
2024-10-24 14:11:53 -07:00
|
|
|
import gradio as gr
|
|
|
|
|
|
|
|
|
|
from typing import List, Optional, Tuple
|
2024-10-28 20:05:06 -04:00
|
|
|
from openai import OpenAI
|
2024-09-19 17:48:50 -07:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
load_dotenv()
|
2024-09-10 14:24:46 -07:00
|
|
|
|
2024-10-28 20:05:06 -04:00
|
|
|
|
2024-10-18 13:25:39 -07:00
|
|
|
logging.basicConfig(
|
|
|
|
|
level=logging.INFO,
|
|
|
|
|
format="%(asctime)s - %(levelname)s - %(message)s",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
2024-09-19 17:48:50 -07:00
|
|
|
CHAT_COMPLETION_ENDPOINT = os.getenv("CHAT_COMPLETION_ENDPOINT")
|
2024-10-18 13:25:39 -07:00
|
|
|
log.info(f"CHAT_COMPLETION_ENDPOINT: {CHAT_COMPLETION_ENDPOINT}")
|
2024-09-20 14:38:10 -07:00
|
|
|
|
2024-10-24 14:11:53 -07:00
|
|
|
|
|
|
|
|
CSS_STYLE = """
|
|
|
|
|
.json-container {
|
|
|
|
|
height: 95vh !important;
|
|
|
|
|
overflow-y: auto !important;
|
|
|
|
|
}
|
|
|
|
|
.chatbot {
|
|
|
|
|
height: calc(95vh - 100px) !important;
|
|
|
|
|
overflow-y: auto !important;
|
|
|
|
|
}
|
|
|
|
|
footer {visibility: hidden}
|
|
|
|
|
"""
|
|
|
|
|
|
2024-10-09 11:25:07 -07:00
|
|
|
client = OpenAI(
|
2024-10-09 15:47:32 -07:00
|
|
|
api_key="--",
|
2024-10-09 11:25:07 -07:00
|
|
|
base_url=CHAT_COMPLETION_ENDPOINT,
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-31 15:51:31 -07:00
|
|
|
|
2024-10-28 21:16:53 -07:00
|
|
|
def chat(query: Optional[str], conversation: Optional[List[Tuple[str, str]]], state):
|
2024-10-09 11:25:07 -07:00
|
|
|
if "history" not in state:
|
|
|
|
|
state["history"] = []
|
2024-10-24 14:11:53 -07:00
|
|
|
|
2024-10-05 19:25:16 -07:00
|
|
|
history = state.get("history")
|
2024-10-24 14:11:53 -07:00
|
|
|
history.append({"role": "user", "content": query})
|
2024-10-18 13:25:39 -07:00
|
|
|
log.info(f"history: {history}")
|
2024-08-06 23:40:06 -07:00
|
|
|
|
2024-07-30 16:23:23 -07:00
|
|
|
try:
|
2024-10-09 11:25:07 -07:00
|
|
|
raw_response = client.chat.completions.with_raw_response.create(
|
2024-10-09 15:47:32 -07:00
|
|
|
model="--",
|
2024-10-09 11:25:07 -07:00
|
|
|
messages=history,
|
|
|
|
|
temperature=1.0,
|
|
|
|
|
)
|
2024-09-19 12:19:14 -07:00
|
|
|
except Exception as e:
|
2024-10-09 11:25:07 -07:00
|
|
|
history.pop()
|
2024-10-28 23:43:08 -07:00
|
|
|
# remove last user message in case of exception
|
|
|
|
|
log.error("Error calling gateway API: {}".format(e))
|
2024-10-28 20:05:06 -04:00
|
|
|
raise gr.Error("Error calling gateway API: {}".format(e))
|
|
|
|
|
|
2024-10-28 23:43:08 -07:00
|
|
|
log.error(f"raw_response: {raw_response.text}")
|
|
|
|
|
response = raw_response.parse()
|
|
|
|
|
|
|
|
|
|
# extract arch_state from metadata and store it in gradio session state
|
|
|
|
|
# this state must be passed back to the gateway in the next request
|
|
|
|
|
response_json = json.loads(raw_response.text)
|
|
|
|
|
log.info(response_json)
|
|
|
|
|
|
|
|
|
|
arch_messages = get_arch_messages(response_json)
|
|
|
|
|
for arch_message in arch_messages:
|
|
|
|
|
history.append(arch_message)
|
|
|
|
|
|
|
|
|
|
content = response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
history.append({"role": "assistant", "content": content, "model": response.model})
|
2024-10-28 20:05:06 -04:00
|
|
|
|
2024-10-28 23:43:08 -07:00
|
|
|
# for gradio UI we don't want to show raw tool calls and messages from developer application
|
|
|
|
|
# so we're filtering those out
|
|
|
|
|
history_view = [h for h in history if h["role"] != "tool" and "content" in h]
|
2024-10-28 20:05:06 -04:00
|
|
|
|
2024-10-28 23:43:08 -07:00
|
|
|
conversation = [
|
|
|
|
|
(history_view[i]["content"], history_view[i + 1]["content"])
|
|
|
|
|
for i in range(0, len(history_view) - 1, 2)
|
|
|
|
|
]
|
2024-10-28 20:05:06 -04:00
|
|
|
|
2024-10-28 23:43:08 -07:00
|
|
|
yield "", conversation, state
|
2024-10-24 14:11:53 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
with gr.Blocks(
|
|
|
|
|
theme=gr.themes.Default(
|
|
|
|
|
font_mono=[gr.themes.GoogleFont("IBM Plex Mono"), "Arial", "sans-serif"]
|
|
|
|
|
),
|
|
|
|
|
fill_height=True,
|
|
|
|
|
css=CSS_STYLE,
|
|
|
|
|
) as demo:
|
|
|
|
|
with gr.Row(equal_height=True):
|
|
|
|
|
state = gr.State({})
|
|
|
|
|
|
|
|
|
|
with gr.Column(scale=4):
|
|
|
|
|
gr.JSON(
|
|
|
|
|
value=get_prompt_targets(),
|
|
|
|
|
open=True,
|
|
|
|
|
show_indices=False,
|
|
|
|
|
label="Available Tools",
|
|
|
|
|
scale=1,
|
|
|
|
|
min_height="95vh",
|
|
|
|
|
elem_classes="json-container",
|
|
|
|
|
)
|
|
|
|
|
with gr.Column(scale=6):
|
|
|
|
|
chatbot = gr.Chatbot(
|
|
|
|
|
label="Arch Chatbot",
|
|
|
|
|
scale=1,
|
|
|
|
|
elem_classes="chatbot",
|
|
|
|
|
)
|
|
|
|
|
textbox = gr.Textbox(
|
|
|
|
|
show_label=False,
|
|
|
|
|
placeholder="Enter text and press enter",
|
|
|
|
|
scale=1,
|
|
|
|
|
autofocus=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
textbox.submit(chat, [textbox, chatbot, state], [textbox, chatbot, state])
|
|
|
|
|
|
|
|
|
|
demo.launch(server_name="0.0.0.0", server_port=8080, show_error=True, debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|