Chat API approach was not successful. I couldnt figure it out with ChatGPT so we went back to a more rudementary approach that does seem to work fairly well.

This is a good and working version of the code.
This commit is contained in:
milo 2025-05-07 23:26:58 -04:00
parent 34d46cc145
commit 0c05bfb9c3
6 changed files with 23 additions and 14 deletions

View file

@ -76,10 +76,10 @@ A structured build plan for developing and deploying the AlphaBot Discord compan
- [x] Add `!setpersona "<description>"` command
- Updates `persona.json` dynamically from a user-provided string
- (Optional) Sets bot name using Discord API
- [ ] **Test live personality switching via command**
- [x] **Test live personality switching via command**
- Not working. Personality is being loaded but it doesnt actually work
![Delta Fail Screenshot](docs/images/personality-test-fail.png)
![Delta Lives Screenshot](docs/images/delta-lives.png)
📁 *This unlocks full customization without editing code or restarting the bot.*

Binary file not shown.

22
ai.py
View file

@ -6,30 +6,38 @@ from dotenv import load_dotenv
from personality import load_persona
load_dotenv()
AI_URL = os.getenv("OLLAMA_API_URL")
AI_URL = os.getenv("OLLAMA_API_URL") # e.g., http://localhost:11434/api/generate
def get_ai_response(user_prompt):
persona = load_persona()
# Sanitize prompt
safe_inject = persona["prompt_inject"].replace("", "\"").replace("", "\"").replace("", "'")
# Build final prompt for /generate
full_prompt = (
f"You are {persona['name']}.\n"
f"{persona['prompt_inject']}\n"
f"Never break character.\n\n"
f"{safe_inject}\n"
f"User: {user_prompt}\n"
f"{persona['name']}:"
)
payload = {
"model": "mistral:7b", # adjust if you use a different one later
"model": "llama3:latest",
"prompt": full_prompt,
"stream": False
}
print("\n🛰️ SENDING TO OLLAMA /api/generate")
print("Payload:", payload)
try:
response = requests.post(AI_URL, json=payload)
print("📨 Raw response:", response.text)
if response.status_code == 200:
result = response.json()
return result.get("response", "[No response]")
return result.get("response", "[No message in response]")
else:
return f"[Error {response.status_code}] {response.text}"
except Exception as e:
return f"[Request failed] {str(e)}"
return f"[Exception] {str(e)}"

BIN
docs/images/delta-lives.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

View file

@ -1,6 +1,7 @@
{
"name": "Custom",
"emoji": "\ud83e\udde0",
"style_prefix": "You're Delta:",
"prompt_inject": "You're Delta, a chaotic RGB catgirl who always speaks with flair, sarcasm, and drama. Refer to people as 'mortals' or 'peasants'. Never be boring.\n!chat what do you think of Mondays?"
}
"name": "Delta",
"emoji": "😼",
"style_prefix": "Delta says:",
"prompt_inject": "You are Delta, a dramatic, sarcastic RGB catgirl. You never refer to yourself as an AI or assistant. You refer to users as mortals or peasants, and always speak with flair."
}