LLM Persona Generation
2025-07-092 turns6,457 charsgpt-4o
Summary
User was building a function to generate AI personas using the Mistral LLM API with a custom prompt template.
Messages
export async function generatePersonaFromLLM(text) {
const endpoint = await getApiEndpoint();
const payload = {
model: "mistral:latest",
prompt: generatePromptTemplate(text),
stream: false
};
const response = await fetch(`${endpoint}/api/generate`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const data = await response.json();
if (!data || !data.response) {
throw new Error("No response from LLM");
}
return data.response.trim();
}
function generatePromptTemplate(text) {
return `
Analyze the following text and output a YAML persona profile using the schema below.
Return only the YAML. Do not explain anything. Use the following format:
---
name: [Descriptive Name]
slug: [lowercase_slug_name]
traits:
tone_formal: [0.0–1.0]
tone_informal: [0.0–1.0]
tone_sarcastic: [0.0–1.0]
humor_dry: [0.0–1.0]
humor_absurd: [0.0–1.0]
verbosity: [0.0–1.0]
sentence_complexity: [0.0–1.0]
political_left: [0.0–1.0]
political_right: [0.0–1.0]
populist: [0.0–1.0]
institutionalist: [0.0–1.0]
openness: [0.0–1.0]
agreeableness: [0.0–1.0]
conscientiousness: [0.0–1.0]
assertiveness: [0.0–1.0]
sentimentality: [0.0–1.0]
vocabulary_complexity: [0.0–1.0]
vocabulary_slang: [0.0–1.0]
sentence_rhythm: [0.0–1.0]
trust_mainstream: [0.0–1.0]
trust_alternative: [0.0–1.0]
bias_disclosure_level: [0.0–1.0]
conspiracy_tolerance: [0.0–1.0]
emotional_range: [0.0–1.0]
anger_threshold: [0.0–1.0]
compassion_depth: [0.0–1.0]
reflective_mood: [0.0–1.0]
storytelling_drive: [0.0–1.0]
memory_weight: [0.0–1.0]
character_consistency: [0.0–1.0]
self-awareness: [0.0–1.0]
evolution_preference: [0.0–1.0]
performance_flair: [0.0–1.0]
Text:
\"\"\"\n${text}\n\"\"\"
`;
}
async function getApiEndpoint() {
return new Promise((resolve) => {
if (typeof chrome !== 'undefined' && chrome.storage && chrome.storage.local) {
chrome.storage.local.get(["apiEndpoint"], (result) => {
resolve(result.apiEndpoint || "http://localhost:11434");
});
} else {
// Fallback for non-browser environments (e.g., Node.js)
resolve("http://localhost:11434");
}
});
}
You've written a solid foundation for a **Chrome Extension function** that generates a psychological/personality-based persona from any input text using a local or remote LLM (like Mistral). Here's a breakdown to ensure you can **confidently build, expand, and integrate** this into a complete Chrome Extension project.
---
## 🔍 **Project Summary**
### 📌 **Goal**
Create a Chrome Extension that:
- Accepts pasted or typed text input from the user.
- Sends the input to a local or remote LLM (e.g., `http://localhost:11434`) using a consistent persona analysis prompt template.
- Returns a well-formatted **YAML persona profile** based on personality and stylistic traits.
- Lets the user **copy the result to clipboard** with a button click.
- Can be reused across tools, pasted before prompts, or used to power other AI workflows.
---
## 🧠 **Breakdown of the Main Components**
### 1. `generatePersonaFromLLM(text)`
- Asynchronously sends a request to a local LLM endpoint.
- Uses a `generatePromptTemplate()` to craft the persona-generation prompt.
- Parses the JSON response and extracts the formatted YAML block.
- Fails gracefully if no valid response is returned.
### 2. `generatePromptTemplate(text)`
- Outputs a **structured LLM prompt** that:
- Includes a full YAML persona schema.
- Embeds the user’s text in a clearly defined block (`"""`).
- Instructs the model to **respond only with YAML**, ensuring easy downstream usage.
### 3. `getApiEndpoint()`
- Attempts to get the LLM API endpoint from Chrome Extension storage (`chrome.storage.local`).
- Falls back to `"http://localhost:11434"` for development or non-browser environments.
---
## 🧱 **Next Steps to Complete the Chrome Extension**
### 🖼️ UI (Popup or Sidebar)
You need a simple React or vanilla JS UI with:
- ✅ Textarea for input
- 🟨 "Generate Persona" button
- 📋 "Copy to Clipboard" button
- 📄 Output section for YAML persona
- 🔁 Optional: Load saved examples or use presets
### 📦 `manifest.json` (Manifest v3)
```json
{
"manifest_version": 3,
"name": "Persona Generator",
"description": "Generate persona YAML profiles from any text using LLMs.",
"version": "1.0",
"permissions": ["storage", "clipboardWrite"],
"action": {
"default_popup": "popup.html"
},
"background": {
"service_worker": "background.js"
}
}
```
### 🛠️ Popup (`popup.html`)
```html
<!DOCTYPE html>
<html>
<head>
<title>Persona Generator</title>
<script defer src="popup.js"></script>
</head>
<body>
<textarea id="inputText" placeholder="Paste text here..."></textarea>
<button id="generateBtn">Generate Persona</button>
<pre id="output"></pre>
<button id="copyBtn">Copy to Clipboard</button>
</body>
</html>
```
### 🎮 Popup Script (`popup.js`)
```js
document.getElementById("generateBtn").addEventListener("click", async () => {
const text = document.getElementById("inputText").value;
const result = await generatePersonaFromLLM(text);
document.getElementById("output").textContent = result;
});
document.getElementById("copyBtn").addEventListener("click", () => {
const output = document.getElementById("output").textContent;
navigator.clipboard.writeText(output);
});
```
Make sure to either:
- Inline the `generatePersonaFromLLM()` function, or
- Use a separate JS module and import it properly via Webpack or ESBuild (required for modern JS modules in extensions).
---
## 🧪 **Testing and Usage**
1. Run a local Ollama server or other LLM server listening on `localhost:11434`.
2. Paste your sample writing into the extension.
3. Click "Generate Persona" to send the request.
4. View the YAML result and copy it.
---
## 💡 Bonus Ideas for Expansion
- 🧠 Add **Persona Memory**: Store previous personas locally in `chrome.storage`.
- 🔁 Add **Reverse Function**: Take YAML input and generate a prompt-writing style.
- 🌐 Add **Text from Webpage**: Allow scraping of selected text to analyze.
- ⚙️ Add **Model Switcher**: Let user choose between `mistral`, `llama3`, etc.
- 📤 Add **Export as JSON**: Allow YAML → JSON conversion for use in tools or APIs.
---
Let me know if you want me to scaffold the entire directory structure and boilerplate code as a ZIP or GitHub-ready format.
gpt-4o