Topic:
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"Analyze the following user'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:"</span>
response = openai.Completion.create(
engine=<span class="hljs-string">"text-davinci-004"</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">"Nature lover, aspiring novelist, RPG enthusiast"</span>
posts = <span class="hljs-string">"Always hiking in the mountains. Writing fantasy stories based on ancient myths."</span>
api_key = <span class="hljs-string">"your_openai_api_key"</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">'axios'</span>);
<span class="hljs-keyword">const</span> openai = <span class="hljs-built_in">require</span>(<span class="hljs-string">'openai'</span>);
openai.<span class="hljs-property">apiKey</span> = <span class="hljs-string">'YOUR_OPENAI_API_KEY'</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">' '</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">'text-davinci-003'</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">'Generated Character Backstory:'</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">'Error analyzing user data:'</span>, error);
}
}
<span class="hljs-title function_">analyzeUserData</span>(<span class="hljs-string">'user123'</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">"fmt"</span>
<span class="hljs-string">"strings"</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">" "</span> + strings.Join(user.Posts, <span class="hljs-string">" "</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">"adventurous"</span>] > <span class="hljs-number">2</span> {
<span class="hljs-keyword">return</span> <span class="hljs-string">"An intrepid explorer always seeking new challenges."</span>
}
<span class="hljs-keyword">return</span> <span class="hljs-string">"A curious intellectual yearning for forbidden knowledge."</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">"fantasy"</span>] > <span class="hljs-number">2</span> {
<span class="hljs-keyword">return</span> <span class="hljs-string">"Enigmatic lands filled with magic and mythical creatures."</span>
}
<span class="hljs-keyword">return</span> <span class="hljs-string">"A dystopian future where technology reigns supreme."</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">"Avid reader and fantasy lover."</span>,
Posts: []<span class="hljs-type">string</span>{<span class="hljs-string">"Just finished a great sci-fi novel!"</span>},
}
backstory, worldIdea := analyze(user)
fmt.Println(<span class="hljs-string">"Backstory:"</span>, backstory)
fmt.Println(<span class="hljs-string">"World Idea:"</span>, worldIdea)
}
</code></pre>