parent
22758f222c
commit
dc891a8cac
@ -1,85 +0,0 @@
|
||||
import json
|
||||
from typing import List
|
||||
|
||||
|
||||
class PromptGenerator:
|
||||
"""A class for generating custom prompt strings."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize the PromptGenerator object."""
|
||||
self.constraints: List[str] = []
|
||||
self.commands: List[str] = []
|
||||
self.resources: List[str] = []
|
||||
self.performance_evaluation: List[str] = []
|
||||
self.response_format = {
|
||||
"thoughts": {
|
||||
"text": "thought",
|
||||
"reasoning": "reasoning",
|
||||
"plan": (
|
||||
"- short bulleted\n- list that conveys\n-"
|
||||
" long-term plan"
|
||||
),
|
||||
"criticism": "constructive self-criticism",
|
||||
"speak": "thoughts summary to say to user",
|
||||
},
|
||||
"command": {
|
||||
"name": "command name",
|
||||
"args": {"arg name": "value"},
|
||||
},
|
||||
}
|
||||
|
||||
def add_constraint(self, constraint: str) -> None:
|
||||
"""
|
||||
Add a constraint to the constraints list.
|
||||
|
||||
Args:
|
||||
constraint (str): The constraint to be added.
|
||||
"""
|
||||
self.constraints.append(constraint)
|
||||
|
||||
def add_command(self, command: str) -> None:
|
||||
"""
|
||||
Add a command to the commands list.
|
||||
|
||||
Args:
|
||||
command (str): The command to be added.
|
||||
"""
|
||||
self.commands.append(command)
|
||||
|
||||
def add_resource(self, resource: str) -> None:
|
||||
"""
|
||||
Add a resource to the resources list.
|
||||
|
||||
Args:
|
||||
resource (str): The resource to be added.
|
||||
"""
|
||||
self.resources.append(resource)
|
||||
|
||||
def add_performance_evaluation(self, evaluation: str) -> None:
|
||||
"""
|
||||
Add a performance evaluation item to the performance_evaluation list.
|
||||
|
||||
Args:
|
||||
evaluation (str): The evaluation item to be added.
|
||||
"""
|
||||
self.performance_evaluation.append(evaluation)
|
||||
|
||||
def generate_prompt_string(self) -> str:
|
||||
"""Generate a prompt string.
|
||||
|
||||
Returns:
|
||||
str: The generated prompt string.
|
||||
"""
|
||||
formatted_response_format = json.dumps(
|
||||
self.response_format, indent=4
|
||||
)
|
||||
prompt_string = (
|
||||
f"Constraints:\n{''.join(self.constraints)}\n\nCommands:\n{''.join(self.commands)}\n\nResources:\n{''.join(self.resources)}\n\nPerformance"
|
||||
f" Evaluation:\n{''.join(self.performance_evaluation)}\n\nYou"
|
||||
" should only respond in JSON format as described below"
|
||||
" \nResponse Format:"
|
||||
f" \n{formatted_response_format} \nEnsure the response"
|
||||
" can be parsed by Python json.loads"
|
||||
)
|
||||
|
||||
return prompt_string
|
@ -1,159 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import abstractmethod
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
class Message:
|
||||
"""
|
||||
The base abstract Message class.
|
||||
Messages are the inputs and outputs of ChatModels.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, content: str, role: str, additional_kwargs: dict = None
|
||||
):
|
||||
self.content = content
|
||||
self.role = role
|
||||
self.additional_kwargs = (
|
||||
additional_kwargs if additional_kwargs else {}
|
||||
)
|
||||
|
||||
@abstractmethod
|
||||
def get_type(self) -> str:
|
||||
pass
|
||||
|
||||
|
||||
class HumanMessage(Message):
|
||||
"""
|
||||
A Message from a human.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
content: str,
|
||||
role: str = "Human",
|
||||
additional_kwargs: dict = None,
|
||||
example: bool = False,
|
||||
):
|
||||
super().__init__(content, role, additional_kwargs)
|
||||
self.example = example
|
||||
|
||||
def get_type(self) -> str:
|
||||
return "human"
|
||||
|
||||
|
||||
class AIMessage(Message):
|
||||
"""
|
||||
A Message from an AI.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
content: str,
|
||||
role: str = "AI",
|
||||
additional_kwargs: dict = None,
|
||||
example: bool = False,
|
||||
):
|
||||
super().__init__(content, role, additional_kwargs)
|
||||
self.example = example
|
||||
|
||||
def get_type(self) -> str:
|
||||
return "ai"
|
||||
|
||||
|
||||
class SystemMessage(Message):
|
||||
"""
|
||||
A Message for priming AI behavior, usually passed in as the first of a sequence
|
||||
of input messages.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
content: str,
|
||||
role: str = "System",
|
||||
additional_kwargs: dict = None,
|
||||
):
|
||||
super().__init__(content, role, additional_kwargs)
|
||||
|
||||
def get_type(self) -> str:
|
||||
return "system"
|
||||
|
||||
|
||||
class FunctionMessage(Message):
|
||||
"""
|
||||
A Message for passing the result of executing a function back to a model.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
content: str,
|
||||
role: str = "Function",
|
||||
name: str = None,
|
||||
additional_kwargs: dict = None,
|
||||
):
|
||||
super().__init__(content, role, additional_kwargs)
|
||||
self.name = name
|
||||
|
||||
def get_type(self) -> str:
|
||||
return "function"
|
||||
|
||||
|
||||
class ChatMessage(Message):
|
||||
"""
|
||||
A Message that can be assigned an arbitrary speaker (i.e. role).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self, content: str, role: str, additional_kwargs: dict = None
|
||||
):
|
||||
super().__init__(content, role, additional_kwargs)
|
||||
|
||||
def get_type(self) -> str:
|
||||
return "chat"
|
||||
|
||||
|
||||
def get_buffer_string(
|
||||
messages: Sequence[Message],
|
||||
human_prefix: str = "Human",
|
||||
ai_prefix: str = "AI",
|
||||
) -> str:
|
||||
string_messages = []
|
||||
for m in messages:
|
||||
message = f"{m.role}: {m.content}"
|
||||
if (
|
||||
isinstance(m, AIMessage)
|
||||
and "function_call" in m.additional_kwargs
|
||||
):
|
||||
message += f"{m.additional_kwargs['function_call']}"
|
||||
string_messages.append(message)
|
||||
|
||||
return "\n".join(string_messages)
|
||||
|
||||
|
||||
def message_to_dict(message: Message) -> dict:
|
||||
return {"type": message.get_type(), "data": message.__dict__}
|
||||
|
||||
|
||||
def messages_to_dict(messages: Sequence[Message]) -> list[dict]:
|
||||
return [message_to_dict(m) for m in messages]
|
||||
|
||||
|
||||
def message_from_dict(message: dict) -> Message:
|
||||
_type = message["type"]
|
||||
if _type == "human":
|
||||
return HumanMessage(**message["data"])
|
||||
elif _type == "ai":
|
||||
return AIMessage(**message["data"])
|
||||
elif _type == "system":
|
||||
return SystemMessage(**message["data"])
|
||||
elif _type == "chat":
|
||||
return ChatMessage(**message["data"])
|
||||
elif _type == "function":
|
||||
return FunctionMessage(**message["data"])
|
||||
else:
|
||||
raise ValueError(f"Got unexpected message type: {_type}")
|
||||
|
||||
|
||||
def messages_from_dict(messages: list[dict]) -> list[Message]:
|
||||
return [message_from_dict(m) for m in messages]
|
@ -1,19 +1,14 @@
|
||||
IMAGE_ENRICHMENT_PROMPT = (
|
||||
"Create a concise and effective image generation prompt within"
|
||||
" 400 characters or less, "
|
||||
"based on Stable Diffusion and Dalle best practices. Starting"
|
||||
" prompt: \n\n'"
|
||||
# f"{prompt}'\n\n"
|
||||
"Improve the prompt with any applicable details or keywords by"
|
||||
" considering the following aspects: \n"
|
||||
"1. Subject details (like actions, emotions, environment) \n"
|
||||
"2. Artistic style (such as surrealism, hyperrealism) \n"
|
||||
"3. Medium (digital painting, oil on canvas) \n"
|
||||
"4. Color themes and lighting (like warm colors, cinematic"
|
||||
" lighting) \n"
|
||||
"5. Composition and framing (close-up, wide-angle) \n"
|
||||
"6. Additional elements (like a specific type of background,"
|
||||
" weather conditions) \n"
|
||||
"7. Any other artistic or thematic details that can make the"
|
||||
" image more vivid and compelling."
|
||||
)
|
||||
IMAGE_ENRICHMENT_PROMPT = """
|
||||
Create a concise and effective image generation prompt within 400 characters or less, based on Stable Diffusion and Dalle best practices.
|
||||
|
||||
Improve the prompt with any applicable details or keywords by considering the following aspects:
|
||||
1. Subject details (like actions, emotions, environment)
|
||||
2. Artistic style (such as surrealism, hyperrealism)
|
||||
3. Medium (digital painting, oil on canvas)
|
||||
4. Color themes and lighting (like warm colors, cinematic lighting)
|
||||
5. Composition and framing (close-up, wide-angle)
|
||||
6. Additional elements (like a specific type of background, weather conditions)
|
||||
7. Any other artistic or thematic details that can make the image more vivid and compelling.
|
||||
|
||||
|
||||
"""
|
||||
|
Loading…
Reference in new issue