DenverJones commited on
Commit
91496ca
·
verified ·
1 Parent(s): aea1742

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -48
app.py CHANGED
@@ -1,34 +1,24 @@
1
  import os
2
  import yaml
3
  import gradio as gr
4
- from langchain.agents import initialize_agent, Tool
5
  from langchain.prompts import PromptTemplate
6
- from langchain_huggingface import HuggingFaceEndpoint # Correct import
 
7
 
8
  # Load environment variables
9
- HF_API_KEY = os.environ.get("HUGGINGFACEHUB_API_TOKEN") # Correct environment variable name
10
  HF_MODEL_ID = os.environ.get("HF_MODEL_ID", "google/flan-t5-xxl")
11
 
12
  # Load prompt templates
13
  with open("prompts.yaml", 'r') as stream:
14
  all_prompts = yaml.safe_load(stream)
15
- prompt_templates = all_prompts["prompts"] # Access the nested 'prompts' dictionary
16
-
17
- # Now you can access prompt_templates["default"], prompt_templates["system_prompt"], etc.
18
- prompt_template = PromptTemplate(
19
- input_variables=["user_input", "tool_names", "tools"],
20
- template=prompt_templates["default"] # This will now work!
21
- )
22
-
23
 
24
- # Define the get_motivational_quote tool
25
  def get_motivational_quote():
26
  quotes = [
27
- "Believe you can and you're halfway there.",
28
- "Your limitation—it's only your imagination.",
29
- "Push yourself, because no one else is going to do it for you.",
30
- "Great things never come from comfort zones.",
31
- "Dream it. Wish it. Do it."
32
  ]
33
  import random
34
  return random.choice(quotes)
@@ -37,13 +27,11 @@ tools = [
37
  Tool(
38
  name="MotivationalQuote",
39
  func=get_motivational_quote,
40
- description="Use this tool to get a motivational quote. It takes no input.", # Clear description
41
  )
42
  ]
43
 
