langchain-ai / opengpts

MIT License
6.3k stars 827 forks source link

Update chatbot and RAG assistant to use `StateGraph` in the backend #305

Closed andrewnguonly closed 2 months ago

andrewnguonly commented 2 months ago

Summary

As a demonstration, get_chatbot_executor() and get_retrieval_executor() are updated to use StateGraph. Moving forward, when using the RAG assistant (assistantType === "chat_retrieval"), the POST /runs and /runs/stream API must be called with the follow request body:

{
    // input is an object instead of a list
    "input": {
        // messages key must be present
        "messages": [
            {
                "content": "hello!", // content key must be present
                "role": "human",     // role key must be present
                ...
            }
        ],
        ...
    },
    ...
}

All other assistant types require the existing request body format (e.g. "input": [{...}]).

Implementation

  1. The MessageGraph in get_chatbot_executor() is migrated to StateGraph.
  2. The MessageGraph in get_retrieval_executor() is migrated to StateGraph. The graph now accepts a TypedDict for the state. The interfaces for the corresponding nodes are updated accordingly.
  3. API GET /threads/<tid>/state (storage layer) is updated to retrieve the graph state based on the assistant type.
  4. Frontend is updated to call the API POST /runs/stream with the correct request body format based on the assistant type.

To Do

andrewnguonly commented 2 months ago

Thanks for the assist @nfcampos 😄