Spaces:
Running
Running
Debug error
Browse files
app.py
CHANGED
|
@@ -1,6 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
from openai import OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
title = None # "ServiceNow-AI Chat" # modelConfig.get('MODE_DISPLAY_NAME')
|
| 6 |
description = None
|
|
@@ -20,23 +31,63 @@ client = OpenAI(
|
|
| 20 |
)
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def chat_fn(message, history):
|
| 24 |
-
|
|
|
|
| 25 |
print(f"Original History: {history}")
|
|
|
|
| 26 |
history = [item for item in history if
|
| 27 |
not (isinstance(item, dict) and
|
| 28 |
item.get("role") == "assistant" and
|
| 29 |
isinstance(item.get("metadata"), dict) and
|
| 30 |
item.get("metadata", {}).get("title") is not None)]
|
| 31 |
print(f"Updated History: {history}")
|
|
|
|
| 32 |
|
| 33 |
-
messages = history + [{"role": "user", "content": message}]
|
| 34 |
-
print(f"Messages: {messages}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Create the streaming response
|
| 37 |
stream = client.chat.completions.create(
|
| 38 |
model=model_config.get('MODEL_NAME'),
|
| 39 |
-
messages=
|
| 40 |
temperature=0.8,
|
| 41 |
stream=True
|
| 42 |
)
|
|
@@ -46,6 +97,8 @@ def chat_fn(message, history):
|
|
| 46 |
content="Thinking...",
|
| 47 |
metadata={"title": "🧠 Thought"}
|
| 48 |
))
|
|
|
|
|
|
|
| 49 |
|
| 50 |
output = ""
|
| 51 |
completion_started = False
|
|
@@ -81,8 +134,12 @@ def chat_fn(message, history):
|
|
| 81 |
|
| 82 |
# only yield the most recent assistant messages
|
| 83 |
messages_to_yield = history[-1:] if not completion_started else history[-2:]
|
|
|
|
| 84 |
yield messages_to_yield
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Add the model display name and Hugging Face URL to the description
|
| 88 |
# description = f"### Model: [{MODE_DISPLAY_NAME}]({MODEL_HF_URL})"
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
from openai import OpenAI
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from gradio.components.chatbot import ChatMessage, Message
|
| 7 |
+
from typing import (
|
| 8 |
+
TYPE_CHECKING,
|
| 9 |
+
Any,
|
| 10 |
+
Literal,
|
| 11 |
+
Optional,
|
| 12 |
+
Union,
|
| 13 |
+
cast,
|
| 14 |
+
)
|
| 15 |
|
| 16 |
title = None # "ServiceNow-AI Chat" # modelConfig.get('MODE_DISPLAY_NAME')
|
| 17 |
description = None
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
|
| 34 |
+
def _check_format(messages: Any, type: Literal["messages", "tuples"] = "messages") -> None:
|
| 35 |
+
if type == "messages":
|
| 36 |
+
all_valid = all(
|
| 37 |
+
isinstance(message, dict)
|
| 38 |
+
and "role" in message
|
| 39 |
+
and "content" in message
|
| 40 |
+
or isinstance(message, ChatMessage | Message)
|
| 41 |
+
for message in messages
|
| 42 |
+
)
|
| 43 |
+
if not all_valid:
|
| 44 |
+
# Display which message is not valid
|
| 45 |
+
for i, message in enumerate(messages):
|
| 46 |
+
if not (isinstance(message, dict) and
|
| 47 |
+
"role" in message and
|
| 48 |
+
"content" in message) and not isinstance(message, ChatMessage | Message):
|
| 49 |
+
print(f"_check_format() --> Invalid message at index {i}: {message}\n", file=sys.stderr)
|
| 50 |
+
break
|
| 51 |
+
|
| 52 |
+
raise Exception(
|
| 53 |
+
"Data incompatible with messages format. Each message should be a dictionary with 'role' and 'content' keys or a ChatMessage object."
|
| 54 |
+
)
|
| 55 |
+
else:
|
| 56 |
+
print("_check_format() --> All messages are valid.")
|
| 57 |
+
elif not all(
|
| 58 |
+
isinstance(message, (tuple, list)) and len(message) == 2
|
| 59 |
+
for message in messages
|
| 60 |
+
):
|
| 61 |
+
raise Exception(
|
| 62 |
+
"Data incompatible with tuples format. Each message should be a list of length 2."
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
def chat_fn(message, history):
|
| 67 |
+
print(f"{'-' * 80}\nchat_fn() --> Message: {message}")
|
| 68 |
+
# Remove any assistant messages with metadata from history for multiple turns
|
| 69 |
print(f"Original History: {history}")
|
| 70 |
+
_check_format(history, "messages")
|
| 71 |
history = [item for item in history if
|
| 72 |
not (isinstance(item, dict) and
|
| 73 |
item.get("role") == "assistant" and
|
| 74 |
isinstance(item.get("metadata"), dict) and
|
| 75 |
item.get("metadata", {}).get("title") is not None)]
|
| 76 |
print(f"Updated History: {history}")
|
| 77 |
+
_check_format(history, "messages")
|
| 78 |
|
| 79 |
+
# messages = history + [{"role": "user", "content": message}]
|
| 80 |
+
# print(f"Messages: {messages}")
|
| 81 |
+
# _check_format(messages, "messages")
|
| 82 |
+
|
| 83 |
+
history.append({"role": "user", "content": message})
|
| 84 |
+
print(f"History with user message: {history}")
|
| 85 |
+
_check_format(history, "messages")
|
| 86 |
|
| 87 |
# Create the streaming response
|
| 88 |
stream = client.chat.completions.create(
|
| 89 |
model=model_config.get('MODEL_NAME'),
|
| 90 |
+
messages=history,
|
| 91 |
temperature=0.8,
|
| 92 |
stream=True
|
| 93 |
)
|
|
|
|
| 97 |
content="Thinking...",
|
| 98 |
metadata={"title": "🧠 Thought"}
|
| 99 |
))
|
| 100 |
+
print(f"History added thinking: {history}")
|
| 101 |
+
_check_format(history, "messages")
|
| 102 |
|
| 103 |
output = ""
|
| 104 |
completion_started = False
|
|
|
|
| 134 |
|
| 135 |
# only yield the most recent assistant messages
|
| 136 |
messages_to_yield = history[-1:] if not completion_started else history[-2:]
|
| 137 |
+
# _check_format(messages_to_yield, "messages")
|
| 138 |
yield messages_to_yield
|
| 139 |
|
| 140 |
+
print(f"Final History: {history}")
|
| 141 |
+
_check_format(history, "messages")
|
| 142 |
+
|
| 143 |
|
| 144 |
# Add the model display name and Hugging Face URL to the description
|
| 145 |
# description = f"### Model: [{MODE_DISPLAY_NAME}]({MODEL_HF_URL})"
|