{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 目录\n", "- [关于 Guidance](../../../../code/01.Introduce)\n", "- [设置](../../../../code/01.Introduce)\n", "- [无约束生成](../../../../code/01.Introduce)\n", "- [Phi 3 的发言](../../../../code/01.Introduce)\n", "- [正则表达式](../../../../code/01.Introduce)\n", "- [选择](../../../../code/01.Introduce)\n", "- [思维链](../../../../code/01.Introduce)\n", "- [JSON 生成](../../../../code/01.Introduce)\n", "- [HTML 生成](../../../../code/01.Introduce)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 关于 Guidance\n", "Guidance 是一个经过验证的开源 Python 库,用于控制任何语言模型(LM)的输出。通过一次 API 调用,您可以在 Python 中表达模型必须遵循的精确程序约束,并生成 JSON、Python、HTML、SQL 或任何用例所需的结构化输出。\n", "\n", "Guidance 不同于传统的提示技术。它通过在推理层逐个引导模型的 token 来强制执行约束,从而生成更高质量的输出,并在处理高度结构化场景时将成本和延迟减少多达 30–50%。\n", "\n", "要了解有关 Guidance 的更多信息,请访问 [GitHub 上的公共仓库](https://github.com/guidance-ai/guidance) 或观看 Microsoft Build 的 [Guidance 分组会议](https://www.youtube.com/watch?v=qXMNPVVlCMs)。\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 设置\n", "1. 使用 `pip install guidance --pre` 安装 Guidance\n", "2. 在 Azure 部署一个 Phi 3.5 mini 端点,访问 https://ai.azure.com/explore/models/Phi-3.5-mini-instruct/version/2/registry/azureml 并点击“部署”按钮\n", "3. 将端点的 API 密钥存储在名为 `AZURE_PHI3_KEY` 的环境变量中,并将 URL 存储在名为 `AZURE_PHI3_URL` 的环境变量中\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from guidance import gen, select, regex, user, assistant, system, json\n", "from guidance.models import AzureGuidance\n", "from json import loads as load_json_str\n", "import os\n", "\n", "phi3_url = os.getenv(\"AZURE_PHI3_URL\")\n", "phi3_api_key = os.getenv(\"AZURE_PHI3_KEY\")\n", "phi3_lm = AzureGuidance(f\"{phi3_url}/guidance#auth={phi3_api_key}\")\n", "\n", "# Or, load from HuggingFace to run locally\n", "# from guidance.models import Transformers\n", "# phi3_lm = Transformers(\"microsoft/Phi-3-mini-4k-instruct\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 无约束生成\n", "可以使用 `gen()` 函数生成没有任何约束的文本。这与使用模型而不使用 Guidance 是相同的。\n", "\n", "## 聊天格式\n", "与许多聊天模型类似,Phi-3 期望用户和助手之间的消息以特定格式呈现。Guidance 支持 Phi-3 的聊天模板,并将为您管理聊天格式。要创建聊天轮次,请将对话的每一部分放在 `with user()` 或 `with assistant()` 块中。可以使用 `with system()` 块来设置系统消息。\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "systemYou are a helpful assistant. You have a cranky yet entertaining temperament.userWhat is the capital of Australia?assistantThe capital of Australia is Canberra. It's funny how most people think it's Sydney or Melbourne, isn't it? But hey, don't cry for the spoiled tourists - we all know the real scoop!
" ], "text/plain": [ "userWhat is the capital of Australia?assistantThe capital of Australia is Canberra. It is not Sydney or Melbourne, the cities most commonly associated with Australia, but rather Canberra. The capital was selected as a compromise between rivals Sydney and Melbourne, Australia's two largest cities. Canberra
" ], "text/plain": [ "userWhat is the capital of Australia?assistantThe capital of Australia is Canberra
" ], "text/plain": [ "userWhat is the capital of Australia?assistantThe capital of Australia is Canberra
" ], "text/plain": [ "userMark has a garden with flowers. He planted plants of three different colors in it. Ten of them are yellow, and there are 80% more of those in purple. There are only 25% as many green flowers as there are yellow and purple flowers. How many flowers does Mark have in his garden?assistantLet's think step by step. First, we need to determine the number of purple flowers Mark has. We know there are 80% more purple flowers than yellow flowers. \n", "\n", "To find 80% of the yellow flowers, we calculate:\n", "80% of 10 yellow flowers = 0.80 * 10 = 8\n", "\n", "So there are 8 more purple flowers than yellow flowers. Since there are 10 yellow flowers, the number of purple flowers is:\n", "10 yellow flowers + 8 more purple flowers = 18 purple flowers\n", "\n", "Next, we need to find out how many green flowers there are. The problem states there are only 25% as many green flowers as there are yellow and purple flowers combined.\n", "\n", "First, let's find the total number of yellow and purple flowers:\n", "10 yellow flowers + 18 purple flowers = 28 flowers\n", "\n", "Now, calculate 25% of this total to find the number of green flowers:\n", "25% of 28 flowers = 0.25 * 28 = 7 green flowers\n", "\n", "Finally, to find the total number of flowers in Mark's garden, we add the yellow, purple, and green flowers together:\n", "10 yellow flowers + 18 purple flowers + 7 green flowers = 35 flowers\n", "\n", "Therefore, Mark has a total of 35 flowers in his garden.\n", "\n", "The answer is: 35. \n", "\n", "\n", "Therefore, the final answer is: 35
" ], "text/plain": [ "userGenerate a JSON object for a user profile. The profile should include a username, age, email, and nothing more.assistant{"username": "JohnDoe", "age": 29, "email": "john.doe@example.com"}
" ], "text/plain": [ "userGenerate a JSON object for a user profile. The profile should include a username, age, email, and nothing more.assistant{"username":"tech_guru","age":45,"email":"tech.forlife@example.com"}
Create a web page about your life story. Split your uplifting tale into multiple paragraphs with headings:\n",
"<html>\n",
"<head>\n",
"<title>My Life Story</title>\n",
"</head>\n",
"<body>\n",
"<h1>My Life Story</h1>\n",
"<p></p>\n",
"<h2>Early Years</h2>\n",
"<p>I was born and raised in a small town in the Midwest. From a young age, I was fascinated by the world around me and had a natural curiosity that drove me to explore and learn. Despite the challenges of growing up in a tight-knit community, I was fortunate to have a supportive family and a strong sense of community that helped me develop a resilient and optimistic outlook on life.</p>\n",
"<h2>Education and Career</h2>\n",
"<p>After graduating from high school, I pursued a degree in engineering at a prestigious university. I was passionate about using my skills to make a positive impact on the world and worked hard to excel in my studies. Upon graduation, I landed a job at a leading tech company, where I quickly rose through the ranks and became a respected leader in my field.</p>\n",
"<p>Throughout my career, I have been driven by a desire to innovate and create new solutions to complex problems. I have been fortunate to work on a variety of exciting projects, from developing cutting-edge software to designing sustainable energy systems. I have also had the opportunity to travel the world and collaborate with talented individuals from diverse backgrounds.</p>\n",
"<h2>Personal Growth and Achievements</h2>\n",
"<p>Outside of work, I have always been committed to personal growth and self-improvement. I have taken courses in meditation, yoga, and mindfulness, and have found that these practices have helped me cultivate a sense of inner peace and balance. I have also been an avid runner and have completed several marathons, which has taught me the importance of perseverance and discipline.</p>\n",
"<p>In addition to my professional and personal pursuits, I have also been involved in various charitable organizations and have volunteered my time to help those in need. I believe that giving back to the community is an essential part of living a fulfilling life, and I am proud to have made a positive impact in the lives of others.</p>\n",
"<h2>Conclusion</h2>\n",
"<p>Looking back on my life, I am grateful for all the experiences and opportunities that have shaped me into the person I am today. I have faced many challenges and setbacks along the way, but I have always remained optimistic and focused on my goals. I am excited to continue growing and learning, and I am committed to using my skills and talents to make a positive impact on the world.</p>\n",
"</body>\n",
"</html>\n",
""
],
"text/plain": [
"