def send_msg(self, session: Session, text: str): actions = self.api.add_story(session.adventure_id, text) history = [ History.from_user(**actions[0]), History.from_ai(**actions[1]), ] session.update([ Session.history.set(Session.history.append(history)), ]) session.refresh() return session
def get_session_or_404(session_id): try: session = Session.get(session_id) except Session.DoesNotExist: return Response(status_code=404, body=f'can not find {session_id} session') return session
def create_session(self, name: str, scene_name: str) -> Session: session = Session() session.name = name session.adventure_id = self.api.create_session()['id'] scene = Scene.get(scene_name) session.scene = scene_name actions = self.api.add_story(session.adventure_id, scene.text) session.history = [ History.from_scene(**actions[0]), History.from_scene_ai(**actions[1]), ] session.save() return session
def create_session(self, name: str, prompt_name: str) -> Session: session = Session() session.name = name session.adventure_id = self.api.create_session()['id'] prompt = Prompt.get(prompt_name) session.prompt = prompt_name actions = self.api.add_story(session.adventure_id, prompt.text) session.history = [ History.from_prompt(**actions[0]), History.from_prompt_ai(**actions[1]), ] session.save() return session
def list_sessions(): return {"results": [session.to_dict() for session in Session.scan()]}