Update app.py
Browse files
app.py
CHANGED
|
@@ -8,34 +8,52 @@ from langchain_core.runnables import RunnablePassthrough, chain
|
|
| 8 |
def create_dynamic_chain(api_key):
|
| 9 |
llm = ChatOpenAI(model="gpt-4o-mini", api_key=api_key)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
("
|
| 14 |
("human", "{question}")
|
| 15 |
])
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
if not api_key:
|
| 29 |
return "", [{"role": "assistant", "content": "Please enter your OpenAI API key."}]
|
| 30 |
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"chat_history": chat_history
|
| 38 |
-
})
|
| 39 |
|
| 40 |
history.append({"role": "user", "content": message})
|
| 41 |
history.append({"role": "assistant", "content": response})
|
|
@@ -44,24 +62,52 @@ def process_message(message, history, api_key):
|
|
| 44 |
except Exception as e:
|
| 45 |
return "", history + [{"role": "assistant", "content": f"Error: {str(e)}"}]
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
-
gr.Markdown("# Dynamic Chain Demo")
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
chatbot = gr.Chatbot(type="messages")
|
| 57 |
-
msg = gr.Textbox(label="Message")
|
| 58 |
clear = gr.ClearButton([msg, chatbot])
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
msg.submit(
|
| 61 |
process_message,
|
| 62 |
-
inputs=[msg, chatbot, api_key],
|
| 63 |
outputs=[msg, chatbot]
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
demo.launch()
|
|
|
|
| 8 |
def create_dynamic_chain(api_key):
|
| 9 |
llm = ChatOpenAI(model="gpt-4o-mini", api_key=api_key)
|
| 10 |
|
| 11 |
+
# Chain for general questions
|
| 12 |
+
general_prompt = ChatPromptTemplate.from_messages([
|
| 13 |
+
("system", "You are a helpful assistant that provides direct answers."),
|
| 14 |
("human", "{question}")
|
| 15 |
])
|
| 16 |
|
| 17 |
+
# Chain for mathematical calculations
|
| 18 |
+
math_prompt = ChatPromptTemplate.from_messages([
|
| 19 |
+
("system", "You are a mathematical assistant. Solve the problem and show your work."),
|
| 20 |
+
("human", "{question}")
|
| 21 |
+
])
|
| 22 |
|
| 23 |
+
# Chain for coding questions
|
| 24 |
+
code_prompt = ChatPromptTemplate.from_messages([
|
| 25 |
+
("system", "You are a coding assistant. Provide code examples and explanations."),
|
| 26 |
+
("human", "{question}")
|
| 27 |
+
])
|
| 28 |
+
|
| 29 |
+
general_chain = general_prompt | llm | StrOutputParser()
|
| 30 |
+
math_chain = math_prompt | llm | StrOutputParser()
|
| 31 |
+
code_chain = code_prompt | llm | StrOutputParser()
|
| 32 |
|
| 33 |
+
@chain
|
| 34 |
+
def dynamic_chain(input_dict):
|
| 35 |
+
question = input_dict["question"].lower()
|
| 36 |
+
|
| 37 |
+
# Detect question type
|
| 38 |
+
if any(word in question for word in ["calculate", "solve", "compute", "sum", "multiply"]):
|
| 39 |
+
return math_chain
|
| 40 |
+
elif any(word in question for word in ["code", "program", "function", "python", "javascript"]):
|
| 41 |
+
return code_chain
|
| 42 |
+
return general_chain
|
| 43 |
|
| 44 |
+
return dynamic_chain
|
| 45 |
+
|
| 46 |
+
def process_message(message, history, api_key, example_select):
|
| 47 |
if not api_key:
|
| 48 |
return "", [{"role": "assistant", "content": "Please enter your OpenAI API key."}]
|
| 49 |
|
| 50 |
try:
|
| 51 |
+
# Handle example selection
|
| 52 |
+
if example_select != "Custom Input":
|
| 53 |
+
message = EXAMPLES[example_select]
|
| 54 |
|
| 55 |
+
chain = create_dynamic_chain(api_key)
|
| 56 |
+
response = chain.invoke({"question": message})
|
|
|
|
|
|
|
| 57 |
|
| 58 |
history.append({"role": "user", "content": message})
|
| 59 |
history.append({"role": "assistant", "content": response})
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
return "", history + [{"role": "assistant", "content": f"Error: {str(e)}"}]
|
| 64 |
|
| 65 |
+
# Example questions for different chain types
|
| 66 |
+
EXAMPLES = {
|
| 67 |
+
"General Question": "What are the main features of renewable energy?",
|
| 68 |
+
"Math Problem": "Calculate the area of a circle with radius 5 units.",
|
| 69 |
+
"Coding Question": "Write a Python function to find the factorial of a number.",
|
| 70 |
+
"Custom Input": ""
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
# Gradio Interface
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
+
gr.Markdown("# Dynamic Chain Demo with Examples")
|
| 76 |
|
| 77 |
+
with gr.Row():
|
| 78 |
+
api_key = gr.Textbox(
|
| 79 |
+
label="OpenAI API Key",
|
| 80 |
+
placeholder="Enter your OpenAI API key",
|
| 81 |
+
type="password"
|
| 82 |
+
)
|
| 83 |
+
example_select = gr.Dropdown(
|
| 84 |
+
choices=list(EXAMPLES.keys()),
|
| 85 |
+
value="Custom Input",
|
| 86 |
+
label="Select Example"
|
| 87 |
+
)
|
| 88 |
|
| 89 |
chatbot = gr.Chatbot(type="messages")
|
| 90 |
+
msg = gr.Textbox(label="Message", placeholder="Type your message or select an example above")
|
| 91 |
clear = gr.ClearButton([msg, chatbot])
|
| 92 |
|
| 93 |
+
# Example descriptions
|
| 94 |
+
gr.Markdown("""
|
| 95 |
+
## Example Types:
|
| 96 |
+
1. **General Questions**: Regular queries that don't require special processing
|
| 97 |
+
2. **Math Problems**: Questions involving calculations and mathematical operations
|
| 98 |
+
3. **Coding Questions**: Programming-related queries that return code examples
|
| 99 |
+
|
| 100 |
+
## Try these patterns:
|
| 101 |
+
- Math: "Calculate...", "Solve...", "Compute..."
|
| 102 |
+
- Code: "Write a function...", "Program...", "Code..."
|
| 103 |
+
- General: Any other type of question
|
| 104 |
+
""")
|
| 105 |
+
|
| 106 |
msg.submit(
|
| 107 |
process_message,
|
| 108 |
+
inputs=[msg, chatbot, api_key, example_select],
|
| 109 |
outputs=[msg, chatbot]
|
| 110 |
)
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
+
demo.launch()
|