从零开始创建自己的提示增强器

部署运行你感兴趣的模型镜像

原文:towardsdatascience.com/create-your-own-prompt-enhancer-from-scratch-b870963a1ca0

使用 Python、Gradio 和 Groq

如果你是一名 OpenAI 用户,你可能知道,他们最近在 OpenAI 游乐场网站上对其系统提示添加了一个增强功能。

现在,你可以选择让 OpenAI 生成一个全新的、详细且高度集中的提示,你可以用它来替代你最初的、更简单的提示。

要测试这个功能,请点击下面的链接进入游乐场,如果你还没有登录,请使用你的 OpenAI 凭据登录。

platform.openai.com/playground/

你应该会看到一个像这样的屏幕。

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/91374f27872400b8e3c546fe43624fef.png

OpenAI 网站上的图片

如果你查看**系统指令**输入框右侧的两个图标,你会看到一个看起来像四角星的图标。这是提示生成器,它将把你的简单提示转换成一个高度集中和详细的提示。

注意,这个图标不会出现在屏幕底部的用户提示输入框中。但,秘密在于,你不必仅仅为了系统提示而使用提示生成器。相反,你可以输入任何你想要增强的提示,然后将提示生成器的输出复制并粘贴到你需要使用它的任何地方,包括用户提示输入框。

因此,点击星形图标,你会看到一个弹出窗口,如下所示,

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/73403f07f14bdad7be5e6b6c5e229167.png

OpenAI 网站上的图片

现在输入,例如,一个相对简单的提示,如下所示,

You are an expert at growing apple trees. What is the best way 
to grow an apple tree.

点击**创建**按钮后,大约一分钟后我收到了这个输出。

Provide detailed guidance on the best way to grow an apple tree, including preparation, planting, care, and maintenance.

# Steps

1\. **Preparation:**
   - Choose a suitable location with full sun and well-drained soil.
   - Test the soil for pH and nutrients, aiming for a pH of 6.0 to 7.0.

2\. **Selecting the Tree:**
   - Select a variety that suits your climate and soil conditions.
   - Decide between a standard, semi-dwarf, or dwarf rootstock based on available space.

3\. **Planting:**
   - Plant bare-root apple trees in early spring, as soon as the ground can be worked.
   - Dig a hole twice as wide but no deeper than the root ball.
   - Backfill the hole with native soil, positioning the graft union above ground level.

4\. **Initial Care:**
   - Water thoroughly after planting to remove air pockets.
   - Apply mulch around the base to retain moisture and prevent weeds.

5\. **Ongoing Care:**
   - Water regularly, especially during dry periods.
   - Prune annually to maintain shape and remove diseased or crossing branches.
   - Fertilize in early spring with a balanced fertilizer formulated for fruit trees.

6\. **Pest and Disease Management:**
   - Monitor for common pests and diseases, applying organic or chemical controls as necessary.

# Output Format

Provide the information in a detailed paragraph format, organizing the content logically under each step heading as listed above.

# Notes

- Make sure to emphasize the importance of selecting a suitable variety for your specific climate conditions.
- Ensure that pruning and pest management techniques are described concisely yet comprehensively.
- Highlight any necessary adjustments if the apple tree exhibits signs of stress or disease.

哇,这确实是一个很大的改进,不是吗?正如之前提到的,你现在可以将上述提示复制并粘贴到用户提示或任何其他你想要使用的 LLM 中。

在本文的剩余部分,我想展示实现这种功能并没有“魔法”。实际上,我将展示如何使用开源工具 GROQ(用于其 LLM 功能)和 Gradio(用于我们的前端)来模拟 OpenAI 的提示增强器。

如果你需要回顾 Groq 是什么,或者你之前从未听说过它,请查看以下文章,以获得快速介绍。

Groq 及其 LPU:革命性的 AI 计算

Gradio 是一个开源的 Python GUI 包,它简化了为机器学习模型、API 或任何 Python 函数构建演示或 Web 应用程序的过程。

你可以在这里了解更多关于 Gradio 的信息。

Gradio:快速 GUI 原型设计

设置 Python 开发环境。

在开始一个新的项目之前,我们应该为它设置一个单独的编码环境。我使用 Conda 来做这件事,但你可以使用你习惯的任何方法。查看这个链接来了解我是如何做的。

一旦你的环境设置完毕,请确保你:-

  • 安装 Groq、Gradio 和 Jupyter
pip install jupyter
pip install groq
pip install gradio
  • 获取 Groq API 密钥

