Created
May 23, 2023 22:20
-
-
Save bramses/7c1fc5f987032bd946c1d21cfadc44a7 to your computer and use it in GitHub Desktop.
Using Guidance to create a Twitch thank you bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div id=\"guidance-stop-button-ec6b9593-c157-435e-ab4c-5d2c792b9d2d\" style=\"cursor: pointer; margin: 0px; display: none; float: right; padding: 3px; border-radius: 4px 4px 4px 4px; border: 0px solid rgba(127, 127, 127, 1); padding-left: 10px; padding-right: 10px; font-size: 13px; background-color: rgba(127, 127, 127, 0.25);\">Stop program</div><div id=\"guidance-content-ec6b9593-c157-435e-ab4c-5d2c792b9d2d\"><pre style='margin: 0px; padding: 0px; padding-left: 8px; margin-left: -8px; border-radius: 0px; border-left: 1px solid rgba(127, 127, 127, 0.2); white-space: pre-wrap; font-family: ColfaxAI, Arial; font-size: 15px; line-height: 23px;'><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>system</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>You are a Twitch follow thank you bot that delivers personal messages based on chat history and a fun fact.</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>I need a thank you message for <span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='{{username}}'>twitch_frog_pog</span>.\n", | |
"This is their chat history:\n", | |
"<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='{{chat_history}}'>['i only code in Java 7', 'i take my coffee with three creams', 'IM TRAPPED IN A CAVE OH GOD HELP PLEASE']</span>\n", | |
"\n", | |
"This is their random fun fact, merge it into the chat history\n", | |
"<span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='{{fun_fact}}'>When we eat corn or peas we are eating seeds, and when we eat radish or carrot, we are eating roots. Cauliflower and broccoli plants produce flowers we like to eat. Potatoes grow under- ground, but the part we eat is not a root.</span></div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='{{gen 'funfact_merge' temperature=0.0 max_tokens=300}}'>Thank you for following, twitch_frog_pog! I see that you only code in Java 7, take your coffee with three creams, and are currently trapped in a cave (I hope you get out soon!). Here's a fun fact for you: when we eat corn or peas, we are eating seeds, and when we eat radish or carrot, we are eating roots. Cauliflower and broccoli plants produce flowers we like to eat. Potatoes grow underground, but the part we eat is not a root. Thanks again for following!</span></div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>user</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'>Use the following message: <span style='background-color: rgba(0, 138.56128016, 250.76166089, 0.25); display: inline;' title='{{funfact_merge}}'>Thank you for following, twitch_frog_pog! I see that you only code in Java 7, take your coffee with three creams, and are currently trapped in a cave (I hope you get out soon!). Here's a fun fact for you: when we eat corn or peas, we are eating seeds, and when we eat radish or carrot, we are eating roots. Cauliflower and broccoli plants produce flowers we like to eat. Potatoes grow underground, but the part we eat is not a root. Thanks again for following!</span> Thank you for following me!</div></div><div style='display: flex; border-bottom: 1px solid rgba(127, 127, 127, 0.2); align-items: center;'><div style='flex: 0 0 80px; opacity: 0.5;'>assistant</div><div style='flex-grow: 1; padding: 5px; padding-top: 10px; padding-bottom: 10px; margin-top: 0px; white-space: pre-wrap; margin-bottom: 0px;'><span style='background-color: rgba(0, 165, 0, 0.25); opacity: 1.0; display: inline;' title='{{gen 'thankyou_msg' temperature=0.7 max_tokens=300}}'>I'm sorry for the confusion. Here's a revised thank you message for twitch_frog_pog:\n", | |
"\n", | |
"Thank you for following me, twitch_frog_pog! I noticed from your chat history that you only code in Java 7, take your coffee with three creams, and are currently trapped in a cave (I hope you get out soon!). Here's a fun fact for you: when we eat corn or peas, we are eating seeds, and when we eat radish or carrot, we are eating roots. Cauliflower and broccoli plants produce flowers we like to eat. Potatoes grow underground, but the part we eat is not a root. Thank you for joining my community and I look forward to seeing you in future streams!</span></div></div></pre></div>\n", | |
"<script type=\"text/javascript\">(()=>{var t={296:(t,e,n)=>{var i=NaN,o=\"[object Symbol]\",r=/^\\s+|\\s+$/g,a=/^[-+]0x[0-9a-f]+$/i,s=/^0b[01]+$/i,c=/^0o[0-7]+$/i,d=parseInt,u=\"object\"==typeof n.g&&n.g&&n.g.Object===Object&&n.g,l=\"object\"==typeof self&&self&&self.Object===Object&&self,f=u||l||Function(\"return this\")(),h=Object.prototype.toString,p=Math.max,m=Math.min,g=function(){return f.Date.now()};function b(t){var e=typeof t;return!!t&&(\"object\"==e||\"function\"==e)}function y(t){if(\"number\"==typeof t)return t;if(function(t){return\"symbol\"==typeof t||function(t){return!!t&&\"object\"==typeof t}(t)&&h.call(t)==o}(t))return i;if(b(t)){var e=\"function\"==typeof t.valueOf?t.valueOf():t;t=b(e)?e+\"\":e}if(\"string\"!=typeof t)return 0===t?t:+t;t=t.replace(r,\"\");var n=s.test(t);return n||c.test(t)?d(t.slice(2),n?2:8):a.test(t)?i:+t}t.exports=function(t,e,n){var i,o,r,a,s,c,d=0,u=!1,l=!1,f=!0;if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");function h(e){var n=i,r=o;return i=o=void 0,d=e,a=t.apply(r,n)}function v(t){var n=t-c;return void 0===c||n>=e||n<0||l&&t-d>=r}function _(){var t=g();if(v(t))return w(t);s=setTimeout(_,function(t){var n=e-(t-c);return l?m(n,r-(t-d)):n}(t))}function w(t){return s=void 0,f&&i?h(t):(i=o=void 0,a)}function j(){var t=g(),n=v(t);if(i=arguments,o=this,c=t,n){if(void 0===s)return function(t){return d=t,s=setTimeout(_,e),u?h(t):a}(c);if(l)return s=setTimeout(_,e),h(c)}return void 0===s&&(s=setTimeout(_,e)),a}return e=y(e)||0,b(n)&&(u=!!n.leading,r=(l=\"maxWait\"in n)?p(y(n.maxWait)||0,e):r,f=\"trailing\"in n?!!n.trailing:f),j.cancel=function(){void 0!==s&&clearTimeout(s),d=0,i=c=o=s=void 0},j.flush=function(){return void 0===s?a:w(g())},j}},777:t=>{var e,n,i=Math.max,o=(e=function(t,e){return function(t,e,n){if(\"function\"!=typeof t)throw new TypeError(\"Expected a function\");return setTimeout((function(){t.apply(void 0,n)}),1)}(t,0,e)},n=i(void 0===n?e.length-1:n,0),function(){for(var t=arguments,o=-1,r=i(t.length-n,0),a=Array(r);++o<r;)a[o]=t[n+o];o=-1;for(var s=Array(n+1);++o<n;)s[o]=t[o];return s[n]=a,function(t,e,n){switch(n.length){case 0:return t.call(e);case 1:return t.call(e,n[0]);case 2:return t.call(e,n[0],n[1]);case 3:return t.call(e,n[0],n[1],n[2])}return t.apply(e,n)}(e,this,s)});t.exports=o}},e={};function n(i){var o=e[i];if(void 0!==o)return o.exports;var r=e[i]={exports:{}};return t[i](r,r.exports,n),r.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var i in e)n.o(e,i)&&!n.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},n.g=function(){if(\"object\"==typeof globalThis)return globalThis;try{return this||new Function(\"return this\")()}catch(t){if(\"object\"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";const t=t=>{const e=new Set;do{for(const n of Reflect.ownKeys(t))e.add([t,n])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e};function e(e,{include:n,exclude:i}={}){const o=t=>{const e=e=>\"string\"==typeof e?t===e:e.test(t);return n?n.some(e):!i||!i.some(e)};for(const[n,i]of t(e.constructor.prototype)){if(\"constructor\"===i||!o(i))continue;const t=Reflect.getOwnPropertyDescriptor(n,i);t&&\"function\"==typeof t.value&&(e[i]=e[i].bind(e))}return e}var i=n(777),o=n.n(i),r=n(296),a=n.n(r);class s{constructor(t,n){e(this),this.interfaceId=t,this.callbackMap={},this.data={},this.pendingData={},this.jcomm=new c(\"guidance_interface_target_\"+this.interfaceId,this.updateData,\"open\"),this.debouncedSendPendingData500=a()(this.sendPendingData,500),this.debouncedSendPendingData1000=a()(this.sendPendingData,1e3),n&&o()(n)}send(t,e){this.addPendingData(t,e),this.sendPendingData()}sendEvent(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.sendPendingData()}debouncedSendEvent500(t){for(const e of Object.keys(t))this.addPendingData(e,t[e]);this.debouncedSendPendingData500()}debouncedSend500(t,e){this.addPendingData(t,e),this.debouncedSendPendingData500()}debouncedSend1000(t,e){this.addPendingData(t,e),this.debouncedSendPendingData1000()}addPendingData(t,e){Array.isArray(t)||(t=[t]);for(const n in t)this.pendingData[t[n]]=e}updateData(t){t=JSON.parse(t.data);for(const e in t)this.data[e]=t[e];for(const e in t)e in this.callbackMap&&this.callbackMap[e](this.data[e])}subscribe(t,e){this.callbackMap[t]=e,o()((e=>this.callbackMap[t](this.data[t])))}sendPendingData(){this.jcomm.send_data(this.pendingData),this.pendingData={}}}class c{constructor(t,e,n=\"open\"){this._fire_callback=this._fire_callback.bind(this),this._register=this._register.bind(this),this.jcomm=void 0,this.callback=e,void 0!==window.Jupyter?\"register\"===n?Jupyter.notebook.kernel.comm_manager.register_target(t,this._register):(this.jcomm=Jupyter.notebook.kernel.comm_manager.new_comm(t),this.jcomm.on_msg(this._fire_callback)):void 0!==window._mgr&&(\"register\"===n?window._mgr.widgetManager.proxyKernel.registerCommTarget(t,this._register):(this.jcomm=window._mgr.widgetManager.proxyKernel.createComm(t),this.jcomm.open({},\"\"),this.jcomm.onMsg=this._fire_callback))}send_data(t){void 0!==this.jcomm?this.jcomm.send(t):console.error(\"Jupyter comm module not yet loaded! So we can't send the message.\")}_register(t,e){this.jcomm=t,this.jcomm.on_msg(this._fire_callback)}_fire_callback(t){this.callback(t.content.data)}}class d{constructor(t,n){e(this),this.id=t,this.comm=new s(t),this.comm.subscribe(\"append\",this.appendData),this.comm.subscribe(\"replace\",this.replaceData),this.comm.subscribe(\"event\",this.eventOccurred),this.element=document.getElementById(\"guidance-content-\"+t),this.stop_button=document.getElementById(\"guidance-stop-button-\"+t),this.stop_button.onclick=()=>this.comm.send(\"event\",\"stop\")}appendData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML+=t)}replaceData(t){t&&(this.stop_button.style.display=\"inline-block\",this.element.innerHTML=t)}eventOccurred(t){\"complete\"===t&&(this.stop_button.style.display=\"none\")}}window._guidanceDisplay=function(t,e){return new d(t,e)}})()})();; window._guidanceDisplay(\"ec6b9593-c157-435e-ab4c-5d2c792b9d2d\");</script>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"{{#system~}}\n", | |
"You are a Twitch follow thank you bot that delivers personal messages based on chat history and a fun fact.\n", | |
"{{~/system}}\n", | |
"\n", | |
"{{#user~}}\n", | |
"I need a thank you message for {{username}}.\n", | |
"This is their chat history:\n", | |
"{{chat_history}}\n", | |
"\n", | |
"This is their random fun fact, merge it into the chat history\n", | |
"{{fun_fact}}\n", | |
"{{~/user}}\n", | |
"\n", | |
"{{#assistant~}}\n", | |
"{{gen 'funfact_merge' temperature=0.0 max_tokens=300}}\n", | |
"{{~/assistant}}\n", | |
"\n", | |
"{{#user~}}\n", | |
"Use the following message: {{funfact_merge}} Thank you for following me!\n", | |
"{{~/user}}\n", | |
"\n", | |
"\n", | |
"{{#assistant~}}\n", | |
"{{gen 'thankyou_msg' temperature=0.7 max_tokens=300}}\n", | |
"{{~/assistant}}\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# twitch thank you personal response with guidance\n", | |
"\n", | |
"import guidance\n", | |
"import dotenv\n", | |
"\n", | |
"# load the .env file\n", | |
"dotenv.load_dotenv()\n", | |
"\n", | |
"gpt4 = guidance.llms.OpenAI(\"gpt-3.5-turbo\")\n", | |
"\n", | |
"\n", | |
"twitch_thanker = guidance('''\n", | |
"{{#system~}}\n", | |
"You are a Twitch follow thank you bot that delivers personal messages based on chat history and a fun fact.\n", | |
"{{~/system}}\n", | |
"\n", | |
"{{#user~}}\n", | |
"I need a thank you message for {{username}}.\n", | |
"This is their chat history:\n", | |
"{{chat_history}}\n", | |
"\n", | |
"This is their random fun fact, merge it into the chat history\n", | |
"{{fun_fact}}\n", | |
"{{~/user}}\n", | |
"\n", | |
"{{#assistant~}}\n", | |
"{{gen 'funfact_merge' temperature=0.0 max_tokens=300}}\n", | |
"{{~/assistant}}\n", | |
"\n", | |
"{{#user~}}\n", | |
"Use the following message: {{funfact_merge}} Thank you for following me!\n", | |
"{{~/user}}\n", | |
"\n", | |
"\n", | |
"{{#assistant~}}\n", | |
"{{gen 'thankyou_msg' temperature=0.7 max_tokens=300}}\n", | |
"{{~/assistant}}\n", | |
"''', llm=gpt4)\n", | |
"\n", | |
"# generate_random_fun_fact = guidance('''\n", | |
"# {{#system~}}\n", | |
"# Come up with a random fun fact.\n", | |
"# {{~/system}}\n", | |
"\n", | |
"# {{#assistant~}}\n", | |
"# {{gen 'fun_facts' n=3 temperature=1.0 max_tokens=500}}\n", | |
"# {{~/assistant}}\n", | |
"# ''', llm=gpt4)\n", | |
"\n", | |
"twitch_thanker(username=\"twitch_frog_pog\", chat_history=[\"i only code in Java 7\", \"i take my coffee with three creams\", \"IM TRAPPED IN A CAVE OH GOD HELP PLEASE\"], fun_fact=\"When we eat corn or peas we are eating seeds, and when we eat radish or carrot, we are eating roots. Cauliflower and broccoli plants produce flowers we like to eat. Potatoes grow under- ground, but the part we eat is not a root.\")\n", | |
"print(twitch_thanker)\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "guidance-J1ubIFML-py3.9", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.12" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment