Question answering bot with chatgpt3.5 turrbo, and chroma database

39 views Asked by At

I am very new to gen ai and databses. I am using chatgpt3.5 to answer the question from chromadb. i have 3 apps,each i have uploaded docs in chromadb. after selection of every app, Using semantic search in app's data not other app's data model should generate response for example app1 has 1 text file and 1 docx file. which i reading a variable, and appending and stored in collection of chroma db, so when i select app 1 ,and ask a question, model will fetch response from app1 collection. code sample

class AppName(BaseModel):
    app_selector: str

class Question(BaseModel):
    user_input : str

#Create vector store instance
def create_chroma():
    client = chromadb.PersistentClient(path="./ChromDB", settings=Settings(allow_reset=True))
    return client
env_vars = dotenv_values('.env')
app = FastAPI()
app.chroma_client = create_chroma()
app.embeddings=openaiembeddings(parameters)
client = OpenAI(api_key="key")
app.memory_hub = dict()

def get_collection(collection_name: str):
    collection = Chroma(client = app.chroma_client,
                        embedding_function = app.embeddings,
                        collection_name = collection_name
                        )
    return collection

async def upload_file(request: Request):
    try:
        app_files = {
            'app1': {
                "text": r"path",
                "docx": r"path"
            },
            'app2': {
                "text": r"path,
                "docx": r"path
            },
            'app3': {
                "text": path,
                "docx": path
            }
        }
        for app_name, files in app_files.items():
            collection_name = f"{app_name}_collection"
            for file, file_path in files.items():
                if file=='text':
                    text_data = open(file_path,'r',encoding="latin-1")
                else:
                    text_data=text_data+'\n'+open(file_path,'r',encoding="latin-1")
                    collection_name = f"{app_name}"
            print(collection_name)  
            collection = request.chroma_client.get_or_create_collection(name=collection_name, embedding_function=app.embeddings.embed_documents)
            collection.upsert(ids=[str(uuid.uuid4())], documents=[text_data])  
except Exception as e:
        raise HTTPException(status_code=500, detail="Error processing the file: " + str(e))
    

@app.post("/query_by_collection")
async def ask_question(app_name: AppName, question: Question):
    col_name = app_name.app_selector
    collection = get_collection(col_name)
    app.memory_hub = ConversationBufferMemory(memory_key = 'chat_history', return_messages = True)
    # conv_qa_chain = ConversationalRetrievalChain.from_llm(llm = completion, 
    #                                                       memory = app.memory_hub, 
    #                                                       retriever = collection.as_retriever(k=1)
    conv_qa_chain=ConversationalRetrievalChain.from_llm(ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.3), collection.as_retriever(), memory=app.memory_hub)
    resp =  conv_qa_chain.run(question.user_input)
    return {"response": resp}
@app.get("/collections")
def get_collections(request: Request):
    return {"collections": request.app.chroma_client.list_collections()}
@app.get('/reset_db')
def reset_chromadb(response: Request):
    response.app.chroma_client.reset()
    return {"status": "Deleted all data from ChromaDB"}

The issue is whenever i send 1 query specific to business for any of the app, it always generate generic response: like i am not aware of this. please refer to your manuals for this. it never generates answers from data loaded in db. is my database not configured properly? or any other issue? please guide me through it.

0

There are 0 answers