44
- # Initialize the language model
45
- from langchain_huggingface import HuggingFaceEndpoint
46
-
47
  llm = HuggingFaceEndpoint(
48
  repo_id=HF_MODEL_ID,
49
  temperature=0.7,
@@ -51,44 +39,65 @@ llm = HuggingFaceEndpoint(
51
  huggingfacehub_api_token=HF_API_KEY,
52
  )
53
 
54
- # Create the prompt template
55
- prompt_template = PromptTemplate(
56
- input_variables=["user_input", "tool_names", "tools"],
57
- template=prompt_templates["default"] # Use the default prompt
58
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
 
 
 
 
 
 
 
 
60
 
61
- # Initialize the agent
62
- agent = initialize_agent(
63
- tools,
64
- llm, # Make sure llm is passed here
65
- agent="zero-shot-react-description",
66
- verbose=True,
67
- prompt=prompt_template
68
- )
69
 
70
 
71
- def chat_with_agent(user_input, history):
72
- # ... (rest of the function)
 
 
 
 
 
73
 
74
- try:
75
- bot_response = agent(user_input) # Use agent(user_input) or agent.invoke(user_input)
76
- except Exception as e:
77
- bot_response = f"Sorry, an error occurred: {e}"
78
 
 
 
 
 
 
79
  history.append({"role": "user", "content": user_input})
80
  history.append({"role": "assistant", "content": bot_response})
81
  return history
82
 
83
 
84
- # Create Gradio Chat UI
85
  with gr.Blocks() as demo:
86
- gr.Markdown("### Welcome to MotivationAgent! 🌟\nType a message to receive a motivational quote or an uplifting response.")
87
-
88
- chatbot = gr.Chatbot(label="MotivationAgent", type="messages")
89
- user_input = gr.Textbox(placeholder="Ask me for motivation... (e.g., 'I need inspiration')")
90
-
91
- submit_btn = gr.Button("Send")
92
- submit_btn.click(chat_with_agent, inputs=[user_input, chatbot], outputs=chatbot)
93
 
94
  demo.launch()
 
1
  import os
2
  import yaml
3
  import gradio as gr
4
+ from langchain.agents import Tool
5
  from langchain.prompts import PromptTemplate
6
+ from langchain_huggingface import HuggingFaceEndpoint
7
+ import json # For parsing JSON responses (if needed)
8
 
9
  # Load environment variables
10
+ HF_API_KEY = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
11
  HF_MODEL_ID = os.environ.get("HF_MODEL_ID", "google/flan-t5-xxl")
12
 
13
  # Load prompt templates
14
  with open("prompts.yaml", 'r') as stream:
15
  all_prompts = yaml.safe_load(stream)
16
+ prompt_templates = all_prompts["prompts"]
 
 
 
 
 
 
 
17
 
18
+ # Define tools (example)
19
  def get_motivational_quote():
20
  quotes = [
21
+ # ... (your quotes)
 
 
 
 
22
  ]
23
  import random
24
  return random.choice(quotes)
 
27
  Tool(
28
  name="MotivationalQuote",
29
  func=get_motivational_quote,
30
+ description="Use this tool to get a motivational quote. It takes no input.",
31
  )
32
  ]
33
 
34
+ # Initialize LLM
 
 
35
  llm = HuggingFaceEndpoint(
36
  repo_id=HF_MODEL_ID,
37
  temperature=0.7,
 
39
  huggingfacehub_api_token=HF_API_KEY,
40
  )
41
 
42
+ # Create Prompt Templates
43
+ system_prompt = PromptTemplate(template=prompt_templates["system_prompt"], input_variables=["tools"])
44
+ planning_prompt = PromptTemplate(template=prompt_templates["planning"]["initial_plan"], input_variables=["task", "tools", "answer_facts"])
45
+ # ... (create PromptTemplates for other prompts as needed)
46
+ default_prompt = PromptTemplate(template=prompt_templates["default"], input_variables=["user_input"])
47
+
48
+ # Agent Logic (Custom Loop - This needs significant implementation)
49
+ def agent_loop(user_input):
50
+ facts = "" # Initialize facts
51
+ plan = planning_prompt.format(task=user_input, tools=tools, answer_facts=facts)
52
+ print("Initial Plan:", plan)
53
+
54
+ # Example (Very simplified - you MUST expand this)
55
+ llm_response = llm(system_prompt.format(tools=tools) + plan) # Call LLM with system and plan prompts
56
+ print("LLM Response:", llm_response)
57
+
58
+ # *** PARSE LLM RESPONSE ***
59
+ # This is where you need to extract the next action (tool call or direct answer)
60
+ # from the LLM's response. You'll likely need a specific format in the LLM's output.
61
+ # Example (assuming the LLM outputs JSON):
62
+ try:
63
+ response_json = json.loads(llm_response) # This will only work if the LLM response is valid JSON
64
+ action = response_json.get("action")
65
+ action_input = response_json.get("action_input")
66
 
67
+ if action == "MotivationalQuote":
68
+ quote = get_motivational_quote()
69
+ return quote
70
+ # ... handle other actions (tool calls or direct answers)
71
+ elif action == "final_answer":
72
+ return action_input
73
+ else:
74
+ return "I don't know how to do that yet."
75
 
76
+ except json.JSONDecodeError:
77
+ return "Invalid LLM Response. I don't know how to do that yet."
 
 
 
 
 
 
78
 
79
 
80
+ # *** UPDATE FACTS AND PLAN ***
81
+ # After each action, update the facts and create a new plan if needed.
82
+ # This will depend on the LLM's response and the results of tool calls.
83
+ # ...
84
+
85
+ # Example (replace with your actual logic)
86
+ return "Agent response (replace with actual agent output)"
87
 
 
 
 
 
88
 
89
+ def chat_with_agent(user_input, history):
90
+ if not user_input.strip():
91
+ return history + [{"role": "assistant", "content": "Please enter a message to start the conversation!"}]
92
+
93
+ bot_response = agent_loop(user_input)
94
  history.append({"role": "user", "content": user_input})
95
  history.append({"role": "assistant", "content": bot_response})
96
  return history
97
 
98
 
99
+ # Gradio UI
100
  with gr.Blocks() as demo:
101
+ # ... (Gradio UI code as before)
 
 
 
 
 
 
102
 
103
  demo.launch()