MarkTechPost@AI 18小时前
Implementing Self-Refine Technique Using Large Language Models LLMs
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了如何使用Mirascope框架和大型语言模型(LLMs)实现Self-Refine技术。Self-Refine是一种提示工程策略,通过模型自我评估、生成反馈并基于反馈迭代改进输出来提升回答质量。文章展示了基础实现和使用Pydantic模型进行增强的两种方法,特别是在数学问题解决方面,通过一次迭代即可获得精确的步骤和答案。该技术能显著提高准确性、清晰度和透明度,适用于需要精确性、结构性和迭代改进的任务,但需注意计算成本。

✨ Self-Refine技术是一种提示工程策略,通过让大型语言模型(LLMs)自我评估、生成反馈并根据反馈迭代改进其输出,从而逐步提升最终答案的质量和准确性。这种方法特别适用于需要逐步优化的任务,如推理、代码生成和内容创作。

🔧 Mirascope框架提供了实现Self-Refine技术的便捷工具,包括`@openai.call`和`@prompt_template`装饰器,以及用于评估响应和生成新响应的函数。基础实现演示了如何通过循环调用模型来完成迭代改进过程。

📊 增强的Self-Refine技术引入了Pydantic模型(如`MathSolution`)来结构化输出,确保答案的清晰性、一致性和易用性,特别适合数学问题求解。通过解析反馈并生成包含详细步骤和最终答案的结构化输出,模型能够更准确地解决复杂问题。

🚀 在数学问题“一列火车以一定速度行驶120公里。如果速度提高20公里/小时,则需要少花30分钟,求原速度?”的示例中,增强的Self-Refine技术通过一次迭代便成功推导出了60公里/小时的正确答案,展示了其在提高准确性、提供清晰推理步骤和增强透明度方面的优势。

⚠️ 虽然Self-Refine技术在提升输出质量方面表现出色,但在实际应用中需要权衡计算成本,并根据具体用例微调迭代深度和反馈提示,以达到最佳效果。

This tutorial demonstrates how to implement the Self-Refine technique using Large Language Models (LLMs) with Mirascope, a powerful framework for building structured prompt workflows. Self-Refine is a prompt engineering strategy where the model evaluates its own output, generates feedback, and iteratively improves its response based on that feedback. This refinement loop can be repeated multiple times to progressively enhance the quality and accuracy of the final answer.

The Self-Refine approach is particularly effective for tasks involving reasoning, code generation, and content creation, where incremental improvements lead to significantly better results. Check out the Full Codes here

Installing the dependencies

!pip install "mirascope[openai]"

OpenAI API Key

To get an OpenAI API key, visit https://platform.openai.com/settings/organization/api-keys and generate a new key. If you’re a new user, you may need to add billing details and make a minimum payment of $5 to activate API access. Check out the Full Codes here

import osfrom getpass import getpassos.environ["OPENAI_API_KEY"] = getpass('Enter OpenAI API Key: ')

Basic Self-Refine Implementation

We begin by implementing the Self-Refine technique using Mirascope’s @openai.call and @prompt_template decorators. The process starts with generating an initial response to a user query. This response is then evaluated by the model itself, which provides constructive feedback. Finally, the model uses this feedback to generate an improved response. The self_refine function allows us to repeat this refinement process for a specified number of iterations, enhancing the quality of the output with each cycle. Check out the Full Codes here

from mirascope.core import openai, prompt_templatefrom mirascope.core.openai import OpenAICallResponse@openai.call(model="gpt-4o-mini")def call(query: str) -> str:    return query@openai.call(model="gpt-4o-mini")@prompt_template(    """    Here is a query and a response to the query. Give feedback about the answer,    noting what was correct and incorrect.    Query:    {query}    Response:    {response}    """)def evaluate_response(query: str, response: OpenAICallResponse): ...@openai.call(model="gpt-4o-mini")@prompt_template(    """    For this query:    {query}    The following response was given:    {response}    Here is some feedback about the response:    {feedback}    Consider the feedback to generate a new response to the query.    """)def generate_new_response(    query: str, response: OpenAICallResponse) -> openai.OpenAIDynamicConfig:    feedback = evaluate_response(query, response)    return {"computed_fields": {"feedback": feedback}}def self_refine(query: str, depth: int) -> str:    response = call(query)    for _ in range(depth):        response = generate_new_response(query, response)    return response.contentquery = "A train travels 120 km at a certain speed. If the speed had been 20 km/h faster, it would have taken 30 minutes less to cover the same distance. What was the original speed of the train?"print(self_refine(query, 1))

Enhanced Self-Refine with Response Model

In this enhanced version, we define a structured response model MathSolution using Pydantic to capture both the solution steps and the final numerical answer. The enhanced_generate_new_response function refines the output by incorporating model-generated feedback and formatting the improved response into a well-defined schema. This approach ensures clarity, consistency, and better downstream usability of the refined answer—especially for tasks like mathematical problem-solving. Check out the Full Codes here

from pydantic import BaseModel, Fieldclass MathSolution(BaseModel):    steps: list[str] = Field(..., description="The steps taken to solve the problem")    final_answer: float = Field(..., description="The final numerical answer")@openai.call(model="gpt-4o-mini", response_model=MathSolution)@prompt_template(    """    For this query:    {query}    The following response was given:    {response}    Here is some feedback about the response:    {feedback}    Consider the feedback to generate a new response to the query.    Provide the solution steps and the final numerical answer.    """)def enhanced_generate_new_response(    query: str, response: OpenAICallResponse) -> openai.OpenAIDynamicConfig:    feedback = evaluate_response(query, response)    return {"computed_fields": {"feedback": feedback}}def enhanced_self_refine(query: str, depth: int) -> MathSolution:    response = call(query)    for _ in range(depth):        solution = enhanced_generate_new_response(query, response)        response = f"Steps: {solution.steps}\nFinal Answer: {solution.final_answer}"    return solution# Example usageresult = enhanced_self_refine(query, 1)print(result)

The Enhanced Self-Refine technique proved effective in accurately solving the given mathematical problem:

“A train travels 120 km at a certain speed. If the speed had been 20 km/h faster, it would have taken 30 minutes less to cover the same distance. What was the original speed of the train?”

Through a single iteration of refinement, the model delivered a logically sound and step-by-step derivation leading to the correct answer of 60 km/h. This illustrates several key benefits of the Self-Refine approach:

In broader applications, this technique holds strong promise for tasks that demand accuracy, structure, and iterative improvement—ranging from technical problem solving to creative and professional writing. However, implementers should remain mindful of the trade-offs in computational cost and fine-tune the depth and feedback prompts to match their specific use case.


Check out the Full Codes here. All credit for this research goes to the researchers of this project. Also, feel free to follow us on Twitter and don’t forget to join our 100k+ ML SubReddit and Subscribe to our Newsletter.

FAQ: Can Marktechpost help me to promote my AI Product and position it in front of AI Devs and Data Engineers?

Ans: Yes, Marktechpost can help promote your AI product by publishing sponsored articles, case studies, or product features, targeting a global audience of AI developers and data engineers. The MTP platform is widely read by technical professionals, increasing your product’s visibility and positioning within the AI community. [SET UP A CALL]

The post Implementing Self-Refine Technique Using Large Language Models LLMs appeared first on MarkTechPost.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Self-Refine Mirascope LLM 提示工程 AI优化
相关文章