Update app.py
Browse files
app.py
CHANGED
|
@@ -45,48 +45,58 @@ hf_pipeline = pipeline(
|
|
| 45 |
)
|
| 46 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 47 |
|
| 48 |
-
# 3.
|
| 49 |
-
unified_prompt_template = """
|
| 50 |
|
| 51 |
-
|
| 52 |
-
{context}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
2. If the User Input is a general question, greeting, or unrelated to UPF, respond normally as a helpful AI assistant. Ignore the UPF Context and do not generate code.
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
unified_prompt = PromptTemplate.from_template(unified_prompt_template)
|
| 66 |
document_chain = create_stuff_documents_chain(llm, unified_prompt)
|
| 67 |
rag_chain = create_retrieval_chain(retriever, document_chain)
|
| 68 |
|
| 69 |
# 4. Gradio Interface & Handling
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def format_history(history):
|
| 71 |
if not history:
|
| 72 |
return "No previous conversation."
|
| 73 |
-
return "\n".join([f"{msg['role'].capitalize()}: {msg['content']}" for msg in history])
|
| 74 |
|
| 75 |
def user_interaction(user_message, history):
|
| 76 |
history = history or []
|
|
|
|
| 77 |
|
| 78 |
response = rag_chain.invoke({
|
| 79 |
-
"input":
|
| 80 |
"chat_history": format_history(history)
|
| 81 |
})
|
| 82 |
|
| 83 |
-
answer = response['answer'].strip()
|
| 84 |
|
| 85 |
upf_syntax_hints = ["create_power_domain", "set_isolation", "set_retention", "create_supply_port", "create_power_switch"]
|
| 86 |
if "```" not in answer and any(kw in answer.lower() for kw in upf_syntax_hints):
|
| 87 |
answer = f"```tcl\n{answer}\n```"
|
| 88 |
|
| 89 |
-
history.append({"role": "user", "content":
|
| 90 |
history.append({"role": "assistant", "content": answer})
|
| 91 |
return history, ""
|
| 92 |
|
|
|
|
| 45 |
)
|
| 46 |
llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
| 47 |
|
| 48 |
+
# 3. Llama-3 Optimized RAG Chain
|
| 49 |
+
unified_prompt_template = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
| 50 |
|
| 51 |
+
You are a highly capable AI assistant specializing in Unified Power Format (UPF 3.0) and VLSI power intent design.
|
|
|
|
| 52 |
|
| 53 |
+
Instructions:
|
| 54 |
+
1. If the User Input asks for UPF code or power intent, act as an expert UPF engineer. Use the Context to generate precise UPF 3.0 code, enclosed in ```tcl ... ``` blocks.
|
| 55 |
+
2. If the User Input is a general question or greeting, respond conversationally and concisely. Ignore the Context if it is not relevant.
|
| 56 |
|
| 57 |
+
Context:
|
| 58 |
+
{context}<|eot_id|><|start_header_id|>user<|end_header_id|>
|
| 59 |
|
| 60 |
+
Conversation History:
|
| 61 |
+
{chat_history}
|
|
|
|
| 62 |
|
| 63 |
+
User Input: {input}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
| 64 |
+
"""
|
| 65 |
|
| 66 |
unified_prompt = PromptTemplate.from_template(unified_prompt_template)
|
| 67 |
document_chain = create_stuff_documents_chain(llm, unified_prompt)
|
| 68 |
rag_chain = create_retrieval_chain(retriever, document_chain)
|
| 69 |
|
| 70 |
# 4. Gradio Interface & Handling
|
| 71 |
+
def get_text_content(msg_content):
|
| 72 |
+
"""Safety wrapper to ensure Gradio doesn't accidentally pass JSON dicts to the LLM"""
|
| 73 |
+
if isinstance(msg_content, str):
|
| 74 |
+
return msg_content
|
| 75 |
+
elif isinstance(msg_content, list):
|
| 76 |
+
return " ".join([item.get('text', '') for item in msg_content if isinstance(item, dict) and 'text' in item])
|
| 77 |
+
return str(msg_content)
|
| 78 |
+
|
| 79 |
def format_history(history):
|
| 80 |
if not history:
|
| 81 |
return "No previous conversation."
|
| 82 |
+
return "\n".join([f"{msg['role'].capitalize()}: {get_text_content(msg['content'])}" for msg in history])
|
| 83 |
|
| 84 |
def user_interaction(user_message, history):
|
| 85 |
history = history or []
|
| 86 |
+
user_text = get_text_content(user_message)
|
| 87 |
|
| 88 |
response = rag_chain.invoke({
|
| 89 |
+
"input": user_text,
|
| 90 |
"chat_history": format_history(history)
|
| 91 |
})
|
| 92 |
|
| 93 |
+
answer = response['answer'].replace("<|eot_id|>", "").strip()
|
| 94 |
|
| 95 |
upf_syntax_hints = ["create_power_domain", "set_isolation", "set_retention", "create_supply_port", "create_power_switch"]
|
| 96 |
if "```" not in answer and any(kw in answer.lower() for kw in upf_syntax_hints):
|
| 97 |
answer = f"```tcl\n{answer}\n```"
|
| 98 |
|
| 99 |
+
history.append({"role": "user", "content": user_text})
|
| 100 |
history.append({"role": "assistant", "content": answer})
|
| 101 |
return history, ""
|
| 102 |
|