你可以通过访问以下链接来获取一个密钥,

console.groq.com

登录(或创建一个账户——这是免费的!)!在屏幕的左侧,将有一个**密钥**菜单项。点击它,按照指示创建一个新的密钥并记下它,因为在你看到它之后,你将无法访问它。

Groq 的一个非常好的特点是它为你提供了访问许多当前可用的顶级开源模型的机会。要查看有哪些,请点击下面的链接,

GroqCloud

我将使用 Meta 最近发布的 Llama 3.1 70B 模型。

Python 代码

我们将代码分为两个主要部分。第一部分实现了从用户获取输入提示的基本功能。这将通过 Groq 传递给 Llama 3.1 70B 模型,以生成一个更详细、更专注的提示,该提示可以用作任何 LLM 的输入,希望这能产生更好的响应。

第二部分将是使用 Gradio 为我们的迷你应用添加前端。这将非常简单,只有一个输入字段用于初始用户提示,一个输出字段用于显示增强的 LLM 生成的提示。我们还将有一个按钮,它将调用我们在第一部分中开发的代码。

第一部分

from groq import Groq
# Initialize the Groq client
# In production systems we would use environment
# variables or another, more secure, method
# to deal with API keys
#
GROQ_API_KEY="YOUR_GROQ_API_KEY"

client = Groq(api_key=GROQ_API_KEY)
MODEL = "llama-3.1-70b-versatile"

# System message for the LLM
SYSTEM_MESSAGE = """
You are an assistant that takes a user's prompt and turns into a highly detailed,
focussed and relevant prompt. Do NOT answer the user's prompt.Just display the final prompt
that you come up with and NOTHING else. If your prompt is less than 200 words
it's UNLIKELY that it's detailed enough and you should try again. 
"""

# User prompt
user_prompt = """
You are an expert at growing apple trees. What is the best way 
to grow an apple tree.
"""

# Initial messages
messages = [
    {"role": "system", "content": SYSTEM_MESSAGE},
    {"role": "user", "content": user_prompt},
]

# Function to call the model
def call_llm(messages):
    response = client.chat.completions.create(
        model=MODEL,
        messages=messages,
        max_tokens=4096,
    )
    return response

response = call_llm(messages)

print(response.choices[0].message.content)

如你所见,我们使用了与 OpenAI 相同的初始提示。让我们看看我们得到了什么。

"Provide a comprehensive, step-by-step guide on growing a high-yielding 
and disease-resistant apple tree, focusing on the key factors that contribute 
to optimal growth and fruit production.nn- Start by specifying the ideal 
climate conditions for apple trees, including the recommended temperature 
range, sunlight exposure, and soil pH, highlighting the best regions and 
microclimates that are most suitable for cultivation.nn- Discuss the 
importance of selecting a disease-resistant and climate-suitable apple tree 
variety, providing examples of popular varieties for different climates and 
regions, such as 'Gala' and 'Granny Smith' for warm climates, and 'Braeburn' 
and 'McIntosh' for cooler climates.nn- Outline the optimal soil preparation 
process, including the necessary nutrients and organic matter requirements for 
apple trees, highlighting the benefits of composting, mulching, and cover 
cropping.nn- Describe the proper technique for planting an apple tree, 
including the recommended planting depth, spacing, and tree size, emphasizing 
the need for adequate support and pruning during the early growth stages.nn- 
Emphasize the critical importance of regular watering, fertilization, and pest 
management, providing tips on how to identify common pests and diseases, such 
as aphids, codling moths, and powdery mildew, and how to control them through 
integrated pest management (IPM) strategies.nn- Discuss the role of pruning 
in promoting healthy growth, fruiting, and air circulation within the tree, 
providing instructions on the best pruning techniques for different tree sizes 
and shapes, including the open center, central leader, and espalier 
systems.nn- Highlight the significance of thinning fruit to achieve optimal 
yields and fruit quality, outlining the recommended thinning times and methods,
 and emphasizing the importance of providing adequate fruit support and 
protection from pests and diseases.nn- Conclude by discussing the importance 
of ongoing monitoring and maintenance, including regular soil testing, nutrient
 management, and tree health assessments, to ensure the long-term productivity 
and health of the apple tree."

完全不赖。

好的,我很高兴我们的提示增强器工作得很好。接下来,我们需要使用 Gradio 设计我们的前端。

第二部分

对于这部分,我向 ChatGPT 询问如何将第一部分的代码集成到 Gradio 应用中。

在 ChatGPT(模型 4o)中,我输入了以下内容,

