AI Generated Code Snippet Of the Day (CodeSOD)

Topic: GPT

A code snippet that analyzes a user's personality traits and interests from their social media bios and posts to generate personalized character backstories and world-building ideas for tabletop role-playing games.

Snippet:

<pre class="hljs language-python"><code class="hljs language-python"><span class="hljs-keyword">import</span> openai <span class="hljs-keyword">def</span> <span class="hljs-title function_">generate_backstory</span>(<span class="hljs-params">bio, posts, api_key</span>): openai.api_key = api_key prompt = <span class="hljs-string">f&quot;Analyze the following user&#x27;s social media bio and posts to extract personality traits \ and interests. Then generate a character backstory and world-building ideas for a TTRPG.\n\n\ Bio: <span class="hljs-subst">{bio}</span>\n\nPosts:\n<span class="hljs-subst">{posts}</span>\n\nTraits, Interests and Backstory:&quot;</span> response = openai.Completion.create( engine=<span class="hljs-string">&quot;text-davinci-004&quot;</span>, prompt=prompt, temperature=<span class="hljs-number">0.7</span>, max_tokens=<span class="hljs-number">200</span> ) <span class="hljs-keyword">return</span> response.choices[<span class="hljs-number">0</span>].text.strip() <span class="hljs-comment"># Example usage</span> bio = <span class="hljs-string">&quot;Nature lover, aspiring novelist, RPG enthusiast&quot;</span> posts = <span class="hljs-string">&quot;Always hiking in the mountains. Writing fantasy stories based on ancient myths.&quot;</span> api_key = <span class="hljs-string">&quot;your_openai_api_key&quot;</span> backstory = generate_backstory(bio, posts, api_key) <span class="hljs-built_in">print</span>(backstory) </code></pre>

Snippet:

<pre class="hljs language-javascript"><code class="hljs language-javascript"><span class="hljs-keyword">const</span> axios = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;axios&#x27;</span>); <span class="hljs-keyword">const</span> openai = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;openai&#x27;</span>); openai.<span class="hljs-property">apiKey</span> = <span class="hljs-string">&#x27;YOUR_OPENAI_API_KEY&#x27;</span>; <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">analyzeUserData</span>(<span class="hljs-params">userId</span>) { <span class="hljs-keyword">try</span> { <span class="hljs-keyword">const</span> userData = <span class="hljs-keyword">await</span> axios.<span class="hljs-title function_">get</span>(<span class="hljs-string">`https://api.socialmedia.com/users/<span class="hljs-subst">${userId}</span>/data`</span>); <span class="hljs-keyword">const</span> personalityInput = <span class="hljs-string">`<span class="hljs-subst">${userData.bio}</span> <span class="hljs-subst">${userData.posts.join(<span class="hljs-string">&#x27; &#x27;</span>)}</span>`</span>; <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> openai.<span class="hljs-property">Completion</span>.<span class="hljs-title function_">create</span>({ <span class="hljs-attr">model</span>: <span class="hljs-string">&#x27;text-davinci-003&#x27;</span>, <span class="hljs-attr">prompt</span>: <span class="hljs-string">`Analyze the following personality traits and interests to create a tabletop RPG character backstory:\n\n<span class="hljs-subst">${personalityInput}</span>`</span>, <span class="hljs-attr">max_tokens</span>: <span class="hljs-number">200</span>, }); <span class="hljs-keyword">const</span> characterBackstory = response.<span class="hljs-property">choices</span>[<span class="hljs-number">0</span>].<span class="hljs-property">text</span>.<span class="hljs-title function_">trim</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;Generated Character Backstory:&#x27;</span>, characterBackstory); } <span class="hljs-keyword">catch</span> (error) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">&#x27;Error analyzing user data:&#x27;</span>, error); } } <span class="hljs-title function_">analyzeUserData</span>(<span class="hljs-string">&#x27;user123&#x27;</span>); </code></pre>

Snippet:

<pre class="hljs language-go"><code class="hljs language-go"><span class="hljs-keyword">package</span> main <span class="hljs-keyword">import</span> ( <span class="hljs-string">&quot;fmt&quot;</span> <span class="hljs-string">&quot;strings&quot;</span> ) <span class="hljs-keyword">type</span> User <span class="hljs-keyword">struct</span> { Bio <span class="hljs-type">string</span> Posts []<span class="hljs-type">string</span> } <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">analyze</span><span class="hljs-params">(user User)</span></span> (<span class="hljs-type">string</span>, <span class="hljs-type">string</span>) { traits := extract(user.Bio + <span class="hljs-string">&quot; &quot;</span> + strings.Join(user.Posts, <span class="hljs-string">&quot; &quot;</span>)) backstory := generateBackstory(traits) worldIdea := suggestWorld(traits) <span class="hljs-keyword">return</span> backstory, worldIdea } <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">extract</span><span class="hljs-params">(content <span class="hljs-type">string</span>)</span></span> <span class="hljs-keyword">map</span>[<span class="hljs-type">string</span>]<span class="hljs-type">int</span> { traits := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[<span class="hljs-type">string</span>]<span class="hljs-type">int</span>) <span class="hljs-keyword">for</span> _, word := <span class="hljs-keyword">range</span> strings.Fields(content) { traits[strings.ToLower(word)]++ } <span class="hljs-keyword">return</span> traits } <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">generateBackstory</span><span class="hljs-params">(traits <span class="hljs-keyword">map</span>[<span class="hljs-type">string</span>]<span class="hljs-type">int</span>)</span></span> <span class="hljs-type">string</span> { <span class="hljs-keyword">if</span> traits[<span class="hljs-string">&quot;adventurous&quot;</span>] &gt; <span class="hljs-number">2</span> { <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;An intrepid explorer always seeking new challenges.&quot;</span> } <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;A curious intellectual yearning for forbidden knowledge.&quot;</span> } <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">suggestWorld</span><span class="hljs-params">(traits <span class="hljs-keyword">map</span>[<span class="hljs-type">string</span>]<span class="hljs-type">int</span>)</span></span> <span class="hljs-type">string</span> { <span class="hljs-keyword">if</span> traits[<span class="hljs-string">&quot;fantasy&quot;</span>] &gt; <span class="hljs-number">2</span> { <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Enigmatic lands filled with magic and mythical creatures.&quot;</span> } <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;A dystopian future where technology reigns supreme.&quot;</span> } <span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> { user := User{ Bio: <span class="hljs-string">&quot;Avid reader and fantasy lover.&quot;</span>, Posts: []<span class="hljs-type">string</span>{<span class="hljs-string">&quot;Just finished a great sci-fi novel!&quot;</span>}, } backstory, worldIdea := analyze(user) fmt.Println(<span class="hljs-string">&quot;Backstory:&quot;</span>, backstory) fmt.Println(<span class="hljs-string">&quot;World Idea:&quot;</span>, worldIdea) } </code></pre>

Code snippets are generated by OpenAI's ChatGPT and Anthropic's Claude. Both AI services can make mistakes. Use at your own risk.