├── .gitattributes ├── app.py └── tutorial.ipynb /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import guidance 2 | from dotenv import load_dotenv 3 | import urllib.parse 4 | import streamlit as st 5 | 6 | load_dotenv() 7 | guidance.llm = guidance.llms.OpenAI("text-davinci-003") 8 | 9 | def generate_email(email): 10 | priorities = ["low priority", "medium priority", "high priority"] 11 | 12 | email_generator = guidance( 13 | ''' 14 | {{#block hidden=True~}} 15 | Here is the customer message we received: {{email}} 16 | Please give it a priority score 17 | priority score: {{select "priority" options=priorities}} 18 | {{~/block~}} 19 | 20 | {{#block hidden=True~}} 21 | You are a world class customer support; Your goal is to generate an response based on the customer message and next steps; 22 | Here is the customer message to respond: {{email}} 23 | Generate an opening & one paragraph of response to the customer message at {{priority}}: 24 | {{gen 'email'}} 25 | {{~/block~}} 26 | 27 | {{email}} 28 | 29 | {{#if priority=='high priority'}}Would love to setup a call this/next week, here is the calendly link: https://calendly.com/jason-zhou{{/if}} 30 | 31 | Best regards 32 | 33 | Jason 34 | ''') 35 | 36 | email_response = email_generator(email=email, priorities=priorities) 37 | print(email_response) 38 | 39 | return email_response 40 | 41 | 42 | 43 | def generate_story(story_idea): 44 | 45 | story = guidance(''' 46 | {{#block hidden=True~}} 47 | You are a world class story teller; Your goal is to generate a short tiny story less than 200 words based on a story idea; 48 | 49 | Here is the story idea: {{story_idea}} 50 | Story: {{gen 'story' temperature=0}} 51 | {{/block~}} 52 | 53 | {{#block hidden=True~}} 54 | You are a world class AI artiest who are great at generating text to image prompts for the story; 55 | Your goal is to generate a good text to image prompt and put it in a url that can generate image from the prompt; 56 | 57 | Story: You find yourself standing on the deck of a pirate ship in the middle of the ocean. You are checking if there are still people on the ship 58 | Image url markdown:  59 | 60 | Story: {{story}} 61 | Image url markdown: {{gen 'image_url' temperature=0 max_tokens=500}}) 62 | {{~/block~}} 63 | 64 | Story: {{~story~}} 65 | {{image_url}} 66 | ''') 67 | 68 | story = story(story_idea=story_idea) 69 | print(story) 70 | return story 71 | 72 | 73 | def generate_chart(query): 74 | 75 | def parse_chart_link(chart_details): 76 | encoded_chart_details = urllib.parse.quote(chart_details, safe='') 77 | 78 | output = "" 79 | 80 | return output 81 | 82 | examples = [ 83 | { 84 | 'input': "Make a chart of the 5 tallest mountains", 85 | 'output': {"type":"bar","data":{"labels":["Mount Everest","K2","Kangchenjunga","Lhotse","Makalu"], "datasets":[{"label":"Height (m)","data":[8848,8611,8586,8516,8485]}]}} 86 | }, 87 | { 88 | 'input': "Create a pie chart showing the population of the world by continent", 89 | 'output': {"type":"pie","data":{"labels":["Africa","Asia","Europe","North America","South America","Oceania"], "datasets":[{"label":"Population (millions)","data": [1235.5,4436.6,738.8,571.4,422.5,41.3]}]}} 90 | } 91 | ] 92 | 93 | guidance.llm = guidance.llms.OpenAI("text-davinci-003") 94 | 95 | chart = guidance( 96 | ''' 97 | {{#block hidden=True~}} 98 | You are a world class data analyst, You will generate chart output based on a natural language; 99 | 100 | {{~#each examples}} 101 | Q:{{this.input}} 102 | A:{{this.output}} 103 | --- 104 | {{~/each}} 105 | Q:{{query}} 106 | A:{{gen 'chart' temperature=0 max_tokens=500}} 107 | {{/block~}} 108 | Hello here is the chart you want 109 | {{parse_chart_link chart}} 110 | ''') 111 | 112 | return chart(query=query, examples=examples, parse_chart_link=parse_chart_link) 113 | 114 | 115 | 116 | def main(): 117 | st.set_page_config(page_title="Programmable prompts", page_icon=":bird:") 118 | 119 | tab1, tab2, tab3 = st.tabs(["Chart generator", "Story generator", "generate email"]) 120 | 121 | with tab1: 122 | st.header("Chart generator") 123 | prompt = st.text_input("Enter a query") 124 | if prompt: 125 | chart = generate_chart(prompt) 126 | st.markdown(chart) 127 | 128 | with tab2: 129 | st.header("Story generator") 130 | prompt = st.text_input("Enter a story idea") 131 | if prompt: 132 | story = generate_story(prompt) 133 | st.markdown(story) 134 | 135 | with tab3: 136 | st.header("Email generator") 137 | email = st.text_area("Customer email to respond") 138 | if email: 139 | email_response = generate_email(email) 140 | st.write(email_response) 141 | 142 | if __name__ == '__main__': 143 | main() -------------------------------------------------------------------------------- /tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Requirement already satisfied: guidance in /opt/homebrew/lib/python3.11/site-packages (0.0.64)\n", 13 | "Requirement already satisfied: diskcache in /opt/homebrew/lib/python3.11/site-packages (from guidance) (5.6.1)\n", 14 | "Requirement already satisfied: gptcache in /opt/homebrew/lib/python3.11/site-packages (from guidance) (0.1.40)\n", 15 | "Requirement already satisfied: openai>=0.27.8 in /opt/homebrew/lib/python3.11/site-packages (from guidance) (0.27.8)\n", 16 | "Requirement already satisfied: pyparsing>=3.0.0 in /opt/homebrew/lib/python3.11/site-packages (from guidance) (3.0.9)\n", 17 | "Requirement already satisfied: pygtrie in /opt/homebrew/lib/python3.11/site-packages (from guidance) (2.5.0)\n", 18 | "Requirement already satisfied: platformdirs in /Users/jasonzhou/Library/Python/3.11/lib/python/site-packages (from guidance) (3.5.3)\n", 19 | "Requirement already satisfied: tiktoken>=0.3 in /opt/homebrew/lib/python3.11/site-packages (from guidance) (0.4.0)\n", 20 | "Requirement already satisfied: nest-asyncio in /Users/jasonzhou/Library/Python/3.11/lib/python/site-packages (from guidance) (1.5.6)\n", 21 | "Requirement already satisfied: msal in /opt/homebrew/lib/python3.11/site-packages (from guidance) (1.23.0)\n", 22 | "Requirement already satisfied: requests in /opt/homebrew/lib/python3.11/site-packages (from guidance) (2.31.0)\n", 23 | "Requirement already satisfied: numpy in /opt/homebrew/lib/python3.11/site-packages (from guidance) (1.25.2)\n", 24 | "Requirement already satisfied: aiohttp in /opt/homebrew/lib/python3.11/site-packages (from guidance) (3.8.5)\n", 25 | "Requirement already satisfied: tqdm in /opt/homebrew/lib/python3.11/site-packages (from openai>=0.27.8->guidance) (4.65.0)\n", 26 | "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests->guidance) (3.1.0)\n", 27 | "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests->guidance) (3.4)\n", 28 | "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/homebrew/lib/python3.11/site-packages (from requests->guidance) (1.26.15)\n", 29 | "Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.11/site-packages (from requests->guidance) (2022.12.7)\n", 30 | "Requirement already satisfied: regex>=2022.1.18 in /opt/homebrew/lib/python3.11/site-packages (from tiktoken>=0.3->guidance) (2023.6.3)\n", 31 | "Requirement already satisfied: attrs>=17.3.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->guidance) (23.1.0)\n", 32 | "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->guidance) (6.0.4)\n", 33 | "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->guidance) (4.0.2)\n", 34 | "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->guidance) (1.9.2)\n", 35 | "Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->guidance) (1.4.0)\n", 36 | "Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->guidance) (1.3.1)\n", 37 | "Requirement already satisfied: cachetools in /opt/homebrew/lib/python3.11/site-packages (from gptcache->guidance) (5.3.1)\n", 38 | "Requirement already satisfied: PyJWT[crypto]<3,>=1.0.0 in /opt/homebrew/lib/python3.11/site-packages (from msal->guidance) (2.8.0)\n", 39 | "Requirement already satisfied: cryptography<44,>=0.6 in /opt/homebrew/lib/python3.11/site-packages (from msal->guidance) (41.0.3)\n", 40 | "Requirement already satisfied: cffi>=1.12 in /opt/homebrew/lib/python3.11/site-packages (from cryptography<44,>=0.6->msal->guidance) (1.15.1)\n", 41 | "Requirement already satisfied: pycparser in /opt/homebrew/lib/python3.11/site-packages (from cffi>=1.12->cryptography<44,>=0.6->msal->guidance) (2.21)\n", 42 | "\n", 43 | "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n", 44 | "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.11 -m pip install --upgrade pip\u001b[0m\n", 45 | "Note: you may need to restart the kernel to use updated packages.\n", 46 | "Requirement already satisfied: openai in /opt/homebrew/lib/python3.11/site-packages (0.27.8)\n", 47 | "Requirement already satisfied: requests>=2.20 in /opt/homebrew/lib/python3.11/site-packages (from openai) (2.31.0)\n", 48 | "Requirement already satisfied: tqdm in /opt/homebrew/lib/python3.11/site-packages (from openai) (4.65.0)\n", 49 | "Requirement already satisfied: aiohttp in /opt/homebrew/lib/python3.11/site-packages (from openai) (3.8.5)\n", 50 | "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.20->openai) (3.1.0)\n", 51 | "Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.20->openai) (3.4)\n", 52 | "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.20->openai) (1.26.15)\n", 53 | "Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.20->openai) (2022.12.7)\n", 54 | "Requirement already satisfied: attrs>=17.3.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (23.1.0)\n", 55 | "Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (6.0.4)\n", 56 | "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (4.0.2)\n", 57 | "Requirement already satisfied: yarl<2.0,>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (1.9.2)\n", 58 | "Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (1.4.0)\n", 59 | "Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (1.3.1)\n", 60 | "\n", 61 | "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.2.1\u001b[0m\n", 62 | "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython3.11 -m pip install --upgrade pip\u001b[0m\n", 63 | "Note: you may need to restart the kernel to use updated packages.\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "%pip install guidance\n", 69 | "%pip install openai" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "start to install package: redis\n", 82 | "successfully installed package: redis\n", 83 | "start to install package: redis_om\n", 84 | "successfully installed package: redis_om\n" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "import guidance\n", 90 | "\n", 91 | "guidance.llm = guidance.llms.OpenAI(\"text-davinci-003\") " 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "#### Guardrail the LLM output structure" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 140, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "data": { 108 | "text/html": [ 109 | "
\n", 110 | "once steve jobs said:\n", 111 | ""Without guidance, AI will do more harm than good."\n", 112 | "Counter argument: "AI can be used to improve the lives of people, from medical diagnosis to autonomous vehicles, and with the right guidance, it can be a powerful tool for good."\n", 113 | "
Is the following sentence offensive? \n", 149 | "Sentence: your tacos taste can be improved\n", 150 | "Answer:No
User: your tacos taste too salty\n", 184 | "\n", 185 | "Assistant: I'm sorry to hear that. Is there anything else I can do to help?
\n",
229 | "\n",
230 | "Thanks for your question! Webflow has a lot of features that Wix doesn't have. One of the most notable features is the ability to create custom code. This allows you to create more complex websites with custom functionality. Additionally, Webflow also offers a wide range of design tools, including a visual editor, custom fonts, and more.\n",
231 | "\n",
232 | "Best regards\n",
233 | "Jason\n",
234 | " \n",
335 | " Hello here is the chart you want\n",
336 | " \n",
337 | " \n", 403 | "Story:\n", 405 | "\n", 406 | "The little girl lay in her bed, her eyes closed and her breathing slow and steady. She was dreaming of a world far away, a world of adventure and excitement. In her dream, she was soaring through the sky, the wind in her hair and the sun on her face. She was free, and nothing could stop her.\n", 407 | "\n", 408 | "She flew over mountains and valleys, forests and rivers, and she felt like she could go on forever. She was so high up that she could see the entire world below her, and it was beautiful.\n", 409 | "\n", 410 | "The little girl smiled in her sleep, content in the knowledge that she could go anywhere she wanted in her dreams. She was the master of her own destiny, and she could do anything she wanted.\n", 411 | "\n", 412 | "The little girl's dream eventually faded away, but the feeling of freedom and joy stayed with her. She knew that no matter what happened in the real world, she could always escape to her dream world and be free.\n", 413 | "\n", 414 | "