yuchenlin commited on
Commit
7d3f3d3
Β·
1 Parent(s): dfd0ee3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -18
app.py CHANGED
@@ -9,24 +9,45 @@ from swiftsage.agents import SwiftSage
9
  from swiftsage.utils.commons import PromptTemplate, api_configs, setup_logging
10
  from pkg_resources import resource_filename
11
 
12
- def solve_problem(problem, max_iterations, reward_threshold, swift_model_id, sage_model_id, feedback_model_id, use_retrieval, start_with_sage):
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Configuration for each LLM
14
  max_iterations = int(max_iterations)
15
  reward_threshold = int(reward_threshold)
16
 
17
  swift_config = {
18
  "model_id": swift_model_id,
19
- "api_config": api_configs['Together']
 
 
 
20
  }
21
 
22
- reward_config = {
23
  "model_id": feedback_model_id,
24
- "api_config": api_configs['Together']
 
 
 
25
  }
26
 
27
  sage_config = {
28
  "model_id": sage_model_id,
29
- "api_config": api_configs['Together']
 
 
 
30
  }
31
 
32
  # specify the path to the prompt templates
@@ -46,7 +67,7 @@ def solve_problem(problem, max_iterations, reward_threshold, swift_model_id, sag
46
  if os.path.exists(path):
47
  prompt_template_dir = path
48
  break
49
-
50
  dataset = []
51
  embeddings = [] # TODO: for retrieval augmentation (not implemented yet now)
52
  s2 = SwiftSage(
@@ -55,32 +76,35 @@ def solve_problem(problem, max_iterations, reward_threshold, swift_model_id, sag
55
  prompt_template_dir,
56
  swift_config,
57
  sage_config,
58
- reward_config,
59
  use_retrieval=use_retrieval,
60
  start_with_sage=start_with_sage,
61
  )
62
 
63
- reasoning, solution = s2.solve(problem, max_iterations, reward_threshold)
64
  solution = solution.replace("Answer (from running the code):\n ", " ")
65
- return reasoning, solution
 
 
 
66
 
67
 
68
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
69
  # gr.Markdown("## SwiftSage: A Multi-Agent Framework for Reasoning")
70
  # use the html and center the title
71
- gr.HTML("<h1 style='text-align: center;'>SwiftSage: A Multi-Agent Framework for Reasoning</h1>")
72
 
73
  with gr.Row():
74
- swift_model_id = gr.Textbox(label="πŸ˜„ Swift Model ID", value="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo")
75
- feedback_model_id = gr.Textbox(label="πŸ€” Feedback Model ID", value="meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo")
76
- sage_model_id = gr.Textbox(label="😎 Sage Model ID", value="meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo")
77
  # the following two should have a smaller width
78
 
79
  with gr.Accordion(label="βš™οΈ Advanced Options", open=False):
80
  with gr.Row():
81
  with gr.Column():
82
  max_iterations = gr.Textbox(label="Max Iterations", value="5")
83
- reward_threshold = gr.Textbox(label="Reward Threshold", value="8")
84
  # TODO: add top-p and temperature for each module for controlling
85
  with gr.Column():
86
  top_p_swift = gr.Textbox(label="Top-p for Swift", value="0.9")
@@ -89,8 +113,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
89
  top_p_sage = gr.Textbox(label="Top-p for Sage", value="0.9")
90
  temperature_sage = gr.Textbox(label="Temperature for Sage", value="0.7")
91
  with gr.Column():
92
- top_p_reward = gr.Textbox(label="Top-p for Feedback", value="0.9")
93
- temperature_reward = gr.Textbox(label="Temperature for Feedback", value="0.7")
94
 
95
  use_retrieval = gr.Checkbox(label="Use Retrieval Augmentation", value=False, visible=False)
96
  start_with_sage = gr.Checkbox(label="Start with Sage", value=False, visible=False)
@@ -101,13 +125,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
101
  reasoning_output = gr.Textbox(label="Reasoning steps with Code", interactive=False)
102
  solution_output = gr.Textbox(label="Final answer", interactive=False)
103
 
 
 
 
 
104
  solve_button.click(
105
  solve_problem,
106
- inputs=[problem, max_iterations, reward_threshold, swift_model_id, sage_model_id, feedback_model_id, use_retrieval, start_with_sage],
107
- outputs=[reasoning_output, solution_output]
108
  )
109
 
110
 
 
111
  if __name__ == '__main__':
112
  # make logs dir if it does not exist
113
  if not os.path.exists('logs'):
 
9
  from swiftsage.utils.commons import PromptTemplate, api_configs, setup_logging
10
  from pkg_resources import resource_filename
11
 
12
+ ENGINE = "Together"
13
+ # SWIFT_MODEL_ID = "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"
14
+ SWIFT_MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct-Reference"
15
+ FEEDBACK_MODEL_ID = "meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo"
16
+ SAGE_MODEL_ID = "meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo"
17
+
18
+ # ENGINE = "SambaNova"
19
+ # SWIFT_MODEL_ID = "Meta-Llama-3.1-8B-Instruct"
20
+ # FEEDBACK_MODEL_ID = "Meta-Llama-3.1-70B-Instruct"
21
+ # SAGE_MODEL_ID = "Meta-Llama-3.1-405B-Instruct"
22
+
23
+ def solve_problem(problem, max_iterations, reward_threshold, swift_model_id, sage_model_id, feedback_model_id, use_retrieval, start_with_sage, swift_temperature, swift_top_p, sage_temperature, sage_top_p, feedback_temperature, feedback_top_p):
24
+ global ENGINE
25
  # Configuration for each LLM
26
  max_iterations = int(max_iterations)
27
  reward_threshold = int(reward_threshold)
28
 
29
  swift_config = {
30
  "model_id": swift_model_id,
31
+ "api_config": api_configs[ENGINE],
32
+ "temperature": float(swift_temperature),
33
+ "top_p": float(swift_top_p),
34
+ "max_tokens": 2048,
35
  }
36
 
37
+ feedback_config = {
38
  "model_id": feedback_model_id,
39
+ "api_config": api_configs[ENGINE],
40
+ "temperature": float(feedback_temperature),
41
+ "top_p": float(feedback_top_p),
42
+ "max_tokens": 2048,
43
  }
44
 
45
  sage_config = {
46
  "model_id": sage_model_id,
47
+ "api_config": api_configs[ENGINE],
48
+ "temperature": float(sage_temperature),
49
+ "top_p": float(sage_top_p),
50
+ "max_tokens": 2048,
51
  }
52
 
53
  # specify the path to the prompt templates
 
67
  if os.path.exists(path):
68
  prompt_template_dir = path
69
  break
70
+
71
  dataset = []
72
  embeddings = [] # TODO: for retrieval augmentation (not implemented yet now)
73
  s2 = SwiftSage(
 
76
  prompt_template_dir,
77
  swift_config,
78
  sage_config,
79
+ feedback_config,
80
  use_retrieval=use_retrieval,
81
  start_with_sage=start_with_sage,
82
  )
83
 
84
+ reasoning, solution, messages = s2.solve(problem, max_iterations, reward_threshold)
85
  solution = solution.replace("Answer (from running the code):\n ", " ")
86
+ # generate HTML for the log messages and display them with wrap and a scroll bar and a max height in the code block with log style
87
+
88
+ log_messages = "<pre style='white-space: pre-wrap; max-height: 500px; overflow-y: scroll;'><code class='log'>" + "\n".join(messages) + "</code></pre>"
89
+ return reasoning, solution, log_messages
90
 
91
 
92
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
93
  # gr.Markdown("## SwiftSage: A Multi-Agent Framework for Reasoning")
94
  # use the html and center the title
95
+ gr.HTML("<h1 style='text-align: center;'>SwiftSage: A General Reasoning Framework with Fast and Slow Thinking</h1> (v2-beta)")
96
 
97
  with gr.Row():
98
+ swift_model_id = gr.Textbox(label="πŸ˜„ Swift Model ID", value=SWIFT_MODEL_ID)
99
+ feedback_model_id = gr.Textbox(label="πŸ€” Feedback Model ID", value=FEEDBACK_MODEL_ID)
100
+ sage_model_id = gr.Textbox(label="😎 Sage Model ID", value=SAGE_MODEL_ID)
101
  # the following two should have a smaller width
102
 
103
  with gr.Accordion(label="βš™οΈ Advanced Options", open=False):
104
  with gr.Row():
105
  with gr.Column():
106
  max_iterations = gr.Textbox(label="Max Iterations", value="5")
107
+ reward_threshold = gr.Textbox(label="feedback Threshold", value="8")
108
  # TODO: add top-p and temperature for each module for controlling
109
  with gr.Column():
110
  top_p_swift = gr.Textbox(label="Top-p for Swift", value="0.9")
 
113
  top_p_sage = gr.Textbox(label="Top-p for Sage", value="0.9")
114
  temperature_sage = gr.Textbox(label="Temperature for Sage", value="0.7")
115
  with gr.Column():
116
+ top_p_feedback = gr.Textbox(label="Top-p for Feedback", value="0.9")
117
+ temperature_feedback = gr.Textbox(label="Temperature for Feedback", value="0.7")
118
 
119
  use_retrieval = gr.Checkbox(label="Use Retrieval Augmentation", value=False, visible=False)
120
  start_with_sage = gr.Checkbox(label="Start with Sage", value=False, visible=False)
 
125
  reasoning_output = gr.Textbox(label="Reasoning steps with Code", interactive=False)
126
  solution_output = gr.Textbox(label="Final answer", interactive=False)
127
 
128
+ # add a log display for showing the log messages
129
+ with gr.Accordion(label="πŸ“œ Log Messages", open=False):
130
+ log_output = gr.HTML("<p>No log messages yet.</p>")
131
+
132
  solve_button.click(
133
  solve_problem,
134
+ inputs=[problem, max_iterations, reward_threshold, swift_model_id, sage_model_id, feedback_model_id, use_retrieval, start_with_sage, temperature_swift, top_p_swift, temperature_sage, top_p_sage, temperature_feedback, top_p_feedback],
135
+ outputs=[reasoning_output, solution_output, log_output],
136
  )
137
 
138
 
139
+
140
  if __name__ == '__main__':
141
  # make logs dir if it does not exist
142
  if not os.path.exists('logs'):