Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: API method /chat/{chat_id}/question is broken in 0.0.300 #3096

Closed
wonderu opened this issue Aug 26, 2024 · 4 comments
Closed

[Bug]: API method /chat/{chat_id}/question is broken in 0.0.300 #3096

wonderu opened this issue Aug 26, 2024 · 4 comments
Labels
area: backend Related to backend functionality or under the /backend directory bug Something isn't working

Comments

@wonderu
Copy link

wonderu commented Aug 26, 2024

What happened?

Send request
curl -X 'POST' \ 'https://api.quivr.app/chat/<chat_id>/question?brain_id=<brain_id>' \ -H 'accept: application/json' \ -H 'Authorization: Bearer <token>' \ -H 'Content-Type: application/json' \ -d '{ "question": "what do you know?", "brain_id": "<brain_id>" }'
Got
500 Internal Server Error

The problem is in https://github.com/QuivrHQ/quivr/blob/main/backend/api/quivr_api/modules/chat/repository/chats.py#L84: system can't serialize Document object

Relevant log output

No response

Twitter / LinkedIn details

No response

@wonderu wonderu added the bug Something isn't working label Aug 26, 2024
Copy link

linear bot commented Aug 26, 2024

@dosubot dosubot bot added the area: backend Related to backend functionality or under the /backend directory label Aug 26, 2024
Copy link

dosubot bot commented Aug 26, 2024

Hello, @wonderu! I'm here to help you with any bugs, questions, or contributions. Let's tackle this together!

A similar issue was reported and resolved in a closed issue: Issue #952. The problem was fixed by a pull request: fix(chatMessages): Fix error on answering question [1].

The 500 Internal Server Error when sending a POST request to the /chat/{chat_id}/question endpoint in version 0.0.300 is likely due to a serialization issue with the Document object in chats.py. To resolve this, ensure that the Document class and all its attributes are serializable. You can implement a custom serialization method if necessary.

Here is an example of how you might define a Document class with a custom serialization method:

from pydantic import BaseModel
from typing import Any, Dict

class Document(BaseModel):
    id: str
    content: str
    # Add other fields as necessary

    def to_dict(self) -> Dict[str, Any]:
        return {
            "id": self.id,
            "content": self.content,
            # Convert other fields to a serializable format
        }

# Example usage in the endpoint
@chat_router.post("/chat/{chat_id}/question", dependencies=[Depends(AuthBearer())], tags=["Chat"])
async def create_question_handler(
    request: Request,
    chat_question: ChatQuestion,
    chat_id: UUID,
    current_user: UserIdentityDep,
    chat_service: ChatServiceDep,
    model_service: ModelServiceDep,
    brain_id: Annotated[UUID | None, Query()] = None,
):
    # Your existing logic here...

    try:
        # Your existing logic here...

        chat_answer = await service.generate_answer(chat_question.question)

        # Ensure the chat_answer is serializable
        if isinstance(chat_answer, Document):
            chat_answer = chat_answer.to_dict()

        return chat_answer

    except AssertionError:
        raise HTTPException(
            status_code=422,
            detail="unprocessable entity",
        )
    except HTTPException as e:
        raise e

Additionally, ensure that the ParsedRAGResponse and RAGResponseMetadata objects are also serializable. You can use the model_dump method provided in the chat_llm_service.py to convert these objects to a serializable format:

# Example usage in the ChatLLMService class
def save_answer(self, question: str, answer: ParsedRAGResponse):
    logger.info(
        f"Saving answer for chat {self.chat_id} with model {self.model_to_use}"
    )
    logger.info(answer)
    return self.chat_service.update_chat_history(
        CreateChatHistory(
            **{
                "chat_id": self.chat_id,
                "user_message": question,
                "assistant": answer.answer,
                "brain_id": None,
                "prompt_id": None,
                "metadata": answer.metadata.model_dump() if answer.metadata else {},
            }
        )
    )

By ensuring that all objects returned in the response are serializable, you can resolve the 500 Internal Server Error and handle the serialization issue with the Document object in chats.py [2][3][4].

To continue talking to Dosu, mention @dosu.

@wonderu
Copy link
Author

wonderu commented Aug 26, 2024

duplicated #3082

@huynhbaobk
Copy link

I still got error from rag_service.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: backend Related to backend functionality or under the /backend directory bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants
@wonderu @huynhbaobk and others