应用蓝图的有效性和生成代码的质量取决于提示的清晰度和详细程度。使用 App Prototyping agent 生成和调试应用时,请参考以下提示编写准则:
- 明确具体内容:明确定义应用的功能、用户互动和数据要求。尽可能要求使用特定技术。例如,如果您尝试创建包含 3D 元素的 Web 应用,则可能需要让 Gemini 使用
three.js
。 - 询问有关设置服务的其他任务:Gemini 可以为您的应用编写代码,但实际上无法设置服务、启用 API 或预配资源(例如 Cloud Storage 存储桶或 Stripe 账号)。请务必告知 Gemini,如果它为特定服务编写代码,还应告知您如何设置这些服务。
- 使用 Gemini 优化提示:使用 Gemini for Google、Code 视图中的 与 Gemini in Firebase 聊天或其他 Gemini 界面优化提示。
- 提供背景信息:提供有关应用用途、目标受众群体和您希望提供的用户体验的背景信息。
- 使用示例:如果可能,请提供示例,说明用户应如何与应用互动或应显示哪些数据。
- 迭代和优化:采用迭代的方式进行开发。不妨先从基本请求开始,然后添加功能、测试并优化功能,然后再添加其他功能。
遵循这些准则,您可以大幅提升 Firebase Studio 生成的应用蓝图质量和相关性,并提高调试成功率。
使用其他提示来优化回答
如果初始结果未达预期,您可以通过添加更多细节或具体指示来优化提示:
- 添加限制条件:指定界面、数据模型或功能的限制条件。
- 提供示例:提供示例,说明用户应如何与应用互动以及应显示哪些数据。
- 使用关键字:使用关键字描述您希望 LLM 与所需输出类别相关联的功能或能力。例如,您可以在提示中使用 Material Design,以表明您希望应用遵循 Google 设计标准。
- 请求进行特定更改:请求对生成的代码或蓝图进行特定更改。请在单独的请求中添加每项功能请求。
- 让模型进行推理:如果您无法获得理想结果,请尝试要求模型通过提示进行推理。例如,您可以这样提示:“请分步思考。我想为任务应用创建一个输入框,其中包含‘添加任务’按钮和‘取消’按钮。”
通过使用这些方法,您可以迭代优化提示,获取更符合预期的结果。
应用蓝图有效提示示例
以下是一些针对不同类型应用的有效提示示例:
简单任务跟踪器:
Create a simple web app that displays a list of to-do items.
Users should be able to add new items to the list and mark items as complete.
Provide a way to delete items and export the list as a text file.
Use a clean and modern UI using Google Material Design principles.
预算管理应用:
A budgeting and expense tracking app with spending categories, charts, and
budget goals. Include a clean dashboard with key insights. It should include
spending categories, charts, and budget goals. The app should allow users to
manually add expenses or upload csv files. The app should also allow users to
upload receipts, then use AI to convert the receipt into an expense entry that
users can edit. Data should be stored in browser cache, with download and
delete options.
游戏应用提示,并辅以 Gemini (Markdown):
Generate a delightful sliding number puzzle game (15-puzzle) with Javascript,
HTML, and CSS.
- **Setup:** Create a 4x4 grid with numbers 1-15 and one empty space. Use a
**solvable shuffling algorithm** to randomize the start.
- **Gameplay:** Allow clicking tiles **adjacent to the empty space** to slide
them. Count and display the number of moves.
- **Timer:** Include a **countdown timer** starting at **120 seconds**
(2 minutes).
- **End Conditions:**
- If solved (numbers 1-15 in order): Alert with an encouraging winning
statement generated by AI.
- If timer reaches 0: Alert with a funny retort generated by AI.
调试技巧
您可以在 Firebase 中使用 Gemini,以便在 Code 工作区或 App Prototyping agent 中通过聊天功能调试代码。
虽然 Gemini 可以为您编写代码,但有时也可能会产生错误。检测到错误时,它会尝试进行修正。如果您发现无法根据错误消息解决问题,可以尝试以下一些方法:
描述问题:在聊天界面中,尽可能简明扼要地描述您遇到的问题。虽然 Gemini 可能能访问错误消息和日志等上下文,但可能无法完全理解全部内容。同时描述行为和错误消息,有助于 Gemini 更快定位并修复错误。
提出具体问题:请大胆直接向 Gemini 询问您的代码相关问题。例如,“此函数中可能导致空指针异常的原因是什么?”或“如何防止这个竞态条件?”
分解复杂问题:如果您面临复杂问题,请将其拆分为更小、更易管理的部分。请 Gemini 帮助您逐一调试每个部分,并逐步分析问题。
使用代码块:分享代码片段时,请使用代码块格式,以确保代码排版正确。这样有助于 Gemini 更好地阅读和理解您的代码。
迭代和优化:Gemini 可能无法在第一次尝试时就提供最佳解决方案。请查看回答,提出澄清性问题,并根据需要提供更多信息。
避免提示循环:如果 Gemini 陷入循环或无法回答您的问题,请尝试重新表述提示或提供更多上下文信息。有时,仅仅换种说法提出问题,就能帮助 Gemini 更好地理解您的意图。
如果换种说法重复提示无法解决循环问题,请尝试以下方法:
发起新聊天:如果您在 Code 工作区中使用 Gemini in Firebase 聊天,请发起新聊天会话以重置 Gemini 的上下文。这有助于消除 Gemini 在之前对话中可能形成的误解或假设。
提供反例:如果 Gemini 出现错误假设,请提供反例,帮助其理解正确的行为。