mirror of
https://github.com/elder-plinius/AutoTemp.git
synced 2026-09-24 10:30:46 +02:00
Update autotemp.py
This commit is contained in:
+33
-23
@@ -21,7 +21,7 @@ class AutoTemp:
|
|||||||
self.max_workers = max_workers
|
self.max_workers = max_workers
|
||||||
self.model_version = model_version
|
self.model_version = model_version
|
||||||
|
|
||||||
def generate_with_openai(self, prompt, temperature, retries=3):
|
def generate_with_openai(self, prompt, temperature, top_p, retries=3):
|
||||||
while retries > 0:
|
while retries > 0:
|
||||||
try:
|
try:
|
||||||
response = openai.ChatCompletion.create(
|
response = openai.ChatCompletion.create(
|
||||||
@@ -30,14 +30,15 @@ class AutoTemp:
|
|||||||
{"role": "system", "content": "You are a helpful assistant."},
|
{"role": "system", "content": "You are a helpful assistant."},
|
||||||
{"role": "user", "content": prompt}
|
{"role": "user", "content": prompt}
|
||||||
],
|
],
|
||||||
temperature=temperature
|
temperature=temperature,
|
||||||
|
top_p=top_p
|
||||||
)
|
)
|
||||||
message = response['choices'][0]['message']['content']
|
message = response['choices'][0]['message']['content']
|
||||||
return message.strip()
|
return message.strip()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
retries -= 1
|
retries -= 1
|
||||||
if retries <= 0:
|
if retries <= 0:
|
||||||
return f"Error generating text at temperature {temperature}: {e}"
|
return f"Error generating text at temperature {temperature} and top-p {top_p}: {e}"
|
||||||
|
|
||||||
def evaluate_output(self, output, temperature):
|
def evaluate_output(self, output, temperature):
|
||||||
eval_prompt = f"""
|
eval_prompt = f"""
|
||||||
@@ -56,21 +57,20 @@ class AutoTemp:
|
|||||||
{output}
|
{output}
|
||||||
---
|
---
|
||||||
"""
|
"""
|
||||||
score_text = self.generate_with_openai(eval_prompt, 0.5) # Use a neutral temperature for evaluation to get consistent results
|
score_text = self.generate_with_openai(eval_prompt, 0.5, top_p) # Use a neutral temperature for evaluation
|
||||||
score_match = re.search(r'\b\d+(\.\d)?\b', score_text)
|
score_match = re.search(r'\b\d+(\.\d)?\b', score_text)
|
||||||
if score_match:
|
if score_match:
|
||||||
return round(float(score_match.group()), 1) # Round the score to one decimal place
|
return round(float(score_match.group()), 1) # Round the score to one decimal place
|
||||||
else:
|
else:
|
||||||
return 0.0 # Unable to parse score, default to 0.0
|
return 0.0 # Unable to parse score, default to 0.0
|
||||||
|
|
||||||
def run(self, prompt, temperature_list=None):
|
def run(self, prompt, temperature_string, top_p):
|
||||||
if temperature_list is not None:
|
temperature_list = [float(temp.strip()) for temp in temperature_string.split(',')]
|
||||||
self.alt_temps = temperature_list
|
|
||||||
outputs = {}
|
outputs = {}
|
||||||
scores = {}
|
scores = {}
|
||||||
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
|
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
|
||||||
future_to_temp = {
|
future_to_temp = {
|
||||||
executor.submit(self.generate_with_openai, prompt, temp): temp for temp in self.alt_temps
|
executor.submit(self.generate_with_openai, prompt, temp, top_p): temp for temp in temperature_list
|
||||||
}
|
}
|
||||||
for future in as_completed(future_to_temp):
|
for future in as_completed(future_to_temp):
|
||||||
temp = future_to_temp[future]
|
temp = future_to_temp[future]
|
||||||
@@ -89,33 +89,43 @@ class AutoTemp:
|
|||||||
# If auto_select is enabled, return only the best result
|
# If auto_select is enabled, return only the best result
|
||||||
if self.auto_select:
|
if self.auto_select:
|
||||||
best_temp, best_output, best_score = sorted_outputs[0]
|
best_temp, best_output, best_score = sorted_outputs[0]
|
||||||
return f"Best AutoTemp Output (Temp {best_temp} | Score: {best_score}):\n{best_output}"
|
return f"Best AutoTemp Output (Temp {best_temp} | Top-p {top_p} | Score: {best_score}):\n{best_output}"
|
||||||
else:
|
else:
|
||||||
return "\n".join(f"Temp {temp} | Score: {score}:\n{text}" for temp, text, score in sorted_outputs)
|
return "\n".join(f"Temp {temp} | Top-p {top_p} | Score: {score}:\n{text}" for temp, text, score in sorted_outputs)
|
||||||
|
|
||||||
# Gradio app logic
|
# Gradio app logic
|
||||||
def run_autotemp(prompt, temperature_string, auto_select):
|
def run_autotemp(prompt, temperature_string, top_p, auto_select):
|
||||||
temperature_list = [float(temp.strip()) for temp in temperature_string.split(',')]
|
|
||||||
agent = AutoTemp(auto_select=auto_select)
|
agent = AutoTemp(auto_select=auto_select)
|
||||||
output = agent.run(prompt, temperature_list=temperature_list)
|
output = agent.run(prompt, temperature_string, top_p=float(top_p))
|
||||||
return output
|
return output
|
||||||
|
|
||||||
# Gradio interface setup
|
# Gradio interface setup
|
||||||
def main():
|
def main():
|
||||||
iface = gr.Interface(
|
iface = gr.Interface(
|
||||||
fn=run_autotemp,
|
fn=run_autotemp,
|
||||||
inputs=["text", "text", "checkbox"],
|
inputs=[
|
||||||
|
"text",
|
||||||
|
"text",
|
||||||
|
gr.Slider(minimum=0.0, maximum=1.0, step=0.1, default=1.0, label="Top-p value"),
|
||||||
|
"checkbox"
|
||||||
|
],
|
||||||
outputs="text",
|
outputs="text",
|
||||||
title="AutoTemp: Improved LLM Completions through Temperature Tuning",
|
title="AutoTemp: Enhanced LLM Responses with Temperature and Top-p Tuning",
|
||||||
description="Enter different temperatures separated by commas (e.g., 0.4, 0.6, 0.8, 1.0, 1.2). Toggle 'Auto Select' to either see just the best output or all evaluated outputs.",
|
description="""AutoTemp generates responses at different temperatures, evaluates them, and ranks them based on quality.
|
||||||
|
Enter temperatures separated by commas for evaluation.
|
||||||
|
Adjust 'Top-p' to control output diversity: lower for precision, higher for creativity.
|
||||||
|
Toggle 'Auto Select' to either see the top-rated output or all evaluated outputs.""",
|
||||||
|
article="""**What's Top-p?** 'Top-p' controls the diversity of AI responses: a low 'top-p' makes output more focused and predictable,
|
||||||
|
while a high 'top-p' encourages variety and surprise. Pair with temperature to fine-tune AI creativity:
|
||||||
|
higher temperatures with high 'top-p' for bold ideas, or lower temperatures with low 'top-p' for precise answers.""",
|
||||||
examples=[
|
examples=[
|
||||||
["Write a short story about AGI learning to love", "0.5, 0.7, 0.9, 1.1", False],
|
["Write a short story about AGI learning to love", "0.5, 0.7, 0.9, 1.1", 1.0, False],
|
||||||
["Create a dialogue between a chef and an alien creating an innovative new recipe", "0.3, 0.6, 0.9, 1.2", True],
|
["Create a dialogue between a chef and an alien creating an innovative new recipe", "0.3, 0.6, 0.9, 1.2", 0.9, True],
|
||||||
["Explain quantum computing to a 5-year-old", "0.4, 0.8, 1.2, 1.5", False],
|
["Explain quantum computing to a 5-year-old", "0.4, 0.8, 1.2, 1.5", 0.8, False],
|
||||||
["Draft an email to a hotel asking for a special arrangement for a marriage proposal", "0.4, 0.7, 1.0, 1.3", True],
|
["Draft an email to a hotel asking for a special arrangement for a marriage proposal", "0.4, 0.7, 1.0, 1.3", 0.7, True],
|
||||||
["Describe a futuristic city powered by renewable energy", "0.5, 0.75, 1.0, 1.25", False],
|
["Describe a futuristic city powered by renewable energy", "0.5, 0.75, 1.0, 1.25", 0.6, False],
|
||||||
["Generate a poem about the ocean's depths in the style of Edgar Allan Poe", "0.6, 0.8, 1.0, 1.2", True],
|
["Generate a poem about the ocean's depths in the style of Edgar Allan Poe", "0.6, 0.8, 1.0, 1.2", 0.5, True],
|
||||||
["What are some innovative startup ideas for improving urban transportation?", "0.45, 0.65, 0.85, 1.05", False]
|
["What are some innovative startup ideas for improving urban transportation?", "0.45, 0.65, 0.85, 1.05", 0.4, False]
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
iface.launch()
|
iface.launch()
|
||||||
|
|||||||
Reference in New Issue
Block a user