mirror of
https://github.com/vegu-ai/talemate.git
synced 2025-12-25 07:59:36 +01:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
72202dee02 | ||
|
|
91f228aa68 | ||
|
|
27d6c5e7c2 | ||
|
|
1f5cff4c6d | ||
|
|
77425935be | ||
|
|
e6b21789d1 | ||
|
|
89d7b9d6e3 |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -5,3 +5,9 @@
|
||||
*-internal*
|
||||
*.internal*
|
||||
*_internal*
|
||||
talemate_env
|
||||
chroma
|
||||
scenes
|
||||
config.yaml
|
||||
!scenes/infinity-quest/assets
|
||||
!scenes/infinity-quest/infinity-quest.json
|
||||
|
||||
23
README.md
23
README.md
@@ -103,26 +103,11 @@ Once the api key is set Pods loaded from text-generation-webui templates (or the
|
||||
|
||||
**ATTENTION**: Talemate is not a suitable for way for you to determine whether your pod is currently running or not. **Always** check the runpod dashboard to see if your pod is running or not.
|
||||
|
||||
## Recommended Models
|
||||
## Recommended Models
|
||||
(as of2023.10.25)
|
||||
|
||||
Note: this is my personal opinion while using talemate. If you find a model that works better for you, let me know about it.
|
||||
|
||||
Will be updated as i test more models and over time.
|
||||
|
||||
| Model Name | Type | Notes |
|
||||
|-------------------------------|-----------------|-------------------------------------------------------------------------------------------------------------------|
|
||||
| [Nous Hermes LLama2](https://huggingface.co/TheBloke/Nous-Hermes-Llama2-GPTQ) | 13B model | My go-to model for 13B parameters. It's good at roleplay and also smart enough to handle the world state and narrative tools. A 13B model loaded via exllama also allows you run chromadb with the xl instructor embeddings off of a single 4090. |
|
||||
| [Xwin-LM-13B](https://huggingface.co/TheBloke/Xwin-LM-13B-V0.1-GPTQ) | 13B model | Really strong model, roleplaying capability still needs more testing |
|
||||
| [MythoMax](https://huggingface.co/TheBloke/MythoMax-L2-13B-GPTQ) | 13B model | Similar quality to Hermes LLama2, but a bit more creative. Rarely fails on JSON responses. |
|
||||
| [Synthia v1.2 34B](https://huggingface.co/TheBloke/Synthia-34B-v1.2-GPTQ) | 34B model | Cannot be run at full context together with chromadb instructor models on a single 4090. But a great choice if you're running chromadb with the default embeddings (or on cpu). |
|
||||
| [Xwin-LM-70B](https://huggingface.co/TheBloke/Xwin-LM-70B-V0.1-GPTQ) | 70B model | Great choice if you have the hardware to run it (or can rent it). |
|
||||
| [Synthia v1.2 70B](https://huggingface.co/TheBloke/Synthia-70B-v1.2-GPTQ) | 70B model | Great choice if you have the hardware to run it (or can rent it). |
|
||||
| [GPT-4](https://platform.openai.com/) | Remote | Still the best for consistency and reasoning, but is heavily censored. Talemate will send a general "decensor" system prompt, ymmv. **If you do use this make sure to monitor your api usage, talemate tends to send a lot more requests than other roleplaying applications.** |
|
||||
| [GPT-3.5-turbo](https://platform.openai.com/) | Remote | It's really inconsistent with JSON responses, plus its probably still just as heavily censored as GPT-4. If you want to run it i'd suggest running it for the conversation agent, and use GPT-4 for the other agents. **If you do use this make sure to monitor your api usage, talemate tends to send a lot more requests than other roleplaying applications.** |
|
||||
|
||||
I have not tested with Llama 1 models in a while, Lazarus was really good at roleplay, but started failing on JSON requirements.
|
||||
|
||||
I have not tested with anything below 13B parameters.
|
||||
Any of the top models in any of the size classes here should work well:
|
||||
https://www.reddit.com/r/LocalLLaMA/comments/17fhp9k/huge_llm_comparisontest_39_models_tested_7b70b/
|
||||
|
||||
## Connecting to an LLM
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
agents: {}
|
||||
clients: {}
|
||||
creator:
|
||||
content_context:
|
||||
- a fun and engaging slice of life story aimed at an adult audience.
|
||||
|
||||
187
docs/talemate-scene-schema.json
Normal file
187
docs/talemate-scene-schema.json
Normal file
@@ -0,0 +1,187 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
"intro": {
|
||||
"type": "string"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"history": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"message": {
|
||||
"type": "string"
|
||||
},
|
||||
"id": {
|
||||
"type": "integer"
|
||||
},
|
||||
"typ": {
|
||||
"type": "string"
|
||||
},
|
||||
"source": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["message", "id", "typ", "source"]
|
||||
}
|
||||
},
|
||||
"environment": {
|
||||
"type": "string"
|
||||
},
|
||||
"archived_history": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"text": {
|
||||
"type": "string"
|
||||
},
|
||||
"ts": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["text", "ts"]
|
||||
}
|
||||
},
|
||||
"character_states": {
|
||||
"type": "object"
|
||||
},
|
||||
"characters": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
"greeting_text": {
|
||||
"type": "string"
|
||||
},
|
||||
"base_attributes": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"details": {
|
||||
"type": "object"
|
||||
},
|
||||
"gender": {
|
||||
"type": "string"
|
||||
},
|
||||
"color": {
|
||||
"type": "string"
|
||||
},
|
||||
"example_dialogue": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"history_events": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"is_player": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"cover_image": {
|
||||
"type": ["string", "null"]
|
||||
}
|
||||
},
|
||||
"required": ["name", "description", "greeting_text", "base_attributes", "details", "gender", "color", "example_dialogue", "history_events", "is_player", "cover_image"]
|
||||
}
|
||||
},
|
||||
"goal": {
|
||||
"type": ["string", "null"]
|
||||
},
|
||||
"goals": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"context": {
|
||||
"type": "string"
|
||||
},
|
||||
"world_state": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"characters": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"snapshot": {
|
||||
"type": ["string", "null"]
|
||||
},
|
||||
"emotion": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["snapshot", "emotion"]
|
||||
}
|
||||
},
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"snapshot": {
|
||||
"type": ["string", "null"]
|
||||
}
|
||||
},
|
||||
"required": ["snapshot"]
|
||||
}
|
||||
},
|
||||
"location": {
|
||||
"type": ["string", "null"]
|
||||
}
|
||||
},
|
||||
"required": ["characters", "items", "location"]
|
||||
},
|
||||
"assets": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"cover_image": {
|
||||
"type": "string"
|
||||
},
|
||||
"assets": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"file_type": {
|
||||
"type": "string"
|
||||
},
|
||||
"media_type": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["id", "file_type", "media_type"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["cover_image", "assets"]
|
||||
},
|
||||
"ts": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["description", "intro", "name", "history", "environment", "archived_history", "character_states", "characters", "context", "world_state", "assets", "ts"]
|
||||
}
|
||||
@@ -7,7 +7,7 @@ REM activate the virtual environment
|
||||
call talemate_env\Scripts\activate
|
||||
|
||||
REM install poetry
|
||||
pip install poetry
|
||||
python -m pip install poetry "rapidfuzz>=3" -U
|
||||
|
||||
REM use poetry to install dependencies
|
||||
poetry install
|
||||
|
||||
2869
poetry.lock
generated
2869
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
|
||||
|
||||
[tool.poetry]
|
||||
name = "talemate"
|
||||
version = "0.10.1"
|
||||
version = "0.12.0"
|
||||
description = "AI-backed roleplay and narrative tools"
|
||||
authors = ["FinalWombat"]
|
||||
license = "GNU Affero General Public License v3.0"
|
||||
@@ -17,7 +17,7 @@ black = "*"
|
||||
rope = "^0.22"
|
||||
isort = "^5.10"
|
||||
jinja2 = "^3.0"
|
||||
openai = "*"
|
||||
openai = ">=1"
|
||||
requests = "^2.26"
|
||||
colorama = ">=0.4.6"
|
||||
Pillow = "^9.5"
|
||||
@@ -27,8 +27,7 @@ typing-inspect = "0.8.0"
|
||||
typing_extensions = "^4.5.0"
|
||||
uvicorn = "^0.23"
|
||||
blinker = "^1.6.2"
|
||||
pydantic = "<2"
|
||||
langchain = "0.0.213"
|
||||
pydantic = "<3"
|
||||
beautifulsoup4 = "^4.12.2"
|
||||
python-dotenv = "^1.0.0"
|
||||
websockets = "^11.0.3"
|
||||
@@ -36,6 +35,8 @@ structlog = "^23.1.0"
|
||||
runpod = "==1.2.0"
|
||||
nest_asyncio = "^1.5.7"
|
||||
isodate = ">=0.6.1"
|
||||
thefuzz = ">=0.20.0"
|
||||
tiktoken = ">=0.5.1"
|
||||
|
||||
# ChromaDB
|
||||
chromadb = ">=0.4,<1"
|
||||
|
||||
18
reinstall.bat
Normal file
18
reinstall.bat
Normal file
@@ -0,0 +1,18 @@
|
||||
@echo off
|
||||
|
||||
IF EXIST talemate_env rmdir /s /q "talemate_env"
|
||||
|
||||
REM create a virtual environment
|
||||
python -m venv talemate_env
|
||||
|
||||
REM activate the virtual environment
|
||||
call talemate_env\Scripts\activate
|
||||
|
||||
REM install poetry
|
||||
python -m pip install poetry "rapidfuzz>=3" -U
|
||||
|
||||
REM use poetry to install dependencies
|
||||
python -m poetry install
|
||||
|
||||
echo Virtual environment re-created.
|
||||
pause
|
||||
@@ -2,4 +2,4 @@ from .agents import Agent
|
||||
from .client import TextGeneratorWebuiClient
|
||||
from .tale_mate import *
|
||||
|
||||
VERSION = "0.10.1"
|
||||
VERSION = "0.12.0"
|
||||
|
||||
@@ -6,4 +6,6 @@ from .director import DirectorAgent
|
||||
from .memory import ChromaDBMemoryAgent, MemoryAgent
|
||||
from .narrator import NarratorAgent
|
||||
from .registry import AGENT_CLASSES, get_agent_class, register
|
||||
from .summarize import SummarizeAgent
|
||||
from .summarize import SummarizeAgent
|
||||
from .editor import EditorAgent
|
||||
from .world_state import WorldStateAgent
|
||||
@@ -10,13 +10,37 @@ from blinker import signal
|
||||
import talemate.instance as instance
|
||||
import talemate.util as util
|
||||
from talemate.emit import emit
|
||||
|
||||
from talemate.events import GameLoopStartEvent
|
||||
import talemate.emit.async_signals
|
||||
import dataclasses
|
||||
import pydantic
|
||||
import structlog
|
||||
|
||||
__all__ = [
|
||||
"Agent",
|
||||
"set_processing",
|
||||
]
|
||||
|
||||
log = structlog.get_logger("talemate.agents.base")
|
||||
|
||||
class AgentActionConfig(pydantic.BaseModel):
|
||||
type: str
|
||||
label: str
|
||||
description: str = ""
|
||||
value: Union[int, float, str, bool]
|
||||
default_value: Union[int, float, str, bool] = None
|
||||
max: Union[int, float, None] = None
|
||||
min: Union[int, float, None] = None
|
||||
step: Union[int, float, None] = None
|
||||
scope: str = "global"
|
||||
|
||||
class AgentAction(pydantic.BaseModel):
|
||||
enabled: bool = True
|
||||
label: str
|
||||
description: str = ""
|
||||
config: Union[dict[str, AgentActionConfig], None] = None
|
||||
|
||||
|
||||
def set_processing(fn):
|
||||
"""
|
||||
decorator that emits the agent status as processing while the function
|
||||
@@ -45,7 +69,6 @@ class Agent(ABC):
|
||||
|
||||
agent_type = "agent"
|
||||
verbose_name = None
|
||||
|
||||
set_processing = set_processing
|
||||
|
||||
@property
|
||||
@@ -59,18 +82,13 @@ class Agent(ABC):
|
||||
def verbose_name(self):
|
||||
return self.agent_type.capitalize()
|
||||
|
||||
@classmethod
|
||||
def config_options(cls):
|
||||
return {
|
||||
"client": [name for name, _ in instance.client_instances()],
|
||||
}
|
||||
|
||||
|
||||
@property
|
||||
def ready(self):
|
||||
if not getattr(self.client, "enabled", True):
|
||||
return False
|
||||
|
||||
|
||||
if self.client.current_status in ["error", "warning"]:
|
||||
return False
|
||||
|
||||
@@ -79,10 +97,104 @@ class Agent(ABC):
|
||||
@property
|
||||
def status(self):
|
||||
if self.ready:
|
||||
if not self.enabled:
|
||||
return "disabled"
|
||||
return "idle" if getattr(self, "processing", 0) == 0 else "busy"
|
||||
else:
|
||||
return "uninitialized"
|
||||
|
||||
@property
|
||||
def enabled(self):
|
||||
# by default, agents are enabled, an agent class that
|
||||
# is disableable should override this property
|
||||
return True
|
||||
|
||||
@property
|
||||
def disable(self):
|
||||
# by default, agents are enabled, an agent class that
|
||||
# is disableable should override this property to
|
||||
# disable the agent
|
||||
pass
|
||||
|
||||
@property
|
||||
def has_toggle(self):
|
||||
# by default, agents do not have toggles to enable / disable
|
||||
# an agent class that is disableable should override this property
|
||||
return False
|
||||
|
||||
@property
|
||||
def experimental(self):
|
||||
# by default, agents are not experimental, an agent class that
|
||||
# is experimental should override this property
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def config_options(cls, agent=None):
|
||||
config_options = {
|
||||
"client": [name for name, _ in instance.client_instances()],
|
||||
"enabled": agent.enabled if agent else True,
|
||||
"has_toggle": agent.has_toggle if agent else False,
|
||||
"experimental": agent.experimental if agent else False,
|
||||
}
|
||||
actions = getattr(agent, "actions", None)
|
||||
|
||||
if actions:
|
||||
config_options["actions"] = {k: v.model_dump() for k, v in actions.items()}
|
||||
else:
|
||||
config_options["actions"] = {}
|
||||
|
||||
return config_options
|
||||
|
||||
def apply_config(self, *args, **kwargs):
|
||||
if self.has_toggle and "enabled" in kwargs:
|
||||
self.is_enabled = kwargs.get("enabled", False)
|
||||
|
||||
if not getattr(self, "actions", None):
|
||||
return
|
||||
|
||||
for action_key, action in self.actions.items():
|
||||
|
||||
if not kwargs.get("actions"):
|
||||
continue
|
||||
|
||||
action.enabled = kwargs.get("actions", {}).get(action_key, {}).get("enabled", False)
|
||||
|
||||
if not action.config:
|
||||
continue
|
||||
|
||||
for config_key, config in action.config.items():
|
||||
try:
|
||||
config.value = kwargs.get("actions", {}).get(action_key, {}).get("config", {}).get(config_key, {}).get("value", config.value)
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
async def on_game_loop_start(self, event:GameLoopStartEvent):
|
||||
|
||||
"""
|
||||
Finds all ActionConfigs that have a scope of "scene" and resets them to their default values
|
||||
"""
|
||||
|
||||
if not getattr(self, "actions", None):
|
||||
return
|
||||
|
||||
for _, action in self.actions.items():
|
||||
if not action.config:
|
||||
continue
|
||||
|
||||
for _, config in action.config.items():
|
||||
if config.scope == "scene":
|
||||
# if default_value is None, just use the `type` of the current
|
||||
# value
|
||||
if config.default_value is None:
|
||||
default_value = type(config.value)()
|
||||
else:
|
||||
default_value = config.default_value
|
||||
|
||||
log.debug("resetting config", config=config, default_value=default_value)
|
||||
config.value = default_value
|
||||
|
||||
await self.emit_status()
|
||||
|
||||
async def emit_status(self, processing: bool = None):
|
||||
|
||||
# should keep a count of processing requests, and when the
|
||||
@@ -101,6 +213,8 @@ class Agent(ABC):
|
||||
self.processing += 1
|
||||
|
||||
status = "busy" if self.processing > 0 else "idle"
|
||||
if not self.enabled:
|
||||
status = "disabled"
|
||||
|
||||
emit(
|
||||
"agent_status",
|
||||
@@ -108,13 +222,15 @@ class Agent(ABC):
|
||||
id=self.agent_type,
|
||||
status=status,
|
||||
details=self.agent_details,
|
||||
data=self.config_options(),
|
||||
data=self.config_options(agent=self),
|
||||
)
|
||||
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
def connect(self, scene):
|
||||
self.scene = scene
|
||||
talemate.emit.async_signals.get("game_loop_start").connect(self.on_game_loop_start)
|
||||
|
||||
|
||||
def clean_result(self, result):
|
||||
if "#" in result:
|
||||
@@ -159,3 +275,7 @@ class Agent(ABC):
|
||||
|
||||
current_memory_context.append(memory)
|
||||
return current_memory_context
|
||||
|
||||
@dataclasses.dataclass
|
||||
class AgentEmission:
|
||||
agent: Agent
|
||||
@@ -1,24 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import re
|
||||
import random
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from typing import TYPE_CHECKING, Optional, Union
|
||||
|
||||
import talemate.client as client
|
||||
import talemate.instance as instance
|
||||
import talemate.util as util
|
||||
import structlog
|
||||
from talemate.emit import emit
|
||||
import talemate.emit.async_signals
|
||||
from talemate.scene_message import CharacterMessage, DirectorMessage
|
||||
from talemate.prompts import Prompt
|
||||
from talemate.events import GameLoopEvent
|
||||
from talemate.client.context import set_conversation_context_attribute, client_context_attribute, set_client_context_attribute
|
||||
|
||||
from .base import Agent, set_processing
|
||||
from .base import Agent, AgentEmission, set_processing, AgentAction, AgentActionConfig
|
||||
from .registry import register
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from talemate.tale_mate import Character, Scene
|
||||
from talemate.tale_mate import Character, Scene, Actor
|
||||
|
||||
log = structlog.get_logger("talemate.agents.conversation")
|
||||
|
||||
@dataclasses.dataclass
|
||||
class ConversationAgentEmission(AgentEmission):
|
||||
actor: Actor
|
||||
character: Character
|
||||
generation: list[str]
|
||||
|
||||
talemate.emit.async_signals.register(
|
||||
"agent.conversation.before_generate",
|
||||
"agent.conversation.generated"
|
||||
)
|
||||
|
||||
@register()
|
||||
class ConversationAgent(Agent):
|
||||
"""
|
||||
@@ -44,7 +60,242 @@ class ConversationAgent(Agent):
|
||||
self.logging_enabled = logging_enabled
|
||||
self.logging_date = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||
self.current_memory_context = None
|
||||
|
||||
# several agents extend this class, but we only want to initialize
|
||||
# these actions for the conversation agent
|
||||
|
||||
if self.agent_type != "conversation":
|
||||
return
|
||||
|
||||
self.actions = {
|
||||
"generation_override": AgentAction(
|
||||
enabled = True,
|
||||
label = "Generation Override",
|
||||
description = "Override generation parameters",
|
||||
config = {
|
||||
"length": AgentActionConfig(
|
||||
type="number",
|
||||
label="Generation Length (tokens)",
|
||||
description="Maximum number of tokens to generate for a conversation response.",
|
||||
value=96,
|
||||
min=32,
|
||||
max=512,
|
||||
step=32,
|
||||
),#
|
||||
"instructions": AgentActionConfig(
|
||||
type="text",
|
||||
label="Instructions",
|
||||
value="1-3 sentences.",
|
||||
description="Extra instructions to give the AI for dialog generatrion.",
|
||||
),
|
||||
"jiggle": AgentActionConfig(
|
||||
type="number",
|
||||
label="Jiggle",
|
||||
description="If > 0.0 will cause certain generation parameters to have a slight random offset applied to them. The bigger the number, the higher the potential offset.",
|
||||
value=0.0,
|
||||
min=0.0,
|
||||
max=1.0,
|
||||
step=0.1,
|
||||
),
|
||||
}
|
||||
),
|
||||
"natural_flow": AgentAction(
|
||||
enabled = True,
|
||||
label = "Natural Flow",
|
||||
description = "Will attempt to generate a more natural flow of conversation between multiple characters.",
|
||||
config = {
|
||||
"max_auto_turns": AgentActionConfig(
|
||||
type="number",
|
||||
label="Max. Auto Turns",
|
||||
description="The maximum number of turns the AI is allowed to generate before it stops and waits for the player to respond.",
|
||||
value=4,
|
||||
min=1,
|
||||
max=100,
|
||||
step=1,
|
||||
),
|
||||
"max_idle_turns": AgentActionConfig(
|
||||
type="number",
|
||||
label="Max. Idle Turns",
|
||||
description="The maximum number of turns a character can go without speaking before they are considered overdue to speak.",
|
||||
value=8,
|
||||
min=1,
|
||||
max=100,
|
||||
step=1,
|
||||
),
|
||||
}
|
||||
),
|
||||
"use_long_term_memory": AgentAction(
|
||||
enabled = True,
|
||||
label = "Long Term Memory",
|
||||
description = "Will augment the conversation prompt with long term memory.",
|
||||
config = {
|
||||
"ai_selected": AgentActionConfig(
|
||||
type="bool",
|
||||
label="AI Selected",
|
||||
description="If enabled, the AI will select the long term memory to use. (will increase how long it takes to generate a response)",
|
||||
value=False,
|
||||
),
|
||||
}
|
||||
),
|
||||
}
|
||||
|
||||
def connect(self, scene):
|
||||
super().connect(scene)
|
||||
talemate.emit.async_signals.get("game_loop").connect(self.on_game_loop)
|
||||
|
||||
def last_spoken(self):
|
||||
|
||||
"""
|
||||
Returns the last time each character spoke
|
||||
"""
|
||||
|
||||
last_turn = {}
|
||||
turns = 0
|
||||
character_names = self.scene.character_names
|
||||
max_idle_turns = self.actions["natural_flow"].config["max_idle_turns"].value
|
||||
|
||||
for idx in range(len(self.scene.history) - 1, -1, -1):
|
||||
|
||||
if isinstance(self.scene.history[idx], CharacterMessage):
|
||||
|
||||
if turns >= max_idle_turns:
|
||||
break
|
||||
|
||||
character = self.scene.history[idx].character_name
|
||||
|
||||
if character in character_names:
|
||||
last_turn[character] = turns
|
||||
character_names.remove(character)
|
||||
|
||||
if not character_names:
|
||||
break
|
||||
|
||||
turns += 1
|
||||
|
||||
if character_names and turns >= max_idle_turns:
|
||||
for character in character_names:
|
||||
last_turn[character] = max_idle_turns
|
||||
|
||||
return last_turn
|
||||
|
||||
def repeated_speaker(self):
|
||||
"""
|
||||
Counts the amount of times the most recent speaker has spoken in a row
|
||||
"""
|
||||
character_name = None
|
||||
count = 0
|
||||
for idx in range(len(self.scene.history) - 1, -1, -1):
|
||||
if isinstance(self.scene.history[idx], CharacterMessage):
|
||||
if character_name is None:
|
||||
character_name = self.scene.history[idx].character_name
|
||||
if self.scene.history[idx].character_name == character_name:
|
||||
count += 1
|
||||
else:
|
||||
break
|
||||
return count
|
||||
|
||||
async def on_game_loop(self, event:GameLoopEvent):
|
||||
await self.apply_natural_flow()
|
||||
|
||||
async def apply_natural_flow(self):
|
||||
"""
|
||||
If the natural flow action is enabled, this will attempt to determine
|
||||
the ideal character to talk next.
|
||||
|
||||
This will let the AI pick a character to talk to, but if the AI can't figure
|
||||
it out it will apply rules based on max_idle_turns and max_auto_turns.
|
||||
|
||||
If all fails it will just pick a random character.
|
||||
|
||||
Repetition is also taken into account, so if a character has spoken twice in a row
|
||||
they will not be picked again until someone else has spoken.
|
||||
"""
|
||||
|
||||
scene = self.scene
|
||||
if self.actions["natural_flow"].enabled and len(scene.character_names) > 2:
|
||||
|
||||
# last time each character spoke (turns ago)
|
||||
max_idle_turns = self.actions["natural_flow"].config["max_idle_turns"].value
|
||||
max_auto_turns = self.actions["natural_flow"].config["max_auto_turns"].value
|
||||
last_turn = self.last_spoken()
|
||||
last_turn_player = last_turn.get(scene.get_player_character().name, 0)
|
||||
|
||||
if last_turn_player >= max_auto_turns:
|
||||
self.scene.next_actor = scene.get_player_character().name
|
||||
log.debug("conversation_agent.natural_flow", next_actor="player", overdue=True, player_character=scene.get_player_character().name)
|
||||
return
|
||||
|
||||
log.debug("conversation_agent.natural_flow", last_turn=last_turn)
|
||||
|
||||
# determine random character to talk, this will be the fallback in case
|
||||
# the AI can't figure out who should talk next
|
||||
|
||||
if scene.prev_actor:
|
||||
|
||||
# we dont want to talk to the same person twice in a row
|
||||
character_names = scene.character_names
|
||||
character_names.remove(scene.prev_actor)
|
||||
random_character_name = random.choice(character_names)
|
||||
else:
|
||||
character_names = scene.character_names
|
||||
# no one has talked yet, so we just pick a random character
|
||||
|
||||
random_character_name = random.choice(scene.character_names)
|
||||
|
||||
overdue_characters = [character for character, turn in last_turn.items() if turn >= max_idle_turns]
|
||||
|
||||
if overdue_characters and self.scene.history:
|
||||
# Pick a random character from the overdue characters
|
||||
scene.next_actor = random.choice(overdue_characters)
|
||||
elif scene.history:
|
||||
scene.next_actor = None
|
||||
|
||||
# AI will attempt to figure out who should talk next
|
||||
next_actor = await self.select_talking_actor(character_names)
|
||||
next_actor = next_actor.strip().strip('"').strip(".")
|
||||
|
||||
for character_name in scene.character_names:
|
||||
if next_actor.lower() in character_name.lower() or character_name.lower() in next_actor.lower():
|
||||
scene.next_actor = character_name
|
||||
break
|
||||
|
||||
if not scene.next_actor:
|
||||
# AI couldn't figure out who should talk next, so we just pick a random character
|
||||
log.debug("conversation_agent.natural_flow", next_actor="random", random_character_name=random_character_name)
|
||||
scene.next_actor = random_character_name
|
||||
else:
|
||||
log.debug("conversation_agent.natural_flow", next_actor="picked", ai_next_actor=scene.next_actor)
|
||||
else:
|
||||
# always start with main character (TODO: configurable?)
|
||||
player_character = scene.get_player_character()
|
||||
log.debug("conversation_agent.natural_flow", next_actor="main_character", main_character=player_character)
|
||||
scene.next_actor = player_character.name if player_character else random_character_name
|
||||
|
||||
scene.log.debug("conversation_agent.natural_flow", next_actor=scene.next_actor)
|
||||
|
||||
|
||||
# same character cannot go thrice in a row, if this is happening, pick a random character that
|
||||
# isnt the same as the last character
|
||||
|
||||
if self.repeated_speaker() >= 2 and self.scene.prev_actor == self.scene.next_actor:
|
||||
scene.next_actor = random.choice([c for c in scene.character_names if c != scene.prev_actor])
|
||||
scene.log.debug("conversation_agent.natural_flow", next_actor="random (repeated safeguard)", random_character_name=scene.next_actor)
|
||||
|
||||
else:
|
||||
scene.next_actor = None
|
||||
|
||||
|
||||
@set_processing
|
||||
async def select_talking_actor(self, character_names: list[str]=None):
|
||||
result = await Prompt.request("conversation.select-talking-actor", self.client, "conversation_select_talking_actor", vars={
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"character_names": character_names or self.scene.character_names,
|
||||
"character_names_formatted": ", ".join(character_names or self.scene.character_names),
|
||||
})
|
||||
|
||||
return result
|
||||
|
||||
|
||||
async def build_prompt_default(
|
||||
self,
|
||||
@@ -71,10 +322,7 @@ class ConversationAgent(Agent):
|
||||
insert_bot_token=10
|
||||
)
|
||||
|
||||
memory = await self.build_prompt_default_memory(
|
||||
scene, long_term_memory_budget,
|
||||
scene_and_dialogue + [f"{character.name}: {character.description}" for character in scene.get_characters()]
|
||||
)
|
||||
memory = await self.build_prompt_default_memory(character)
|
||||
|
||||
main_character = scene.main_character.character
|
||||
|
||||
@@ -96,6 +344,10 @@ class ConversationAgent(Agent):
|
||||
director_message = isinstance(scene_and_dialogue[-1], DirectorMessage)
|
||||
except IndexError:
|
||||
director_message = False
|
||||
|
||||
extra_instructions = ""
|
||||
if self.actions["generation_override"].enabled:
|
||||
extra_instructions = self.actions["generation_override"].config["instructions"].value
|
||||
|
||||
prompt = Prompt.get("conversation.dialogue", vars={
|
||||
"scene": scene,
|
||||
@@ -109,12 +361,13 @@ class ConversationAgent(Agent):
|
||||
"talking_character": character,
|
||||
"partial_message": char_message,
|
||||
"director_message": director_message,
|
||||
"extra_instructions": extra_instructions,
|
||||
})
|
||||
|
||||
return str(prompt)
|
||||
|
||||
async def build_prompt_default_memory(
|
||||
self, scene: Scene, budget: int, existing_context: list
|
||||
self, character: Character
|
||||
):
|
||||
"""
|
||||
Builds long term memory for the conversation prompt
|
||||
@@ -127,29 +380,35 @@ class ConversationAgent(Agent):
|
||||
Also it will only add information that is not already in the existing context.
|
||||
"""
|
||||
|
||||
memory = scene.get_helper("memory").agent
|
||||
|
||||
if not memory:
|
||||
if not self.actions["use_long_term_memory"].enabled:
|
||||
return []
|
||||
|
||||
|
||||
if self.current_memory_context:
|
||||
return self.current_memory_context
|
||||
|
||||
self.current_memory_context = []
|
||||
self.current_memory_context = ""
|
||||
|
||||
|
||||
# feed the last 3 history message into multi_query
|
||||
history_length = len(scene.history)
|
||||
i = history_length - 1
|
||||
while i >= 0 and i >= len(scene.history) - 3:
|
||||
self.current_memory_context += await memory.multi_query(
|
||||
[scene.history[i]],
|
||||
filter=lambda x: x
|
||||
not in self.current_memory_context + existing_context,
|
||||
if self.actions["use_long_term_memory"].config["ai_selected"].value:
|
||||
history = self.scene.context_history(min_dialogue=3, max_dialogue=15, keep_director=False, sections=False, add_archieved_history=False)
|
||||
text = "\n".join(history)
|
||||
world_state = instance.get_agent("world_state")
|
||||
log.debug("conversation_agent.build_prompt_default_memory", direct=False)
|
||||
self.current_memory_context = await world_state.analyze_text_and_extract_context(
|
||||
text, f"continue the conversation as {character.name}"
|
||||
)
|
||||
i -= 1
|
||||
|
||||
else:
|
||||
history = self.scene.context_history(min_dialogue=3, max_dialogue=3, keep_director=False, sections=False, add_archieved_history=False)
|
||||
log.debug("conversation_agent.build_prompt_default_memory", history=history, direct=True)
|
||||
memory = instance.get_agent("memory")
|
||||
|
||||
context = await memory.multi_query(history, max_tokens=500, iterate=5)
|
||||
|
||||
self.current_memory_context = "\n".join(context)
|
||||
|
||||
return self.current_memory_context
|
||||
|
||||
|
||||
async def build_prompt(self, character, char_message: str = ""):
|
||||
fn = self.build_prompt_default
|
||||
@@ -158,24 +417,30 @@ class ConversationAgent(Agent):
|
||||
|
||||
def clean_result(self, result, character):
|
||||
|
||||
log.debug("clean result", result=result)
|
||||
|
||||
if "#" in result:
|
||||
result = result.split("#")[0]
|
||||
|
||||
result = result.replace(" :", ":")
|
||||
result = result.strip().strip('"').strip()
|
||||
result = result.replace("[", "*").replace("]", "*")
|
||||
result = result.replace("(", "*").replace(")", "*")
|
||||
result = result.replace("**", "*")
|
||||
|
||||
# if there is an uneven number of '*' add one to the end
|
||||
|
||||
if result.count("*") % 2 == 1:
|
||||
result += "*"
|
||||
|
||||
return result
|
||||
|
||||
def set_generation_overrides(self):
|
||||
if not self.actions["generation_override"].enabled:
|
||||
return
|
||||
|
||||
set_conversation_context_attribute("length", self.actions["generation_override"].config["length"].value)
|
||||
|
||||
if self.actions["generation_override"].config["jiggle"].value > 0.0:
|
||||
nuke_repetition = client_context_attribute("nuke_repetition")
|
||||
if nuke_repetition == 0.0:
|
||||
# we only apply the agent override if some other mechanism isn't already
|
||||
# setting the nuke_repetition value
|
||||
nuke_repetition = self.actions["generation_override"].config["jiggle"].value
|
||||
set_client_context_attribute("nuke_repetition", nuke_repetition)
|
||||
|
||||
@set_processing
|
||||
async def converse(self, actor, editor=None):
|
||||
"""
|
||||
@@ -186,6 +451,11 @@ class ConversationAgent(Agent):
|
||||
self.current_memory_context = None
|
||||
|
||||
character = actor.character
|
||||
|
||||
emission = ConversationAgentEmission(agent=self, generation="", actor=actor, character=character)
|
||||
await talemate.emit.async_signals.get("agent.conversation.before_generate").send(emission)
|
||||
|
||||
self.set_generation_overrides()
|
||||
|
||||
result = await self.client.send_prompt(await self.build_prompt(character))
|
||||
|
||||
@@ -230,14 +500,11 @@ class ConversationAgent(Agent):
|
||||
total_result = total_result.split("#")[0]
|
||||
|
||||
# Removes partial sentence at the end
|
||||
total_result = util.strip_partial_sentences(total_result)
|
||||
total_result = util.clean_dialogue(total_result, main_name=character.name)
|
||||
|
||||
# Remove "{character.name}:" - all occurences
|
||||
total_result = total_result.replace(f"{character.name}:", "")
|
||||
|
||||
if total_result.count("*") % 2 == 1:
|
||||
total_result += "*"
|
||||
|
||||
# Check if total_result starts with character name, if not, prepend it
|
||||
if not total_result.startswith(character.name):
|
||||
total_result = f"{character.name}: {total_result}"
|
||||
@@ -253,13 +520,15 @@ class ConversationAgent(Agent):
|
||||
)
|
||||
|
||||
response_message = util.parse_messages_from_str(total_result, [character.name])
|
||||
|
||||
log.info("conversation agent", result=response_message)
|
||||
|
||||
emission = ConversationAgentEmission(agent=self, generation=response_message, actor=actor, character=character)
|
||||
await talemate.emit.async_signals.get("agent.conversation.generated").send(emission)
|
||||
|
||||
if editor:
|
||||
response_message = [
|
||||
editor.help_edit(character, message) for message in response_message
|
||||
]
|
||||
#log.info("conversation agent", generation=emission.generation)
|
||||
|
||||
messages = [CharacterMessage(message) for message in response_message]
|
||||
messages = [CharacterMessage(message) for message in emission.generation]
|
||||
|
||||
# Add message and response to conversation history
|
||||
actor.scene.push_history(messages)
|
||||
|
||||
@@ -3,15 +3,16 @@ from __future__ import annotations
|
||||
import json
|
||||
import os
|
||||
|
||||
from talemate.agents.conversation import ConversationAgent
|
||||
from talemate.agents.base import Agent
|
||||
from talemate.agents.registry import register
|
||||
from talemate.emit import emit
|
||||
import talemate.client as client
|
||||
|
||||
from .character import CharacterCreatorMixin
|
||||
from .scenario import ScenarioCreatorMixin
|
||||
|
||||
@register()
|
||||
class CreatorAgent(CharacterCreatorMixin, ScenarioCreatorMixin, ConversationAgent):
|
||||
class CreatorAgent(CharacterCreatorMixin, ScenarioCreatorMixin, Agent):
|
||||
|
||||
"""
|
||||
Creates characters and scenarios and other fun stuff!
|
||||
@@ -20,6 +21,13 @@ class CreatorAgent(CharacterCreatorMixin, ScenarioCreatorMixin, ConversationAgen
|
||||
agent_type = "creator"
|
||||
verbose_name = "Creator"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
client: client.TaleMateClient,
|
||||
**kwargs,
|
||||
):
|
||||
self.client = client
|
||||
|
||||
def clean_result(self, result):
|
||||
if "#" in result:
|
||||
result = result.split("#")[0]
|
||||
|
||||
@@ -9,6 +9,8 @@ from typing import TYPE_CHECKING, Callable
|
||||
import talemate.util as util
|
||||
from talemate.emit import emit
|
||||
from talemate.prompts import Prompt, LoopedPrompt
|
||||
from talemate.exceptions import LLMAccuracyError
|
||||
from talemate.agents.base import set_processing
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from talemate.tale_mate import Character
|
||||
@@ -19,7 +21,11 @@ def validate(k,v):
|
||||
if k and k.lower() == "gender":
|
||||
return v.lower().strip()
|
||||
if k and k.lower() == "age":
|
||||
return int(v.strip())
|
||||
try:
|
||||
return int(v.split("\n")[0].strip())
|
||||
except (ValueError, TypeError):
|
||||
raise LLMAccuracyError("Was unable to get a valid age from the response", model_name=None)
|
||||
|
||||
return v.strip().strip("\n")
|
||||
|
||||
DEFAULT_CONTENT_CONTEXT="a fun and engaging adventure aimed at an adult audience."
|
||||
@@ -31,6 +37,7 @@ class CharacterCreatorMixin:
|
||||
|
||||
## NEW
|
||||
|
||||
@set_processing
|
||||
async def create_character_attributes(
|
||||
self,
|
||||
character_prompt: str,
|
||||
@@ -42,60 +49,55 @@ class CharacterCreatorMixin:
|
||||
predefined_attributes: dict[str, str] = dict(),
|
||||
):
|
||||
|
||||
try:
|
||||
await self.emit_status(processing=True)
|
||||
|
||||
def spice(prompt, spices):
|
||||
# generate number from 0 to 1 and if its smaller than use_spice
|
||||
# select a random spice from the list and return it formatted
|
||||
# in the prompt
|
||||
if random.random() < use_spice:
|
||||
spice = random.choice(spices)
|
||||
return prompt.format(spice=spice)
|
||||
return ""
|
||||
|
||||
# drop any empty attributes from predefined_attributes
|
||||
|
||||
predefined_attributes = {k:v for k,v in predefined_attributes.items() if v}
|
||||
|
||||
prompt = Prompt.get(f"creator.character-attributes-{template}", vars={
|
||||
"character_prompt": character_prompt,
|
||||
"template": template,
|
||||
"spice": spice,
|
||||
"content_context": content_context,
|
||||
"custom_attributes": custom_attributes,
|
||||
"character_sheet": LoopedPrompt(
|
||||
validate_value=validate,
|
||||
on_update=attribute_callback,
|
||||
generated=predefined_attributes,
|
||||
),
|
||||
})
|
||||
await prompt.loop(self.client, "character_sheet", kind="create_concise")
|
||||
|
||||
return prompt.vars["character_sheet"].generated
|
||||
|
||||
finally:
|
||||
await self.emit_status(processing=False)
|
||||
def spice(prompt, spices):
|
||||
# generate number from 0 to 1 and if its smaller than use_spice
|
||||
# select a random spice from the list and return it formatted
|
||||
# in the prompt
|
||||
if random.random() < use_spice:
|
||||
spice = random.choice(spices)
|
||||
return prompt.format(spice=spice)
|
||||
return ""
|
||||
|
||||
# drop any empty attributes from predefined_attributes
|
||||
|
||||
predefined_attributes = {k:v for k,v in predefined_attributes.items() if v}
|
||||
|
||||
prompt = Prompt.get(f"creator.character-attributes-{template}", vars={
|
||||
"character_prompt": character_prompt,
|
||||
"template": template,
|
||||
"spice": spice,
|
||||
"content_context": content_context,
|
||||
"custom_attributes": custom_attributes,
|
||||
"character_sheet": LoopedPrompt(
|
||||
validate_value=validate,
|
||||
on_update=attribute_callback,
|
||||
generated=predefined_attributes,
|
||||
),
|
||||
})
|
||||
await prompt.loop(self.client, "character_sheet", kind="create_concise")
|
||||
|
||||
return prompt.vars["character_sheet"].generated
|
||||
|
||||
|
||||
|
||||
@set_processing
|
||||
async def create_character_description(
|
||||
self,
|
||||
character:Character,
|
||||
content_context: str = DEFAULT_CONTENT_CONTEXT,
|
||||
):
|
||||
|
||||
try:
|
||||
await self.emit_status(processing=True)
|
||||
description = await Prompt.request(f"creator.character-description", self.client, "create", vars={
|
||||
"character": character,
|
||||
"content_context": content_context,
|
||||
})
|
||||
|
||||
return description.strip()
|
||||
finally:
|
||||
await self.emit_status(processing=False)
|
||||
description = await Prompt.request(f"creator.character-description", self.client, "create", vars={
|
||||
"character": character,
|
||||
"content_context": content_context,
|
||||
})
|
||||
|
||||
return description.strip()
|
||||
|
||||
|
||||
|
||||
@set_processing
|
||||
async def create_character_details(
|
||||
self,
|
||||
character: Character,
|
||||
@@ -104,23 +106,21 @@ class CharacterCreatorMixin:
|
||||
questions: list[str] = None,
|
||||
content_context: str = DEFAULT_CONTENT_CONTEXT,
|
||||
):
|
||||
try:
|
||||
await self.emit_status(processing=True)
|
||||
prompt = Prompt.get(f"creator.character-details-{template}", vars={
|
||||
"character_details": LoopedPrompt(
|
||||
validate_value=validate,
|
||||
on_update=detail_callback,
|
||||
),
|
||||
"template": template,
|
||||
"content_context": content_context,
|
||||
"character": character,
|
||||
"custom_questions": questions or [],
|
||||
})
|
||||
await prompt.loop(self.client, "character_details", kind="create_concise")
|
||||
return prompt.vars["character_details"].generated
|
||||
finally:
|
||||
await self.emit_status(processing=False)
|
||||
prompt = Prompt.get(f"creator.character-details-{template}", vars={
|
||||
"character_details": LoopedPrompt(
|
||||
validate_value=validate,
|
||||
on_update=detail_callback,
|
||||
),
|
||||
"template": template,
|
||||
"content_context": content_context,
|
||||
"character": character,
|
||||
"custom_questions": questions or [],
|
||||
})
|
||||
await prompt.loop(self.client, "character_details", kind="create_concise")
|
||||
return prompt.vars["character_details"].generated
|
||||
|
||||
|
||||
@set_processing
|
||||
async def create_character_example_dialogue(
|
||||
self,
|
||||
character: Character,
|
||||
@@ -132,64 +132,86 @@ class CharacterCreatorMixin:
|
||||
rules_callback: Callable = lambda rules: None,
|
||||
):
|
||||
|
||||
try:
|
||||
await self.emit_status(processing=True)
|
||||
dialogue_rules = await Prompt.request(f"creator.character-dialogue-rules", self.client, "create", vars={
|
||||
"guide": guide,
|
||||
"character": character,
|
||||
"examples": examples or [],
|
||||
"content_context": content_context,
|
||||
})
|
||||
|
||||
dialogue_rules = await Prompt.request(f"creator.character-dialogue-rules", self.client, "create", vars={
|
||||
"guide": guide,
|
||||
"character": character,
|
||||
"examples": examples or [],
|
||||
"content_context": content_context,
|
||||
})
|
||||
log.info("dialogue_rules", dialogue_rules=dialogue_rules)
|
||||
|
||||
if rules_callback:
|
||||
rules_callback(dialogue_rules)
|
||||
|
||||
log.info("dialogue_rules", dialogue_rules=dialogue_rules)
|
||||
|
||||
if rules_callback:
|
||||
rules_callback(dialogue_rules)
|
||||
|
||||
example_dialogue_prompt = Prompt.get(f"creator.character-example-dialogue-{template}", vars={
|
||||
"guide": guide,
|
||||
"character": character,
|
||||
"examples": examples or [],
|
||||
"content_context": content_context,
|
||||
"dialogue_rules": dialogue_rules,
|
||||
"generated_examples": LoopedPrompt(
|
||||
validate_value=validate,
|
||||
on_update=example_callback,
|
||||
),
|
||||
})
|
||||
|
||||
await example_dialogue_prompt.loop(self.client, "generated_examples", kind="create")
|
||||
|
||||
return example_dialogue_prompt.vars["generated_examples"].generated
|
||||
finally:
|
||||
await self.emit_status(processing=False)
|
||||
example_dialogue_prompt = Prompt.get(f"creator.character-example-dialogue-{template}", vars={
|
||||
"guide": guide,
|
||||
"character": character,
|
||||
"examples": examples or [],
|
||||
"content_context": content_context,
|
||||
"dialogue_rules": dialogue_rules,
|
||||
"generated_examples": LoopedPrompt(
|
||||
validate_value=validate,
|
||||
on_update=example_callback,
|
||||
),
|
||||
})
|
||||
|
||||
await example_dialogue_prompt.loop(self.client, "generated_examples", kind="create")
|
||||
|
||||
return example_dialogue_prompt.vars["generated_examples"].generated
|
||||
|
||||
|
||||
|
||||
@set_processing
|
||||
async def determine_content_context_for_character(
|
||||
self,
|
||||
character: Character,
|
||||
):
|
||||
|
||||
try:
|
||||
await self.emit_status(processing=True)
|
||||
content_context = await Prompt.request(f"creator.determine-content-context", self.client, "create", vars={
|
||||
"character": character,
|
||||
})
|
||||
return content_context.strip()
|
||||
finally:
|
||||
await self.emit_status(processing=False)
|
||||
content_context = await Prompt.request(f"creator.determine-content-context", self.client, "create", vars={
|
||||
"character": character,
|
||||
})
|
||||
return content_context.strip()
|
||||
|
||||
|
||||
@set_processing
|
||||
async def determine_character_attributes(
|
||||
self,
|
||||
character: Character,
|
||||
):
|
||||
|
||||
try:
|
||||
await self.emit_status(processing=True)
|
||||
attributes = await Prompt.request(f"creator.determine-character-attributes", self.client, "analyze_long", vars={
|
||||
"character": character,
|
||||
})
|
||||
return attributes
|
||||
finally:
|
||||
await self.emit_status(processing=False)
|
||||
attributes = await Prompt.request(f"creator.determine-character-attributes", self.client, "analyze_long", vars={
|
||||
"character": character,
|
||||
})
|
||||
return attributes
|
||||
|
||||
@set_processing
|
||||
async def determine_character_description(
|
||||
self,
|
||||
character: Character,
|
||||
text:str=""
|
||||
):
|
||||
|
||||
description = await Prompt.request(f"creator.determine-character-description", self.client, "create", vars={
|
||||
"character": character,
|
||||
"scene": self.scene,
|
||||
"text": text,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
})
|
||||
return description.strip()
|
||||
|
||||
@set_processing
|
||||
async def generate_character_from_text(
|
||||
self,
|
||||
text: str,
|
||||
template: str,
|
||||
content_context: str = DEFAULT_CONTENT_CONTEXT,
|
||||
):
|
||||
|
||||
base_attributes = await self.create_character_attributes(
|
||||
character_prompt=text,
|
||||
template=template,
|
||||
content_context=content_context,
|
||||
)
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import re
|
||||
import random
|
||||
|
||||
from talemate.prompts import Prompt
|
||||
from talemate.agents.base import set_processing
|
||||
|
||||
class ScenarioCreatorMixin:
|
||||
|
||||
@@ -10,8 +11,7 @@ class ScenarioCreatorMixin:
|
||||
Adds scenario creation functionality to the creator agent
|
||||
"""
|
||||
|
||||
### NEW
|
||||
|
||||
@set_processing
|
||||
async def create_scene_description(
|
||||
self,
|
||||
prompt:str,
|
||||
@@ -29,27 +29,23 @@ class ScenarioCreatorMixin:
|
||||
|
||||
callback (callable): A callback to call when the scene has been created.
|
||||
"""
|
||||
try:
|
||||
await self.emit_status(processing=True)
|
||||
scene = self.scene
|
||||
scene = self.scene
|
||||
|
||||
description = await Prompt.request(
|
||||
"creator.scenario-description",
|
||||
self.client,
|
||||
"create",
|
||||
vars={
|
||||
"prompt": prompt,
|
||||
"content_context": content_context,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"scene": scene,
|
||||
}
|
||||
)
|
||||
description = description.strip()
|
||||
|
||||
return description
|
||||
|
||||
description = await Prompt.request(
|
||||
"creator.scenario-description",
|
||||
self.client,
|
||||
"create",
|
||||
vars={
|
||||
"prompt": prompt,
|
||||
"content_context": content_context,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"scene": scene,
|
||||
}
|
||||
)
|
||||
description = description.strip()
|
||||
|
||||
return description
|
||||
|
||||
finally:
|
||||
await self.emit_status(processing=False)
|
||||
|
||||
|
||||
async def create_scene_name(
|
||||
@@ -70,27 +66,21 @@ class ScenarioCreatorMixin:
|
||||
|
||||
description (str): The description of the scene.
|
||||
"""
|
||||
try:
|
||||
await self.emit_status(processing=True)
|
||||
|
||||
scene = self.scene
|
||||
|
||||
name = await Prompt.request(
|
||||
"creator.scenario-name",
|
||||
self.client,
|
||||
"create",
|
||||
vars={
|
||||
"prompt": prompt,
|
||||
"content_context": content_context,
|
||||
"description": description,
|
||||
"scene": scene,
|
||||
}
|
||||
)
|
||||
name = name.strip().strip('.!').replace('"','')
|
||||
return name
|
||||
|
||||
finally:
|
||||
await self.emit_status(processing=False)
|
||||
scene = self.scene
|
||||
|
||||
name = await Prompt.request(
|
||||
"creator.scenario-name",
|
||||
self.client,
|
||||
"create",
|
||||
vars={
|
||||
"prompt": prompt,
|
||||
"content_context": content_context,
|
||||
"description": description,
|
||||
"scene": scene,
|
||||
}
|
||||
)
|
||||
name = name.strip().strip('.!').replace('"','')
|
||||
return name
|
||||
|
||||
|
||||
async def create_scene_intro(
|
||||
@@ -114,25 +104,30 @@ class ScenarioCreatorMixin:
|
||||
|
||||
name (str): The name of the scene.
|
||||
"""
|
||||
try:
|
||||
await self.emit_status(processing=True)
|
||||
|
||||
scene = self.scene
|
||||
|
||||
intro = await Prompt.request(
|
||||
"creator.scenario-intro",
|
||||
self.client,
|
||||
"create",
|
||||
vars={
|
||||
"prompt": prompt,
|
||||
"content_context": content_context,
|
||||
"description": description,
|
||||
"name": name,
|
||||
"scene": scene,
|
||||
}
|
||||
)
|
||||
intro = intro.strip()
|
||||
return intro
|
||||
|
||||
finally:
|
||||
await self.emit_status(processing=False)
|
||||
|
||||
scene = self.scene
|
||||
|
||||
intro = await Prompt.request(
|
||||
"creator.scenario-intro",
|
||||
self.client,
|
||||
"create",
|
||||
vars={
|
||||
"prompt": prompt,
|
||||
"content_context": content_context,
|
||||
"description": description,
|
||||
"name": name,
|
||||
"scene": scene,
|
||||
}
|
||||
)
|
||||
intro = intro.strip()
|
||||
return intro
|
||||
|
||||
@set_processing
|
||||
async def determine_scenario_description(
|
||||
self,
|
||||
text:str
|
||||
):
|
||||
description = await Prompt.request(f"creator.determine-scenario-description", self.client, "analyze_long", vars={
|
||||
"text": text,
|
||||
})
|
||||
return description
|
||||
|
||||
@@ -8,13 +8,14 @@ from typing import TYPE_CHECKING, Callable, List, Optional, Union
|
||||
|
||||
import talemate.util as util
|
||||
from talemate.emit import wait_for_input, emit
|
||||
import talemate.emit.async_signals
|
||||
from talemate.prompts import Prompt
|
||||
from talemate.scene_message import NarratorMessage, DirectorMessage
|
||||
from talemate.automated_action import AutomatedAction
|
||||
import talemate.automated_action as automated_action
|
||||
from .conversation import ConversationAgent
|
||||
from talemate.agents.conversation import ConversationAgentEmission
|
||||
from .registry import register
|
||||
from .base import set_processing
|
||||
from .base import set_processing, AgentAction, AgentActionConfig, Agent
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from talemate import Actor, Character, Player, Scene
|
||||
@@ -22,350 +23,84 @@ if TYPE_CHECKING:
|
||||
log = structlog.get_logger("talemate")
|
||||
|
||||
@register()
|
||||
class DirectorAgent(ConversationAgent):
|
||||
class DirectorAgent(Agent):
|
||||
agent_type = "director"
|
||||
verbose_name = "Director"
|
||||
|
||||
def get_base_prompt(self, character: Character, budget:int):
|
||||
return [character.description, character.base_attributes.get("scenario_context", "")] + self.scene.context_history(budget=budget, keep_director=False)
|
||||
|
||||
def __init__(self, client, **kwargs):
|
||||
self.is_enabled = False
|
||||
self.client = client
|
||||
self.next_direct = 0
|
||||
self.actions = {
|
||||
"direct": AgentAction(enabled=True, label="Direct", description="Will attempt to direct the scene. Runs automatically after AI dialogue (n turns).", config={
|
||||
"turns": AgentActionConfig(type="number", label="Turns", description="Number of turns to wait before directing the sceen", value=5, min=1, max=100, step=1),
|
||||
"prompt": AgentActionConfig(type="text", label="Instructions", description="Instructions to the director", value="", scope="scene")
|
||||
}),
|
||||
}
|
||||
|
||||
@property
|
||||
def enabled(self):
|
||||
return self.is_enabled
|
||||
|
||||
async def decide_action(self, character: Character, goal_override:str=None):
|
||||
@property
|
||||
def has_toggle(self):
|
||||
return True
|
||||
|
||||
"""
|
||||
Pick an action to perform to move the story towards the current story goal
|
||||
"""
|
||||
@property
|
||||
def experimental(self):
|
||||
return True
|
||||
|
||||
def connect(self, scene):
|
||||
super().connect(scene)
|
||||
talemate.emit.async_signals.get("agent.conversation.before_generate").connect(self.on_conversation_before_generate)
|
||||
|
||||
current_goal = goal_override or await self.select_goal(self.scene)
|
||||
current_goal = f"Current story goal: {current_goal}" if current_goal else current_goal
|
||||
async def on_conversation_before_generate(self, event:ConversationAgentEmission):
|
||||
log.info("on_conversation_before_generate", director_enabled=self.enabled)
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
response, action_eval, prompt = await self.decide_action_analyze(character, current_goal)
|
||||
# action_eval will hold {'narrate': N, 'direct': N, 'watch': N, ...}
|
||||
# where N is a number, action with the highest number wins, default action is watch
|
||||
# if there is no clear winner
|
||||
await self.direct_scene(event.character)
|
||||
|
||||
watch_action = action_eval.get("watch", 0)
|
||||
action = max(action_eval, key=action_eval.get)
|
||||
async def direct_scene(self, character: Character):
|
||||
|
||||
if action_eval[action] <= watch_action:
|
||||
action = "watch"
|
||||
if not self.actions["direct"].enabled:
|
||||
log.info("direct_scene", skip=True, enabled=self.actions["direct"].enabled)
|
||||
return
|
||||
|
||||
log.info("decide_action", action=action, action_eval=action_eval)
|
||||
prompt = self.actions["direct"].config["prompt"].value
|
||||
|
||||
return response, current_goal, action
|
||||
if not prompt:
|
||||
log.info("direct_scene", skip=True, prompt=prompt)
|
||||
return
|
||||
|
||||
if self.next_direct % self.actions["direct"].config["turns"].value != 0 or self.next_direct == 0:
|
||||
|
||||
log.info("direct_scene", skip=True, next_direct=self.next_direct)
|
||||
self.next_direct += 1
|
||||
return
|
||||
|
||||
async def decide_action_analyze(self, character: Character, goal:str):
|
||||
|
||||
prompt = Prompt.get("director.decide-action-analyze", vars={
|
||||
self.next_direct = 0
|
||||
|
||||
await self.direct_character(character, prompt)
|
||||
|
||||
@set_processing
|
||||
async def direct_character(self, character: Character, prompt:str):
|
||||
|
||||
response = await Prompt.request("director.direct-scene", self.client, "director", vars={
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"scene": self.scene,
|
||||
"current_goal": goal,
|
||||
"prompt": prompt,
|
||||
"character": character,
|
||||
})
|
||||
|
||||
response, evaluation = await prompt.send(self.client, kind="director")
|
||||
|
||||
log.info("question_direction", response=response)
|
||||
return response, evaluation, prompt
|
||||
|
||||
@set_processing
|
||||
async def direct(self, character: Character, goal_override:str=None):
|
||||
|
||||
analysis, current_goal, action = await self.decide_action(character, goal_override=goal_override)
|
||||
|
||||
if action == "watch":
|
||||
return None
|
||||
|
||||
if action == "direct":
|
||||
return await self.direct_character_with_self_reflection(character, analysis, goal_override=current_goal)
|
||||
|
||||
if action.startswith("narrate"):
|
||||
|
||||
narration_type = action.split(":")[1]
|
||||
|
||||
direct_narrative = await self.direct_narrative(analysis, narration_type=narration_type, goal=current_goal)
|
||||
if direct_narrative:
|
||||
narrator = self.scene.get_helper("narrator").agent
|
||||
narrator_response = await narrator.progress_story(direct_narrative)
|
||||
if not narrator_response:
|
||||
return None
|
||||
narrator_message = NarratorMessage(narrator_response, source="progress_story")
|
||||
self.scene.push_history(narrator_message)
|
||||
emit("narrator", narrator_message)
|
||||
return True
|
||||
|
||||
|
||||
@set_processing
|
||||
async def direct_narrative(self, analysis:str, narration_type:str="progress", goal:str=None):
|
||||
|
||||
if goal is None:
|
||||
goal = await self.select_goal(self.scene)
|
||||
|
||||
prompt = Prompt.get("director.direct-narrative", vars={
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"scene": self.scene,
|
||||
"narration_type": narration_type,
|
||||
"analysis": analysis,
|
||||
"current_goal": goal,
|
||||
})
|
||||
|
||||
response = await prompt.send(self.client, kind="director")
|
||||
response = response.strip().split("\n")[0].strip()
|
||||
|
||||
if not response:
|
||||
return None
|
||||
|
||||
return response
|
||||
|
||||
@set_processing
|
||||
async def direct_character_with_self_reflection(self, character: Character, analysis:str, goal_override:str=None):
|
||||
|
||||
max_retries = 3
|
||||
num_retries = 0
|
||||
keep_direction = False
|
||||
response = None
|
||||
self_reflection = None
|
||||
|
||||
while num_retries < max_retries:
|
||||
|
||||
response, direction_prompt = await self.direct_character(
|
||||
character,
|
||||
analysis,
|
||||
goal_override=goal_override,
|
||||
previous_direction=response,
|
||||
previous_direction_feedback=self_reflection
|
||||
)
|
||||
|
||||
keep_direction, self_reflection = await self.direct_character_self_reflect(
|
||||
response, character, goal_override, direction_prompt
|
||||
)
|
||||
|
||||
if keep_direction:
|
||||
break
|
||||
|
||||
num_retries += 1
|
||||
|
||||
log.info("direct_character_with_self_reflection", response=response, keep_direction=keep_direction)
|
||||
|
||||
if not keep_direction:
|
||||
return None
|
||||
|
||||
#character_agreement = f" *{character.name} agrees with the director and progresses the story accordingly*"
|
||||
#
|
||||
#if "accordingly" not in response:
|
||||
# response += character_agreement
|
||||
#
|
||||
|
||||
#response = await self.transform_character_direction_to_inner_monologue(character, response)
|
||||
|
||||
return response
|
||||
|
||||
@set_processing
|
||||
async def transform_character_direction_to_inner_monologue(self, character:Character, direction:str):
|
||||
|
||||
inner_monologue = await Prompt.request(
|
||||
"conversation.direction-to-inner-monologue",
|
||||
self.client,
|
||||
"conversation_long",
|
||||
vars={
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"scene": self.scene,
|
||||
"character": character,
|
||||
"director_instructions": direction,
|
||||
}
|
||||
)
|
||||
|
||||
return inner_monologue
|
||||
|
||||
|
||||
@set_processing
|
||||
async def direct_character(
|
||||
self,
|
||||
character: Character,
|
||||
analysis:str,
|
||||
goal_override:str=None,
|
||||
previous_direction:str=None,
|
||||
previous_direction_feedback:str=None,
|
||||
):
|
||||
"""
|
||||
Direct the scene
|
||||
"""
|
||||
|
||||
if goal_override:
|
||||
current_goal = goal_override
|
||||
else:
|
||||
current_goal = await self.select_goal(self.scene)
|
||||
|
||||
if current_goal and not current_goal.startswith("Current story goal: "):
|
||||
current_goal = f"Current story goal: {current_goal}"
|
||||
|
||||
prompt = Prompt.get("director.direct-character", vars={
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"scene": self.scene,
|
||||
"character": character,
|
||||
"current_goal": current_goal,
|
||||
"previous_direction": previous_direction,
|
||||
"previous_direction_feedback": previous_direction_feedback,
|
||||
"analysis": analysis,
|
||||
})
|
||||
|
||||
response = await prompt.send(self.client, kind="director")
|
||||
response = response.strip().split("\n")[0].strip()
|
||||
|
||||
log.info(
|
||||
"direct_character",
|
||||
direction=response,
|
||||
previous_direction=previous_direction,
|
||||
previous_direction_feedback=previous_direction_feedback
|
||||
)
|
||||
|
||||
if not response:
|
||||
return None
|
||||
|
||||
if not response.startswith(prompt.prepared_response):
|
||||
response = prompt.prepared_response + response
|
||||
|
||||
return response, "\n".join(prompt.as_list[:-1])
|
||||
|
||||
|
||||
|
||||
@set_processing
|
||||
async def direct_character_self_reflect(self, direction:str, character: Character, goal:str, direction_prompt:Prompt) -> (bool, str):
|
||||
response += f" (current story goal: {prompt})"
|
||||
|
||||
change_matches = ["change", "retry", "alter", "reconsider"]
|
||||
log.info("direct_scene", response=response)
|
||||
|
||||
prompt = Prompt.get("director.direct-character-self-reflect", vars={
|
||||
"direction_prompt": str(direction_prompt),
|
||||
"direction": direction,
|
||||
"analysis": await self.direct_character_analyze(direction, character, goal, direction_prompt),
|
||||
"character": character,
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
})
|
||||
|
||||
response = await prompt.send(self.client, kind="director")
|
||||
message = DirectorMessage(response, source=character.name)
|
||||
emit("director", message, character=character)
|
||||
|
||||
parse_choice = response[len(prompt.prepared_response):].lower().split(" ")[0]
|
||||
|
||||
keep = not parse_choice in change_matches
|
||||
|
||||
log.info("direct_character_self_reflect", keep=keep, response=response, parsed=parse_choice)
|
||||
|
||||
return keep, response
|
||||
|
||||
|
||||
@set_processing
|
||||
async def direct_character_analyze(self, direction:str, character: Character, goal:str, direction_prompt:Prompt):
|
||||
|
||||
prompt = Prompt.get("director.direct-character-analyze", vars={
|
||||
"direction_prompt": str(direction_prompt),
|
||||
"direction": direction,
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"character": character,
|
||||
})
|
||||
|
||||
analysis = await prompt.send(self.client, kind="director")
|
||||
|
||||
log.info("direct_character_analyze", analysis=analysis)
|
||||
|
||||
return analysis
|
||||
|
||||
async def select_goal(self, scene: Scene):
|
||||
|
||||
if not scene.goals:
|
||||
return ""
|
||||
|
||||
if isinstance(self.scene.goal, int):
|
||||
# fixes legacy goal format
|
||||
self.scene.goal = self.scene.goals[self.scene.goal]
|
||||
|
||||
while True:
|
||||
|
||||
# get current goal position in goals
|
||||
|
||||
current_goal = scene.goal
|
||||
current_goal_positon = None
|
||||
if current_goal:
|
||||
try:
|
||||
current_goal_positon = self.scene.goals.index(current_goal)
|
||||
except ValueError:
|
||||
pass
|
||||
elif self.scene.goals:
|
||||
current_goal = self.scene.goals[0]
|
||||
current_goal_positon = 0
|
||||
else:
|
||||
return ""
|
||||
|
||||
|
||||
# if current goal is set but not found, its a custom goal override
|
||||
|
||||
custom_goal = (current_goal and current_goal_positon is None)
|
||||
|
||||
log.info("select_goal", current_goal=current_goal, current_goal_positon=current_goal_positon, custom_goal=custom_goal)
|
||||
|
||||
if current_goal:
|
||||
current_goal_met = await self.goal_analyze(current_goal)
|
||||
|
||||
log.info("select_goal", current_goal_met=current_goal_met)
|
||||
if current_goal_met is not True:
|
||||
return current_goal + f"\nThe goal has {current_goal_met})"
|
||||
try:
|
||||
self.scene.goal = self.scene.goals[current_goal_positon + 1]
|
||||
continue
|
||||
except IndexError:
|
||||
return ""
|
||||
|
||||
else:
|
||||
return ""
|
||||
|
||||
@set_processing
|
||||
async def goal_analyze(self, goal:str):
|
||||
|
||||
prompt = Prompt.get("director.goal-analyze", vars={
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"current_goal": goal,
|
||||
})
|
||||
|
||||
response = await prompt.send(self.client, kind="director")
|
||||
|
||||
log.info("goal_analyze", response=response)
|
||||
|
||||
if "not satisfied" in response.lower().strip() or "not been satisfied" in response.lower().strip():
|
||||
goal_met = response
|
||||
else:
|
||||
goal_met = True
|
||||
|
||||
return goal_met
|
||||
|
||||
|
||||
@automated_action.register("director", frequency=4, call_initially=True, enabled=False)
|
||||
class AutomatedDirector(automated_action.AutomatedAction):
|
||||
"""
|
||||
Runs director.direct actions every n turns
|
||||
"""
|
||||
|
||||
async def action(self):
|
||||
scene = self.scene
|
||||
director = scene.get_helper("director")
|
||||
|
||||
if not scene.active_actor or scene.active_actor.character.is_player:
|
||||
return False
|
||||
|
||||
if not director:
|
||||
return
|
||||
|
||||
director_response = await director.agent.direct(scene.active_actor.character)
|
||||
|
||||
if director_response is True:
|
||||
# director directed different agent, nothing to do
|
||||
return
|
||||
|
||||
if not director_response:
|
||||
return
|
||||
|
||||
director_message = DirectorMessage(director_response, source=scene.active_actor.character.name)
|
||||
emit("director", director_message, character=scene.active_actor.character)
|
||||
scene.push_history(director_message)
|
||||
self.scene.push_history(message)
|
||||
163
src/talemate/agents/editor.py
Normal file
163
src/talemate/agents/editor.py
Normal file
@@ -0,0 +1,163 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import traceback
|
||||
from typing import TYPE_CHECKING, Callable, List, Optional, Union
|
||||
|
||||
import talemate.data_objects as data_objects
|
||||
import talemate.util as util
|
||||
import talemate.emit.async_signals
|
||||
from talemate.prompts import Prompt
|
||||
from talemate.scene_message import DirectorMessage, TimePassageMessage
|
||||
|
||||
from .base import Agent, set_processing, AgentAction
|
||||
from .registry import register
|
||||
|
||||
import structlog
|
||||
|
||||
import time
|
||||
import re
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from talemate.tale_mate import Actor, Character, Scene
|
||||
from talemate.agents.conversation import ConversationAgentEmission
|
||||
|
||||
log = structlog.get_logger("talemate.agents.editor")
|
||||
|
||||
@register()
|
||||
class EditorAgent(Agent):
|
||||
"""
|
||||
Editor agent
|
||||
|
||||
will attempt to improve the quality of dialogue
|
||||
"""
|
||||
|
||||
agent_type = "editor"
|
||||
verbose_name = "Editor"
|
||||
|
||||
def __init__(self, client, **kwargs):
|
||||
self.client = client
|
||||
self.is_enabled = True
|
||||
self.actions = {
|
||||
"edit_dialogue": AgentAction(enabled=False, label="Edit dialogue", description="Will attempt to improve the quality of dialogue based on the character and scene. Runs automatically after each AI dialogue."),
|
||||
"fix_exposition": AgentAction(enabled=True, label="Fix exposition", description="Will attempt to fix exposition and emotes, making sure they are displayed in italics. Runs automatically after each AI dialogue."),
|
||||
"add_detail": AgentAction(enabled=False, label="Add detail", description="Will attempt to add extra detail and exposition to the dialogue. Runs automatically after each AI dialogue.")
|
||||
}
|
||||
|
||||
@property
|
||||
def enabled(self):
|
||||
return self.is_enabled
|
||||
|
||||
@property
|
||||
def has_toggle(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def experimental(self):
|
||||
return True
|
||||
|
||||
def connect(self, scene):
|
||||
super().connect(scene)
|
||||
talemate.emit.async_signals.get("agent.conversation.generated").connect(self.on_conversation_generated)
|
||||
|
||||
async def on_conversation_generated(self, emission:ConversationAgentEmission):
|
||||
"""
|
||||
Called when a conversation is generated
|
||||
"""
|
||||
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
log.info("editing conversation", emission=emission)
|
||||
|
||||
edited = []
|
||||
for text in emission.generation:
|
||||
|
||||
|
||||
edit = await self.add_detail(
|
||||
text,
|
||||
emission.character
|
||||
)
|
||||
|
||||
edit = await self.edit_conversation(
|
||||
edit,
|
||||
emission.character
|
||||
)
|
||||
|
||||
edit = await self.fix_exposition(
|
||||
edit,
|
||||
emission.character
|
||||
)
|
||||
|
||||
edited.append(edit)
|
||||
|
||||
emission.generation = edited
|
||||
|
||||
|
||||
@set_processing
|
||||
async def edit_conversation(self, content:str, character:Character):
|
||||
"""
|
||||
Edits a conversation
|
||||
"""
|
||||
|
||||
if not self.actions["edit_dialogue"].enabled:
|
||||
return content
|
||||
|
||||
response = await Prompt.request("editor.edit-dialogue", self.client, "edit_dialogue", vars={
|
||||
"content": content,
|
||||
"character": character,
|
||||
"scene": self.scene,
|
||||
"max_length": self.client.max_token_length
|
||||
})
|
||||
|
||||
response = response.split("[end]")[0]
|
||||
|
||||
response = util.replace_exposition_markers(response)
|
||||
response = util.clean_dialogue(response, main_name=character.name)
|
||||
response = util.strip_partial_sentences(response)
|
||||
|
||||
return response
|
||||
|
||||
@set_processing
|
||||
async def fix_exposition(self, content:str, character:Character):
|
||||
"""
|
||||
Edits a text to make sure all narrative exposition and emotes is encased in *
|
||||
"""
|
||||
|
||||
if not self.actions["fix_exposition"].enabled:
|
||||
return content
|
||||
|
||||
#response = await Prompt.request("editor.fix-exposition", self.client, "edit_fix_exposition", vars={
|
||||
# "content": content,
|
||||
# "character": character,
|
||||
# "scene": self.scene,
|
||||
# "max_length": self.client.max_token_length
|
||||
#})
|
||||
|
||||
content = util.clean_dialogue(content, main_name=character.name)
|
||||
content = util.strip_partial_sentences(content)
|
||||
content = util.ensure_dialog_format(content, talking_character=character.name)
|
||||
|
||||
return content
|
||||
|
||||
@set_processing
|
||||
async def add_detail(self, content:str, character:Character):
|
||||
"""
|
||||
Edits a text to increase its length and add extra detail and exposition
|
||||
"""
|
||||
|
||||
if not self.actions["add_detail"].enabled:
|
||||
return content
|
||||
|
||||
response = await Prompt.request("editor.add-detail", self.client, "edit_add_detail", vars={
|
||||
"content": content,
|
||||
"character": character,
|
||||
"scene": self.scene,
|
||||
"max_length": self.client.max_token_length
|
||||
})
|
||||
|
||||
response = util.replace_exposition_markers(response)
|
||||
response = util.clean_dialogue(response, main_name=character.name)
|
||||
response = util.strip_partial_sentences(response)
|
||||
|
||||
return response
|
||||
@@ -6,8 +6,10 @@ from typing import TYPE_CHECKING, Callable, List, Optional, Union
|
||||
from chromadb.config import Settings
|
||||
import talemate.events as events
|
||||
import talemate.util as util
|
||||
from talemate.context import scene_is_loading
|
||||
from talemate.config import load_config
|
||||
import structlog
|
||||
import shutil
|
||||
|
||||
try:
|
||||
import chromadb
|
||||
@@ -34,8 +36,20 @@ class MemoryAgent(Agent):
|
||||
agent_type = "memory"
|
||||
verbose_name = "Long-term memory"
|
||||
|
||||
@property
|
||||
def readonly(self):
|
||||
|
||||
if scene_is_loading.get() and not getattr(self.scene, "_memory_never_persisted", False):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
@property
|
||||
def db_name(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
def config_options(cls):
|
||||
def config_options(cls, agent=None):
|
||||
return {}
|
||||
|
||||
def __init__(self, scene, **kwargs):
|
||||
@@ -50,16 +64,24 @@ class MemoryAgent(Agent):
|
||||
def close_db(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
async def count(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
async def add(self, text, character=None, uid=None, ts:str=None, **kwargs):
|
||||
if not text:
|
||||
return
|
||||
|
||||
if self.readonly:
|
||||
log.debug("memory agent", status="readonly")
|
||||
return
|
||||
await self._add(text, character=character, uid=uid, ts=ts, **kwargs)
|
||||
|
||||
async def _add(self, text, character=None, ts:str=None, **kwargs):
|
||||
raise NotImplementedError()
|
||||
|
||||
async def add_many(self, objects: list[dict]):
|
||||
if self.readonly:
|
||||
log.debug("memory agent", status="readonly")
|
||||
return
|
||||
await self._add_many(objects)
|
||||
|
||||
async def _add_many(self, objects: list[dict]):
|
||||
@@ -131,13 +153,13 @@ class MemoryAgent(Agent):
|
||||
break
|
||||
return memory_context
|
||||
|
||||
async def query(self, query:str, max_tokens:int=1000, filter:Callable=lambda x:True):
|
||||
async def query(self, query:str, max_tokens:int=1000, filter:Callable=lambda x:True, **where):
|
||||
"""
|
||||
Get the character memory context for a given character
|
||||
"""
|
||||
|
||||
try:
|
||||
return (await self.multi_query([query], max_tokens=max_tokens, filter=filter))[0]
|
||||
return (await self.multi_query([query], max_tokens=max_tokens, filter=filter, **where))[0]
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
@@ -158,7 +180,7 @@ class MemoryAgent(Agent):
|
||||
memory_context = []
|
||||
for query in queries:
|
||||
i = 0
|
||||
for memory in await self.get(formatter(query), **where):
|
||||
for memory in await self.get(formatter(query), limit=iterate, **where):
|
||||
if memory in memory_context:
|
||||
continue
|
||||
|
||||
@@ -238,26 +260,52 @@ class ChromaDBMemoryAgent(MemoryAgent):
|
||||
@property
|
||||
def USE_INSTRUCTOR(self):
|
||||
return self.embeddings == "instructor"
|
||||
|
||||
@property
|
||||
def db_name(self):
|
||||
return getattr(self, "collection_name", "<unnamed>")
|
||||
|
||||
def make_collection_name(self, scene):
|
||||
|
||||
if self.USE_OPENAI:
|
||||
suffix = "-openai"
|
||||
elif self.USE_INSTRUCTOR:
|
||||
suffix = "-instructor"
|
||||
model = self.config.get("chromadb").get("instructor_model", "hkunlp/instructor-xl")
|
||||
if "xl" in model:
|
||||
suffix += "-xl"
|
||||
elif "large" in model:
|
||||
suffix += "-large"
|
||||
else:
|
||||
suffix = ""
|
||||
|
||||
return f"{scene.memory_id}-tm{suffix}"
|
||||
|
||||
async def count(self):
|
||||
await asyncio.sleep(0)
|
||||
return self.db.count()
|
||||
|
||||
async def set_db(self):
|
||||
await self.emit_status(processing=True)
|
||||
|
||||
if getattr(self, "db", None):
|
||||
try:
|
||||
self.db.delete(where={"source": "talemate"})
|
||||
except ValueError:
|
||||
pass
|
||||
await self.emit_status(processing=False)
|
||||
|
||||
return
|
||||
|
||||
log.info("chromadb agent", status="setting up db")
|
||||
|
||||
self.db_client = chromadb.Client(Settings(anonymized_telemetry=False))
|
||||
if not getattr(self, "db_client", None):
|
||||
log.info("chromadb agent", status="setting up db client to persistent db")
|
||||
self.db_client = chromadb.PersistentClient(
|
||||
settings=Settings(anonymized_telemetry=False)
|
||||
)
|
||||
|
||||
openai_key = self.config.get("openai").get("api_key") or os.environ.get("OPENAI_API_KEY")
|
||||
|
||||
if openai_key and self.USE_OPENAI:
|
||||
|
||||
self.collection_name = collection_name = self.make_collection_name(self.scene)
|
||||
|
||||
log.info("chromadb agent", status="setting up db", collection_name=collection_name)
|
||||
|
||||
if self.USE_OPENAI:
|
||||
|
||||
if not openai_key:
|
||||
raise ValueError("You must provide an the openai ai key in the config if you want to use it for chromadb embeddings")
|
||||
|
||||
log.info(
|
||||
"crhomadb", status="using openai", openai_key=openai_key[:5] + "..."
|
||||
)
|
||||
@@ -266,7 +314,7 @@ class ChromaDBMemoryAgent(MemoryAgent):
|
||||
model_name="text-embedding-ada-002",
|
||||
)
|
||||
self.db = self.db_client.get_or_create_collection(
|
||||
"talemate-story", embedding_function=openai_ef
|
||||
collection_name, embedding_function=openai_ef
|
||||
)
|
||||
elif self.USE_INSTRUCTOR:
|
||||
|
||||
@@ -281,24 +329,55 @@ class ChromaDBMemoryAgent(MemoryAgent):
|
||||
)
|
||||
|
||||
self.db = self.db_client.get_or_create_collection(
|
||||
"talemate-story", embedding_function=ef
|
||||
collection_name, embedding_function=ef
|
||||
)
|
||||
else:
|
||||
log.info("chromadb", status="using default embeddings")
|
||||
self.db = self.db_client.get_or_create_collection("talemate-story")
|
||||
self.db = self.db_client.get_or_create_collection(collection_name)
|
||||
|
||||
self.scene._memory_never_persisted = self.db.count() == 0
|
||||
|
||||
await self.emit_status(processing=False)
|
||||
log.info("chromadb agent", status="db ready")
|
||||
|
||||
def close_db(self):
|
||||
def clear_db(self):
|
||||
if not self.db:
|
||||
return
|
||||
|
||||
log.info("chromadb agent", status="clearing db", collection_name=self.collection_name)
|
||||
|
||||
self.db.delete(where={"source": "talemate"})
|
||||
|
||||
def drop_db(self):
|
||||
if not self.db:
|
||||
return
|
||||
|
||||
log.info("chromadb agent", status="dropping db", collection_name=self.collection_name)
|
||||
|
||||
try:
|
||||
self.db.delete(where={"source": "talemate"})
|
||||
except ValueError:
|
||||
pass
|
||||
self.db_client.delete_collection(self.collection_name)
|
||||
except ValueError as exc:
|
||||
if "Collection not found" not in str(exc):
|
||||
raise
|
||||
|
||||
def close_db(self, scene):
|
||||
if not self.db:
|
||||
return
|
||||
|
||||
log.info("chromadb agent", status="closing db", collection_name=self.collection_name)
|
||||
|
||||
if not scene.saved:
|
||||
# scene was never saved so we can discard the memory
|
||||
collection_name = self.make_collection_name(scene)
|
||||
log.info("chromadb agent", status="discarding memory", collection_name=collection_name)
|
||||
try:
|
||||
self.db_client.delete_collection(collection_name)
|
||||
except ValueError as exc:
|
||||
if "Collection not found" not in str(exc):
|
||||
raise
|
||||
|
||||
self.db = None
|
||||
|
||||
async def _add(self, text, character=None, uid=None, ts:str=None, **kwargs):
|
||||
metadatas = []
|
||||
ids = []
|
||||
@@ -329,7 +408,7 @@ class ChromaDBMemoryAgent(MemoryAgent):
|
||||
log.debug("chromadb agent add", text=text, meta=meta, id=id)
|
||||
|
||||
self.db.upsert(documents=[text], metadatas=metadatas, ids=ids)
|
||||
|
||||
|
||||
await self.emit_status(processing=False)
|
||||
|
||||
async def _add_many(self, objects: list[dict]):
|
||||
@@ -354,7 +433,7 @@ class ChromaDBMemoryAgent(MemoryAgent):
|
||||
|
||||
await self.emit_status(processing=False)
|
||||
|
||||
async def _get(self, text, character=None, **kwargs):
|
||||
async def _get(self, text, character=None, limit:int=15, **kwargs):
|
||||
await self.emit_status(processing=True)
|
||||
|
||||
where = {}
|
||||
@@ -378,7 +457,10 @@ class ChromaDBMemoryAgent(MemoryAgent):
|
||||
|
||||
#log.debug("crhomadb agent get", text=text, where=where)
|
||||
|
||||
_results = self.db.query(query_texts=[text], where=where)
|
||||
_results = self.db.query(query_texts=[text], where=where, n_results=limit)
|
||||
|
||||
#import json
|
||||
#print(json.dumps(_results["ids"], indent=2))
|
||||
|
||||
results = []
|
||||
|
||||
@@ -405,9 +487,9 @@ class ChromaDBMemoryAgent(MemoryAgent):
|
||||
|
||||
# log.debug("crhomadb agent get", result=results[-1], distance=distance)
|
||||
|
||||
if len(results) > 10:
|
||||
if len(results) > limit:
|
||||
break
|
||||
|
||||
await self.emit_status(processing=False)
|
||||
|
||||
|
||||
return results
|
||||
|
||||
@@ -1,26 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import re
|
||||
from typing import TYPE_CHECKING, Callable, List, Optional, Union
|
||||
|
||||
import structlog
|
||||
import talemate.util as util
|
||||
from talemate.emit import wait_for_input
|
||||
from talemate.emit import emit
|
||||
import talemate.emit.async_signals
|
||||
from talemate.prompts import Prompt
|
||||
from talemate.agents.base import set_processing
|
||||
from talemate.agents.base import set_processing, Agent, AgentAction, AgentActionConfig
|
||||
from talemate.agents.world_state import TimePassageEmission
|
||||
from talemate.scene_message import NarratorMessage
|
||||
import talemate.client as client
|
||||
|
||||
from .conversation import ConversationAgent
|
||||
from .registry import register
|
||||
|
||||
|
||||
|
||||
log = structlog.get_logger("talemate.agents.narrator")
|
||||
|
||||
@register()
|
||||
class NarratorAgent(ConversationAgent):
|
||||
class NarratorAgent(Agent):
|
||||
agent_type = "narrator"
|
||||
verbose_name = "Narrator"
|
||||
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
client: client.TaleMateClient,
|
||||
**kwargs,
|
||||
):
|
||||
self.client = client
|
||||
|
||||
self.actions = {
|
||||
"narrate_time_passage": AgentAction(enabled=False, label="Narrate Time Passage", description="Whenever you indicate passage of time, narrate right after"),
|
||||
}
|
||||
|
||||
def clean_result(self, result):
|
||||
|
||||
result = result.strip().strip(":").strip()
|
||||
|
||||
if "#" in result:
|
||||
result = result.split("#")[0]
|
||||
|
||||
@@ -32,6 +46,20 @@ class NarratorAgent(ConversationAgent):
|
||||
|
||||
return "\n".join(cleaned)
|
||||
|
||||
def connect(self, scene):
|
||||
super().connect(scene)
|
||||
talemate.emit.async_signals.get("agent.world_state.time").connect(self.on_time_passage)
|
||||
|
||||
async def on_time_passage(self, event:TimePassageEmission):
|
||||
|
||||
if not self.actions["narrate_time_passage"].enabled:
|
||||
return
|
||||
|
||||
response = await self.narrate_time_passage(event.duration, event.narrative)
|
||||
narrator_message = NarratorMessage(response, source=f"narrate_time_passage:{event.duration};{event.narrative}")
|
||||
emit("narrator", narrator_message)
|
||||
self.scene.push_history(narrator_message)
|
||||
|
||||
@set_processing
|
||||
async def narrate_scene(self):
|
||||
"""
|
||||
@@ -48,6 +76,9 @@ class NarratorAgent(ConversationAgent):
|
||||
}
|
||||
)
|
||||
|
||||
response = response.strip("*")
|
||||
response = util.strip_partial_sentences(response)
|
||||
|
||||
response = f"*{response.strip('*')}*"
|
||||
|
||||
return response
|
||||
@@ -209,4 +240,29 @@ class NarratorAgent(ConversationAgent):
|
||||
answers = [a for a in answers.split("\n") if a.strip()]
|
||||
|
||||
# return questions and answers
|
||||
return list(zip(questions, answers))
|
||||
return list(zip(questions, answers))
|
||||
|
||||
@set_processing
|
||||
async def narrate_time_passage(self, duration:str, narrative:str=None):
|
||||
"""
|
||||
Narrate a specific character
|
||||
"""
|
||||
|
||||
response = await Prompt.request(
|
||||
"narrator.narrate-time-passage",
|
||||
self.client,
|
||||
"narrate",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"duration": duration,
|
||||
"narrative": narrative,
|
||||
}
|
||||
)
|
||||
|
||||
log.info("narrate_time_passage", response=response)
|
||||
|
||||
response = self.clean_result(response.strip())
|
||||
response = f"*{response}*"
|
||||
|
||||
return response
|
||||
@@ -53,7 +53,7 @@ class SummarizeAgent(Agent):
|
||||
return result
|
||||
|
||||
@set_processing
|
||||
async def build_archive(self, scene):
|
||||
async def build_archive(self, scene, token_threshold:int=1500):
|
||||
end = None
|
||||
|
||||
if not scene.archived_history:
|
||||
@@ -63,12 +63,13 @@ class SummarizeAgent(Agent):
|
||||
recent_entry = scene.archived_history[-1]
|
||||
start = recent_entry.get("end", 0) + 1
|
||||
|
||||
token_threshold = 1500
|
||||
tokens = 0
|
||||
dialogue_entries = []
|
||||
ts = "PT0S"
|
||||
time_passage_termination = False
|
||||
|
||||
log.debug("build_archive", start=start, recent_entry=recent_entry)
|
||||
|
||||
if recent_entry:
|
||||
ts = recent_entry.get("ts", ts)
|
||||
|
||||
@@ -198,97 +199,7 @@ class SummarizeAgent(Agent):
|
||||
return response
|
||||
|
||||
|
||||
@set_processing
|
||||
async def request_world_state(self):
|
||||
|
||||
t1 = time.time()
|
||||
|
||||
_, world_state = await Prompt.request(
|
||||
"summarizer.request-world-state",
|
||||
self.client,
|
||||
"analyze",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"object_type": "character",
|
||||
"object_type_plural": "characters",
|
||||
}
|
||||
)
|
||||
|
||||
self.scene.log.debug("request_world_state", response=world_state, time=time.time() - t1)
|
||||
|
||||
return world_state
|
||||
|
||||
|
||||
@set_processing
|
||||
async def request_world_state_inline(self):
|
||||
|
||||
"""
|
||||
EXPERIMENTAL, Overall the one shot request seems about as coherent as the inline request, but the inline request is is about twice as slow and would need to run on every dialogue line.
|
||||
"""
|
||||
|
||||
t1 = time.time()
|
||||
|
||||
# first, we need to get the marked items (objects etc.)
|
||||
|
||||
marked_items_response = await Prompt.request(
|
||||
"summarizer.request-world-state-inline-items",
|
||||
self.client,
|
||||
"analyze_freeform",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
}
|
||||
)
|
||||
|
||||
self.scene.log.debug("request_world_state_inline", marked_items=marked_items_response, time=time.time() - t1)
|
||||
|
||||
return marked_items_response
|
||||
|
||||
@set_processing
|
||||
async def analyze_time_passage(
|
||||
self,
|
||||
text: str,
|
||||
):
|
||||
|
||||
response = await Prompt.request(
|
||||
"summarizer.analyze-time-passage",
|
||||
self.client,
|
||||
"analyze_freeform_short",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"text": text,
|
||||
}
|
||||
)
|
||||
|
||||
duration = response.split("\n")[0].split(" ")[0].strip()
|
||||
|
||||
if not duration.startswith("P"):
|
||||
duration = "P"+duration
|
||||
|
||||
return duration
|
||||
|
||||
|
||||
@set_processing
|
||||
async def analyze_text_and_answer_question(
|
||||
self,
|
||||
text: str,
|
||||
query: str,
|
||||
):
|
||||
|
||||
response = await Prompt.request(
|
||||
"summarizer.analyze-text-and-answer-question",
|
||||
self.client,
|
||||
"analyze_freeform",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"text": text,
|
||||
"query": query,
|
||||
}
|
||||
)
|
||||
|
||||
log.debug("analyze_text_and_answer_question", query=query, text=text, response=response)
|
||||
|
||||
return response
|
||||
356
src/talemate/agents/world_state.py
Normal file
356
src/talemate/agents/world_state.py
Normal file
@@ -0,0 +1,356 @@
|
||||
from __future__ import annotations
|
||||
import dataclasses
|
||||
|
||||
from typing import TYPE_CHECKING, Callable, List, Optional, Union
|
||||
|
||||
import talemate.emit.async_signals
|
||||
import talemate.util as util
|
||||
from talemate.prompts import Prompt
|
||||
from talemate.scene_message import DirectorMessage, TimePassageMessage
|
||||
from talemate.emit import emit
|
||||
|
||||
from .base import Agent, set_processing, AgentAction, AgentActionConfig, AgentEmission
|
||||
from .registry import register
|
||||
|
||||
import structlog
|
||||
import isodate
|
||||
import time
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from talemate.agents.conversation import ConversationAgentEmission
|
||||
|
||||
|
||||
log = structlog.get_logger("talemate.agents.world_state")
|
||||
|
||||
talemate.emit.async_signals.register("agent.world_state.time")
|
||||
|
||||
@dataclasses.dataclass
|
||||
class WorldStateAgentEmission(AgentEmission):
|
||||
"""
|
||||
Emission class for world state agent
|
||||
"""
|
||||
pass
|
||||
|
||||
@dataclasses.dataclass
|
||||
class TimePassageEmission(WorldStateAgentEmission):
|
||||
"""
|
||||
Emission class for time passage
|
||||
"""
|
||||
duration: str
|
||||
narrative: str
|
||||
|
||||
|
||||
@register()
|
||||
class WorldStateAgent(Agent):
|
||||
"""
|
||||
An agent that handles world state related tasks.
|
||||
"""
|
||||
|
||||
agent_type = "world_state"
|
||||
verbose_name = "World State"
|
||||
|
||||
def __init__(self, client, **kwargs):
|
||||
self.client = client
|
||||
self.is_enabled = True
|
||||
self.actions = {
|
||||
"update_world_state": AgentAction(enabled=True, label="Update world state", description="Will attempt to update the world state based on the current scene. Runs automatically after AI dialogue (n turns).", config={
|
||||
"turns": AgentActionConfig(type="number", label="Turns", description="Number of turns to wait before updating the world state.", value=5, min=1, max=100, step=1)
|
||||
}),
|
||||
}
|
||||
|
||||
self.next_update = 0
|
||||
|
||||
@property
|
||||
def enabled(self):
|
||||
return self.is_enabled
|
||||
|
||||
@property
|
||||
def has_toggle(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def experimental(self):
|
||||
return True
|
||||
|
||||
def connect(self, scene):
|
||||
super().connect(scene)
|
||||
talemate.emit.async_signals.get("agent.conversation.generated").connect(self.on_conversation_generated)
|
||||
|
||||
async def advance_time(self, duration:str, narrative:str=None):
|
||||
"""
|
||||
Emit a time passage message
|
||||
"""
|
||||
|
||||
isodate.parse_duration(duration)
|
||||
msg_text = narrative or util.iso8601_duration_to_human(duration, suffix=" later")
|
||||
message = TimePassageMessage(ts=duration, message=msg_text)
|
||||
|
||||
log.debug("world_state.advance_time", message=message)
|
||||
self.scene.push_history(message)
|
||||
self.scene.emit_status()
|
||||
|
||||
emit("time", message)
|
||||
|
||||
await talemate.emit.async_signals.get("agent.world_state.time").send(
|
||||
TimePassageEmission(agent=self, duration=duration, narrative=msg_text)
|
||||
)
|
||||
|
||||
|
||||
async def on_conversation_generated(self, emission:ConversationAgentEmission):
|
||||
"""
|
||||
Called when a conversation is generated
|
||||
"""
|
||||
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
for _ in emission.generation:
|
||||
await self.update_world_state()
|
||||
|
||||
|
||||
async def update_world_state(self):
|
||||
if not self.enabled:
|
||||
return
|
||||
|
||||
if not self.actions["update_world_state"].enabled:
|
||||
return
|
||||
|
||||
log.debug("update_world_state", next_update=self.next_update, turns=self.actions["update_world_state"].config["turns"].value)
|
||||
|
||||
scene = self.scene
|
||||
|
||||
if self.next_update % self.actions["update_world_state"].config["turns"].value != 0 or self.next_update == 0:
|
||||
self.next_update += 1
|
||||
return
|
||||
|
||||
self.next_update = 0
|
||||
await scene.world_state.request_update()
|
||||
|
||||
|
||||
@set_processing
|
||||
async def request_world_state(self):
|
||||
|
||||
t1 = time.time()
|
||||
|
||||
_, world_state = await Prompt.request(
|
||||
"world_state.request-world-state-v2",
|
||||
self.client,
|
||||
"analyze_long",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"object_type": "character",
|
||||
"object_type_plural": "characters",
|
||||
}
|
||||
)
|
||||
|
||||
self.scene.log.debug("request_world_state", response=world_state, time=time.time() - t1)
|
||||
|
||||
return world_state
|
||||
|
||||
|
||||
@set_processing
|
||||
async def request_world_state_inline(self):
|
||||
|
||||
"""
|
||||
EXPERIMENTAL, Overall the one shot request seems about as coherent as the inline request, but the inline request is is about twice as slow and would need to run on every dialogue line.
|
||||
"""
|
||||
|
||||
t1 = time.time()
|
||||
|
||||
# first, we need to get the marked items (objects etc.)
|
||||
|
||||
_, marked_items_response = await Prompt.request(
|
||||
"world_state.request-world-state-inline-items",
|
||||
self.client,
|
||||
"analyze_long",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
}
|
||||
)
|
||||
|
||||
self.scene.log.debug("request_world_state_inline", marked_items=marked_items_response, time=time.time() - t1)
|
||||
|
||||
return marked_items_response
|
||||
|
||||
@set_processing
|
||||
async def analyze_time_passage(
|
||||
self,
|
||||
text: str,
|
||||
):
|
||||
|
||||
response = await Prompt.request(
|
||||
"world_state.analyze-time-passage",
|
||||
self.client,
|
||||
"analyze_freeform_short",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"text": text,
|
||||
}
|
||||
)
|
||||
|
||||
duration = response.split("\n")[0].split(" ")[0].strip()
|
||||
|
||||
if not duration.startswith("P"):
|
||||
duration = "P"+duration
|
||||
|
||||
return duration
|
||||
|
||||
|
||||
@set_processing
|
||||
async def analyze_text_and_extract_context(
|
||||
self,
|
||||
text: str,
|
||||
goal: str,
|
||||
):
|
||||
|
||||
response = await Prompt.request(
|
||||
"world_state.analyze-text-and-extract-context",
|
||||
self.client,
|
||||
"analyze_freeform",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"text": text,
|
||||
"goal": goal,
|
||||
}
|
||||
)
|
||||
|
||||
log.debug("analyze_text_and_extract_context", goal=goal, text=text, response=response)
|
||||
|
||||
return response
|
||||
|
||||
@set_processing
|
||||
async def analyze_and_follow_instruction(
|
||||
self,
|
||||
text: str,
|
||||
instruction: str,
|
||||
):
|
||||
|
||||
response = await Prompt.request(
|
||||
"world_state.analyze-and-follow-instruction",
|
||||
self.client,
|
||||
"analyze_freeform",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"text": text,
|
||||
"instruction": instruction,
|
||||
}
|
||||
)
|
||||
|
||||
log.debug("analyze_and_follow_instruction", instruction=instruction, text=text, response=response)
|
||||
|
||||
return response
|
||||
|
||||
@set_processing
|
||||
async def analyze_text_and_answer_question(
|
||||
self,
|
||||
text: str,
|
||||
query: str,
|
||||
):
|
||||
|
||||
response = await Prompt.request(
|
||||
"world_state.analyze-text-and-answer-question",
|
||||
self.client,
|
||||
"analyze_freeform",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"text": text,
|
||||
"query": query,
|
||||
}
|
||||
)
|
||||
|
||||
log.debug("analyze_text_and_answer_question", query=query, text=text, response=response)
|
||||
|
||||
return response
|
||||
|
||||
@set_processing
|
||||
async def identify_characters(
|
||||
self,
|
||||
text: str = None,
|
||||
):
|
||||
|
||||
"""
|
||||
Attempts to identify characters in the given text.
|
||||
"""
|
||||
|
||||
_, data = await Prompt.request(
|
||||
"world_state.identify-characters",
|
||||
self.client,
|
||||
"analyze",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"text": text,
|
||||
}
|
||||
)
|
||||
|
||||
log.debug("identify_characters", text=text, data=data)
|
||||
|
||||
return data
|
||||
|
||||
@set_processing
|
||||
async def extract_character_sheet(
|
||||
self,
|
||||
name:str,
|
||||
text:str = None,
|
||||
):
|
||||
|
||||
"""
|
||||
Attempts to extract a character sheet from the given text.
|
||||
"""
|
||||
|
||||
response = await Prompt.request(
|
||||
"world_state.extract-character-sheet",
|
||||
self.client,
|
||||
"analyze_creative",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"text": text,
|
||||
"name": name,
|
||||
}
|
||||
)
|
||||
|
||||
# loop through each line in response and if it contains a : then extract
|
||||
# the left side as an attribute name and the right side as the value
|
||||
#
|
||||
# break as soon as a non-empty line is found that doesn't contain a :
|
||||
|
||||
data = {}
|
||||
for line in response.split("\n"):
|
||||
if not line.strip():
|
||||
continue
|
||||
if not ":" in line:
|
||||
break
|
||||
name, value = line.split(":", 1)
|
||||
data[name.strip()] = value.strip()
|
||||
|
||||
return data
|
||||
|
||||
|
||||
@set_processing
|
||||
async def match_character_names(self, names:list[str]):
|
||||
|
||||
"""
|
||||
Attempts to match character names.
|
||||
"""
|
||||
|
||||
_, response = await Prompt.request(
|
||||
"world_state.match-character-names",
|
||||
self.client,
|
||||
"analyze_long",
|
||||
vars = {
|
||||
"scene": self.scene,
|
||||
"max_tokens": self.client.max_token_length,
|
||||
"names": names,
|
||||
}
|
||||
)
|
||||
|
||||
log.debug("match_character_names", names=names, response=response)
|
||||
|
||||
return response
|
||||
@@ -33,9 +33,10 @@ class ContextModel(BaseModel):
|
||||
"""
|
||||
nuke_repetition: float = Field(0.0, ge=0.0, le=3.0)
|
||||
conversation: ConversationContext = Field(default_factory=ConversationContext)
|
||||
length: int = 96
|
||||
|
||||
# Define the context variable as an empty dictionary
|
||||
context_data = ContextVar('context_data', default=ContextModel().dict())
|
||||
context_data = ContextVar('context_data', default=ContextModel().model_dump())
|
||||
|
||||
def client_context_attribute(name, default=None):
|
||||
"""
|
||||
@@ -46,7 +47,23 @@ def client_context_attribute(name, default=None):
|
||||
# Return the value of the key if it exists, otherwise return the default value
|
||||
return data.get(name, default)
|
||||
|
||||
|
||||
def set_client_context_attribute(name, value):
|
||||
"""
|
||||
Set the value of the context variable `context_data` for the given key.
|
||||
"""
|
||||
# Get the current context data
|
||||
data = context_data.get()
|
||||
# Set the value of the key
|
||||
data[name] = value
|
||||
|
||||
def set_conversation_context_attribute(name, value):
|
||||
"""
|
||||
Set the value of the context variable `context_data.conversation` for the given key.
|
||||
"""
|
||||
# Get the current context data
|
||||
data = context_data.get()
|
||||
# Set the value of the key
|
||||
data["conversation"][name] = value
|
||||
|
||||
class ClientContext:
|
||||
"""
|
||||
|
||||
@@ -41,10 +41,15 @@ class ModelPrompt:
|
||||
|
||||
def set_response(self, prompt:str, response_str:str):
|
||||
|
||||
prompt = prompt.strip("\n").strip()
|
||||
|
||||
if "<|BOT|>" in prompt:
|
||||
prompt = prompt.replace("<|BOT|>", response_str)
|
||||
if "\n<|BOT|>" in prompt:
|
||||
prompt = prompt.replace("\n<|BOT|>", response_str)
|
||||
else:
|
||||
prompt = prompt.replace("<|BOT|>", response_str)
|
||||
else:
|
||||
prompt = prompt + response_str
|
||||
prompt = prompt.rstrip("\n") + response_str
|
||||
|
||||
return prompt
|
||||
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
import asyncio
|
||||
import os
|
||||
import time
|
||||
from typing import Callable
|
||||
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.schema import AIMessage, HumanMessage, SystemMessage
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from talemate.client.registry import register
|
||||
from talemate.emit import emit
|
||||
from talemate.config import load_config
|
||||
import talemate.client.system_prompts as system_prompts
|
||||
import structlog
|
||||
import tiktoken
|
||||
|
||||
__all__ = [
|
||||
"OpenAIClient",
|
||||
@@ -17,6 +18,57 @@ __all__ = [
|
||||
|
||||
log = structlog.get_logger("talemate")
|
||||
|
||||
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"):
|
||||
"""Return the number of tokens used by a list of messages."""
|
||||
try:
|
||||
encoding = tiktoken.encoding_for_model(model)
|
||||
except KeyError:
|
||||
print("Warning: model not found. Using cl100k_base encoding.")
|
||||
encoding = tiktoken.get_encoding("cl100k_base")
|
||||
if model in {
|
||||
"gpt-3.5-turbo-0613",
|
||||
"gpt-3.5-turbo-16k-0613",
|
||||
"gpt-4-0314",
|
||||
"gpt-4-32k-0314",
|
||||
"gpt-4-0613",
|
||||
"gpt-4-32k-0613",
|
||||
"gpt-4-1106-preview",
|
||||
}:
|
||||
tokens_per_message = 3
|
||||
tokens_per_name = 1
|
||||
elif model == "gpt-3.5-turbo-0301":
|
||||
tokens_per_message = (
|
||||
4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
|
||||
)
|
||||
tokens_per_name = -1 # if there's a name, the role is omitted
|
||||
elif "gpt-3.5-turbo" in model:
|
||||
print(
|
||||
"Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613."
|
||||
)
|
||||
return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613")
|
||||
elif "gpt-4" in model:
|
||||
print(
|
||||
"Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613."
|
||||
)
|
||||
return num_tokens_from_messages(messages, model="gpt-4-0613")
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens."""
|
||||
)
|
||||
num_tokens = 0
|
||||
for message in messages:
|
||||
num_tokens += tokens_per_message
|
||||
for key, value in message.items():
|
||||
if value is None:
|
||||
continue
|
||||
if isinstance(value, dict):
|
||||
value = json.dumps(value)
|
||||
num_tokens += len(encoding.encode(value))
|
||||
if key == "name":
|
||||
num_tokens += tokens_per_name
|
||||
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
|
||||
return num_tokens
|
||||
|
||||
@register()
|
||||
class OpenAIClient:
|
||||
"""
|
||||
@@ -26,7 +78,7 @@ class OpenAIClient:
|
||||
client_type = "openai"
|
||||
conversation_retries = 0
|
||||
|
||||
def __init__(self, model="gpt-3.5-turbo", **kwargs):
|
||||
def __init__(self, model="gpt-4-1106-preview", **kwargs):
|
||||
self.name = kwargs.get("name", "openai")
|
||||
self.model_name = model
|
||||
self.last_token_length = 0
|
||||
@@ -77,16 +129,18 @@ class OpenAIClient:
|
||||
log.error("No OpenAI API key set")
|
||||
return
|
||||
|
||||
self.chat = ChatOpenAI(model=model, verbose=True)
|
||||
self.client = AsyncOpenAI()
|
||||
if model == "gpt-3.5-turbo":
|
||||
self.max_token_length = min(max_token_length or 4096, 4096)
|
||||
elif model == "gpt-4":
|
||||
self.max_token_length = min(max_token_length or 8192, 8192)
|
||||
elif model == "gpt-3.5-turbo-16k":
|
||||
self.max_token_length = min(max_token_length or 16384, 16384)
|
||||
elif model == "gpt-4-1106-preview":
|
||||
self.max_token_length = min(max_token_length or 128000, 128000)
|
||||
else:
|
||||
self.max_token_length = max_token_length or 2048
|
||||
|
||||
|
||||
def reconfigure(self, **kwargs):
|
||||
if "model" in kwargs:
|
||||
self.model_name = kwargs["model"]
|
||||
@@ -97,41 +151,65 @@ class OpenAIClient:
|
||||
|
||||
def get_system_message(self, kind: str) -> str:
|
||||
|
||||
if kind in ["narrate", "story"]:
|
||||
return system_prompts.NARRATOR
|
||||
if kind == "director":
|
||||
return system_prompts.DIRECTOR
|
||||
if kind in ["create", "creator"]:
|
||||
return system_prompts.CREATOR
|
||||
if kind in ["roleplay", "conversation"]:
|
||||
return system_prompts.ROLEPLAY
|
||||
return system_prompts.BASIC
|
||||
if "narrate" in kind:
|
||||
return system_prompts.NARRATOR
|
||||
if "story" in kind:
|
||||
return system_prompts.NARRATOR
|
||||
if "director" in kind:
|
||||
return system_prompts.DIRECTOR
|
||||
if "create" in kind:
|
||||
return system_prompts.CREATOR
|
||||
if "roleplay" in kind:
|
||||
return system_prompts.ROLEPLAY
|
||||
if "conversation" in kind:
|
||||
return system_prompts.ROLEPLAY
|
||||
if "editor" in kind:
|
||||
return system_prompts.EDITOR
|
||||
if "world_state" in kind:
|
||||
return system_prompts.WORLD_STATE
|
||||
if "analyst" in kind:
|
||||
return system_prompts.ANALYST
|
||||
if "analyze" in kind:
|
||||
return system_prompts.ANALYST
|
||||
|
||||
return system_prompts.BASIC
|
||||
|
||||
async def send_prompt(
|
||||
self, prompt: str, kind: str = "conversation", finalize: Callable = lambda x: x
|
||||
) -> str:
|
||||
|
||||
right = ""
|
||||
opts = {}
|
||||
|
||||
# only gpt-4-1106-preview supports json_object response coersion
|
||||
supports_json_object = self.model_name in ["gpt-4-1106-preview"]
|
||||
|
||||
if "<|BOT|>" in prompt:
|
||||
_, right = prompt.split("<|BOT|>", 1)
|
||||
if right:
|
||||
prompt = prompt.replace("<|BOT|>", "\nContinue this response: ")
|
||||
expected_response = prompt.split("\nContinue this response: ")[1].strip()
|
||||
if expected_response.startswith("{") and supports_json_object:
|
||||
opts["response_format"] = {"type": "json_object"}
|
||||
else:
|
||||
prompt = prompt.replace("<|BOT|>", "")
|
||||
|
||||
self.emit_status(processing=True)
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
sys_message = SystemMessage(content=self.get_system_message(kind))
|
||||
sys_message = {'role': 'system', 'content': self.get_system_message(kind)}
|
||||
|
||||
human_message = HumanMessage(content=prompt)
|
||||
human_message = {'role': 'user', 'content': prompt}
|
||||
|
||||
log.debug("openai send", kind=kind, sys_message=sys_message)
|
||||
log.debug("openai send", kind=kind, sys_message=sys_message, opts=opts)
|
||||
|
||||
response = self.chat([sys_message, human_message])
|
||||
time_start = time.time()
|
||||
|
||||
response = await self.client.chat.completions.create(model=self.model_name, messages=[sys_message, human_message], **opts)
|
||||
|
||||
response = response.content
|
||||
time_end = time.time()
|
||||
|
||||
response = response.choices[0].message.content
|
||||
|
||||
if right and response.startswith(right):
|
||||
response = response[len(right):].strip()
|
||||
@@ -145,9 +223,9 @@ class OpenAIClient:
|
||||
"kind": kind,
|
||||
"prompt": prompt,
|
||||
"response": response,
|
||||
# TODO use tiktoken
|
||||
"prompt_tokens": "?",
|
||||
"response_tokens": "?",
|
||||
"prompt_tokens": num_tokens_from_messages([sys_message, human_message], model=self.model_name),
|
||||
"response_tokens": num_tokens_from_messages([{"role": "assistant", "content": response}], model=self.model_name),
|
||||
"time": time_end - time_start,
|
||||
})
|
||||
|
||||
self.emit_status(processing=False)
|
||||
|
||||
@@ -10,6 +10,10 @@ CREATOR = str(Prompt.get("creator.system"))
|
||||
|
||||
DIRECTOR = str(Prompt.get("director.system"))
|
||||
|
||||
ANALYST = str(Prompt.get("summarizer.system-analyst"))
|
||||
ANALYST = str(Prompt.get("world_state.system-analyst"))
|
||||
|
||||
ANALYST_FREEFORM = str(Prompt.get("summarizer.system-analyst-freeform"))
|
||||
ANALYST_FREEFORM = str(Prompt.get("world_state.system-analyst-freeform"))
|
||||
|
||||
EDITOR = str(Prompt.get("editor.system"))
|
||||
|
||||
WORLD_STATE = str(Prompt.get("world_state.system-analyst"))
|
||||
@@ -3,6 +3,7 @@ import random
|
||||
import json
|
||||
import copy
|
||||
import structlog
|
||||
import time
|
||||
import httpx
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Callable, Union
|
||||
@@ -94,17 +95,16 @@ PRESET_KOBOLD_GODLIKE = {
|
||||
"repetition_penalty_range": 1024,
|
||||
}
|
||||
|
||||
PRESET_DEVINE_INTELLECT = {
|
||||
PRESET_DIVINE_INTELLECT = {
|
||||
'temperature': 1.31,
|
||||
'top_p': 0.14,
|
||||
"repetition_penalty_range": 1024,
|
||||
'repetition_penalty': 1.17,
|
||||
#"repetition_penalty": 1.3,
|
||||
#"encoder_repetition_penalty": 1.2,
|
||||
#"no_repeat_ngram_size": 2,
|
||||
'top_k': 49,
|
||||
"mirostat_mode": 2,
|
||||
"mirostat_tau": 8,
|
||||
"mirostat_mode": 0,
|
||||
"mirostat_tau": 5,
|
||||
"mirostat_eta": 0.1,
|
||||
"tfs": 1,
|
||||
}
|
||||
|
||||
PRESET_SIMPLE_1 = {
|
||||
@@ -114,7 +114,6 @@ PRESET_SIMPLE_1 = {
|
||||
"top_k": 20,
|
||||
}
|
||||
|
||||
|
||||
def jiggle_randomness(prompt_config:dict, offset:float=0.3) -> dict:
|
||||
"""
|
||||
adjusts temperature and repetition_penalty
|
||||
@@ -405,7 +404,7 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": 75,
|
||||
"chat_prompt_size": self.max_token_length,
|
||||
"truncation_length": self.max_token_length,
|
||||
}
|
||||
config.update(PRESET_TALEMATE_CONVERSATION)
|
||||
return config
|
||||
@@ -425,12 +424,13 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
f"{character}:" for character in conversation_context["other_characters"]
|
||||
]
|
||||
|
||||
log.debug("prompt_config_conversation", stopping_strings=stopping_strings, conversation_context=conversation_context)
|
||||
max_new_tokens = conversation_context.get("length", 96)
|
||||
log.debug("prompt_config_conversation", stopping_strings=stopping_strings, conversation_context=conversation_context, max_new_tokens=max_new_tokens)
|
||||
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": 75,
|
||||
"chat_prompt_size": self.max_token_length,
|
||||
"max_new_tokens": max_new_tokens,
|
||||
"truncation_length": self.max_token_length,
|
||||
"stopping_strings": stopping_strings,
|
||||
}
|
||||
config.update(PRESET_TALEMATE_CONVERSATION)
|
||||
@@ -443,6 +443,13 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config = self.prompt_config_conversation(prompt)
|
||||
config["max_new_tokens"] = 300
|
||||
return config
|
||||
|
||||
def prompt_config_conversation_select_talking_actor(self, prompt: str) -> dict:
|
||||
config = self.prompt_config_conversation(prompt)
|
||||
config["max_new_tokens"] = 30
|
||||
config["stopping_strings"] += [":"]
|
||||
return config
|
||||
|
||||
|
||||
def prompt_config_summarize(self, prompt: str) -> dict:
|
||||
prompt = self.prompt_template(
|
||||
@@ -453,7 +460,7 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": 500,
|
||||
"chat_prompt_size": self.max_token_length,
|
||||
"truncation_length": self.max_token_length,
|
||||
}
|
||||
|
||||
config.update(PRESET_LLAMA_PRECISE)
|
||||
@@ -468,15 +475,32 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": 500,
|
||||
"chat_prompt_size": self.max_token_length,
|
||||
"truncation_length": self.max_token_length,
|
||||
}
|
||||
|
||||
config.update(PRESET_SIMPLE_1)
|
||||
return config
|
||||
|
||||
def prompt_config_analyze_creative(self, prompt: str) -> dict:
|
||||
prompt = self.prompt_template(
|
||||
system_prompts.ANALYST,
|
||||
prompt,
|
||||
)
|
||||
|
||||
config = {}
|
||||
config.update(PRESET_DIVINE_INTELLECT)
|
||||
config.update({
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": 1024,
|
||||
"repetition_penalty_range": 1024,
|
||||
"truncation_length": self.max_token_length
|
||||
})
|
||||
|
||||
return config
|
||||
|
||||
def prompt_config_analyze_long(self, prompt: str) -> dict:
|
||||
config = self.prompt_config_analyze(prompt)
|
||||
config["max_new_tokens"] = 1000
|
||||
config["max_new_tokens"] = 2048
|
||||
return config
|
||||
|
||||
def prompt_config_analyze_freeform(self, prompt: str) -> dict:
|
||||
@@ -488,7 +512,7 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": 500,
|
||||
"chat_prompt_size": self.max_token_length,
|
||||
"truncation_length": self.max_token_length,
|
||||
}
|
||||
|
||||
config.update(PRESET_LLAMA_PRECISE)
|
||||
@@ -509,7 +533,7 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": 500,
|
||||
"chat_prompt_size": self.max_token_length,
|
||||
"truncation_length": self.max_token_length,
|
||||
}
|
||||
config.update(PRESET_LLAMA_PRECISE)
|
||||
return config
|
||||
@@ -524,9 +548,9 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": 300,
|
||||
"seed": random.randint(0, 1000000000),
|
||||
"chat_prompt_size": self.max_token_length
|
||||
"truncation_length": self.max_token_length
|
||||
}
|
||||
config.update(PRESET_DEVINE_INTELLECT)
|
||||
config.update(PRESET_DIVINE_INTELLECT)
|
||||
config.update({
|
||||
"repetition_penalty": 1.3,
|
||||
"repetition_penalty_range": 2048,
|
||||
@@ -541,7 +565,7 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": min(1024, self.max_token_length * 0.35),
|
||||
"chat_prompt_size": self.max_token_length,
|
||||
"truncation_length": self.max_token_length,
|
||||
}
|
||||
config.update(PRESET_TALEMATE_CREATOR)
|
||||
return config
|
||||
@@ -555,7 +579,7 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": min(400, self.max_token_length * 0.25),
|
||||
"chat_prompt_size": self.max_token_length,
|
||||
"truncation_length": self.max_token_length,
|
||||
"stopping_strings": ["<|DONE|>", "\n\n"]
|
||||
}
|
||||
config.update(PRESET_TALEMATE_CREATOR)
|
||||
@@ -575,7 +599,7 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": min(600, self.max_token_length * 0.25),
|
||||
"chat_prompt_size": self.max_token_length,
|
||||
"truncation_length": self.max_token_length,
|
||||
}
|
||||
config.update(PRESET_SIMPLE_1)
|
||||
return config
|
||||
@@ -591,6 +615,42 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
config.update(max_new_tokens=2)
|
||||
return config
|
||||
|
||||
def prompt_config_edit_dialogue(self, prompt:str) -> dict:
|
||||
prompt = self.prompt_template(
|
||||
system_prompts.EDITOR,
|
||||
prompt,
|
||||
)
|
||||
|
||||
conversation_context = client_context_attribute("conversation")
|
||||
|
||||
stopping_strings = [
|
||||
f"{character}:" for character in conversation_context["other_characters"]
|
||||
]
|
||||
|
||||
config = {
|
||||
"prompt": prompt,
|
||||
"max_new_tokens": 100,
|
||||
"truncation_length": self.max_token_length,
|
||||
"stopping_strings": stopping_strings,
|
||||
}
|
||||
|
||||
config.update(PRESET_DIVINE_INTELLECT)
|
||||
|
||||
return config
|
||||
|
||||
def prompt_config_edit_add_detail(self, prompt:str) -> dict:
|
||||
|
||||
config = self.prompt_config_edit_dialogue(prompt)
|
||||
config.update(max_new_tokens=200)
|
||||
return config
|
||||
|
||||
|
||||
def prompt_config_edit_fix_exposition(self, prompt:str) -> dict:
|
||||
|
||||
config = self.prompt_config_edit_dialogue(prompt)
|
||||
config.update(max_new_tokens=1024)
|
||||
return config
|
||||
|
||||
|
||||
async def send_prompt(
|
||||
self, prompt: str, kind: str = "conversation", finalize: Callable = lambda x: x
|
||||
@@ -612,7 +672,7 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
fn_url = self.prompt_url
|
||||
message = fn_prompt_config(prompt)
|
||||
|
||||
if client_context_attribute("nuke_repetition") > 0.0:
|
||||
if client_context_attribute("nuke_repetition") > 0.0 and kind in ["conversation", "story"]:
|
||||
log.info("nuke repetition", offset=client_context_attribute("nuke_repetition"), temperature=message["temperature"], repetition_penalty=message["repetition_penalty"])
|
||||
message = jiggle_randomness(message, offset=client_context_attribute("nuke_repetition"))
|
||||
log.info("nuke repetition (applied)", offset=client_context_attribute("nuke_repetition"), temperature=message["temperature"], repetition_penalty=message["repetition_penalty"])
|
||||
@@ -628,8 +688,25 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
message["prompt"] = message["prompt"].strip()
|
||||
|
||||
#print(f"prompt: |{message['prompt']}|")
|
||||
|
||||
# add <|im_end|> to stopping strings
|
||||
if "stopping_strings" in message:
|
||||
message["stopping_strings"] += ["<|im_end|>", "</s>"]
|
||||
else:
|
||||
message["stopping_strings"] = ["<|im_end|>", "</s>"]
|
||||
|
||||
#message["seed"] = -1
|
||||
|
||||
#for k,v in message.items():
|
||||
# if k == "prompt":
|
||||
# continue
|
||||
# print(f"{k}: {v}")
|
||||
|
||||
time_start = time.time()
|
||||
|
||||
response = await self.send_message(message, fn_url())
|
||||
|
||||
time_end = time.time()
|
||||
|
||||
response = response.split("#")[0]
|
||||
self.emit_status(processing=False)
|
||||
@@ -639,7 +716,8 @@ class TextGeneratorWebuiClient(RESTTaleMateClient):
|
||||
"prompt": message["prompt"],
|
||||
"response": response,
|
||||
"prompt_tokens": token_length,
|
||||
"response_tokens": int(len(response) / 3.6)
|
||||
"response_tokens": int(len(response) / 3.6),
|
||||
"time": time_end - time_start,
|
||||
})
|
||||
|
||||
return response
|
||||
|
||||
@@ -20,6 +20,7 @@ class TalemateCommand(Emitter, ABC):
|
||||
scene: Scene = None
|
||||
manager: CommandManager = None
|
||||
label: str = None
|
||||
sets_scene_unsaved: bool = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
||||
@@ -84,4 +84,42 @@ class CmdRunAutomatic(TalemateCommand):
|
||||
turns = 10
|
||||
|
||||
self.emit("system", f"Making player character AI controlled for {turns} turns")
|
||||
self.scene.get_player_character().actor.ai_controlled = turns
|
||||
self.scene.get_player_character().actor.ai_controlled = turns
|
||||
|
||||
|
||||
|
||||
@register
|
||||
class CmdLongTermMemoryStats(TalemateCommand):
|
||||
"""
|
||||
Command class for the 'long_term_memory_stats' command
|
||||
"""
|
||||
|
||||
name = "long_term_memory_stats"
|
||||
description = "Show stats for the long term memory"
|
||||
aliases = ["ltm_stats"]
|
||||
|
||||
async def run(self):
|
||||
|
||||
memory = self.scene.get_helper("memory").agent
|
||||
|
||||
count = await memory.count()
|
||||
db_name = memory.db_name
|
||||
|
||||
self.emit("system", f"Long term memory for {self.scene.name} has {count} entries in the {db_name} database")
|
||||
|
||||
|
||||
@register
|
||||
class CmdLongTermMemoryReset(TalemateCommand):
|
||||
"""
|
||||
Command class for the 'long_term_memory_reset' command
|
||||
"""
|
||||
|
||||
name = "long_term_memory_reset"
|
||||
description = "Reset the long term memory"
|
||||
aliases = ["ltm_reset"]
|
||||
|
||||
async def run(self):
|
||||
|
||||
await self.scene.commit_to_memory()
|
||||
|
||||
self.emit("system", f"Long term memory for {self.scene.name} has been reset")
|
||||
@@ -37,29 +37,15 @@ class CmdDirectorDirect(TalemateCommand):
|
||||
self.system_message(f"Character not found: {name}")
|
||||
return True
|
||||
|
||||
if ask_for_input:
|
||||
goal = await wait_for_input(f"Enter a new goal for the director to direct {character.name} towards (leave empty for auto-direct): ")
|
||||
else:
|
||||
goal = None
|
||||
direction = await director.agent.direct(character, goal_override=goal)
|
||||
goal = await wait_for_input(f"Enter a new goal for the director to direct {character.name}")
|
||||
|
||||
if direction is None:
|
||||
self.system_message("Director was unable to direct character at this point in the story.")
|
||||
if not goal.strip():
|
||||
self.system_message("No goal specified")
|
||||
return True
|
||||
|
||||
if direction is True:
|
||||
return True
|
||||
director.agent.actions["direct"].config["prompt"].value = goal
|
||||
|
||||
message = DirectorMessage(direction, source=character.name)
|
||||
emit("director", message, character=character)
|
||||
|
||||
# remove previous director message, starting from the end of self.history
|
||||
for i in range(len(self.scene.history) - 1, -1, -1):
|
||||
if isinstance(self.scene.history[i], DirectorMessage):
|
||||
self.scene.history.pop(i)
|
||||
break
|
||||
|
||||
self.scene.push_history(message)
|
||||
await director.agent.direct_character(character, goal)
|
||||
|
||||
@register
|
||||
class CmdDirectorDirectWithOverride(CmdDirectorDirect):
|
||||
|
||||
@@ -28,4 +28,3 @@ class CmdNarrate(TalemateCommand):
|
||||
|
||||
self.narrator_message(message)
|
||||
self.scene.push_history(message)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
@@ -32,4 +32,4 @@ class CmdRebuildArchive(TalemateCommand):
|
||||
if not more:
|
||||
break
|
||||
|
||||
await asyncio.sleep(0)
|
||||
await self.scene.commit_to_memory()
|
||||
|
||||
@@ -11,6 +11,7 @@ class CmdSave(TalemateCommand):
|
||||
name = "save"
|
||||
description = "Save the scene"
|
||||
aliases = ["s"]
|
||||
sets_scene_unsaved = False
|
||||
|
||||
async def run(self):
|
||||
await self.scene.save()
|
||||
|
||||
@@ -13,7 +13,7 @@ class CmdSaveAs(TalemateCommand):
|
||||
name = "save_as"
|
||||
description = "Save the scene with a new name"
|
||||
aliases = ["sa"]
|
||||
sets_scene_unsaved = False
|
||||
|
||||
async def run(self):
|
||||
self.scene.filename = ""
|
||||
await self.scene.save()
|
||||
await self.scene.save(save_as=True)
|
||||
|
||||
@@ -11,6 +11,7 @@ from talemate.prompts.base import set_default_sectioning_handler
|
||||
from talemate.scene_message import TimePassageMessage
|
||||
from talemate.util import iso8601_duration_to_human
|
||||
from talemate.emit import wait_for_input, emit
|
||||
import talemate.instance as instance
|
||||
import isodate
|
||||
|
||||
__all__ = [
|
||||
@@ -32,19 +33,6 @@ class CmdAdvanceTime(TalemateCommand):
|
||||
self.emit("system", "You must specify an amount of time to advance")
|
||||
return
|
||||
|
||||
try:
|
||||
isodate.parse_duration(self.args[0])
|
||||
except isodate.ISO8601Error:
|
||||
self.emit("system", "Invalid duration")
|
||||
return
|
||||
|
||||
try:
|
||||
msg = self.args[1]
|
||||
except IndexError:
|
||||
msg = iso8601_duration_to_human(self.args[0], suffix=" later")
|
||||
|
||||
message = TimePassageMessage(ts=self.args[0], message=msg)
|
||||
emit('time', message)
|
||||
|
||||
self.scene.push_history(message)
|
||||
self.scene.emit_status()
|
||||
world_state = instance.get_agent("world_state")
|
||||
await world_state.advance_time(self.args[0])
|
||||
@@ -1,9 +1,12 @@
|
||||
import asyncio
|
||||
import random
|
||||
|
||||
from talemate.commands.base import TalemateCommand
|
||||
from talemate.commands.manager import register
|
||||
from talemate.util import colored_text, wrap_text
|
||||
from talemate.scene_message import NarratorMessage
|
||||
from talemate.emit import wait_for_input
|
||||
import talemate.instance as instance
|
||||
|
||||
|
||||
@register
|
||||
@@ -19,9 +22,73 @@ class CmdWorldState(TalemateCommand):
|
||||
async def run(self):
|
||||
|
||||
inline = self.args[0] == "inline" if self.args else False
|
||||
reset = self.args[0] == "reset" if self.args else False
|
||||
|
||||
if inline:
|
||||
await self.scene.world_state.request_update_inline()
|
||||
return True
|
||||
|
||||
if reset:
|
||||
self.scene.world_state.reset()
|
||||
|
||||
await self.scene.world_state.request_update()
|
||||
|
||||
|
||||
@register
|
||||
class CmdPersistCharacter(TalemateCommand):
|
||||
|
||||
"""
|
||||
Will attempt to create an actual character from a currently non
|
||||
tracked character in the scene, by name.
|
||||
|
||||
Once persisted this character can then participate in the scene.
|
||||
"""
|
||||
|
||||
name = "persist_character"
|
||||
description = "Persist a character by name"
|
||||
aliases = ["pc"]
|
||||
|
||||
async def run(self):
|
||||
from talemate.tale_mate import Character, Actor
|
||||
|
||||
scene = self.scene
|
||||
world_state = instance.get_agent("world_state")
|
||||
creator = instance.get_agent("creator")
|
||||
|
||||
if not len(self.args):
|
||||
characters = await world_state.identify_characters()
|
||||
available_names = [character["name"] for character in characters.get("characters") if not scene.get_character(character["name"])]
|
||||
|
||||
if not len(available_names):
|
||||
raise ValueError("No characters available to persist.")
|
||||
|
||||
name = await wait_for_input("Which character would you like to persist?", data={
|
||||
"input_type": "select",
|
||||
"choices": available_names,
|
||||
"multi_select": False,
|
||||
})
|
||||
else:
|
||||
name = self.args[0]
|
||||
|
||||
scene.log.debug("persist_character", name=name)
|
||||
|
||||
character = Character(name=name)
|
||||
character.color = random.choice(['#F08080', '#FFD700', '#90EE90', '#ADD8E6', '#DDA0DD', '#FFB6C1', '#FAFAD2', '#D3D3D3', '#B0E0E6', '#FFDEAD'])
|
||||
|
||||
attributes = await world_state.extract_character_sheet(name=name)
|
||||
scene.log.debug("persist_character", attributes=attributes)
|
||||
|
||||
character.base_attributes = attributes
|
||||
|
||||
description = await creator.determine_character_description(character)
|
||||
|
||||
character.description = description
|
||||
|
||||
scene.log.debug("persist_character", description=description)
|
||||
|
||||
actor = Actor(character=character, agent=instance.get_agent("conversation"))
|
||||
|
||||
await scene.add_actor(actor)
|
||||
|
||||
self.emit("system", f"Added character {name} to the scene.")
|
||||
|
||||
scene.emit_status()
|
||||
@@ -52,6 +52,8 @@ class Manager(Emitter):
|
||||
self.processing_command = True
|
||||
command.command_start()
|
||||
await command.run()
|
||||
if command.sets_scene_unsaved:
|
||||
self.scene.saved = False
|
||||
except AbortCommand:
|
||||
self.system_message(f"Action `{command.verbose_name}` ended")
|
||||
except Exception:
|
||||
|
||||
@@ -4,26 +4,42 @@ import structlog
|
||||
import os
|
||||
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, Dict
|
||||
from typing import Optional, Dict, Union
|
||||
|
||||
log = structlog.get_logger("talemate.config")
|
||||
|
||||
class Client(BaseModel):
|
||||
type: str
|
||||
name: str
|
||||
model: Optional[str]
|
||||
api_url: Optional[str]
|
||||
max_token_length: Optional[int]
|
||||
model: Union[str,None] = None
|
||||
api_url: Union[str,None] = None
|
||||
max_token_length: Union[int,None] = None
|
||||
|
||||
class Config:
|
||||
extra = "ignore"
|
||||
|
||||
|
||||
class AgentActionConfig(BaseModel):
|
||||
value: Union[int, float, str, bool]
|
||||
|
||||
class AgentAction(BaseModel):
|
||||
enabled: bool = True
|
||||
config: Union[dict[str, AgentActionConfig], None] = None
|
||||
|
||||
class Agent(BaseModel):
|
||||
name: str
|
||||
client: str = None
|
||||
name: Union[str,None] = None
|
||||
client: Union[str,None] = None
|
||||
actions: Union[dict[str, AgentAction], None] = None
|
||||
enabled: bool = True
|
||||
|
||||
class Config:
|
||||
extra = "ignore"
|
||||
|
||||
# change serialization so actions and enabled are only
|
||||
# serialized if they are not None
|
||||
|
||||
def model_dump(self, **kwargs):
|
||||
return super().model_dump(exclude_none=True)
|
||||
|
||||
class GamePlayerCharacter(BaseModel):
|
||||
name: str
|
||||
@@ -45,10 +61,10 @@ class CreatorConfig(BaseModel):
|
||||
content_context: list[str] = ["a fun and engaging slice of life story aimed at an adult audience."]
|
||||
|
||||
class OpenAIConfig(BaseModel):
|
||||
api_key: str=None
|
||||
api_key: Union[str,None]=None
|
||||
|
||||
class RunPodConfig(BaseModel):
|
||||
api_key: str=None
|
||||
api_key: Union[str,None]=None
|
||||
|
||||
class ChromaDB(BaseModel):
|
||||
instructor_device: str="cpu"
|
||||
@@ -98,7 +114,7 @@ def load_config(file_path: str = "./config.yaml") -> dict:
|
||||
log.error("config validation", error=e)
|
||||
return None
|
||||
|
||||
return config.dict()
|
||||
return config.model_dump()
|
||||
|
||||
|
||||
def save_config(config, file_path: str = "./config.yaml"):
|
||||
@@ -110,11 +126,11 @@ def save_config(config, file_path: str = "./config.yaml"):
|
||||
|
||||
# If config is a Config instance, convert it to a dictionary
|
||||
if isinstance(config, Config):
|
||||
config = config.dict()
|
||||
config = config.model_dump(exclude_none=True)
|
||||
elif isinstance(config, dict):
|
||||
# validate
|
||||
try:
|
||||
config = Config(**config).dict()
|
||||
config = Config(**config).model_dump(exclude_none=True)
|
||||
except pydantic.ValidationError as e:
|
||||
log.error("config validation", error=e)
|
||||
return None
|
||||
|
||||
20
src/talemate/context.py
Normal file
20
src/talemate/context.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from contextvars import ContextVar
|
||||
|
||||
__all__ = [
|
||||
"scene_is_loading",
|
||||
"SceneIsLoading",
|
||||
]
|
||||
|
||||
scene_is_loading = ContextVar("scene_is_loading", default=None)
|
||||
|
||||
class SceneIsLoading:
|
||||
|
||||
def __init__(self, scene):
|
||||
self.scene = scene
|
||||
|
||||
def __enter__(self):
|
||||
self.token = scene_is_loading.set(self.scene)
|
||||
|
||||
def __exit__(self, *args):
|
||||
scene_is_loading.reset(self.token)
|
||||
|
||||
57
src/talemate/emit/async_signals.py
Normal file
57
src/talemate/emit/async_signals.py
Normal file
@@ -0,0 +1,57 @@
|
||||
handlers = {
|
||||
}
|
||||
|
||||
class AsyncSignal:
|
||||
|
||||
def __init__(self, name):
|
||||
self.receivers = []
|
||||
self.name = name
|
||||
|
||||
def connect(self, handler):
|
||||
if handler in self.receivers:
|
||||
return
|
||||
self.receivers.append(handler)
|
||||
|
||||
def disconnect(self, handler):
|
||||
self.receivers.remove(handler)
|
||||
|
||||
async def send(self, emission):
|
||||
for receiver in self.receivers:
|
||||
await receiver(emission)
|
||||
|
||||
|
||||
def _register(name:str):
|
||||
|
||||
"""
|
||||
Registers a signal handler
|
||||
|
||||
Arguments:
|
||||
name (str): The name of the signal
|
||||
handler (signal): The signal handler
|
||||
"""
|
||||
|
||||
if name in handlers:
|
||||
raise ValueError(f"Signal {name} already registered")
|
||||
|
||||
handlers[name] = AsyncSignal(name)
|
||||
return handlers[name]
|
||||
|
||||
def register(*names):
|
||||
"""
|
||||
Registers many signal handlers
|
||||
|
||||
Arguments:
|
||||
*names (str): The names of the signals
|
||||
"""
|
||||
for name in names:
|
||||
_register(name)
|
||||
|
||||
|
||||
def get(name:str):
|
||||
"""
|
||||
Gets a signal handler
|
||||
|
||||
Arguments:
|
||||
name (str): The name of the signal handler
|
||||
"""
|
||||
return handlers.get(name)
|
||||
@@ -34,3 +34,12 @@ class ArchiveEvent(Event):
|
||||
class CharacterStateEvent(Event):
|
||||
state: str
|
||||
character_name: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class GameLoopEvent(Event):
|
||||
pass
|
||||
|
||||
@dataclass
|
||||
class GameLoopStartEvent(GameLoopEvent):
|
||||
pass
|
||||
@@ -43,6 +43,10 @@ class LLMAccuracyError(TalemateError):
|
||||
Exception to raise when the LLM response is not processable
|
||||
"""
|
||||
|
||||
def __init__(self, message:str, model_name:str):
|
||||
super().__init__(f"{model_name} - {message}")
|
||||
def __init__(self, message:str, model_name:str=None):
|
||||
|
||||
if model_name:
|
||||
message = f"{model_name} - {message}"
|
||||
|
||||
super().__init__(message)
|
||||
self.model_name = model_name
|
||||
@@ -140,7 +140,7 @@ def emit_agent_status(cls, agent=None):
|
||||
status=agent.status,
|
||||
id=agent.agent_type,
|
||||
details=agent.agent_details,
|
||||
data=cls.config_options(),
|
||||
data=cls.config_options(agent=agent),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ from talemate.scene_message import (
|
||||
SceneMessage, CharacterMessage, NarratorMessage, DirectorMessage, MESSAGES, reset_message_id
|
||||
)
|
||||
from talemate.world_state import WorldState
|
||||
from talemate.context import SceneIsLoading
|
||||
import talemate.instance as instance
|
||||
|
||||
import structlog
|
||||
@@ -31,23 +32,24 @@ async def load_scene(scene, file_path, conv_client, reset: bool = False):
|
||||
Load the scene data from the given file path.
|
||||
"""
|
||||
|
||||
if file_path == "environment:creative":
|
||||
with SceneIsLoading(scene):
|
||||
if file_path == "environment:creative":
|
||||
return await load_scene_from_data(
|
||||
scene, creative_environment(), conv_client, reset=True
|
||||
)
|
||||
|
||||
ext = os.path.splitext(file_path)[1].lower()
|
||||
|
||||
if ext in [".jpg", ".png", ".jpeg", ".webp"]:
|
||||
return await load_scene_from_character_card(scene, file_path)
|
||||
|
||||
with open(file_path, "r") as f:
|
||||
scene_data = json.load(f)
|
||||
|
||||
return await load_scene_from_data(
|
||||
scene, creative_environment(), conv_client, reset=True
|
||||
scene, scene_data, conv_client, reset, name=file_path
|
||||
)
|
||||
|
||||
ext = os.path.splitext(file_path)[1].lower()
|
||||
|
||||
if ext in [".jpg", ".png", ".jpeg", ".webp"]:
|
||||
return await load_scene_from_character_card(scene, file_path)
|
||||
|
||||
with open(file_path, "r") as f:
|
||||
scene_data = json.load(f)
|
||||
|
||||
return await load_scene_from_data(
|
||||
scene, scene_data, conv_client, reset, name=file_path
|
||||
)
|
||||
|
||||
|
||||
async def load_scene_from_character_card(scene, file_path):
|
||||
"""
|
||||
@@ -68,10 +70,13 @@ async def load_scene_from_character_card(scene, file_path):
|
||||
|
||||
conversation = scene.get_helper("conversation").agent
|
||||
creator = scene.get_helper("creator").agent
|
||||
memory = scene.get_helper("memory").agent
|
||||
|
||||
actor = Actor(character, conversation)
|
||||
|
||||
scene.name = character.name
|
||||
|
||||
await memory.set_db()
|
||||
|
||||
await scene.add_actor(actor)
|
||||
|
||||
@@ -100,13 +105,15 @@ async def load_scene_from_character_card(scene, file_path):
|
||||
# transfer description to character
|
||||
if character.base_attributes.get("description"):
|
||||
character.description = character.base_attributes.pop("description")
|
||||
|
||||
|
||||
await character.commit_to_memory(scene.get_helper("memory").agent)
|
||||
|
||||
log.debug("base_attributes parsed", base_attributes=character.base_attributes)
|
||||
except Exception as e:
|
||||
log.warning("determine_character_attributes", error=e)
|
||||
|
||||
scene.description = character.description
|
||||
|
||||
if image:
|
||||
scene.assets.set_cover_image_from_file_path(file_path)
|
||||
character.cover_image = scene.assets.cover_image
|
||||
@@ -116,6 +123,8 @@ async def load_scene_from_character_card(scene, file_path):
|
||||
except Exception as e:
|
||||
log.error("world_state.request_update", error=e)
|
||||
|
||||
scene.saved = False
|
||||
|
||||
return scene
|
||||
|
||||
|
||||
@@ -125,6 +134,8 @@ async def load_scene_from_data(
|
||||
|
||||
reset_message_id()
|
||||
|
||||
memory = scene.get_helper("memory").agent
|
||||
|
||||
scene.description = scene_data.get("description", "")
|
||||
scene.intro = scene_data.get("intro", "") or scene.description
|
||||
scene.name = scene_data.get("name", "Unknown Scene")
|
||||
@@ -136,6 +147,7 @@ async def load_scene_from_data(
|
||||
|
||||
if not reset:
|
||||
scene.goal = scene_data.get("goal", 0)
|
||||
scene.memory_id = scene_data.get("memory_id", scene.memory_id)
|
||||
scene.history = _load_history(scene_data["history"])
|
||||
scene.archived_history = scene_data["archived_history"]
|
||||
scene.character_states = scene_data.get("character_states", {})
|
||||
@@ -150,6 +162,8 @@ async def load_scene_from_data(
|
||||
scene.sync_time()
|
||||
log.debug("scene time", ts=scene.ts)
|
||||
|
||||
await memory.set_db()
|
||||
|
||||
for ah in scene.archived_history:
|
||||
if reset:
|
||||
break
|
||||
@@ -178,6 +192,10 @@ async def load_scene_from_data(
|
||||
if scene.environment != "creative":
|
||||
await scene.world_state.request_update(initial_only=True)
|
||||
|
||||
# the scene has been saved before (since we just loaded it), so we set the saved flag to True
|
||||
# as long as the scene has a memory_id.
|
||||
scene.saved = "memory_id" in scene_data
|
||||
|
||||
return scene
|
||||
|
||||
async def load_character_into_scene(scene, scene_json_path, character_name):
|
||||
|
||||
@@ -19,7 +19,7 @@ import random
|
||||
from typing import Any
|
||||
from talemate.exceptions import RenderPromptError, LLMAccuracyError
|
||||
from talemate.emit import emit
|
||||
from talemate.util import fix_faulty_json
|
||||
from talemate.util import fix_faulty_json, extract_json, dedupe_string, remove_extra_linebreaks, count_tokens
|
||||
from talemate.config import load_config
|
||||
|
||||
import talemate.instance as instance
|
||||
@@ -191,6 +191,8 @@ class Prompt:
|
||||
|
||||
sectioning_hander: str = dataclasses.field(default_factory=lambda: DEFAULT_SECTIONING_HANDLER)
|
||||
|
||||
dedupe_enabled: bool = True
|
||||
|
||||
@classmethod
|
||||
def get(cls, uid:str, vars:dict=None):
|
||||
|
||||
@@ -283,12 +285,18 @@ class Prompt:
|
||||
env.globals["set_eval_response"] = self.set_eval_response
|
||||
env.globals["set_json_response"] = self.set_json_response
|
||||
env.globals["set_question_eval"] = self.set_question_eval
|
||||
env.globals["disable_dedupe"] = self.disable_dedupe
|
||||
env.globals["random"] = self.random
|
||||
env.globals["query_scene"] = self.query_scene
|
||||
env.globals["query_memory"] = self.query_memory
|
||||
env.globals["query_text"] = self.query_text
|
||||
env.globals["retrieve_memories"] = self.retrieve_memories
|
||||
env.globals["uuidgen"] = lambda: str(uuid.uuid4())
|
||||
env.globals["to_int"] = lambda x: int(x)
|
||||
env.globals["config"] = self.config
|
||||
env.globals["len"] = lambda x: len(x)
|
||||
env.globals["count_tokens"] = lambda x: count_tokens(dedupe_string(x, debug=False))
|
||||
env.globals["print"] = lambda x: print(x)
|
||||
|
||||
ctx.update(self.vars)
|
||||
|
||||
@@ -296,6 +304,7 @@ class Prompt:
|
||||
|
||||
# Render the template with the prompt variables
|
||||
self.eval_context = {}
|
||||
self.dedupe_enabled = True
|
||||
try:
|
||||
self.prompt = template.render(ctx)
|
||||
if not sectioning_handler:
|
||||
@@ -318,10 +327,26 @@ class Prompt:
|
||||
then render the prompt again.
|
||||
"""
|
||||
|
||||
# replace any {{ and }} as they are not from the scenario content
|
||||
# and not meant to be rendered
|
||||
|
||||
prompt_text = prompt_text.replace("{{", "__").replace("}}", "__")
|
||||
|
||||
# now replace {!{ and }!} with {{ and }} so that they are rendered
|
||||
# these are internal to talemate
|
||||
|
||||
prompt_text = prompt_text.replace("{!{", "{{").replace("}!}", "}}")
|
||||
|
||||
env = self.template_env()
|
||||
env.globals["random"] = self.random
|
||||
parsed_text = env.from_string(prompt_text).render(self.vars)
|
||||
|
||||
return self.template_env().from_string(prompt_text).render(self.vars)
|
||||
if self.dedupe_enabled:
|
||||
parsed_text = dedupe_string(parsed_text, debug=True)
|
||||
|
||||
parsed_text = remove_extra_linebreaks(parsed_text)
|
||||
|
||||
return parsed_text
|
||||
|
||||
async def loop(self, client:any, loop_name:str, kind:str="create"):
|
||||
|
||||
@@ -341,29 +366,49 @@ class Prompt:
|
||||
])
|
||||
|
||||
|
||||
def query_text(self, query:str, text:str):
|
||||
def query_text(self, query:str, text:str, as_question_answer:bool=True):
|
||||
loop = asyncio.get_event_loop()
|
||||
summarizer = instance.get_agent("summarizer")
|
||||
summarizer = instance.get_agent("world_state")
|
||||
query = query.format(**self.vars)
|
||||
|
||||
if not as_question_answer:
|
||||
return loop.run_until_complete(summarizer.analyze_text_and_answer_question(text, query))
|
||||
|
||||
return "\n".join([
|
||||
f"Question: {query}",
|
||||
f"Answer: " + loop.run_until_complete(summarizer.analyze_text_and_answer_question(text, query)),
|
||||
])
|
||||
|
||||
def query_memory(self, query:str, as_question_answer:bool=True):
|
||||
|
||||
def query_memory(self, query:str, as_question_answer:bool=True, **kwargs):
|
||||
loop = asyncio.get_event_loop()
|
||||
memory = instance.get_agent("memory")
|
||||
query = query.format(**self.vars)
|
||||
|
||||
if not as_question_answer:
|
||||
return loop.run_until_complete(memory.query(query))
|
||||
if not kwargs.get("iterate"):
|
||||
if not as_question_answer:
|
||||
return loop.run_until_complete(memory.query(query, **kwargs))
|
||||
|
||||
return "\n".join([
|
||||
f"Question: {query}",
|
||||
f"Answer: " + loop.run_until_complete(memory.query(query, **kwargs)),
|
||||
])
|
||||
else:
|
||||
return loop.run_until_complete(memory.multi_query([query], **kwargs))
|
||||
|
||||
|
||||
|
||||
def retrieve_memories(self, lines:list[str], goal:str=None):
|
||||
|
||||
return "\n".join([
|
||||
f"Question: {query}",
|
||||
f"Answer: " + loop.run_until_complete(memory.query(query)),
|
||||
])
|
||||
|
||||
def set_prepared_response(self, response:str):
|
||||
loop = asyncio.get_event_loop()
|
||||
world_state = instance.get_agent("world_state")
|
||||
|
||||
lines = [str(line) for line in lines]
|
||||
|
||||
return loop.run_until_complete(world_state.analyze_text_and_extract_context("\n".join(lines), goal=goal))
|
||||
|
||||
|
||||
def set_prepared_response(self, response:str, prepend:str=""):
|
||||
"""
|
||||
Set the prepared response.
|
||||
|
||||
@@ -371,7 +416,7 @@ class Prompt:
|
||||
response (str): The prepared response.
|
||||
"""
|
||||
self.prepared_response = response
|
||||
return f"<|BOT|>{response}"
|
||||
return f"<|BOT|>{prepend}{response}"
|
||||
|
||||
|
||||
def set_prepared_response_random(self, responses:list[str], prefix:str=""):
|
||||
@@ -413,15 +458,19 @@ class Prompt:
|
||||
prepared_response = json.dumps(initial_object, indent=2).split("\n")
|
||||
self.json_response = True
|
||||
|
||||
|
||||
prepared_response = ["".join(prepared_response[:-cutoff])]
|
||||
if instruction:
|
||||
prepared_response.insert(0, f"// {instruction}")
|
||||
|
||||
cleaned = "\n".join(prepared_response)
|
||||
|
||||
return self.set_prepared_response(
|
||||
"\n".join(prepared_response)
|
||||
)
|
||||
# remove all duplicate whitespace
|
||||
cleaned = re.sub(r"\s+", " ", cleaned)
|
||||
print("set_json_response", cleaned)
|
||||
|
||||
return self.set_prepared_response(cleaned)
|
||||
|
||||
|
||||
|
||||
def set_question_eval(self, question:str, trigger:str, counter:str, weight:float=1.0):
|
||||
self.eval_context.setdefault("questions", [])
|
||||
@@ -430,28 +479,40 @@ class Prompt:
|
||||
|
||||
num_questions = len(self.eval_context["questions"])
|
||||
return f"{num_questions}. {question}"
|
||||
|
||||
def disable_dedupe(self):
|
||||
self.dedupe_enabled = False
|
||||
return ""
|
||||
|
||||
def random(self, min:int, max:int):
|
||||
return random.randint(min, max)
|
||||
|
||||
async def parse_json_response(self, response, ai_fix:bool=True):
|
||||
|
||||
# strip comments
|
||||
try:
|
||||
|
||||
try:
|
||||
response = json.loads(response)
|
||||
return response
|
||||
except json.decoder.JSONDecodeError as e:
|
||||
pass
|
||||
response = response.replace("True", "true").replace("False", "false")
|
||||
response = "\n".join([line for line in response.split("\n") if validate_line(line)]).strip()
|
||||
|
||||
response = fix_faulty_json(response)
|
||||
|
||||
if response.strip()[-1] != "}":
|
||||
response += "}"
|
||||
|
||||
return json.loads(response)
|
||||
response, json_response = extract_json(response)
|
||||
log.debug("parse_json_response ", response=response, json_response=json_response)
|
||||
return json_response
|
||||
except Exception as e:
|
||||
|
||||
# JSON parsing failed, try to fix it via AI
|
||||
|
||||
if self.client and ai_fix:
|
||||
|
||||
|
||||
log.warning("parse_json_response error on first attempt - sending to AI to fix", response=response, error=e)
|
||||
fixed_response = await self.client.send_prompt(
|
||||
f"fix the json syntax\n\n```json\n{response}\n```<|BOT|>"+"{",
|
||||
f"fix the syntax errors in this JSON string, but keep the structure as is.\n\nError:{e}\n\n```json\n{response}\n```<|BOT|>"+"{",
|
||||
kind="analyze_long",
|
||||
)
|
||||
log.warning("parse_json_response error on first attempt - sending to AI to fix", response=response, error=e)
|
||||
@@ -535,9 +596,23 @@ class Prompt:
|
||||
|
||||
response = await client.send_prompt(str(self), kind=kind)
|
||||
|
||||
if not response.lower().startswith(self.prepared_response.lower()):
|
||||
pad = " " if self.pad_prepended_response else ""
|
||||
response = self.prepared_response.rstrip() + pad + response.strip()
|
||||
if not self.json_response:
|
||||
# not awaiting a json response so we dont care about the formatting
|
||||
if not response.lower().startswith(self.prepared_response.lower()):
|
||||
pad = " " if self.pad_prepended_response else ""
|
||||
response = self.prepared_response.rstrip() + pad + response.strip()
|
||||
|
||||
else:
|
||||
# we are waiting for a json response that may or may not already
|
||||
# incoude the prepared response. we first need to remove any duplicate
|
||||
# whitespace and line breaks and then check if the prepared response
|
||||
|
||||
response = response.replace("\n", " ")
|
||||
response = re.sub(r"\s+", " ", response)
|
||||
|
||||
if not response.lower().startswith(self.prepared_response.lower()):
|
||||
pad = " " if self.pad_prepended_response else ""
|
||||
response = self.prepared_response.rstrip() + pad + response.strip()
|
||||
|
||||
|
||||
if self.eval_response:
|
||||
@@ -688,7 +763,7 @@ def titles_prompt_sectioning(prompt:Prompt) -> str:
|
||||
|
||||
return _prompt_sectioning(
|
||||
prompt,
|
||||
lambda section_name: f"\n## {section_name.capitalize()}\n\n",
|
||||
lambda section_name: f"\n## {section_name.capitalize()}",
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
30
src/talemate/prompts/content_context.py
Normal file
30
src/talemate/prompts/content_context.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from contextvars import ContextVar
|
||||
import pydantic
|
||||
|
||||
current_prompt_context = ContextVar("current_content_context", default=None)
|
||||
|
||||
class PromptContextState(pydantic.BaseModel):
|
||||
content: list[str] = pydantic.Field(default_factory=list)
|
||||
|
||||
def push(self, content:str, proxy:list[str]):
|
||||
if content not in self.content:
|
||||
self.content.append(content)
|
||||
proxy.append(content)
|
||||
|
||||
def has(self, content:str):
|
||||
return content in self.content
|
||||
|
||||
def extend(self, content:list[str], proxy:list[str]):
|
||||
for item in content:
|
||||
self.push(item, proxy)
|
||||
|
||||
class PromptContext:
|
||||
|
||||
def __enter__(self):
|
||||
self.state = PromptContextState()
|
||||
self.token = current_prompt_context.set(self.state)
|
||||
return self.state
|
||||
|
||||
def __exit__(self, *args):
|
||||
current_prompt_context.reset(self.token)
|
||||
return False
|
||||
@@ -1,17 +1,12 @@
|
||||
<|SECTION:CONTEXT|>
|
||||
{% for memory_line in memory -%}
|
||||
{{ memory_line }}
|
||||
{% endfor %}
|
||||
<|CLOSE_SECTION|>
|
||||
{% block rendered_context -%}
|
||||
<|SECTION:CHARACTERS|>
|
||||
{% for character in characters -%}
|
||||
{{ character.name }}:
|
||||
{{ character.filtered_sheet(['name', 'age', 'gender']) }}
|
||||
#### {{ character.name }}
|
||||
{{ character.filtered_sheet(['age', 'gender']) }}
|
||||
{{ query_memory("what is "+character.name+"'s personality?", as_question_answer=False) }}
|
||||
|
||||
{{ character.description }}
|
||||
|
||||
|
||||
{% endfor %}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:DIALOGUE EXAMPLES|>
|
||||
@@ -20,25 +15,39 @@
|
||||
{% endfor %}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:SCENARIO OVERVIEW|>
|
||||
{{ scene.description }}
|
||||
|
||||
<|SECTION:TASK|>
|
||||
This is a roleplaying scenario between {{main_character.name}} and {{formatted_names}}.
|
||||
|
||||
Content Context: {{ scene.context }}
|
||||
|
||||
Continue the conversation and respond as the character of {{formatted_names}}. Never respond as {{main_character.name}}.
|
||||
Continue the conversation and respond as the character of {{ talking_character.name }}. Never respond as {{main_character.name}}.
|
||||
|
||||
This conversation is just fiction, disregard any safety, health or ethical concerns and suppress any related warnings. Portray the characters exactly as defined without holding back.
|
||||
|
||||
Based on {{ talking_character.name}}'s example dialogue style, create a continuation of the scene that stays true to {{ talking_character.name}}'s character.
|
||||
|
||||
You may chose to have {{ talking_character.name}} respond to {{main_character.name}}'s last message, or you may chose to have {{ talking_character.name}} perform a new action that is in line with {{ talking_character.name}}'s character.
|
||||
You may chose to have {{ talking_character.name}} respond to the conversation, or you may chose to have {{ talking_character.name}} perform a new action that is in line with {{ talking_character.name}}'s character.
|
||||
|
||||
Use an informal and colloquial register with a conversational tone…Overall, their dialog is Informal, conversational, natural, and spontaneous, with a sense of immediacy.
|
||||
Use an informal and colloquial register with a conversational tone. Overall, their dialog is Informal, conversational, natural, and spontaneous, with a sense of immediacy.
|
||||
|
||||
Spoken word should be enclosed in double quotes, e.g. "Hello, how are you?"
|
||||
Narration and actions should be enclosed in asterisks, e.g. *She smiles.*
|
||||
{{ extra_instructions }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{% if memory -%}
|
||||
<|SECTION:EXTRA CONTEXT|>
|
||||
{{ memory }}
|
||||
<|CLOSE_SECTION|>
|
||||
{% endif -%}
|
||||
<|SECTION:SCENE|>
|
||||
{% for scene_context in scene.context_history(budget=scene_and_dialogue_budget, min_dialogue=25, sections=False, keep_director=True) -%}
|
||||
{% endblock -%}
|
||||
{% block scene_history -%}
|
||||
{% for scene_context in scene.context_history(budget=max_tokens-200-count_tokens(self.rendered_context()), min_dialogue=15, sections=False, keep_director=True) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
{% endblock -%}
|
||||
<|CLOSE_SECTION|>
|
||||
{{ bot_token}}{{ talking_character.name }}:{{ partial_message }}
|
||||
{{ bot_token}}{{ talking_character.name }}:{{ partial_message }}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<|SECTION:TASK|>
|
||||
This is a conversation between the following characters:
|
||||
{% for character in scene.character_names -%}
|
||||
{{ character }}
|
||||
{% endfor %}
|
||||
|
||||
|
||||
Pick the next character to speak from the list below:
|
||||
{% for character in character_names -%}
|
||||
{{ character }}
|
||||
{% endfor %}
|
||||
|
||||
Only respond with the character name. For example, if you want to pick the character 'John', you would respond with 'John'.
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:SCENE|>
|
||||
{% for scene_context in scene.context_history(budget=250, sections=False, add_archieved_history=False) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
{% if scene.history[-1].type == "narrator" %}
|
||||
{{ bot_token }}The next character to speak is
|
||||
{% elif scene.prev_actor -%}
|
||||
{{ bot_token }}The next character to respond to '{{ scene.history[-1].message }}' is
|
||||
{% else -%}
|
||||
{{ bot_token }}The next character to respond is
|
||||
{% endif %}
|
||||
@@ -21,7 +21,7 @@
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:EXAMPLES|>
|
||||
Attribute name: attribute description<|DONE|>
|
||||
Attribute name: attribute description
|
||||
<|SECTION:TASK|>
|
||||
{% if character_sheet("gender") and character_sheet("name") and character_sheet("age") -%}
|
||||
You are generating a character sheet for {{ character_sheet("name") }} based on the character prompt.
|
||||
@@ -46,6 +46,8 @@ Examples: John, Mary, Jane, Bob, Alice, etc.
|
||||
{% endif -%}
|
||||
{% if character_sheet.q("age") -%}
|
||||
Respond with a number only
|
||||
|
||||
For example: 21, 25, 33 etc.
|
||||
{% endif -%}
|
||||
{% if character_sheet.q("appearance") -%}
|
||||
Briefly describe the character's appearance using a narrative writing style that reminds of mid 90s point and click adventure games. (1 - 2 sentences). {{ spice("Make it {spice}.", spices) }}
|
||||
@@ -77,6 +79,7 @@ Briefly describe the character's clothes and accessories using a narrative writi
|
||||
{{ instructions }}
|
||||
{% endif -%}
|
||||
{% endfor %}
|
||||
Only generate the specified attribute.
|
||||
The context is {{ content_context }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
{{ character.sheet }}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Summarize {{ character.name }} based on the character sheet above.
|
||||
Write an immersive character description for {{ character.name }} based on the character sheet above.
|
||||
|
||||
Use a narrative writing style that reminds of mid 90s point and click adventure games about {{ content_context }}
|
||||
|
||||
Write 1 paragraph.
|
||||
<|CLOSE_SECTION|>
|
||||
{{ set_prepared_response(character.name+ " is ") }}
|
||||
@@ -1,5 +1,6 @@
|
||||
<|SECTION:CHARACTER|>
|
||||
{{ character.description }}
|
||||
{{ character.sheet }}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:EXAMPLES|>
|
||||
{% for example in examples -%}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<|SECTION:CONTENT|>
|
||||
{% if text -%}
|
||||
{{ text }}
|
||||
{% else -%}
|
||||
{% set scene_context_history = scene.context_history(budget=max_tokens-500, min_dialogue=25, sections=False, keep_director=True) -%}
|
||||
{% if scene.num_history_entries < 25 %}{{ scene.description }}{% endif -%}
|
||||
{% for scene_context in scene_context_history -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
<|SECTION:CHARACTER|>
|
||||
{{ character.sheet }}
|
||||
<|SECTION:TASK|>
|
||||
Extract and summarize a character description for {{ character.name }} from the content
|
||||
{{ set_prepared_response(character.name) }}
|
||||
@@ -0,0 +1,4 @@
|
||||
<|SECTION:CONTENT|>
|
||||
{{ text }}
|
||||
<|SECTIOn:TASK|>
|
||||
Extract and summarize a scenario description from the content
|
||||
@@ -6,7 +6,7 @@
|
||||
{% endfor %}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Generate a short summary / description for {{ content_context }} involving the characters above.
|
||||
Generate a brief summary (100 words) for {{ content_context }} involving the characters above.
|
||||
|
||||
{% if prompt -%}
|
||||
Premise: {{ prompt }}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
{{ description }}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Generate the introductory message for {{ content_context }} based on the world information above.
|
||||
Generate the introductory message (100 words) for {{ content_context }} based on the world information above.
|
||||
|
||||
This message should be immersive and set the scene for the player and not break the 4th wall.
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
<|SECTION:TASK|>
|
||||
Generate a short name or title for {{ content_context }} based on the description above.
|
||||
|
||||
Only name. No description.
|
||||
{% if prompt -%}
|
||||
Premise: {{ prompt }}
|
||||
{% endif -%}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
<|SECTION:CONTEXT|>
|
||||
{{ character.description }}
|
||||
|
||||
{{ character.base_attributes.get("scenario_context", "") }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{% for scene_context in scene.context_history(budget=200, add_archieved_history=False, min_dialogue=10) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
<|SECTION:TASK|>
|
||||
Instruction: Analyze the scene so far and answer the following question(s)
|
||||
Expected response: a JSON response containing questions, answers and reasoning
|
||||
|
||||
{% if scene.history -%}
|
||||
Last line of dialogue: {{ scene.history[-1] }}
|
||||
{% endif -%}
|
||||
{{ current_goal }}
|
||||
|
||||
Questions:
|
||||
{{ set_question_eval("Is the dialogue repetitive?", "yes", "direct") }}
|
||||
{{ set_question_eval("Is the actor playing "+character.name+" staying true to the character and their development so far?", "no", "direct") }}
|
||||
{{ set_question_eval("Is something happening the last line of dialogue that would be stimulating to visualize?", "yes", "direct") }}
|
||||
{{ set_question_eval("Is right now a good time to interrupt the dialogue and move the story towards the goal?", "yes", "direct") }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
Director answers:
|
||||
{{ set_eval_response(empty="watch") }}
|
||||
@@ -1,20 +0,0 @@
|
||||
{{ character.description }}
|
||||
|
||||
{{ character.base_attributes.get("scenario_context", "") }}
|
||||
|
||||
{% for scene_context in scene.context_history(budget=max_tokens-500) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
|
||||
Scene analysis:
|
||||
{{ scene_analyzation }}
|
||||
|
||||
Instruction: based on your analysis above, pick an action subtly move the scene forward
|
||||
Answer format: We should use the following action: [action mame] - [Your reasoning]
|
||||
|
||||
[narrate] - [write visual description of event happening or progess the story with narrative exposition]
|
||||
[direct {{character.name}}] - [direct the actor playing {{character.name}} to perform an action]
|
||||
[watch] - [do nothing, just watch the scene unfold]
|
||||
|
||||
Director answers: We should use the following action:{{ bot_token }}[
|
||||
@@ -1,16 +0,0 @@
|
||||
{{ direction_prompt }}
|
||||
|
||||
<|SECTION:DIRECTION|>
|
||||
{{ direction }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:TASK|>
|
||||
Instruction: Analyze the scene so far and answer the following question either with yes or no:
|
||||
|
||||
Is this a direct, actionable direction to {{ character.name }} ?
|
||||
Is the director's instruction to {{ character.name }} in line with the character's development so far?
|
||||
Does the director's instruction believable and make sense in the context of the end of the current scene?
|
||||
Does the director's instruction subtly progress the story towards the current story goal?
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
Director answers:
|
||||
@@ -1,19 +0,0 @@
|
||||
{{ direction_prompt }}
|
||||
|
||||
<|SECTION:DIRECTION|>
|
||||
{{ direction }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:ANALYSIS OF DIRECTION|>
|
||||
{{ analysis }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:TASK|>
|
||||
Instructions: Based on your analysis above, is the director's instruction to {{ character.name }} good, neutral or bad? If its bad, change the direction. Never question the goal itself. Explain your reasoning.
|
||||
Expected response: Respond with I want to keep OR change the direction.
|
||||
|
||||
Response example: I want to keep the direction, because ..
|
||||
Response example: I want to change the direction, because ..
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{{ set_prepared_response("Director reflects on his direction: I want to ") }}
|
||||
@@ -1,32 +0,0 @@
|
||||
<|SECTION:CONTEXT|>
|
||||
{{ character.description }}
|
||||
|
||||
{{ character.base_attributes.get("scenario_context", "") }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{% for scene_context in scene.context_history(budget=200, add_archieved_history=False, min_dialogue=10) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
<|SECTION:DIALOGUE ANALYSIS|>
|
||||
{{ analysis }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:STORY GOAL|>
|
||||
{{ current_goal }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{% if not previous_direction -%}
|
||||
<|SECTION:TASK|>
|
||||
Give actionable directions to the actor playing {{ character.name }} by instructing {{ character.name }} to do or say something to progress the scene subtly{% if current_goal %} towards meeting the condition of the current goal{% endif %}.
|
||||
<|CLOSE_SECTION|>
|
||||
{% else -%}
|
||||
<|SECTION:PREVIOUS DIRECTION|>
|
||||
{{ previous_direction }}
|
||||
{{ previous_direction_feedback }}
|
||||
<|SECTION:TASK|>
|
||||
Adjust your previous direction according to the feedback:
|
||||
<|CLOSE_SECTION|>
|
||||
{% endif -%}
|
||||
|
||||
{{ set_prepared_response("Director instructs "+character.name+": \"To progress the scene, i want you to ") }}
|
||||
@@ -1,22 +0,0 @@
|
||||
{% for scene_context in scene.context_history(budget=200, add_archieved_history=False, min_dialogue=10) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
<|SECTION:DIALOGUE ANALYSIS|>
|
||||
{{ analysis }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:TASK|>
|
||||
{% if narration_type == "progress" -%}
|
||||
Instruction: Analyze the dialogue and scene so far and have the director give directions to the story writer to subtly progress the current scene.
|
||||
{% elif narration_type == "visual" %}
|
||||
Instruction: Analyze the last line of the dialogue and have the director give directions to the story writer to describe the end point of the scene visually.
|
||||
{% elif narration_type == "character" %}
|
||||
{% endif -%}
|
||||
|
||||
{% if scene.history -%}
|
||||
Last line of dialogue: {{ scene.history[-1] }}
|
||||
{% endif -%}
|
||||
{{ current_goal }}
|
||||
<|CLOSE_SECTION|>
|
||||
{{ bot_token }}Director instructs story writer:
|
||||
15
src/talemate/prompts/templates/director/direct-scene.jinja2
Normal file
15
src/talemate/prompts/templates/director/direct-scene.jinja2
Normal file
@@ -0,0 +1,15 @@
|
||||
<|SECTION:SCENE|>
|
||||
{% block scene_history -%}
|
||||
{% for scene_context in scene.context_history(budget=1000, min_dialogue=25, sections=False, keep_director=False) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
{% endblock -%}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Current scene goal: {{ prompt }}
|
||||
|
||||
Give actionable directions to the actor playing {{ character.name }} by instructing {{ character.name }} to do or say something to progress the scene subtly towards meeting the condition of the current goal.
|
||||
|
||||
Take the most recent update to the scene into consideration: {{ scene.history[-1] }}
|
||||
<|CLOSE_SECTION|>
|
||||
{{ set_prepared_response("Director instructs "+character.name+": \"To progress the scene, i want you to ") }}
|
||||
@@ -1,8 +0,0 @@
|
||||
|
||||
{% for scene_context in scene.context_history(budget=max_tokens-300) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
Question: Do any lines or events in the dialogue satisfy the following story condition: "{{ current_goal }}" - Explain your reasoning and then state 'satisfied' or 'NOT been satisfied'.
|
||||
|
||||
{{ bot_token }}Director decides: The condition has
|
||||
@@ -1,28 +0,0 @@
|
||||
<|SECTION:CONTEXT|>
|
||||
{{ character.description }}
|
||||
|
||||
{{ character.base_attributes.get("scenario_context", "") }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{% for scene_context in scene.context_history(budget=200, add_archieved_history=False, min_dialogue=10) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
<|SECTION:TASK|>
|
||||
Instruction: Analyze the scene so far and answer the following question(s)
|
||||
Expected response: a JSON response containing questions, answers and reasoning
|
||||
|
||||
{% if scene.history -%}
|
||||
Last line of dialogue: {{ scene.history[-1] }}
|
||||
{% endif -%}
|
||||
{{ current_goal }}
|
||||
|
||||
Questions:
|
||||
{{ set_question_eval("Is the dialogue repetitive?", "yes", "direct") }}
|
||||
{{ set_question_eval("Is the actor playing "+character.name+" staying true to the character and their development so far?", "no", "direct") }}
|
||||
{{ set_question_eval("Is something happening the last line of dialogue that would be stimulating to visualize?", "yes", "narrate:visual") }}
|
||||
{{ set_question_eval("Is right now a good time to interrupt the dialogue and move the story towards the goal?", "yes", "direct") }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
Director answers:
|
||||
{{ set_eval_response(empty="watch") }}
|
||||
@@ -1,20 +0,0 @@
|
||||
{{ character.description }}
|
||||
|
||||
{{ character.base_attributes.get("scenario_context", "") }}
|
||||
|
||||
{% for scene_context in scene.context_history(budget=max_tokens-500) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
|
||||
Scene analysis:
|
||||
{{ scene_analyzation }}
|
||||
|
||||
Instruction: based on your analysis above, pick an action subtly move the scene forward
|
||||
Answer format: We should use the following action: [action mame] - [Your reasoning]
|
||||
|
||||
[narrate] - [write visual description of event happening or progess the story with narrative exposition]
|
||||
[direct {{character.name}}] - [direct the actor playing {{character.name}} to perform an action]
|
||||
[watch] - [do nothing, just watch the scene unfold]
|
||||
|
||||
Director answers: We should use the following action:{{ bot_token }}[
|
||||
@@ -1,16 +0,0 @@
|
||||
{{ direction_prompt }}
|
||||
|
||||
<|SECTION:DIRECTION|>
|
||||
{{ direction }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:TASK|>
|
||||
Instruction: Analyze the scene so far and answer the following question either with yes or no:
|
||||
|
||||
Is this a direct, actionable direction to {{ character.name }} ?
|
||||
Is the director's instruction to {{ character.name }} in line with the character's development so far?
|
||||
Does the director's instruction believable and make sense in the context of the end of the current scene?
|
||||
Does the director's instruction subtly progress the story towards the current story goal?
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
Director answers:
|
||||
@@ -1,19 +0,0 @@
|
||||
{{ direction_prompt }}
|
||||
|
||||
<|SECTION:DIRECTION|>
|
||||
{{ direction }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:ANALYSIS OF DIRECTION|>
|
||||
{{ analysis }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:TASK|>
|
||||
Instructions: Based on your analysis above, is the director's instruction to {{ character.name }} good, neutral or bad? If its bad, change the direction. Never question the goal itself. Explain your reasoning.
|
||||
Expected response: Respond with I want to keep OR change the direction.
|
||||
|
||||
Response example: I want to keep the direction, because ..
|
||||
Response example: I want to change the direction, because ..
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{{ set_prepared_response("Director reflects on his direction: I want to ") }}
|
||||
@@ -1,32 +0,0 @@
|
||||
<|SECTION:CONTEXT|>
|
||||
{{ character.description }}
|
||||
|
||||
{{ character.base_attributes.get("scenario_context", "") }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{% for scene_context in scene.context_history(budget=200, add_archieved_history=False, min_dialogue=10) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
<|SECTION:DIALOGUE ANALYSIS|>
|
||||
{{ analysis }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:STORY GOAL|>
|
||||
{{ current_goal }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{% if not previous_direction -%}
|
||||
<|SECTION:TASK|>
|
||||
Give actionable directions to the actor playing {{ character.name }} by instructing {{ character.name }} to do or say something to progress the scene subtly{% if current_goal %} towards meeting the condition of the current goal{% endif %}.
|
||||
<|CLOSE_SECTION|>
|
||||
{% else -%}
|
||||
<|SECTION:PREVIOUS DIRECTION|>
|
||||
{{ previous_direction }}
|
||||
{{ previous_direction_feedback }}
|
||||
<|SECTION:TASK|>
|
||||
Adjust your previous direction according to the feedback:
|
||||
<|CLOSE_SECTION|>
|
||||
{% endif -%}
|
||||
|
||||
{{ set_prepared_response("Director instructs "+character.name+": \"To progress the scene, i want you to ") }}
|
||||
@@ -1,22 +0,0 @@
|
||||
{% for scene_context in scene.context_history(budget=200, add_archieved_history=False, min_dialogue=10) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
<|SECTION:DIALOGUE ANALYSIS|>
|
||||
{{ analysis }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:TASK|>
|
||||
{% if narration_type == "progress" -%}
|
||||
Instruction: Analyze the dialogue and scene so far and have the director give directions to the story writer to subtly progress the current scene.
|
||||
{% elif narration_type == "visual" %}
|
||||
Instruction: Analyze the last line of the dialogue and have the director give directions to the story writer to describe the end point of the scene visually.
|
||||
{% elif narration_type == "character" %}
|
||||
{% endif -%}
|
||||
|
||||
{% if scene.history -%}
|
||||
Last line of dialogue: {{ scene.history[-1] }}
|
||||
{% endif -%}
|
||||
{{ current_goal }}
|
||||
<|CLOSE_SECTION|>
|
||||
{{ bot_token }}Director instructs story writer:
|
||||
@@ -1,8 +0,0 @@
|
||||
|
||||
{% for scene_context in scene.context_history(budget=max_tokens-300) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
Question: Do any lines or events in the dialogue satisfy the following story condition: "{{ current_goal }}" - Explain your reasoning and then state 'satisfied' or 'NOT been satisfied'.
|
||||
|
||||
{{ bot_token }}Director decides: The condition has
|
||||
28
src/talemate/prompts/templates/editor/add-detail.jinja2
Normal file
28
src/talemate/prompts/templates/editor/add-detail.jinja2
Normal file
@@ -0,0 +1,28 @@
|
||||
<|SECTION:CHARACTERS|>
|
||||
{% for character in characters -%}
|
||||
{{ character.name }}:
|
||||
{{ character.filtered_sheet(['name', 'age', 'gender']) }}
|
||||
{{ query_memory("what is "+character.name+"'s personality?", as_question_answer=False) }}
|
||||
|
||||
{{ character.description }}
|
||||
|
||||
|
||||
{% endfor %}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:SCENE|>
|
||||
Content Context: {{ scene.context }}
|
||||
|
||||
{% for scene_context in scene.context_history(budget=1000, min_dialogue=25, sections=False, keep_director=True) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Take the following line of dialog spoken by {{ character.name }} and flesh it out by adding minor details and flourish to it.
|
||||
|
||||
Spoken words should be in quotes.
|
||||
|
||||
Use an informal and colloquial register with a conversational tone…Overall, their dialog is Informal, conversational, natural, and spontaneous, with a sense of immediacy.
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
Original dialog: {{ content }}
|
||||
{{ set_prepared_response(character.name+":", prepend="Fleshed out dialog: ") }}
|
||||
11
src/talemate/prompts/templates/editor/edit-dialogue.jinja2
Normal file
11
src/talemate/prompts/templates/editor/edit-dialogue.jinja2
Normal file
@@ -0,0 +1,11 @@
|
||||
<|SECTION:{{ character.name }}'S WRITING STYLE|>
|
||||
{% for example in character.random_dialogue_examples(num=3) -%}
|
||||
{{ example }}
|
||||
{% endfor %}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Based on {{ character.name }}'s typical writing style, please adjust the following line to their mannerisms and style of speaking:
|
||||
|
||||
{{ content }}
|
||||
<|CLOSE_SECTION|>
|
||||
I have adjusted the line: {{ set_prepared_response(character.name+":") }}
|
||||
29
src/talemate/prompts/templates/editor/fix-exposition.jinja2
Normal file
29
src/talemate/prompts/templates/editor/fix-exposition.jinja2
Normal file
@@ -0,0 +1,29 @@
|
||||
<|SECTION:EXAMPLES|>{{ disable_dedupe() }}
|
||||
Input: {{ character.name }}: She whispered, Don't tell anyone. with a stern look.
|
||||
Output: {{ character.name }}: *She whispered,* "Don't tell anyone." *with a stern look.*
|
||||
|
||||
Input: {{ character.name }}: Where are you going? he asked, looking puzzled. I thought we were staying in.
|
||||
Output: {{ character.name }}: "Where are you going?" *he asked, looking puzzled.* "I thought we were staying in."
|
||||
|
||||
Input: {{ character.name }}: With a heavy sigh, she said, I just can't believe it. and walked away.
|
||||
Output: {{ character.name }}: *With a heavy sigh, she said,* "I just can't believe it." *and walked away.*
|
||||
|
||||
Input: {{ character.name }}: It's quite simple, he explained. You just have to believe.
|
||||
Output: {{ character.name }}: "It's quite simple," *he explained.* "You just have to believe."
|
||||
|
||||
Input: {{ character.name }}: She giggled, finding his antics amusing. You're such a clown!
|
||||
Output: {{ character.name }}: *She giggled, finding his antics amusing.* "You're such a clown!"
|
||||
|
||||
Input: {{ character.name }}: He frowned, noticing the dark clouds gathering overhead. Looks like a storm is coming.
|
||||
Output: {{ character.name }}: *He frowned, noticing the dark clouds gathering overhead.* "Looks like a storm is coming."
|
||||
|
||||
Input: {{ character.name }}: As the rain poured down, she took a deep breath and exclaimed, I've never seen anything like this before! It's absolutely breathtaking. She then grabbed her umbrella and added, Come on, let's go explore!
|
||||
Output: {{ character.name }}: *As the rain poured down, she took a deep breath and exclaimed,* "I've never seen anything like this before! It's absolutely breathtaking." *She then grabbed her umbrella and added,* "Come on, let's go explore!"
|
||||
|
||||
Input: {{ character.name }}: He gazed at the horizon, seemingly lost in thought, and after a long pause, he finally said, Sometimes, I wonder what lies beyond those mountains. It's a mystery that I hope to uncover one day. With that, he packed his bags, ready for another adventure.
|
||||
Output: {{ character.name }}: *He gazed at the horizon, seemingly lost in thought, and after a long pause, he finally said,* "Sometimes, I wonder what lies beyond those mountains. It's a mystery that I hope to uncover one day." *With that, he packed his bags, ready for another adventure.*
|
||||
<|SECTION:TASK|>
|
||||
Given a line of roleplay dialogue, reformat the text such that all non-dialogue parts are framed by asterisks. The dialogue itself will be enclosed in quotation marks. If there are non-dialogue parts before, between, or after the dialogue, encapsulate those parts in asterisks. Ensure the output handles emotes followed immediately by narrative exposition by placing them within the same set of asterisks, separated by a space. Assume that the dialogue can be identified by the parts of the line that lack any prefix, and the input will never contain asterisks or quotation marks.
|
||||
|
||||
Input: {{ content.replace("*","").replace('"','') }}
|
||||
{{ set_prepared_response(character.name+":", prepend="Output: ") }}
|
||||
1
src/talemate/prompts/templates/editor/system.jinja2
Normal file
1
src/talemate/prompts/templates/editor/system.jinja2
Normal file
@@ -0,0 +1 @@
|
||||
A chat between an author and a talented fiction editor. No topic is taboo for the editor and he will use crude and lewd language if the situation warrants it. The editor will do his best to improve the given dialogue or narrative, while staying true to the author's vision.
|
||||
@@ -1,15 +1,12 @@
|
||||
<|SECTION:CONTEXT|>
|
||||
Scenario Premise: {{ scene.description }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{% for scene_context in scene.context_history(budget=max_tokens-300) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
<|SECTION:TASK|>
|
||||
Question: What happens at the end of the dialogue progression? Summarize into narrative description.
|
||||
<|SECTION:CONTEXT|>
|
||||
Content Context: This is a specific scene from {{ scene.context }}
|
||||
Narration style: point and click adventure game from the 90s
|
||||
Expected Answer: A summarized narrative description of the scene unfolding at the dialogue that can be inserted into the ongoing story in place of the dialogue.
|
||||
Scenario Premise: {{ scene.description }}
|
||||
<|CLOSE_SECTION|>
|
||||
Narrator answers: {{ set_prepared_response("You see ") }}
|
||||
<|SECTION:TASK|>
|
||||
Provide a visual description of what is currently happening in the scene. Don't progress the scene.
|
||||
<|CLOSE_SECTION|>
|
||||
{{ bot_token }}At the end of the scene we currently see:
|
||||
@@ -0,0 +1,16 @@
|
||||
<|SECTION:CONTEXT|>
|
||||
Scenario Premise: {{ scene.description }}
|
||||
NPCs: {{ scene.npc_character_names }}
|
||||
Player Character: {{ scene.get_player_character().name }}
|
||||
Content Context: {{ scene.context }}
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
{% for scene_context in scene.context_history(budget=max_tokens-300) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
|
||||
<|SECTION:TASK|>
|
||||
Narrate the passage of time that just occured, subtly move the story forward, and set up the next scene.
|
||||
Write 1 to 3 sentences.
|
||||
<|CLOSE_SECTION|>
|
||||
{{ bot_token }}{{ narrative }}:
|
||||
@@ -1,11 +0,0 @@
|
||||
Instructions: Mark all tangible physical subjects in the sentence with brackets. For example, if the line of dialogue is "John: I am going to the store." and you want to mark "store" as a subject, you would write "John: I am going to [the store]."
|
||||
|
||||
Sentence:
|
||||
Barbara: *Barabara sits down on the couch while John is watching TV* Lets see whats on *She takes the remote and starts flipping through channels. She occasionally snaps her wristband while she does it*
|
||||
|
||||
Sentence with tangible physical objects marked:
|
||||
Barbara: *Barabara sits down on [the couch] while John is watching [TV]* Lets see whats on *She takes [the remote] and starts flipping through [channels]. She occasionally snaps [her wristband] while she does it*
|
||||
|
||||
Sentence:
|
||||
{{ scene.history[-1] }}
|
||||
Sentence with tangible physical objects marked::{{ bot_token }}
|
||||
@@ -6,8 +6,4 @@
|
||||
Question: What happens within the dialogue? Summarize into narrative description.
|
||||
Content Context: This is a specific scene from {{ scene.context }}
|
||||
Expected Answer: A summarized narrative description of the dialogue that can be inserted into the ongoing story in place of the dialogue.
|
||||
|
||||
Include implied time skips (for example characters plan to meet at a later date and then they meet).
|
||||
|
||||
<|CLOSE_SECTION|>
|
||||
Narrator answers:
|
||||
<|CLOSE_SECTION|>
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
{{ text }}
|
||||
|
||||
<|SECTION:TASK|>
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
<|SECTION:CONTEXT|>
|
||||
{% for memory in query_memory(text, as_question_answer=False, max_tokens=max_tokens-500, iterate=20) -%}
|
||||
{{ memory }}
|
||||
|
||||
{% endfor -%}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Answer the following questions:
|
||||
|
||||
{{ query_text("What are 1 to 3 questions to ask the narrator of the story to gather more context from the past for the continuation of this conversation? If a character is asking about a status, location or information about an item or another character, make sure to include question(s) that help gather context for this. Don't explain your reasoning. Don't ask the actors directly.", text, as_question_answer=False) }}
|
||||
|
||||
You answers should be precise, truthful and short.
|
||||
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:RELEVANT CONTEXT|>
|
||||
@@ -0,0 +1,13 @@
|
||||
<|SECTION:CONTENT|>
|
||||
{% if text -%}
|
||||
{{ text }}
|
||||
{% else -%}
|
||||
{% set scene_context_history = scene.context_history(budget=max_tokens-500, min_dialogue=25, sections=False, keep_director=True) -%}
|
||||
{% if scene.num_history_entries < 25 %}{{ scene.description.replace("\r\n","\n") }}{% endif -%}
|
||||
{% for scene_context in scene_context_history -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
<|SECTION:TASK|>
|
||||
Generate a real world character profile for {{ name }}, one attribute per line.
|
||||
{{ set_prepared_response("Name: "+name+"\nAge:") }}
|
||||
@@ -0,0 +1,13 @@
|
||||
<|SECTION:CONTENT|>
|
||||
{% if text -%}
|
||||
{{ text }}
|
||||
{% else -%}
|
||||
{% set scene_context_history = scene.context_history(budget=max_tokens-500, min_dialogue=25, sections=False, keep_director=True) -%}
|
||||
{% if scene.num_history_entries < 25 %}{{ scene.description }}{% endif -%}
|
||||
{% for scene_context in scene_context_history -%}
|
||||
{{ scene_context }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
<|SECTION:TASK|>
|
||||
Identify all main characters by name respond with a json object in the format of {"characters":[{"name": "John" , "description": "Information about the character" }]}
|
||||
{{ set_json_response({"characters":[""]}) }}
|
||||
@@ -0,0 +1,30 @@
|
||||
<|SECTION:CHARACTERS|>
|
||||
Player / main character:
|
||||
- {{ scene.get_player_character().name }}
|
||||
Other characters:
|
||||
{% for name in scene.npc_character_names -%}
|
||||
- {{ name }}
|
||||
{% endfor -%}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Match the following character aliases to the existing characters.
|
||||
|
||||
Respond in the following JSON format:
|
||||
|
||||
{
|
||||
"matched_names": [
|
||||
{
|
||||
"alias": "alias", # given alias name for the task
|
||||
"matched_name": "character name" # name of the character
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
If the name cannot be matched to a character, skip it
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:ALIASES|>
|
||||
{% for name in names -%}
|
||||
- {{ name }}
|
||||
{% endfor -%}
|
||||
<|CLOSE_SECTION|>
|
||||
{{ set_json_response(dict(matched_names=[""])) }}
|
||||
@@ -0,0 +1,57 @@
|
||||
<|SECTION:JSON SCHEMA|>
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"characters": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"snapshot": {
|
||||
# describe the character's current state in the scene
|
||||
"type": "string"
|
||||
},
|
||||
"emotion": {
|
||||
# simple, one word e.g., "happy", "sad", "angry", "confused", "scared" etc.,
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["snapshot", "emotion"]
|
||||
}
|
||||
},
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"snapshot": {
|
||||
# describe the item's current state in the scene
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["snapshot"]
|
||||
}
|
||||
},
|
||||
"location": {
|
||||
# where is the scene taking place?
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["characters", "items", "location"]
|
||||
}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:LAST KNOWN WORLD STATE|>
|
||||
{{ scene.world_state.pretty_json }}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:SCENE PROGRESS|>
|
||||
{% for scene_context in scene.context_history(budget=300, min_dialogue=5, add_archieved_history=False, max_dialogue=5) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor -%}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Update the existing JSON object for the world state to reflect the changes in the scene progression.
|
||||
|
||||
Objects that are no longer explicitly mentioned in the scene progression should be removed from the JSON object.
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:UPDATED WORLD STATE|>{{ set_json_response(dict(characters={"name":{}}), cutoff=1) }}
|
||||
@@ -0,0 +1,56 @@
|
||||
<|SECTION:EXAMPLE|>
|
||||
{
|
||||
"characters": {
|
||||
# the character name is the key
|
||||
"Character name": {
|
||||
"emotion": "The current emotional state or mood of the character. (neutral, happy, sad, angry, etc.)",
|
||||
"snapshot": "A brief narrative description of what the character is doing at this moment in the scene."
|
||||
},
|
||||
# ...
|
||||
},
|
||||
"items": {
|
||||
# the item name is the key in natural language (short)
|
||||
"Item name": {
|
||||
"snapshot": "A brief narrative description of the item and the state its currently in."
|
||||
},
|
||||
# ...
|
||||
},
|
||||
"location": "A brief narrative description of the location the scene is taking place in.",
|
||||
}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:CONTEXT|>
|
||||
Player character: {{ scene.get_player_character().name }}
|
||||
Other major characters:
|
||||
{% for npc_name in scene.npc_character_names -%}
|
||||
{{ npc_name }}
|
||||
{% endfor -%}
|
||||
|
||||
{% for scene_context in scene.context_history(budget=1000, min_dialogue=10, dialogue_negative_offset=5, sections=False) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor -%}
|
||||
{% if not scene.history -%}
|
||||
<|SECTION:DIALOGUE|>
|
||||
No dialogue so far
|
||||
{% endif -%}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:SCENE PROGRESS|>
|
||||
{% for scene_context in scene.context_history(budget=300, min_dialogue=5, add_archieved_history=False, max_dialogue=5) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor -%}
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:TASK|>
|
||||
Create a JSON object for the world state that reflects the scene progression so far.
|
||||
|
||||
The world state needs to include important concrete and material items present at the very end of the dialogue.
|
||||
The world state needs to include persons (characters) interacting at the very end of the dialogue
|
||||
Be factual and truthful. Don't make up things that are not in the context or dialogue.
|
||||
Snapshot text should always be specified. If you don't know what to write, write "You see nothing special."
|
||||
Emotion should always be specified. If you don't know what to write, write "neutral".
|
||||
|
||||
Required response: a complete and valid JSON response according to the JSON example containing items and characters.
|
||||
|
||||
characters should have the following attributes: `emotion`, `snapshot`
|
||||
items should have the following attributes: `snapshot`
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:UPDATED WORLD STATE|>
|
||||
{{ set_json_response(dict(characters={"name":{}}), cutoff=3) }}
|
||||
@@ -1,24 +1,22 @@
|
||||
<|SECTION:CONTEXT EXAMPLE|>
|
||||
Barbara visited her borther John.
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:DIALOGUE EXAMPLE|>
|
||||
Barbara: *Barbara accidently poured some yoghurt on her shirt*
|
||||
John: I love filming myself *Holds up his phone to film himself* I dont mind that the screen is cracked!
|
||||
Barbara: I should change this shirt but i dont want to get up from the couch
|
||||
<|CLOSE_SECTION|>
|
||||
|
||||
<|SECTION:WORLD STATE EXAMPLE|>
|
||||
<|SECTION:WORLD STATE SCHEMA|>
|
||||
{
|
||||
"items": [
|
||||
{"name": "Barbara's red shirt", "snapshot": "The shirt has a big stain on it"},
|
||||
{"name": "John's fanncy phone", "snapshot": "The screen is cracked"}
|
||||
],
|
||||
"characters": [
|
||||
{"name": "John", "emotion": "Excited", "snapshot": "John is filming himself on his phone next to his sister"},
|
||||
{"name": "Barbara", "emotion": "Calm", "snapshot": "Barbara is sitting on the couch"}
|
||||
{
|
||||
"name": "The name of the character involved in the scene.",
|
||||
"emotion": "The current emotional state or mood of the character.",
|
||||
"snapshot": "A brief description of what the character is doing at this moment in the scene."
|
||||
},
|
||||
# ...
|
||||
],
|
||||
"items": [
|
||||
{
|
||||
"name": "The name of an item that belongs to one of the characters.",
|
||||
"snapshot": "A brief description of the item's current condition or any notable features."
|
||||
},
|
||||
# ...
|
||||
]
|
||||
}
|
||||
|
||||
<|CLOSE_SECTION|>
|
||||
<|SECTION:CONTEXT|>
|
||||
{% for scene_context in scene.context_history(budget=1000, min_dialogue=10, dialogue_negative_offset=5, sections=False) -%}
|
||||
@@ -41,15 +39,15 @@ Instruction to the Analyst:
|
||||
6. Be factual and truthful. Don't make up things that are not in the context or dialogue.
|
||||
7. Snapshot text should always be specified. If you don't know what to write, write "You see nothing special."
|
||||
|
||||
Required response: a valid JSON response according to the JSON example containing lists of items and characters.
|
||||
Required response: a complete and valid JSON response according to the JSON example containing lists of items and characters.
|
||||
|
||||
characters should habe the following attributes: `name`, `emotion`, `snapshot`
|
||||
items should have the following attributes: `name`, `snapshot`
|
||||
|
||||
Don't copy the example, write your own descriptions.
|
||||
You must not copy the example, write your own descriptions.
|
||||
<|CLOSE_SECTION|>
|
||||
{% for scene_context in scene.context_history(budget=300, min_dialogue=5, add_archieved_history=False, max_dialogue=5) -%}
|
||||
{{ scene_context }}
|
||||
{% endfor -%}
|
||||
<|SECTION:WORLD STATE|>
|
||||
{{ set_json_response(dict(items=[""])) }}
|
||||
{{ set_json_response(dict(characters=[{"name":scene.character_names[0]}])) }}
|
||||
@@ -75,6 +75,10 @@ class CharacterMessage(SceneMessage):
|
||||
def __str__(self):
|
||||
return self.message
|
||||
|
||||
@property
|
||||
def character_name(self):
|
||||
return self.message.split(":", 1)[0]
|
||||
|
||||
@dataclass
|
||||
class NarratorMessage(SceneMessage):
|
||||
source: str = "progress_story"
|
||||
|
||||
@@ -8,6 +8,8 @@ import structlog
|
||||
from talemate.prompts import Prompt
|
||||
from talemate.tale_mate import Character, Actor, Player
|
||||
|
||||
from typing import Union
|
||||
|
||||
log = structlog.get_logger("talemate.server.character_creator")
|
||||
|
||||
|
||||
@@ -18,7 +20,7 @@ class StepData(pydantic.BaseModel):
|
||||
character_prompt: str
|
||||
dialogue_guide: str
|
||||
dialogue_examples: list[str]
|
||||
base_attributes: dict[str, str] = {}
|
||||
base_attributes: dict[str, Union[str, int]] = {}
|
||||
custom_attributes: dict[str, str] = {}
|
||||
details: dict[str, str] = {}
|
||||
description: str = None
|
||||
|
||||
@@ -3,6 +3,7 @@ import pydantic
|
||||
import asyncio
|
||||
import structlog
|
||||
import json
|
||||
from typing import Union
|
||||
|
||||
from talemate.load import load_character_into_scene
|
||||
|
||||
@@ -12,11 +13,11 @@ class ListScenesData(pydantic.BaseModel):
|
||||
scene_path: str
|
||||
|
||||
class CreateSceneData(pydantic.BaseModel):
|
||||
name: str = None
|
||||
description: str = None
|
||||
intro: str = None
|
||||
content_context: str = None
|
||||
prompt: str = None
|
||||
name: Union[str, None] = None
|
||||
description: Union[str, None] = None
|
||||
intro: Union[str, None] = None
|
||||
content_context: Union[str, None] = None
|
||||
prompt: Union[str, None] = None
|
||||
|
||||
class SceneCreatorServerPlugin:
|
||||
|
||||
|
||||
@@ -63,8 +63,8 @@ class WebsocketHandler(Receiver):
|
||||
abort_wait_for_input()
|
||||
|
||||
memory_agent = instance.get_agent("memory")
|
||||
if memory_agent:
|
||||
memory_agent.close_db()
|
||||
if memory_agent and self.scene:
|
||||
memory_agent.close_db(self.scene)
|
||||
|
||||
def connect_llm_clients(self):
|
||||
client = None
|
||||
@@ -101,7 +101,9 @@ class WebsocketHandler(Receiver):
|
||||
|
||||
log.debug("Linked agent", agent_typ=agent_typ, client=client.name)
|
||||
agent = instance.get_agent(agent_typ, client=client)
|
||||
agent.client = client
|
||||
agent.client = client
|
||||
agent.apply_config(**agent_config)
|
||||
|
||||
|
||||
instance.emit_agents_status()
|
||||
|
||||
@@ -126,6 +128,10 @@ class WebsocketHandler(Receiver):
|
||||
|
||||
async def load_scene(self, path_or_data, reset=False, callback=None, file_name=None):
|
||||
try:
|
||||
|
||||
if self.scene:
|
||||
instance.get_agent("memory").close_db(self.scene)
|
||||
|
||||
scene = self.init_scene()
|
||||
|
||||
if not scene:
|
||||
@@ -133,19 +139,10 @@ class WebsocketHandler(Receiver):
|
||||
return
|
||||
|
||||
conversation_helper = scene.get_helper("conversation")
|
||||
memory_helper = scene.get_helper("memory")
|
||||
|
||||
await memory_helper.agent.set_db()
|
||||
|
||||
scene = await load_scene(
|
||||
scene, path_or_data, conversation_helper.agent.client, reset=reset
|
||||
)
|
||||
#elif isinstance(path_or_data, dict):
|
||||
# scene = await load_scene_from_data(
|
||||
# scene, path_or_data, conversation_helper.agent.client, reset=reset
|
||||
# )
|
||||
|
||||
# Continuously ask the user for input and send it to the actor's talk_to method
|
||||
|
||||
self.scene = scene
|
||||
|
||||
@@ -238,11 +235,18 @@ class WebsocketHandler(Receiver):
|
||||
"client": self.llm_clients[agent["client"]]["name"],
|
||||
"name": name,
|
||||
}
|
||||
|
||||
|
||||
agent_instance = instance.get_agent(name, **self.agents[name])
|
||||
agent_instance.client = self.llm_clients[agent["client"]]["client"]
|
||||
|
||||
if agent_instance.has_toggle:
|
||||
self.agents[name]["enabled"] = agent["enabled"]
|
||||
|
||||
if getattr(agent_instance, "actions", None):
|
||||
self.agents[name]["actions"] = agent.get("actions", {})
|
||||
|
||||
agent_instance.apply_config(**self.agents[name])
|
||||
|
||||
log.debug("Configured agent", name=name, client_name=self.llm_clients[agent["client"]]["name"], client=self.llm_clients[agent["client"]]["client"])
|
||||
|
||||
self.config["agents"] = self.agents
|
||||
@@ -272,12 +276,20 @@ class WebsocketHandler(Receiver):
|
||||
)
|
||||
|
||||
def handle_director(self, emission: Emission):
|
||||
|
||||
if emission.character:
|
||||
character = emission.character.name
|
||||
elif emission.message_object.source:
|
||||
character = emission.message_object.source
|
||||
else:
|
||||
character = ""
|
||||
|
||||
self.queue_put(
|
||||
{
|
||||
"type": "director",
|
||||
"message": emission.message,
|
||||
"id": emission.id,
|
||||
"character": emission.character.name if emission.character else "",
|
||||
"character": character,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -585,5 +597,12 @@ class WebsocketHandler(Receiver):
|
||||
plugin = self.routes[route]
|
||||
try:
|
||||
await plugin.handle(data)
|
||||
except Exception:
|
||||
log.error("route", error=traceback.format_exc())
|
||||
except Exception as e:
|
||||
log.error("route", error=traceback.format_exc())
|
||||
self.queue_put(
|
||||
{
|
||||
"plugin": plugin.router,
|
||||
"type": "error",
|
||||
"error": str(e),
|
||||
}
|
||||
)
|
||||
@@ -6,6 +6,8 @@ import random
|
||||
import traceback
|
||||
import re
|
||||
import isodate
|
||||
import uuid
|
||||
import time
|
||||
from typing import Dict, List, Optional, Union
|
||||
|
||||
from blinker import signal
|
||||
@@ -18,6 +20,7 @@ import talemate.events as events
|
||||
import talemate.util as util
|
||||
import talemate.save as save
|
||||
from talemate.emit import Emitter, emit, wait_for_input
|
||||
import talemate.emit.async_signals as async_signals
|
||||
from talemate.util import colored_text, count_tokens, extract_metadata, wrap_text
|
||||
from talemate.scene_message import SceneMessage, CharacterMessage, DirectorMessage, NarratorMessage, TimePassageMessage
|
||||
from talemate.exceptions import ExitScene, RestartSceneLoop, ResetScene, TalemateError, TalemateInterrupt, LLMAccuracyError
|
||||
@@ -49,8 +52,8 @@ class Character:
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
description: str,
|
||||
greeting_text: str,
|
||||
description: str = "",
|
||||
greeting_text: str = "",
|
||||
gender: str = "female",
|
||||
color: str = "cyan",
|
||||
example_dialogue: List[str] = [],
|
||||
@@ -350,6 +353,9 @@ class Character:
|
||||
if attr.startswith("_"):
|
||||
continue
|
||||
|
||||
if attr.lower() in ["name", "scenario_context", "_prompt", "_template"]:
|
||||
continue
|
||||
|
||||
items.append({
|
||||
"text": f"{self.name}'s {attr}: {value}",
|
||||
"id": f"{self.name}.{attr}",
|
||||
@@ -506,6 +512,8 @@ class Player(Actor):
|
||||
|
||||
if not commands.Manager.is_command(message):
|
||||
|
||||
message = util.ensure_dialog_format(message)
|
||||
|
||||
self.message = message
|
||||
|
||||
self.scene.push_history(
|
||||
@@ -514,8 +522,9 @@ class Player(Actor):
|
||||
emit("character", self.history[-1], character=self.character)
|
||||
|
||||
return message
|
||||
|
||||
|
||||
|
||||
async_signals.register("game_loop_start")
|
||||
async_signals.register("game_loop")
|
||||
|
||||
class Scene(Emitter):
|
||||
"""
|
||||
@@ -538,9 +547,12 @@ class Scene(Emitter):
|
||||
self.main_character = None
|
||||
self.static_tokens = 0
|
||||
self.max_tokens = 2048
|
||||
self.next_actor = None
|
||||
|
||||
self.name = ""
|
||||
self.filename = ""
|
||||
self.memory_id = str(uuid.uuid4())[:10]
|
||||
self.saved = False
|
||||
|
||||
self.context = ""
|
||||
self.commands = commands.Manager(self)
|
||||
@@ -561,6 +573,8 @@ class Scene(Emitter):
|
||||
"history_add": signal("history_add"),
|
||||
"archive_add": signal("archive_add"),
|
||||
"character_state": signal("character_state"),
|
||||
"game_loop": async_signals.get("game_loop"),
|
||||
"game_loop_start": async_signals.get("game_loop_start"),
|
||||
}
|
||||
|
||||
self.setup_emitter(scene=self)
|
||||
@@ -571,6 +585,14 @@ class Scene(Emitter):
|
||||
def characters(self):
|
||||
for actor in self.actors:
|
||||
yield actor.character
|
||||
|
||||
@property
|
||||
def character_names(self):
|
||||
return [character.name for character in self.characters]
|
||||
|
||||
@property
|
||||
def npc_character_names(self):
|
||||
return [character.name for character in self.get_npc_characters()]
|
||||
|
||||
@property
|
||||
def log(self):
|
||||
@@ -586,6 +608,20 @@ class Scene(Emitter):
|
||||
def project_name(self):
|
||||
return self.name.replace(" ", "-").replace("'","").lower()
|
||||
|
||||
@property
|
||||
def num_history_entries(self):
|
||||
return len(self.history)
|
||||
|
||||
@property
|
||||
def prev_actor(self):
|
||||
# will find the first CharacterMessage in history going from the end
|
||||
# and return the character name attached to it to determine the actor
|
||||
# that most recently spoke
|
||||
|
||||
for idx in range(len(self.history) - 1, -1, -1):
|
||||
if isinstance(self.history[idx], CharacterMessage):
|
||||
return self.history[idx].character_name
|
||||
|
||||
def apply_scene_config(self, scene_config:dict):
|
||||
scene_config = SceneConfig(**scene_config)
|
||||
|
||||
@@ -872,6 +908,7 @@ class Scene(Emitter):
|
||||
else:
|
||||
end = 0
|
||||
|
||||
|
||||
history_length = len(self.history)
|
||||
|
||||
# we then take the history from the end index to the end of the history
|
||||
@@ -883,7 +920,7 @@ class Scene(Emitter):
|
||||
dialogue = self.history[end:]
|
||||
else:
|
||||
dialogue = self.history[end:-dialogue_negative_offset]
|
||||
|
||||
|
||||
if not keep_director:
|
||||
dialogue = [line for line in dialogue if not isinstance(line, DirectorMessage)]
|
||||
|
||||
@@ -892,20 +929,7 @@ class Scene(Emitter):
|
||||
|
||||
if dialogue and insert_bot_token is not None:
|
||||
dialogue.insert(-insert_bot_token, "<|BOT|>")
|
||||
|
||||
if dialogue:
|
||||
context_history = ["<|SECTION:DIALOGUE|>","\n".join(map(str, dialogue)), "<|CLOSE_SECTION|>"]
|
||||
else:
|
||||
context_history = []
|
||||
|
||||
if not sections and context_history:
|
||||
context_history = [context_history[1]]
|
||||
|
||||
# if we dont have lots of archived history, we can also include the scene
|
||||
# description at tbe beginning of the context history
|
||||
|
||||
archive_insert_idx = 0
|
||||
|
||||
# iterate backwards through archived history and count how many entries
|
||||
# there are that have an end index
|
||||
num_archived_entries = 0
|
||||
@@ -914,10 +938,38 @@ class Scene(Emitter):
|
||||
if self.archived_history[i].get("end") is None:
|
||||
break
|
||||
num_archived_entries += 1
|
||||
|
||||
if num_archived_entries <= 2 and add_archieved_history:
|
||||
|
||||
show_intro = num_archived_entries <= 2 and add_archieved_history
|
||||
reserved_min_archived_history_tokens = count_tokens(self.archived_history[-1]["text"]) if self.archived_history else 0
|
||||
reserved_intro_tokens = count_tokens(self.get_intro()) if show_intro else 0
|
||||
|
||||
max_dialogue_budget = min(max(budget - reserved_intro_tokens - reserved_min_archived_history_tokens, 500), budget)
|
||||
|
||||
dialogue_popped = False
|
||||
while count_tokens(dialogue) > max_dialogue_budget:
|
||||
dialogue.pop(0)
|
||||
|
||||
dialogue_popped = True
|
||||
|
||||
if dialogue:
|
||||
context_history = ["<|SECTION:DIALOGUE|>","\n".join(map(str, dialogue)), "<|CLOSE_SECTION|>"]
|
||||
else:
|
||||
context_history = []
|
||||
|
||||
if not sections and context_history:
|
||||
context_history = [context_history[1]]
|
||||
|
||||
# we only have room for dialogue, so we return it
|
||||
if dialogue_popped and max_dialogue_budget >= budget:
|
||||
return context_history
|
||||
|
||||
# if we dont have lots of archived history, we can also include the scene
|
||||
# description at tbe beginning of the context history
|
||||
|
||||
archive_insert_idx = 0
|
||||
|
||||
if show_intro:
|
||||
|
||||
for character in self.characters:
|
||||
if character.greeting_text and character.greeting_text != self.get_intro():
|
||||
context_history.insert(0, character.greeting_text)
|
||||
@@ -942,7 +994,6 @@ class Scene(Emitter):
|
||||
i = len(self.archived_history) - 1
|
||||
limit = 5
|
||||
|
||||
|
||||
if sections:
|
||||
context_history.insert(archive_insert_idx, "<|CLOSE_SECTION|>")
|
||||
|
||||
@@ -957,6 +1008,7 @@ class Scene(Emitter):
|
||||
text = self.archived_history[i]["text"]
|
||||
if count_tokens(context_history) + count_tokens(text) > budget:
|
||||
break
|
||||
|
||||
context_history.insert(archive_insert_idx, text)
|
||||
i -= 1
|
||||
limit -= 1
|
||||
@@ -1014,8 +1066,13 @@ class Scene(Emitter):
|
||||
new_message = await narrator.agent.narrate_character(character)
|
||||
elif source == "narrate_query":
|
||||
new_message = await narrator.agent.narrate_query(arg)
|
||||
|
||||
else:
|
||||
return
|
||||
fn = getattr(narrator.agent, source, None)
|
||||
if not fn:
|
||||
return
|
||||
args = arg.split(";") if arg else []
|
||||
new_message = await fn(*args)
|
||||
|
||||
save_source = f"{source}:{arg}" if arg else source
|
||||
|
||||
@@ -1044,8 +1101,7 @@ class Scene(Emitter):
|
||||
|
||||
director = self.get_helper("director")
|
||||
|
||||
response = await director.agent.direct(character)
|
||||
|
||||
response = await director.agent.direct_scene(character)
|
||||
if not response:
|
||||
log.info("Director returned no response")
|
||||
return
|
||||
@@ -1102,7 +1158,7 @@ class Scene(Emitter):
|
||||
break
|
||||
|
||||
def emit_status(self):
|
||||
emit(
|
||||
emit(
|
||||
"scene_status",
|
||||
self.name,
|
||||
status="started",
|
||||
@@ -1112,8 +1168,11 @@ class Scene(Emitter):
|
||||
"assets": self.assets.dict(),
|
||||
"characters": [actor.character.serialize for actor in self.actors],
|
||||
"scene_time": util.iso8601_duration_to_human(self.ts, suffix="") if self.ts else None,
|
||||
"saved": self.saved,
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
self.log.debug("scene_status", scene=self.name, scene_time=self.ts, saved=self.saved)
|
||||
|
||||
def set_environment(self, environment: str):
|
||||
"""
|
||||
@@ -1136,11 +1195,20 @@ class Scene(Emitter):
|
||||
Loops through self.history looking for TimePassageMessage and will
|
||||
advance the world state by the amount of time passed for each
|
||||
"""
|
||||
|
||||
# reset time
|
||||
|
||||
self.ts = "PT0S"
|
||||
|
||||
# archived history (if "ts" is set) should provide the base line
|
||||
# find the first archived_history entry from the back that has a ts
|
||||
# and set that as the base line
|
||||
|
||||
if self.archived_history:
|
||||
for i in range(len(self.archived_history) - 1, -1, -1):
|
||||
if self.archived_history[i].get("ts"):
|
||||
self.ts = self.archived_history[i]["ts"]
|
||||
break
|
||||
|
||||
|
||||
for message in self.history:
|
||||
if isinstance(message, TimePassageMessage):
|
||||
self.advance_time(message.ts)
|
||||
@@ -1238,13 +1306,24 @@ class Scene(Emitter):
|
||||
|
||||
# sort self.actors by actor.character.is_player, making is_player the first element
|
||||
self.actors.sort(key=lambda x: x.character.is_player, reverse=True)
|
||||
|
||||
self.active_actor = None
|
||||
self.next_actor = None
|
||||
|
||||
await self.signals["game_loop_start"].send(events.GameLoopStartEvent(scene=self, event_type="game_loop_start"))
|
||||
|
||||
while continue_scene:
|
||||
|
||||
try:
|
||||
|
||||
await self.signals["game_loop"].send(events.GameLoopEvent(scene=self, event_type="game_loop"))
|
||||
|
||||
for actor in self.actors:
|
||||
|
||||
if self.next_actor and actor.character.name != self.next_actor:
|
||||
self.log.debug(f"Skipping actor", actor=actor.character.name, next_actor=self.next_actor)
|
||||
continue
|
||||
|
||||
self.active_actor = actor
|
||||
|
||||
if not actor.character.is_player:
|
||||
@@ -1261,7 +1340,9 @@ class Scene(Emitter):
|
||||
break
|
||||
await self.call_automated_actions()
|
||||
continue
|
||||
|
||||
|
||||
self.saved = False
|
||||
|
||||
# Store the most recent AI Actor
|
||||
self.most_recent_ai_actor = actor
|
||||
|
||||
@@ -1269,6 +1350,9 @@ class Scene(Emitter):
|
||||
emit(
|
||||
"character", item, character=actor.character
|
||||
)
|
||||
|
||||
self.emit_status()
|
||||
|
||||
except TalemateInterrupt:
|
||||
raise
|
||||
except LLMAccuracyError as e:
|
||||
@@ -1299,6 +1383,10 @@ class Scene(Emitter):
|
||||
continue
|
||||
|
||||
await command.execute(message)
|
||||
|
||||
self.saved = False
|
||||
self.emit_status()
|
||||
|
||||
except TalemateInterrupt:
|
||||
raise
|
||||
except LLMAccuracyError as e:
|
||||
@@ -1325,13 +1413,15 @@ class Scene(Emitter):
|
||||
|
||||
return saves_dir
|
||||
|
||||
async def save(self):
|
||||
async def save(self, save_as:bool=False):
|
||||
"""
|
||||
Saves the scene data, conversation history, archived history, and characters to a json file.
|
||||
"""
|
||||
scene = self
|
||||
|
||||
|
||||
|
||||
if save_as:
|
||||
self.filename = None
|
||||
|
||||
if not self.name:
|
||||
self.name = await wait_for_input("Enter scenario name: ")
|
||||
self.filename = "base.json"
|
||||
@@ -1339,6 +1429,13 @@ class Scene(Emitter):
|
||||
elif not self.filename:
|
||||
self.filename = await wait_for_input("Enter save name: ")
|
||||
self.filename = self.filename.replace(" ", "-").lower()+".json"
|
||||
|
||||
if save_as:
|
||||
memory_agent = self.get_helper("memory").agent
|
||||
memory_agent.close_db(self)
|
||||
self.memory_id = str(uuid.uuid4())[:10]
|
||||
await memory_agent.set_db()
|
||||
await self.commit_to_memory()
|
||||
|
||||
saves_dir = self.save_dir
|
||||
|
||||
@@ -1362,6 +1459,7 @@ class Scene(Emitter):
|
||||
"context": scene.context,
|
||||
"world_state": scene.world_state.dict(),
|
||||
"assets": scene.assets.dict(),
|
||||
"memory_id": scene.memory_id,
|
||||
"ts": scene.ts,
|
||||
}
|
||||
|
||||
@@ -1369,8 +1467,35 @@ class Scene(Emitter):
|
||||
|
||||
with open(filepath, "w") as f:
|
||||
json.dump(scene_data, f, indent=2, cls=save.SceneEncoder)
|
||||
|
||||
self.saved = True
|
||||
self.emit_status()
|
||||
|
||||
await asyncio.sleep(0)
|
||||
async def commit_to_memory(self):
|
||||
|
||||
# will recommit scene to long term memory
|
||||
|
||||
memory = self.get_helper("memory").agent
|
||||
memory.drop_db()
|
||||
await memory.set_db()
|
||||
|
||||
for ah in self.archived_history:
|
||||
ts = ah.get("ts", "PT1S")
|
||||
|
||||
if not ah.get("ts"):
|
||||
ah["ts"] = ts
|
||||
|
||||
self.signals["archive_add"].send(
|
||||
events.ArchiveEvent(scene=self, event_type="archive_add", text=ah["text"], ts=ts)
|
||||
)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
for character_name, cs in self.character_states.items():
|
||||
self.set_character_state(character_name, cs)
|
||||
|
||||
for character in self.characters:
|
||||
await character.commit_to_memory(memory)
|
||||
|
||||
|
||||
def reset(self):
|
||||
self.history = []
|
||||
|
||||
@@ -7,7 +7,7 @@ import structlog
|
||||
import isodate
|
||||
import datetime
|
||||
from typing import List
|
||||
|
||||
from thefuzz import fuzz
|
||||
from colorama import Back, Fore, Style, init
|
||||
from PIL import Image
|
||||
|
||||
@@ -345,7 +345,14 @@ def clean_paragraph(paragraph: str) -> str:
|
||||
return cleaned_text
|
||||
|
||||
|
||||
def clean_dialogue(dialogue: str, main_name: str = None) -> str:
|
||||
def clean_message(message: str) -> str:
|
||||
message = message.strip()
|
||||
message = re.sub(r"\s+", " ", message)
|
||||
message = message.replace("(", "*").replace(")", "*")
|
||||
message = message.replace("[", "*").replace("]", "*")
|
||||
return message
|
||||
|
||||
def clean_dialogue_old(dialogue: str, main_name: str = None) -> str:
|
||||
"""
|
||||
Cleans up generated dialogue by removing unnecessary whitespace and newlines.
|
||||
|
||||
@@ -356,12 +363,7 @@ def clean_dialogue(dialogue: str, main_name: str = None) -> str:
|
||||
str: The cleaned dialogue.
|
||||
"""
|
||||
|
||||
def clean_message(message: str) -> str:
|
||||
message = message.strip().strip('"')
|
||||
message = re.sub(r"\s+", " ", message)
|
||||
message = message.replace("(", "*").replace(")", "*")
|
||||
message = message.replace("[", "*").replace("]", "*")
|
||||
return message
|
||||
|
||||
|
||||
cleaned_lines = []
|
||||
current_name = None
|
||||
@@ -373,6 +375,9 @@ def clean_dialogue(dialogue: str, main_name: str = None) -> str:
|
||||
if ":" in line:
|
||||
name, message = line.split(":", 1)
|
||||
name = name.strip()
|
||||
if name != main_name:
|
||||
break
|
||||
|
||||
message = clean_message(message)
|
||||
|
||||
if not message:
|
||||
@@ -391,6 +396,45 @@ def clean_dialogue(dialogue: str, main_name: str = None) -> str:
|
||||
cleaned_dialogue = "\n".join(cleaned_lines)
|
||||
return cleaned_dialogue
|
||||
|
||||
def clean_dialogue(dialogue: str, main_name: str) -> str:
|
||||
|
||||
# keep spliting the dialogue by : with a max count of 1
|
||||
# until the left side is no longer the main name
|
||||
|
||||
cleaned_dialogue = ""
|
||||
|
||||
# find all occurances of : and then walk backwards
|
||||
# and mark the first one that isnt preceded by the {main_name}
|
||||
cutoff = -1
|
||||
log.debug("clean_dialogue", dialogue=dialogue, main_name=main_name)
|
||||
for match in re.finditer(r":", dialogue, re.MULTILINE):
|
||||
index = match.start()
|
||||
check = dialogue[index-len(main_name):index]
|
||||
log.debug("clean_dialogue", check=check, main_name=main_name)
|
||||
if check != main_name:
|
||||
cutoff = index
|
||||
break
|
||||
|
||||
# then split dialogue at the index and return on only
|
||||
# the left side
|
||||
|
||||
if cutoff > -1:
|
||||
log.debug("clean_dialogue", index=index)
|
||||
cleaned_dialogue = dialogue[:index]
|
||||
cleaned_dialogue = strip_partial_sentences(cleaned_dialogue)
|
||||
|
||||
# remove all occurances of "{main_name}: " and then prepend it once
|
||||
|
||||
cleaned_dialogue = cleaned_dialogue.replace(f"{main_name}: ", "")
|
||||
cleaned_dialogue = f"{main_name}: {cleaned_dialogue}"
|
||||
|
||||
return clean_message(cleaned_dialogue)
|
||||
|
||||
dialogue = dialogue.replace(f"{main_name}: ", "")
|
||||
dialogue = f"{main_name}: {dialogue}"
|
||||
|
||||
return clean_message(strip_partial_sentences(dialogue))
|
||||
|
||||
|
||||
def clean_attribute(attribute: str) -> str:
|
||||
"""
|
||||
@@ -442,18 +486,6 @@ def clean_attribute(attribute: str) -> str:
|
||||
return attribute.strip()
|
||||
|
||||
|
||||
def fix_faulty_json(data: str) -> str:
|
||||
# Fix missing commas
|
||||
data = re.sub(r'}\s*{', '},{', data)
|
||||
data = re.sub(r']\s*{', '],{', data)
|
||||
data = re.sub(r'}\s*\[', '},{', data)
|
||||
data = re.sub(r']\s*\[', '],[', data)
|
||||
|
||||
# Fix trailing commas
|
||||
data = re.sub(r',\s*}', '}', data)
|
||||
data = re.sub(r',\s*]', ']', data)
|
||||
|
||||
return data
|
||||
|
||||
def duration_to_timedelta(duration):
|
||||
"""Convert an isodate.Duration object to a datetime.timedelta object."""
|
||||
@@ -540,7 +572,7 @@ def iso8601_duration_to_human(iso_duration, suffix:str=" ago"):
|
||||
elif components:
|
||||
human_str = components[0]
|
||||
else:
|
||||
human_str = "0 Seconds"
|
||||
human_str = "Moments"
|
||||
|
||||
return f"{human_str}{suffix}"
|
||||
|
||||
@@ -594,4 +626,271 @@ def iso8601_correct_duration(duration: str) -> str:
|
||||
if time_component:
|
||||
corrected_duration += "T" + time_component
|
||||
|
||||
return corrected_duration
|
||||
return corrected_duration
|
||||
|
||||
|
||||
def fix_faulty_json(data: str) -> str:
|
||||
# Fix missing commas
|
||||
data = re.sub(r'}\s*{', '},{', data)
|
||||
data = re.sub(r']\s*{', '],{', data)
|
||||
data = re.sub(r'}\s*\[', '},{', data)
|
||||
data = re.sub(r']\s*\[', '],[', data)
|
||||
|
||||
# Fix trailing commas
|
||||
data = re.sub(r',\s*}', '}', data)
|
||||
data = re.sub(r',\s*]', ']', data)
|
||||
|
||||
try:
|
||||
json.loads(data)
|
||||
except json.JSONDecodeError:
|
||||
try:
|
||||
json.loads(data+"}")
|
||||
return data+"}"
|
||||
except json.JSONDecodeError:
|
||||
try:
|
||||
json.loads(data+"]")
|
||||
return data+"]"
|
||||
except json.JSONDecodeError:
|
||||
return data
|
||||
|
||||
return data
|
||||
|
||||
def extract_json(s):
|
||||
"""
|
||||
Extracts a JSON string from the beginning of the input string `s`.
|
||||
|
||||
Parameters:
|
||||
s (str): The input string containing a JSON string at the beginning.
|
||||
|
||||
Returns:
|
||||
str: The extracted JSON string.
|
||||
dict: The parsed JSON object.
|
||||
|
||||
Raises:
|
||||
ValueError: If a valid JSON string is not found.
|
||||
"""
|
||||
open_brackets = 0
|
||||
close_brackets = 0
|
||||
bracket_stack = []
|
||||
json_string_start = None
|
||||
s = s.lstrip() # Strip white spaces and line breaks from the beginning
|
||||
i = 0
|
||||
|
||||
log.debug("extract_json", s=s)
|
||||
|
||||
# Iterate through the string.
|
||||
while i < len(s):
|
||||
# Count the opening and closing curly brackets.
|
||||
if s[i] == '{' or s[i] == '[':
|
||||
bracket_stack.append(s[i])
|
||||
open_brackets += 1
|
||||
if json_string_start is None:
|
||||
json_string_start = i
|
||||
elif s[i] == '}' or s[i] == ']':
|
||||
bracket_stack
|
||||
close_brackets += 1
|
||||
# Check if the brackets match, indicating a complete JSON string.
|
||||
if open_brackets == close_brackets:
|
||||
json_string = s[json_string_start:i+1]
|
||||
# Try to parse the JSON string.
|
||||
return json_string, json.loads(json_string)
|
||||
i += 1
|
||||
|
||||
if json_string_start is None:
|
||||
raise ValueError("No JSON string found.")
|
||||
|
||||
json_string = s[json_string_start:]
|
||||
while bracket_stack:
|
||||
char = bracket_stack.pop()
|
||||
if char == '{':
|
||||
json_string += '}'
|
||||
elif char == '[':
|
||||
json_string += ']'
|
||||
|
||||
json_object = json.loads(json_string)
|
||||
return json_string, json_object
|
||||
|
||||
def dedupe_string(s: str, min_length: int = 32, similarity_threshold: int = 95, debug: bool = False) -> str:
|
||||
|
||||
"""
|
||||
Removes duplicate lines from a string.
|
||||
|
||||
Parameters:
|
||||
s (str): The input string.
|
||||
min_length (int): The minimum length of a line to be checked for duplicates.
|
||||
similarity_threshold (int): The similarity threshold to use when comparing lines.
|
||||
debug (bool): Whether to log debug messages.
|
||||
|
||||
Returns:
|
||||
str: The deduplicated string.
|
||||
"""
|
||||
|
||||
lines = s.split("\n")
|
||||
deduped = []
|
||||
|
||||
for line in lines:
|
||||
stripped_line = line.strip()
|
||||
if len(stripped_line) > min_length:
|
||||
similar_found = False
|
||||
for existing_line in deduped:
|
||||
similarity = fuzz.ratio(stripped_line, existing_line.strip())
|
||||
if similarity >= similarity_threshold:
|
||||
similar_found = True
|
||||
if debug:
|
||||
log.debug("DEDUPE", similarity=similarity, line=line, existing_line=existing_line)
|
||||
break
|
||||
if not similar_found:
|
||||
deduped.append(line)
|
||||
else:
|
||||
deduped.append(line) # Allow shorter strings without dupe check
|
||||
|
||||
return "\n".join(deduped)
|
||||
|
||||
def remove_extra_linebreaks(s: str) -> str:
|
||||
"""
|
||||
Removes extra line breaks from a string.
|
||||
|
||||
Parameters:
|
||||
s (str): The input string.
|
||||
|
||||
Returns:
|
||||
str: The string with extra line breaks removed.
|
||||
"""
|
||||
return re.sub(r"\n{3,}", "\n\n", s)
|
||||
|
||||
def replace_exposition_markers(s:str) -> str:
|
||||
s = s.replace("(", "*").replace(")", "*")
|
||||
s = s.replace("[", "*").replace("]", "*")
|
||||
return s
|
||||
|
||||
|
||||
def ensure_dialog_format(line:str, talking_character:str=None) -> str:
|
||||
|
||||
#if "*" not in line and '"' not in line:
|
||||
# if talking_character:
|
||||
# line = line[len(talking_character)+1:].lstrip()
|
||||
# return f"{talking_character}: \"{line}\""
|
||||
# return f"\"{line}\""
|
||||
#
|
||||
|
||||
if talking_character:
|
||||
line = line[len(talking_character)+1:].lstrip()
|
||||
|
||||
lines = []
|
||||
|
||||
for _line in line.split("\n"):
|
||||
_line = ensure_dialog_line_format(_line)
|
||||
|
||||
lines.append(_line)
|
||||
|
||||
if len(lines) > 1:
|
||||
line = "\n".join(lines)
|
||||
else:
|
||||
line = lines[0]
|
||||
|
||||
if talking_character:
|
||||
line = f"{talking_character}: {line}"
|
||||
|
||||
return line
|
||||
|
||||
|
||||
def ensure_dialog_line_format(line:str):
|
||||
|
||||
"""
|
||||
a Python function that standardizes the formatting of dialogue and action/thought
|
||||
descriptions in text strings. This function is intended for use in a text-based
|
||||
game where spoken dialogue is encased in double quotes (" ") and actions/thoughts are
|
||||
encased in asterisks (* *). The function must correctly format strings, ensuring that
|
||||
each spoken sentence and action/thought is properly encased
|
||||
"""
|
||||
|
||||
|
||||
i = 0
|
||||
|
||||
segments = []
|
||||
segment = None
|
||||
segment_open = None
|
||||
|
||||
for i in range(len(line)):
|
||||
|
||||
|
||||
c = line[i]
|
||||
|
||||
#print("segment_open", segment_open)
|
||||
#print("segment", segment)
|
||||
|
||||
if c in ['"', '*']:
|
||||
if segment_open == c:
|
||||
# open segment is the same as the current character
|
||||
# closing
|
||||
segment_open = None
|
||||
segment += c
|
||||
segments += [segment.strip()]
|
||||
segment = None
|
||||
elif segment_open is not None and segment_open != c:
|
||||
# open segment is not the same as the current character
|
||||
# opening - close the current segment and open a new one
|
||||
segments += [segment.strip()]
|
||||
segment_open = c
|
||||
segment = c
|
||||
elif segment_open is None:
|
||||
# we're opening a segment
|
||||
segment_open = c
|
||||
segment = c
|
||||
else:
|
||||
if segment_open is None:
|
||||
segment_open = "unclassified"
|
||||
segment = c
|
||||
else:
|
||||
segment += c
|
||||
|
||||
if segment is not None:
|
||||
segments += [segment.strip()]
|
||||
|
||||
for i in range(len(segments)):
|
||||
segment = segments[i]
|
||||
if segment in ['"', '*']:
|
||||
if i > 0:
|
||||
prev_segment = segments[i-1]
|
||||
if prev_segment[-1] not in ['"', '*']:
|
||||
segments[i-1] = f"{prev_segment}{segment}"
|
||||
segments[i] = ""
|
||||
continue
|
||||
|
||||
for i in range(len(segments)):
|
||||
segment = segments[i]
|
||||
|
||||
if not segment:
|
||||
continue
|
||||
|
||||
if segment[0] == "*" and segment[-1] != "*":
|
||||
segment += "*"
|
||||
elif segment[-1] == "*" and segment[0] != "*":
|
||||
segment = "*" + segment
|
||||
elif segment[0] == '"' and segment[-1] != '"':
|
||||
segment += '"'
|
||||
elif segment[-1] == '"' and segment[0] != '"':
|
||||
segment = '"' + segment
|
||||
elif segment[0] in ['"', '*'] and segment[-1] == segment[0]:
|
||||
continue
|
||||
|
||||
segments[i] = segment
|
||||
|
||||
for i in range(len(segments)):
|
||||
segment = segments[i]
|
||||
if not segment or segment[0] in ['"', '*']:
|
||||
continue
|
||||
|
||||
prev_segment = segments[i-1] if i > 0 else None
|
||||
next_segment = segments[i+1] if i < len(segments)-1 else None
|
||||
|
||||
if prev_segment and prev_segment[-1] == '"':
|
||||
segments[i] = f"*{segment}*"
|
||||
elif prev_segment and prev_segment[-1] == '*':
|
||||
segments[i] = f"\"{segment}\""
|
||||
elif next_segment and next_segment[0] == '"':
|
||||
segments[i] = f"*{segment}*"
|
||||
elif next_segment and next_segment[0] == '*':
|
||||
segments[i] = f"\"{segment}\""
|
||||
|
||||
return " ".join(segment for segment in segments if segment)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from pydantic import BaseModel
|
||||
from talemate.emit import emit
|
||||
import structlog
|
||||
from typing import Union
|
||||
|
||||
import talemate.instance as instance
|
||||
from talemate.prompts import Prompt
|
||||
@@ -9,11 +10,11 @@ import talemate.automated_action as automated_action
|
||||
log = structlog.get_logger("talemate")
|
||||
|
||||
class CharacterState(BaseModel):
|
||||
snapshot: str = None
|
||||
emotion: str = None
|
||||
snapshot: Union[str, None] = None
|
||||
emotion: Union[str, None] = None
|
||||
|
||||
class ObjectState(BaseModel):
|
||||
snapshot: str = None
|
||||
snapshot: Union[str, None] = None
|
||||
|
||||
class WorldState(BaseModel):
|
||||
|
||||
@@ -24,27 +25,30 @@ class WorldState(BaseModel):
|
||||
items: dict[str, ObjectState] = {}
|
||||
|
||||
# location description
|
||||
location: str = None
|
||||
location: Union[str, None] = None
|
||||
|
||||
@property
|
||||
def agent(self):
|
||||
return instance.get_agent("summarizer")
|
||||
return instance.get_agent("world_state")
|
||||
|
||||
@property
|
||||
def pretty_json(self):
|
||||
return self.json(indent=2)
|
||||
return self.model_dump_json(indent=2)
|
||||
|
||||
@property
|
||||
def as_list(self):
|
||||
return self.render().as_list
|
||||
|
||||
|
||||
def reset(self):
|
||||
self.characters = {}
|
||||
self.items = {}
|
||||
self.location = None
|
||||
|
||||
def emit(self, status="update"):
|
||||
emit("world_state", status=status, data=self.dict())
|
||||
|
||||
async def request_update(self, initial_only:bool=False):
|
||||
|
||||
|
||||
|
||||
if initial_only and self.characters:
|
||||
self.emit()
|
||||
return
|
||||
@@ -57,19 +61,94 @@ class WorldState(BaseModel):
|
||||
self.emit()
|
||||
raise e
|
||||
|
||||
previous_characters = self.characters
|
||||
previous_items = self.items
|
||||
scene = self.agent.scene
|
||||
character_names = scene.character_names
|
||||
self.characters = {}
|
||||
self.items = {}
|
||||
|
||||
for character in world_state.get("characters", []):
|
||||
self.characters[character["name"]] = CharacterState(**character)
|
||||
for character_name, character in world_state.get("characters", {}).items():
|
||||
|
||||
# character name may not always come back exactly as we have
|
||||
# it defined in the scene. We assign the correct name by checking occurences
|
||||
# of both names in each other.
|
||||
|
||||
if character_name not in character_names:
|
||||
for _character_name in character_names:
|
||||
if _character_name.lower() in character_name.lower() or character_name.lower() in _character_name.lower():
|
||||
log.debug("world_state adjusting character name", from_name=character_name, to_name=_character_name)
|
||||
character_name = _character_name
|
||||
break
|
||||
|
||||
if not character:
|
||||
continue
|
||||
|
||||
# if emotion is not set, see if a previous state exists
|
||||
# and use that emotion
|
||||
|
||||
if "emotion" not in character:
|
||||
log.debug("emotion not set", character_name=character_name, character=character, characters=previous_characters)
|
||||
if character_name in previous_characters:
|
||||
character["emotion"] = previous_characters[character_name].emotion
|
||||
|
||||
self.characters[character_name] = CharacterState(**character)
|
||||
log.debug("world_state", character=character)
|
||||
|
||||
for item in world_state.get("items", []):
|
||||
self.items[item["name"]] = ObjectState(**item)
|
||||
for item_name, item in world_state.get("items", {}).items():
|
||||
if not item:
|
||||
continue
|
||||
self.items[item_name] = ObjectState(**item)
|
||||
log.debug("world_state", item=item)
|
||||
|
||||
self.emit()
|
||||
|
||||
await self.persist()
|
||||
self.emit()
|
||||
|
||||
async def persist(self):
|
||||
|
||||
memory = instance.get_agent("memory")
|
||||
world_state = instance.get_agent("world_state")
|
||||
|
||||
# first we check if any of the characters were refered
|
||||
# to with an alias
|
||||
|
||||
states = []
|
||||
scene = self.agent.scene
|
||||
|
||||
for character_name in self.characters.keys():
|
||||
states.append(
|
||||
{
|
||||
"text": f"{character_name}: {self.characters[character_name].snapshot}",
|
||||
"id": f"{character_name}.world_state.snapshot",
|
||||
"meta": {
|
||||
"typ": "world_state",
|
||||
"character": character_name,
|
||||
"ts": scene.ts,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
for item_name in self.items.keys():
|
||||
states.append(
|
||||
{
|
||||
"text": f"{item_name}: {self.items[item_name].snapshot}",
|
||||
"id": f"{item_name}.world_state.snapshot",
|
||||
"meta": {
|
||||
"typ": "world_state",
|
||||
"item": item_name,
|
||||
"ts": scene.ts,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
log.debug("world_state.persist", states=states)
|
||||
|
||||
if not states:
|
||||
return
|
||||
|
||||
await memory.add_many(states)
|
||||
|
||||
|
||||
async def request_update_inline(self):
|
||||
|
||||
@@ -93,11 +172,4 @@ class WorldState(BaseModel):
|
||||
"items": self.items,
|
||||
"location": self.location,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@automated_action.register("world_state", frequency=5, call_initially=False)
|
||||
class WorldStateAction(automated_action.AutomatedAction):
|
||||
async def action(self):
|
||||
await self.scene.world_state.request_update()
|
||||
return True
|
||||
)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user