added tasks to act as user or assistant

This commit is contained in:
Yannic Kilcher
2022-12-16 10:36:40 +01:00
parent 084668294b
commit 0f2a8971e5
3 changed files with 145 additions and 24 deletions
+34 -6
View File
@@ -16,9 +16,9 @@ router = APIRouter()
def generate_task(request: protocol_schema.TaskRequest) -> protocol_schema.Task:
match (request.type):
case protocol_schema.TaskRequestType.generic:
logger.info("Frontend requested a generic task.")
while request.type == protocol_schema.TaskRequestType.generic:
case protocol_schema.TaskRequestType.random:
logger.info("Frontend requested a random task.")
while request.type == protocol_schema.TaskRequestType.random:
request.type = random.choice(list(protocol_schema.TaskRequestType)).value
return generate_task(request)
case protocol_schema.TaskRequestType.summarize_story:
@@ -38,6 +38,34 @@ def generate_task(request: protocol_schema.TaskRequest) -> protocol_schema.Task:
task = protocol_schema.InitialPromptTask(
hint="Ask the assistant about a current event." # this is optional
)
case protocol_schema.TaskRequestType.user_reply:
logger.info("Generating a UserReplyTask.")
task = protocol_schema.UserReplyTask(
conversation=protocol_schema.Conversation(
messages=[
protocol_schema.ConversationMessage(
text="Hey, assistant, what's going on in the world?",
is_assistant=False,
),
protocol_schema.ConversationMessage(
text="I'm not sure I understood correctly, could you rephrase that?",
is_assistant=True,
),
],
)
)
case protocol_schema.TaskRequestType.assistant_reply:
logger.info("Generating a AssistantReplyTask.")
task = protocol_schema.AssistantReplyTask(
conversation=protocol_schema.Conversation(
messages=[
protocol_schema.ConversationMessage(
text="Hey, assistant, write me an English essay about water.",
is_assistant=False,
),
],
)
)
case _:
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
@@ -45,7 +73,7 @@ def generate_task(request: protocol_schema.TaskRequest) -> protocol_schema.Task:
)
logger.info(f"Generated {task=}.")
if request.user is not None:
task.addressed_users = [request.user]
task.addressed_user = request.user
return task
@@ -122,7 +150,7 @@ def post_interaction(
# here we would store the text reply in the database
return protocol_schema.TaskDone(
reply_to_post_id=interaction.user_post_id,
addressed_users=[interaction.user],
addressed_user=interaction.user,
)
case protocol_schema.PostRating:
logger.info(
@@ -132,7 +160,7 @@ def post_interaction(
# here we would store the rating in the database
return protocol_schema.TaskDone(
reply_to_post_id=interaction.post_id,
addressed_users=[interaction.user],
addressed_user=interaction.user,
)
case _:
raise HTTPException(
+38 -5
View File
@@ -8,21 +8,37 @@ from pydantic import BaseModel
class TaskRequestType(str, enum.Enum):
generic = "generic"
random = "random"
summarize_story = "summarize_story"
rate_summary = "rate_summary"
initial_prompt = "initial_prompt"
user_reply = "user_reply"
assistant_reply = "assistant_reply"
class User(BaseModel):
id: str
name: str
display_name: str
auth_method: Literal["discord", "local"]
class ConversationMessage(BaseModel):
"""Represents a message in a conversation between the user and the assistant."""
text: str
is_assistant: bool
class Conversation(BaseModel):
"""Represents a conversation between the user and the assistant."""
messages: list[ConversationMessage] = []
class TaskRequest(BaseModel):
"""The frontend asks the backend for a task."""
type: TaskRequestType = TaskRequestType.generic
type: TaskRequestType = TaskRequestType.random
user: Optional[User] = None
@@ -31,7 +47,7 @@ class Task(BaseModel):
id: UUID = pydantic.Field(default_factory=uuid4)
type: str
addressed_users: Optional[list[User]] = None
addressed_user: Optional[User] = None
class TaskResponse(BaseModel):
@@ -91,6 +107,21 @@ class InitialPromptTask(Task):
)
class UserReplyTask(Task):
"""A task to prompt the user to submit a reply to the assistant."""
type: Literal["user_reply"] = "user_reply"
conversation: Conversation # the conversation so far
hint: str | None = None # e.g. "Try to ask for clarification."
class AssistantReplyTask(Task):
"""A task to prompt the user to act as the assistant."""
type: Literal["assistant_reply"] = "assistant_reply"
conversation: Conversation # the conversation so far
class TaskDone(Task):
"""Signals to the frontend that the task is done."""
@@ -99,10 +130,12 @@ class TaskDone(Task):
AnyTask = Union[
TaskDone,
SummarizeStoryTask,
RateSummaryTask,
InitialPromptTask,
TaskDone,
UserReplyTask,
AssistantReplyTask,
]