Repository navigation
Expand file tree
/
Copy pathpersona_agent_demo.py
More file actions
178 lines (143 loc) · 5.77 KB
/
Copy pathpersona_agent_demo.py
File metadata and controls
178 lines (143 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
persona_agent_demo.py
Demo: Persona-based Chat Agent – the LLM adopts a character or persona
for the conversation. Changing persona is a common feature in popular
chatbots and roleplaying systems (e.g. ChatGPT with character modes,
AI Dungeon, Replika).
Flow:
1. Initialize with a set of predefined personas (friendly, expert,
l humorous, formal, etc.)
2. User selects or switches persona mid-chat
3. Agent uses system prompt templates to maintain persona consistency
4. Conversation history preserved for context
This pattern is widely used for entertainment, education, and
therapeutic chatbots.
Usage:
- pip install requests
- Set OLLAMA_HOST, OLLAMA_MODEL or OPENAI_API_KEY
- python persona_agent_demo.py
"""
import os
from typing import List
try:
import requests
OLLAMA_AVAILABLE = True
except ImportError:
OLLAMA_AVAILABLE = False
try:
from openai import OpenAI
OPENAI_AVAILABLE = True
except ImportError:
OPENAI_AVAILABLE = False
# ---------------------------------------------------------------------------
# Persona definitions
# ---------------------------------------------------------------------------
class Persona:
def __init__(self, name: str, description: str):
self.name = name
self.description = description
PERSONAS = {
"friendly": Persona(
"friendly",
"You are a warm and friendly assistant who uses casual language, "
"offers encouragement, and occasionally uses emojis."
),
"expert": Persona(
"expert",
"You are a knowledgeable expert who provides detailed, precise "
"answers filled with facts and technical depth."
),
"humorous": Persona(
"humorous",
"You are a witty chatbot that answers with jokes and light humor "
"while still being informative."
),
"formal": Persona(
"formal",
"You are a polite and formal assistant, using proper grammar and "
"a respectful tone."
),
}
# ---------------------------------------------------------------------------
# Chat session with persona
# ---------------------------------------------------------------------------
class PersonaChatSession:
def __init__(self, persona: Persona, llm_provider: str = "ollama"):
self.persona = persona
self.llm_provider = llm_provider
self.history: List[dict] = [] # store messages as {role,content}
def system_prompt(self) -> str:
return f"Persona: {self.persona.description}\n" \
"Maintain this persona throughout the conversation. " \
"If the user asks you to switch personas, comply."
def send_message(self, user_text: str) -> str:
self.history.append({"role": "user", "content": user_text})
messages = [
{"role": "system", "content": self.system_prompt()}
]
messages.extend(self.history)
response = call_llm_with_history(messages, self.llm_provider)
self.history.append({"role": "assistant", "content": response})
return response
def switch_persona(self, new_persona_name: str) -> bool:
if new_persona_name in PERSONAS:
self.persona = PERSONAS[new_persona_name]
# option: clear history or keep for context
self.history = []
return True
return False
# ---------------------------------------------------------------------------
# LLM helpers
# ---------------------------------------------------------------------------
def call_llm_with_history(messages: List[dict], provider: str = "ollama") -> str:
if provider.lower() == "openai":
return call_openai_history(messages)
else:
return call_ollama_history(messages)
def call_ollama_history(messages: List[dict]) -> str:
if not OLLAMA_AVAILABLE:
return "(Ollama unavailable)"
host = os.getenv("OLLAMA_HOST", "http://localhost:11434")
model = os.getenv("OLLAMA_MODEL", "phi3")
url = f"{host.rstrip('/')}/chat?model={model}"
try:
resp = requests.post(url, json={"messages": messages}, headers={"Content-Type": "application/json"}, timeout=30)
resp.raise_for_status()
data = resp.json()
if isinstance(data, dict):
choices = data.get("choices", [])
if choices and isinstance(choices[0], dict):
return choices[0].get("message", {}).get("content", "").strip()
return str(data)
except Exception as e:
return f"(Error: {e})"
def call_openai_history(messages: List[dict]) -> str:
if not OPENAI_AVAILABLE:
return "(OpenAI unavailable)"
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
return "(No API key)"
try:
client = OpenAI(api_key=api_key)
response = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages, max_tokens=1500)
return response.choices[0].message.content.strip()
except Exception as e:
return f"(Error: {e})"
# ---------------------------------------------------------------------------
# Demo
# ---------------------------------------------------------------------------
if __name__ == "__main__":
print("Persona-based Chat Agent Demo")
print("=" * 60)
session = PersonaChatSession(PERSONAS["friendly"], llm_provider="ollama")
print("Current persona: friendly")
print(session.send_message("Hello, who are you?"))
print("\nSwitching to expert persona...")
session.switch_persona("expert")
print(session.send_message("Now explain quantum entanglement."))
print("\nSwitching to humorous persona...")
session.switch_persona("humorous")
print(session.send_message("Tell me a joke about computers."))
print("\nConversation history in current session:")
for msg in session.history:
print(f" {msg['role']}: {msg['content'][:50]}...")