I have developed some prompt enhancer code, shown below that I want to 
incorporate into a Gradio app. Can you code this up for me. The Gradio 
screen should be simple, one input box to hold the initial user's prompt 
string, and below that, a larger output box to dipslay the result of the 
prompt enhancer output. Blow both of these should be a button titled generate 
that calls my prompt enhancer function. Here is my existing prompt enhancer 
code. Please amend as required so that it works with my Gradio app ...

... I put my code from part 1 was here ... 

经过几次调整,主要是由于使用 CSS 使输出框可调整大小,ChatGPT 返回了以下代码。

import gradio as gr
from groq import Groq

# Initialize the Groq client with your API key
GROQ_API_KEY="YOUR_GROQ_API_KEY"
client = Groq(api_key=GROQ_API_KEY)
MODEL = "llama-3.1-70b-versatile"

# System message for the LLM
SYSTEM_MESSAGE = """
DO NOT answer the user's prompt, instead you will take the user's prompt 
and convert it into a highly detailed, focused, and relevant prompt.
If your prompt is less than 200 words, it's UNLIKELY that it's detailed enough 
and you should try again. Just display the final prompt that you come up with 
and NOTHING else. 
"""

# Function to call the LLM
def call_llm(user_prompt):
    try:
        messages = [
            {"role": "system", "content": SYSTEM_MESSAGE},
            {"role": "user", "content": user_prompt},
        ]
        response = client.chat.completions.create(
            model=MODEL,
            messages=messages,
            max_tokens=4096,
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio interface function
def enhance_prompt(user_input):
    return call_llm(user_input)

# Custom CSS to make only the output box resizable with the stretch icon
custom_css = """
#output_box textarea {
    resize: both; /* Allow both horizontal and vertical resizing */
    overflow: auto; /* Enable scrollbars when needed */
    min-height: 150px; /* Minimum height for the box */
    max-height: 500px; /* Maximum height for the box */
}
"""

# Create the Gradio interface
with gr.Blocks(css=custom_css) as demo:
    gr.Markdown("## Prompt Enhancer")

    # Input for user's prompt
    user_input = gr.Textbox(
        label="Enter your prompt here:",
        placeholder="Type your prompt here...",
        lines=3
    )

    # Output for enhanced prompt with resizable option
    output = gr.Textbox(
        label="Enhanced Prompt:",
        placeholder="Your enhanced prompt will appear here...",
        lines=8,
        elem_id="output_box"  # Adding an ID for custom CSS
    ) 

    # Button to generate the enhanced prompt
    generate_btn = gr.Button("Generate Enhanced Prompt")

    # Bind the button to the function
    generate_btn.click(fn=enhance_prompt, inputs=user_input, outputs=output)

# Launch the app
demo.launch()

我运行了这段代码,输入了之前的相同提示,输出看起来像这样,

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/89caacf7ac88d91677403f5420b7357b.png

作者图片

我对此非常满意。但如果增强提示不是你想要的,只需重新生成另一个即可。因为我们使用 Groq,所以这非常快。

摘要

在这篇文章中,我介绍了 OpenAI 最近添加的增强提示生成功能,并演示了如何使用它。此外,我还提供了关于如何使用编码复制此类功能的见解。

我首先引导你设置一个新的开发环境,这对于任何编码项目都是必要的。接下来,我展示了如何使用开源的 LLM(大型语言模型)来模拟 OpenAI 的功能,特别是通过 Groq 使用 Meta 的 Llama 3.1 70B 模型,并结合 Gradio 开发的前端。我还包含了实现此解决方案所需的完整源代码。

我们的开发分为两个关键部分。最初,代码处理基本的用户提示,并使用 Groq 对其进行细化,以生成一个更专注、更详细的提示。在第二部分,我利用 ChatGPT 帮助将初始代码集成到我们的 Gradio 前端中,成功运行了组合应用程序。

我的目标是展示使用 Groq 和 Gradio 等工具增强用户与机器学习模型交互的有效性和便捷性。本指南是一个全面的教程,用于复制类似于 OpenAI 的复杂提示增强功能。

_ 好的,现在就到这里吧。我希望你觉得这篇文章有用。如果你觉得有用,请点击这个链接查看我的个人资料页面。从那里,你可以看到我发布的其他故事,并订阅以获取我发布新内容的通知。_

如果你喜欢这个内容,我想你会觉得这些文章很有趣。

使用 Gemini Pro 1.5 处理音频数据

DuckDB V1 发布

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值