Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,45 +14,45 @@ from optillm.leap import leap
|
|
| 14 |
|
| 15 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
message,
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
max_tokens,
|
| 24 |
-
temperature,
|
| 25 |
-
top_p,
|
| 26 |
-
):
|
| 27 |
client = OpenAI(api_key=API_KEY, base_url="https://openrouter.ai/api/v1")
|
| 28 |
-
system_prompt = system_message
|
| 29 |
-
initial_query = message
|
| 30 |
messages = [{"role": "system", "content": system_message}]
|
| 31 |
-
|
| 32 |
for val in history:
|
| 33 |
-
if val[0]:
|
| 34 |
-
|
| 35 |
-
if val[1]:
|
| 36 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 37 |
-
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
|
| 40 |
-
if approach ==
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
return final_response
|
| 58 |
|
|
@@ -68,32 +68,69 @@ def respond(
|
|
| 68 |
# response += token
|
| 69 |
# yield response
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
gr.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
demo.launch()
|
|
|
|
| 14 |
|
| 15 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
| 16 |
|
| 17 |
+
def compare_responses(message, model1, approach1, model2, approach2, system_message, max_tokens, temperature, top_p):
|
| 18 |
+
response1 = respond(message, [], model1, approach1, system_message, max_tokens, temperature, top_p)
|
| 19 |
+
response2 = respond(message, [], model2, approach2, system_message, max_tokens, temperature, top_p)
|
| 20 |
+
return response1, response2
|
| 21 |
+
|
| 22 |
+
def respond(message, history, model, approach, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
client = OpenAI(api_key=API_KEY, base_url="https://openrouter.ai/api/v1")
|
|
|
|
|
|
|
| 24 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 25 |
for val in history:
|
| 26 |
+
if val[0]: messages.append({"role": "user", "content": val[0]})
|
| 27 |
+
if val[1]: messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
|
|
|
|
|
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
|
| 30 |
+
if approach == "none":
|
| 31 |
+
response = client.chat.completions.create(
|
| 32 |
+
model=model,
|
| 33 |
+
messages=messages,
|
| 34 |
+
max_tokens=max_tokens,
|
| 35 |
+
temperature=temperature,
|
| 36 |
+
top_p=top_p,
|
| 37 |
+
)
|
| 38 |
+
return response.choices[0].message.content
|
| 39 |
+
else:
|
| 40 |
+
if approach == 'rto':
|
| 41 |
+
final_response = round_trip_optimization(system_prompt, initial_query, client, model)
|
| 42 |
+
elif approach == 'z3':
|
| 43 |
+
z3_solver = Z3SolverSystem(system_prompt, client, model)
|
| 44 |
+
final_response = z3_solver.process_query(initial_query)
|
| 45 |
+
elif approach == "self_consistency":
|
| 46 |
+
final_response = advanced_self_consistency_approach(system_prompt, initial_query, client, model)
|
| 47 |
+
elif approach == "rstar":
|
| 48 |
+
rstar = RStar(system_prompt, client, model)
|
| 49 |
+
final_response = rstar.solve(initial_query)
|
| 50 |
+
elif approach == "cot_reflection":
|
| 51 |
+
final_response = cot_reflection(system_prompt, initial_query, client, model)
|
| 52 |
+
elif approach == 'plansearch':
|
| 53 |
+
final_response = plansearch(system_prompt, initial_query, client, model)
|
| 54 |
+
elif approach == 'leap':
|
| 55 |
+
final_response = leap(system_prompt, initial_query, client, model)
|
| 56 |
|
| 57 |
return final_response
|
| 58 |
|
|
|
|
| 68 |
# response += token
|
| 69 |
# yield response
|
| 70 |
|
| 71 |
+
def create_model_dropdown():
|
| 72 |
+
return gr.Dropdown(
|
| 73 |
+
["nousresearch/hermes-3-llama-3.1-405b:free", "meta-llama/llama-3.1-8b-instruct:free",
|
| 74 |
+
"qwen/qwen-2-7b-instruct:free", "google/gemma-2-9b-it:free", "mistralai/mistral-7b-instruct:free"],
|
| 75 |
+
value="nousresearch/hermes-3-llama-3.1-405b:free", label="Model"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
def create_approach_dropdown():
|
| 79 |
+
return gr.Dropdown(
|
| 80 |
+
["none", "leap", "plansearch", "rstar", "cot_reflection", "rto", "self_consistency", "z3"],
|
| 81 |
+
value="none", label="Approach"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown("# LLM Optimization Comparison")
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
system_message = gr.Textbox(value="", label="System message")
|
| 89 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
| 90 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 91 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 92 |
+
|
| 93 |
+
with gr.Tabs():
|
| 94 |
+
with gr.TabItem("Single Chat"):
|
| 95 |
+
model = create_model_dropdown()
|
| 96 |
+
approach = create_approach_dropdown()
|
| 97 |
+
chatbot = gr.Chatbot()
|
| 98 |
+
msg = gr.Textbox()
|
| 99 |
+
clear = gr.Button("Clear")
|
| 100 |
+
|
| 101 |
+
def user(user_message, history):
|
| 102 |
+
return "", history + [[user_message, None]]
|
| 103 |
+
|
| 104 |
+
def bot(history, model, approach, system_message, max_tokens, temperature, top_p):
|
| 105 |
+
user_message = history[-1][0]
|
| 106 |
+
bot_message = respond(user_message, history[:-1], model, approach, system_message, max_tokens, temperature, top_p)
|
| 107 |
+
history[-1][1] = bot_message
|
| 108 |
+
return history
|
| 109 |
+
|
| 110 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot]).then(
|
| 111 |
+
bot, [chatbot, model, approach, system_message, max_tokens, temperature, top_p], chatbot
|
| 112 |
+
)
|
| 113 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 114 |
+
|
| 115 |
+
with gr.TabItem("Compare"):
|
| 116 |
+
with gr.Row():
|
| 117 |
+
model1 = create_model_dropdown()
|
| 118 |
+
approach1 = create_approach_dropdown()
|
| 119 |
+
model2 = create_model_dropdown()
|
| 120 |
+
approach2 = create_approach_dropdown()
|
| 121 |
+
|
| 122 |
+
compare_input = gr.Textbox(label="Enter your message for comparison")
|
| 123 |
+
compare_button = gr.Button("Compare")
|
| 124 |
+
|
| 125 |
+
with gr.Row():
|
| 126 |
+
output1 = gr.Textbox(label="Response 1")
|
| 127 |
+
output2 = gr.Textbox(label="Response 2")
|
| 128 |
+
|
| 129 |
+
compare_button.click(
|
| 130 |
+
compare_responses,
|
| 131 |
+
inputs=[compare_input, model1, approach1, model2, approach2, system_message, max_tokens, temperature, top_p],
|
| 132 |
+
outputs=[output1, output2]
|
| 133 |
+
)
|
| 134 |
|
| 135 |
if __name__ == "__main__":
|
| 136 |
demo.launch()
|