Skip to content

Commit

Permalink
Wraps honcho operations in promise handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
bLopata committed Oct 18, 2024
1 parent c044b58 commit 42f503c
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions api/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@


@router.post("/stream")
async def stream(inp: schemas.ConversationInput, background_tasks: BackgroundTasks):
"""Stream the response to the user, currently only used by the Web UI and has integration to be able to use Honcho if not anonymous"""
async def stream(inp: schemas.ConversationInput):
try:
user = honcho.apps.users.get_or_create(app_id=app.id, name=inp.user_id)

def convo_turn():
async def convo_turn():
thought = ""
response = ""
try:
Expand Down Expand Up @@ -50,8 +49,7 @@ def convo_turn():
yield f"Error: {str(e)}"
return

background_tasks.add_task(
create_messages_and_metamessages,
await create_messages_and_metamessages(
app.id, user.id, inp.conversation_id, inp.message, thought, response
)

Expand All @@ -69,45 +67,24 @@ def convo_turn():
content={"error": "internal_server_error", "message": "An internal server error has occurred."}
)


@router.get("/thought/{message_id}")
async def get_thought(conversation_id: str, message_id: str, user_id: str):
try:
user = honcho.apps.users.get_or_create(app_id=app.id, name=user_id)
thought = honcho.apps.users.sessions.metamessages.list(
session_id=conversation_id,
app_id=app.id,
user_id=user.id,
message_id=message_id,
metamessage_type="thought",
)
# In practice, there should only be one thought per message
return {"thought": thought.items[0].content if thought.items else None}
except Exception as e:
logging.error(f"An error occurred: {str(e)}")
return JSONResponse(
status_code=500,
content={"error": "internal_server_error", "message": "An internal server error has occurred."}
)


def create_messages_and_metamessages(app_id, user_id, conversation_id, user_message, thought, ai_response):
async def create_messages_and_metamessages(app_id, user_id, conversation_id, user_message, thought, ai_response):
try:
honcho.apps.users.sessions.messages.create(
# These operations will use the DB layer's built-in retry logic
await honcho.apps.users.sessions.messages.create(
is_user=True,
session_id=str(conversation_id),
app_id=app_id,
user_id=user_id,
content=user_message,
)
new_ai_message = honcho.apps.users.sessions.messages.create(
new_ai_message = await honcho.apps.users.sessions.messages.create(
is_user=False,
session_id=str(conversation_id),
app_id=app_id,
user_id=user_id,
content=ai_response,
)
honcho.apps.users.sessions.metamessages.create(
await honcho.apps.users.sessions.metamessages.create(
app_id=app_id,
session_id=str(conversation_id),
user_id=user_id,
Expand All @@ -116,4 +93,26 @@ def create_messages_and_metamessages(app_id, user_id, conversation_id, user_mess
content=thought,
)
except Exception as e:
logging.error(f"Error in background task: {str(e)}")
logging.error(f"Error in create_messages_and_metamessages: {str(e)}")
raise # Re-raise the exception to be handled by the caller


@router.get("/thought/{message_id}")
async def get_thought(conversation_id: str, message_id: str, user_id: str):
try:
user = honcho.apps.users.get_or_create(app_id=app.id, name=user_id)
thought = honcho.apps.users.sessions.metamessages.list(
session_id=conversation_id,
app_id=app.id,
user_id=user.id,
message_id=message_id,
metamessage_type="thought",
)
# In practice, there should only be one thought per message
return {"thought": thought.items[0].content if thought.items else None}
except Exception as e:
logging.error(f"An error occurred: {str(e)}")
return JSONResponse(
status_code=500,
content={"error": "internal_server_error", "message": "An internal server error has occurred."}
)

0 comments on commit 42f503c

Please sign in to comment.