trying out the LLM interface
Browse files
app.py
CHANGED
|
@@ -12,14 +12,15 @@ model_reply_with_graph = pipeline('text2text-generation', model='google/flan-t5-
|
|
| 12 |
def generate_input_graph(request_text):
|
| 13 |
"""
|
| 14 |
Model 1: Convert the customer request into a structured graph.
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
prompt = (
|
| 18 |
"You are an assistant that converts a natural language customer request into a structured graph. "
|
| 19 |
-
"Output only valid JSON with exactly two keys: 'nodes' and 'edges'.
|
| 20 |
-
"
|
| 21 |
-
|
| 22 |
-
"
|
|
|
|
| 23 |
f"Customer Request: \"{request_text}\"\n\n"
|
| 24 |
"Structured Graph JSON:"
|
| 25 |
)
|
|
@@ -29,13 +30,14 @@ def generate_input_graph(request_text):
|
|
| 29 |
def generate_reply_and_graph(request_text):
|
| 30 |
"""
|
| 31 |
Model 2: Generate a detailed response along with a structured graph.
|
| 32 |
-
|
| 33 |
"""
|
| 34 |
prompt = (
|
| 35 |
-
"You are an assistant that responds to a customer request with a detailed and structured
|
| 36 |
-
"First, provide a helpful textual reply to the request. Then
|
| 37 |
-
"
|
| 38 |
-
"
|
|
|
|
| 39 |
"Do not include any extra text or commentary outside the JSON.\n\n"
|
| 40 |
f"Customer Request: \"{request_text}\"\n\n"
|
| 41 |
"Detailed Response and Structured Graph JSON:"
|
|
@@ -48,6 +50,7 @@ def try_parse_json(text):
|
|
| 48 |
Attempt to extract a valid JSON substring from the model output using a non-greedy match.
|
| 49 |
Returns the parsed JSON if valid, otherwise logs the error and returns None.
|
| 50 |
"""
|
|
|
|
| 51 |
# Non-greedy match for JSON object
|
| 52 |
json_pattern = re.compile(r'\{.*?\}', re.DOTALL)
|
| 53 |
match = json_pattern.search(text)
|
|
@@ -96,7 +99,7 @@ def main():
|
|
| 96 |
st.title("Two-LLM Pipeline: Request & Reply with Graph Visualization")
|
| 97 |
st.write(
|
| 98 |
"Enter a customer request to see it transformed into an input graph and then receive a detailed reply "
|
| 99 |
-
"with its own structured graph. Both graphs are built using non-empty 'nodes' and 'edges'
|
| 100 |
)
|
| 101 |
|
| 102 |
customer_request = st.text_area("Enter Customer Request:", height=150)
|
|
@@ -121,7 +124,6 @@ def main():
|
|
| 121 |
# --- Stage 2: Detailed Reply with Structured Graph ---
|
| 122 |
st.subheader("Stage 2: Detailed Response with Structured Graph")
|
| 123 |
model2_output = generate_reply_and_graph(customer_request)
|
| 124 |
-
st.write("Detailed Response and Graph (Raw Output):")
|
| 125 |
st.code(model2_output, language="json")
|
| 126 |
output_graph_data = try_parse_json(model2_output)
|
| 127 |
if output_graph_data:
|
|
@@ -133,5 +135,3 @@ def main():
|
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
main()
|
| 136 |
-
|
| 137 |
-
|
|
|
|
| 12 |
def generate_input_graph(request_text):
|
| 13 |
"""
|
| 14 |
Model 1: Convert the customer request into a structured graph.
|
| 15 |
+
The prompt now includes an explicit example to guide the model.
|
| 16 |
"""
|
| 17 |
prompt = (
|
| 18 |
"You are an assistant that converts a natural language customer request into a structured graph. "
|
| 19 |
+
"Output only valid JSON with exactly two keys: 'nodes' and 'edges'.\n"
|
| 20 |
+
"Example output:\n"
|
| 21 |
+
'{"nodes": ["City A", "City B", "City C", "City D", "City E"], '
|
| 22 |
+
'"edges": [["City A", "City B"], ["City B", "City C"], ["City C", "City D"], ["City D", "City E"]]} \n\n'
|
| 23 |
+
"Do not include any extra text, explanation, or commentary.\n\n"
|
| 24 |
f"Customer Request: \"{request_text}\"\n\n"
|
| 25 |
"Structured Graph JSON:"
|
| 26 |
)
|
|
|
|
| 30 |
def generate_reply_and_graph(request_text):
|
| 31 |
"""
|
| 32 |
Model 2: Generate a detailed response along with a structured graph.
|
| 33 |
+
The prompt now includes an explicit example of the expected JSON format.
|
| 34 |
"""
|
| 35 |
prompt = (
|
| 36 |
+
"You are an assistant that responds to a customer request with a detailed reply and a structured graph. "
|
| 37 |
+
"First, provide a helpful textual reply to the request. Then output a valid JSON object with exactly two keys: 'nodes' and 'edges'.\n"
|
| 38 |
+
"Example JSON output:\n"
|
| 39 |
+
'{"nodes": ["City A", "City B", "City C", "City D", "City E"], '
|
| 40 |
+
'"edges": [["City A", "City B"], ["City B", "City C"], ["City C", "City D"], ["City D", "City E"]]} \n\n'
|
| 41 |
"Do not include any extra text or commentary outside the JSON.\n\n"
|
| 42 |
f"Customer Request: \"{request_text}\"\n\n"
|
| 43 |
"Detailed Response and Structured Graph JSON:"
|
|
|
|
| 50 |
Attempt to extract a valid JSON substring from the model output using a non-greedy match.
|
| 51 |
Returns the parsed JSON if valid, otherwise logs the error and returns None.
|
| 52 |
"""
|
| 53 |
+
st.write("Raw model output:", text)
|
| 54 |
# Non-greedy match for JSON object
|
| 55 |
json_pattern = re.compile(r'\{.*?\}', re.DOTALL)
|
| 56 |
match = json_pattern.search(text)
|
|
|
|
| 99 |
st.title("Two-LLM Pipeline: Request & Reply with Graph Visualization")
|
| 100 |
st.write(
|
| 101 |
"Enter a customer request to see it transformed into an input graph and then receive a detailed reply "
|
| 102 |
+
"with its own structured graph. Both graphs are built using non-empty 'nodes' and 'edges'."
|
| 103 |
)
|
| 104 |
|
| 105 |
customer_request = st.text_area("Enter Customer Request:", height=150)
|
|
|
|
| 124 |
# --- Stage 2: Detailed Reply with Structured Graph ---
|
| 125 |
st.subheader("Stage 2: Detailed Response with Structured Graph")
|
| 126 |
model2_output = generate_reply_and_graph(customer_request)
|
|
|
|
| 127 |
st.code(model2_output, language="json")
|
| 128 |
output_graph_data = try_parse_json(model2_output)
|
| 129 |
if output_graph_data:
|
|
|
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
main()
|
|
|
|
|
|