ajndkr / lanarky

The web framework for building LLM microservices
https://lanarky.ajndkr.com/
MIT License
976 stars 74 forks source link

Issue with Gradio Integration in conversation_retrieval in examples/app #120

Closed ImanLakjiri closed 11 months ago

ImanLakjiri commented 1 year ago

Issue Description

Script : lanarky/examples/app/conversational_retrieval.py

When launching a FastAPI application using Lanarky with Gradio integration and accessing the Gradio interface at http://localhost:8000/gradio, I encountered an issue. This issue occurs after modifying the code to include Gradio, while FastAPI cURL requests work well, and Gradio works well with another script (for example : retrieval_qa_w_sources.py). The specific error message observed is:

INFO: 127.0.0.1:48912 - "POST /chat HTTP/1.1" 422 Unprocessable Entity
HTTP error occurred: 422 Client Error: Unprocessable Entity for url: http://localhost:8000/chat

Steps I followed :

  1. Launch the FastAPI application using the following command:

    uvicorn app.conversational_retrieval:app --reload
  2. Open a web browser and navigate to http://localhost:8000/gradio.

  3. Observe the issue with the "conversational_retrieval" example in the Gradio interface.

Expected Behavior

The "conversational_retrieval" example in the Gradio interface should work as expected, similar to how it works with other scripts such as retrieval_qa_w_sources.py.

Actual Behavior

The Gradio interface for the "conversational_retrieval" example encounters a "422 Unprocessable Entity" error.

Code

The code used to reproduce the issue is as follows:

from dotenv import load_dotenv
from fastapi import FastAPI,Request
from fastapi.templating import Jinja2Templates
from langchain.chains import ConversationalRetrievalChain, LLMChain
from langchain.chains.conversational_retrieval.prompts import CONDENSE_QUESTION_PROMPT
from langchain.chains.question_answering import load_qa_chain
from langchain.chat_models import ChatOpenAI
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import ElasticVectorSearch
from lanarky.testing import mount_gradio_app
from lanarky import LangchainRouter

load_dotenv()

def create_chain():
    db = ElasticVectorSearch(
        elasticsearch_url="http://localhost:9200",
        index_name="elastic-index",
        embedding=OpenAIEmbeddings(),
    )
    question_generator = LLMChain(
        llm=ChatOpenAI(
            temperature=0,
            streaming=True,
        ),
        prompt=CONDENSE_QUESTION_PROMPT,
    )
    doc_chain = load_qa_chain(
        llm=ChatOpenAI(
            temperature=0,
            streaming=True,
        ),
        chain_type="stuff",
    )

    return ConversationalRetrievalChain(
        combine_docs_chain=doc_chain,
        question_generator=question_generator,
        retriever=db.as_retriever(),
        return_source_documents=False,
        verbose=False,
    )

chain = create_chain()

app = mount_gradio_app(FastAPI(title="ConversationRetrieval"))
templates = Jinja2Templates(directory="templates")

@app.get("/")
async def get(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

langchain_router = LangchainRouter(
    langchain_url="/chat", langchain_object=chain, streaming_mode=1
)
langchain_router.add_langchain_api_route(
    "/chat_json", langchain_object=chain, streaming_mode=2
)

app.include_router(langchain_router)

Steps Taken to Debug

I have reviewed the code and configuration, and I have verified that FastAPI cURL requests work correctly. Additionally, the Gradio interface works as expected with other scripts, indicating that the issue might be specific to the integration with Lanarky for this type of chain ConversationalRetrievalChain .

Sincerely, Iman L.

ajndkr commented 1 year ago

hi @ImanLakjiri i will look into this soon. thanks for bringing it up!

(fyi, if you have already solved it on your end before I look into it, please feel free to open a PR! you can follow the guidelines here: https://github.com/ajndkr/lanarky/blob/main/CONTRIBUTING.md)

ImanLakjiri commented 1 year ago

.

ImanLakjiri commented 1 year ago

Hello @ajndkr, I have fixed the bug. I will submit my PR. Thank you

ajndkr commented 11 months ago

closing this issue as gradio integration has been removed from v0.8 release.

reason: the implementation needs to be agnostic of the microservice logic. I will rethink this and possibly implement a new playground in a future release. no definite plans yet.