Pass tool call and app function response back in metadata

This commit is contained in:
Adil Hafeez 2024-10-17 17:05:35 -07:00
parent 6cd05572c4
commit 169bf8d4ea
8 changed files with 160 additions and 108 deletions

View file

@ -5,6 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"python": "${workspaceFolder}/venv/bin/python",
"name": "chatbot-ui",
"cwd": "${workspaceFolder}/app",
"type": "debugpy",

View file

@ -2,14 +2,21 @@ import json
import os
from openai import OpenAI, DefaultHttpxClient
import gradio as gr
import logging as log
import logging
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
log = logging.getLogger(__name__)
CHAT_COMPLETION_ENDPOINT = os.getenv("CHAT_COMPLETION_ENDPOINT")
ARCH_STATE_HEADER = "x-arch-state"
log.info("CHAT_COMPLETION_ENDPOINT: ", CHAT_COMPLETION_ENDPOINT)
log.info(f"CHAT_COMPLETION_ENDPOINT: {CHAT_COMPLETION_ENDPOINT}")
client = OpenAI(
api_key="--",
@ -23,23 +30,19 @@ def predict(message, state):
state["history"] = []
history = state.get("history")
history.append({"role": "user", "content": message})
log.info("history: ", history)
log.info(f"history: {history}")
# Custom headers
custom_headers = {
"x-arch-deterministic-provider": "openai",
}
metadata = None
if "arch_state" in state:
metadata = {ARCH_STATE_HEADER: state["arch_state"]}
try:
raw_response = client.chat.completions.with_raw_response.create(
model="--",
messages=history,
temperature=1.0,
metadata=metadata,
# metadata=metadata,
extra_headers=custom_headers,
)
except Exception as e:
@ -49,26 +52,34 @@ def predict(message, state):
log.info("Error calling gateway API: {}".format(e.message))
raise gr.Error("Error calling gateway API: {}".format(e.message))
log.info("raw_response: ", raw_response.text)
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)
arch_state = None
arch_state = {}
if response_json:
metadata = response_json.get("metadata", {})
if metadata:
arch_state = metadata.get(ARCH_STATE_HEADER, None)
arch_state_str = metadata.get(ARCH_STATE_HEADER, "{}")
arch_state = json.loads(arch_state_str)
if arch_state:
state["arch_state"] = arch_state
arch_messages_str = arch_state.get("messages", "[]")
arch_messages = json.loads(arch_messages_str)
content = response.choices[0].message.content
for message in arch_messages:
# arch_msg = {"role": message["role"], "content": message.get("content", None), "model": message.get("model", None), "tool_calls": message.get("tool_calls", None), "tool_call_id": message.get("tool_call_id", None)}
history.append(message)
history.append({"role": "assistant", "content": content, "model": response.model})
history_view = [h for h in history if h["role"] != "tool" and "content" in h]
messages = [
(history[i]["content"], history[i + 1]["content"])
for i in range(0, len(history) - 1, 2)
(history_view[i]["content"], history_view[i + 1]["content"])
for i in range(0, len(history_view) - 1, 2)
]
return messages, state