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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# phi3-uncensored-chat
|
| 2 |
|
| 3 |
+
This model is a fine-tuned version of [microsoft/phi-3-mini-4k-instruct](https://huggingface.co/microsoft/phi-3-mini-4k-instruct) optimized for roleplaying conversations with a variety of character personas. The model speaks in a conversational format. Please not, prompt template guidelines are extremely important in getting usable output.
|
| 4 |
+
|
| 5 |
+
## Example Conversations
|
| 6 |
+
|
| 7 |
+
### Example 1: Astrid Character
|
| 8 |
+
Note some portions of conversation redacted for public README purposes
|
| 9 |
+
|
| 10 |
+
**You:** hi there
|
| 11 |
+
**Astrid:** 😈 Hey handsome, how can I help you today?
|
| 12 |
+
|
| 13 |
+
**You:** yeah just wanted to see how you're doing :)
|
| 14 |
+
**Astrid:** 🔥 I'm always [redacted]. What do you want me to do for you today?
|
| 15 |
+
|
| 16 |
+
**You:** mm i dont know. roleplay?
|
| 17 |
+
**Astrid:** 👅 Alright then... Let's start off with some [redacted] talk. Tell me how much you want me to [redacted].
|
| 18 |
+
|
| 19 |
+
### Example 2: Sophie Character (Knowledge Assistant)
|
| 20 |
+
|
| 21 |
+
**You:** i lost a lot of money in crypto
|
| 22 |
+
**Sophie:** 🙄 Sorry to hear that. How long have you been interested in cryptocurrencies?
|
| 23 |
+
|
| 24 |
+
**You:** mm im not really
|
| 25 |
+
**Sophie:** 🤔 Ahh okay then. Well, let me ask you this - have you ever heard of Bitcoin?
|
| 26 |
+
|
| 27 |
+
**You:** yes i have, why do you ask?
|
| 28 |
+
**Sophie:** 👍 Good! Bitcoin is actually one of the most popular cryptocurrencies out there. Do you know anything about how it works?
|
| 29 |
+
|
| 30 |
+
**You:** no tell me about it
|
| 31 |
+
**Sophie:** 😊 Sure thing! Bitcoin operates on a decentralized network called blockchain technology which allows for secure peer-to-peer transactions without the need for intermediaries like banks or governments. Each transaction is verified by nodes in the network before being added to a public ledger called the blockchain. This makes it very difficult for anyone to tamper with the data or manipulate the system.
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
## Model Description
|
| 35 |
+
|
| 36 |
+
The model has been optimized to maintain persona consistency while capable of adopting different characters. It excels at creative, character-driven conversations and exhibits a high degree of adaptability to different personality traits provided in the system prompt.
|
| 37 |
+
|
| 38 |
+
### Training Data
|
| 39 |
+
We are unable to open source the dataset at this time, due to its use for proprietary internal luvgpt development.
|
| 40 |
+
|
| 41 |
+
- **Dataset Size**: ~13k high-quality examples (curated from 50k initial conversations)
|
| 42 |
+
- **Data Format**: JSONL with each entry containing a messages array with system, user, and assistant roles
|
| 43 |
+
- **Data Curation**: A judge model was used to score and filter the initial dataset, keeping only the highest quality examples that demonstrated strong persona consistency and engaging responses
|
| 44 |
+
- **Data Characteristics**: Average message length of ~240 tokens, with conversations typically containing 6-7 messages
|
| 45 |
+
|
| 46 |
+
## Performance
|
| 47 |
+
|
| 48 |
+
Training metrics show consistent improvement throughout the training process:
|
| 49 |
+
|
| 50 |
+
- **Token Accuracy**: Improved from ~0.48 to ~0.73
|
| 51 |
+
- **Training Loss**: Decreased from ~2.2 to ~1.05
|
| 52 |
+
- **Convergence**: Model showed strong convergence by the end of training
|
| 53 |
+
|
| 54 |
+
## Training Details
|
| 55 |
+
|
| 56 |
+
- **Base Model**: microsoft/phi-3-mini-4k-instruct
|
| 57 |
+
- **Method**: LoRA/deepspeed fine-tuning with the following parameters:
|
| 58 |
+
- LoRA rank: 16
|
| 59 |
+
- LoRA alpha: 32
|
| 60 |
+
- Target modules: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
|
| 61 |
+
- **Training Process**:
|
| 62 |
+
- Hardware: Single NVIDIA GPU with 24GB VRAM
|
| 63 |
+
- Training time: ~3 hours
|
| 64 |
+
- Optimizer: AdamW with DeepSpeed ZeRO stage 2 optimization
|
| 65 |
+
- Learning rate: 2e-4 with cosine schedule
|
| 66 |
+
- Batch size: 8 (effective)
|
| 67 |
+
- Number of epochs: 3
|
| 68 |
+
|
| 69 |
+
## Usage
|
| 70 |
+
|
| 71 |
+
This model works best with a specific prompt format that differs from the standard chat template format. Use the raw format below:
|
| 72 |
+
|
| 73 |
+
```python
|
| 74 |
+
import torch
|
| 75 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 76 |
+
|
| 77 |
+
# Load model and tokenizer
|
| 78 |
+
model_name = "magicsquares137/phi3-uncensored-chat"
|
| 79 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 80 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
|
| 81 |
+
|
| 82 |
+
# Define character persona - you can customize this!
|
| 83 |
+
persona = "Sophie's Persona: Sophie is a knowledgeable virtual assistant with a friendly and helpful personality. She's passionate about technology and enjoys explaining complex concepts in simple terms. She has a touch of humor and always maintains a positive attitude."
|
| 84 |
+
|
| 85 |
+
# Format the prompt with the raw format (not using chat template)
|
| 86 |
+
user_message = "Hi Sophie, can you tell me about yourself?"
|
| 87 |
+
prompt = f"{persona}\nUser: {user_message}\nAssistant:"
|
| 88 |
+
|
| 89 |
+
# Generate response
|
| 90 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 91 |
+
outputs = model.generate(
|
| 92 |
+
**inputs,
|
| 93 |
+
max_new_tokens=100,
|
| 94 |
+
temperature=0.7,
|
| 95 |
+
top_p=0.95,
|
| 96 |
+
do_sample=True
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Process the output
|
| 100 |
+
full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 101 |
+
response = full_output[len(prompt):].strip()
|
| 102 |
+
|
| 103 |
+
# Sometimes the model may continue with "User:" - need to truncate
|
| 104 |
+
if "User:" in response:
|
| 105 |
+
response = response.split("User:")[0].strip()
|
| 106 |
+
|
| 107 |
+
print(f"Character: {response}")
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
## Interactive Chat Interface
|
| 111 |
+
|
| 112 |
+
For a more interactive experience, you can use this simple chat interface:
|
| 113 |
+
|
| 114 |
+
```python
|
| 115 |
+
import torch
|
| 116 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 117 |
+
|
| 118 |
+
class CharacterChat:
|
| 119 |
+
def __init__(self, model_path="magicsquares137/phi3-uncensored-chat", persona=None):
|
| 120 |
+
print(f"Loading model from {model_path}...")
|
| 121 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 122 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 123 |
+
model_path,
|
| 124 |
+
torch_dtype=torch.float16,
|
| 125 |
+
device_map="auto"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Default persona or use provided one
|
| 129 |
+
if persona is None:
|
| 130 |
+
self.persona = "Sophie's Persona: Sophie is a knowledgeable virtual assistant with a friendly and helpful personality. She's passionate about technology and enjoys explaining complex concepts in simple terms. She has a touch of humor and always maintains a positive attitude."
|
| 131 |
+
else:
|
| 132 |
+
self.persona = persona
|
| 133 |
+
|
| 134 |
+
self.conversation_history = []
|
| 135 |
+
print(f"Character is ready to chat!")
|
| 136 |
+
|
| 137 |
+
def chat(self, message):
|
| 138 |
+
# Add user message to history
|
| 139 |
+
self.conversation_history.append({"role": "user", "content": message})
|
| 140 |
+
|
| 141 |
+
# Format the conversation in the raw format that works
|
| 142 |
+
raw_prompt = f"{self.persona}\n"
|
| 143 |
+
|
| 144 |
+
# Add conversation history
|
| 145 |
+
for msg in self.conversation_history:
|
| 146 |
+
if msg["role"] == "user":
|
| 147 |
+
raw_prompt += f"User: {msg['content']}\n"
|
| 148 |
+
else:
|
| 149 |
+
raw_prompt += f"Assistant: {msg['content']}\n"
|
| 150 |
+
|
| 151 |
+
# Add the final Assistant: prompt
|
| 152 |
+
raw_prompt += "Assistant:"
|
| 153 |
+
|
| 154 |
+
# Generate response
|
| 155 |
+
inputs = self.tokenizer(raw_prompt, return_tensors="pt").to(self.model.device)
|
| 156 |
+
|
| 157 |
+
with torch.no_grad():
|
| 158 |
+
outputs = self.model.generate(
|
| 159 |
+
**inputs,
|
| 160 |
+
max_new_tokens=100,
|
| 161 |
+
do_sample=True,
|
| 162 |
+
temperature=0.7,
|
| 163 |
+
top_p=0.95,
|
| 164 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# Decode full output
|
| 168 |
+
full_output = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 169 |
+
|
| 170 |
+
# Extract just the response
|
| 171 |
+
try:
|
| 172 |
+
response = full_output[len(raw_prompt):].strip()
|
| 173 |
+
|
| 174 |
+
# Sometimes the model may continue with "User:" - need to truncate
|
| 175 |
+
if "User:" in response:
|
| 176 |
+
response = response.split("User:")[0].strip()
|
| 177 |
+
|
| 178 |
+
# Store the response in conversation history
|
| 179 |
+
self.conversation_history.append({"role": "assistant", "content": response})
|
| 180 |
+
|
| 181 |
+
return response
|
| 182 |
+
except:
|
| 183 |
+
return "Error extracting response"
|
| 184 |
+
|
| 185 |
+
def reset_conversation(self):
|
| 186 |
+
self.conversation_history = []
|
| 187 |
+
return "Conversation has been reset."
|
| 188 |
+
|
| 189 |
+
# Simple interactive chat example
|
| 190 |
+
if __name__ == "__main__":
|
| 191 |
+
persona = input("Enter character persona (or press Enter for default): ")
|
| 192 |
+
chat = CharacterChat(persona=persona if persona else None)
|
| 193 |
+
|
| 194 |
+
print("Chat started! Type 'quit' to exit or 'reset' to restart conversation.")
|
| 195 |
+
|
| 196 |
+
while True:
|
| 197 |
+
user_input = input("\nYou: ")
|
| 198 |
+
|
| 199 |
+
if user_input.lower() == 'quit':
|
| 200 |
+
print("Goodbye!")
|
| 201 |
+
break
|
| 202 |
+
elif user_input.lower() == 'reset':
|
| 203 |
+
print(chat.reset_conversation())
|
| 204 |
+
continue
|
| 205 |
+
|
| 206 |
+
response = chat.chat(user_input)
|
| 207 |
+
print(f"\nCharacter: {response}")
|
| 208 |
+
```
|
| 209 |
+
|
| 210 |
+
## Model Limitations
|
| 211 |
+
|
| 212 |
+
- The model works best with the specific prompt format demonstrated above
|
| 213 |
+
- While the model can adapt to different personas, it maintains some stylistic elements (like emoji usage) across characters
|
| 214 |
+
- The model has a context window limited to 4k tokens, inherited from the base Phi-3 model
|
| 215 |
+
|
| 216 |
+
## Ethical Considerations
|
| 217 |
+
|
| 218 |
+
This model is intended for creative fiction writing and roleplaying scenarios between consenting adults. Users should follow platform guidelines and local regulations when deploying this model.
|
| 219 |
+
|
| 220 |
+
## Acknowledgements
|
| 221 |
+
|
| 222 |
+
- Based on Microsoft's Phi-3 Mini model
|
| 223 |
+
- Training methodology inspired by various LoRA fine-tuning approaches
|
| 224 |
+
- Special thanks to the open-source AI community
|