veda1997/Film_Production
0
1from flask import Flask, request, jsonify, render_template2import os3import json4from langchain_community.vectorstores import Qdrant5from langchain_community.llms import LlamaCpp6from langchain_community.embeddings import HuggingFaceBgeEmbeddings7from langchain_core.prompts import PromptTemplate8from langchain.chains import RetrievalQA9from qdrant_client import QdrantClient10app = Flask(__name__)11 12# Initialize LLM and other components as in the original code13local_llm = "models/Calme-7B-Instruct-v0.2.Q4_K_M.gguf"14config = {15 # configuration settings as in the original code16'max_tokens': 512,17'temperature': 0.1,18'top_k': 50,19'top_p': 0.920}21 22llm = LlamaCpp(23 model_path=local_llm,24 **config25 # n_gpu_layers=-1, # Uncomment to use GPU acceleration26 # seed=1337, # Uncomment to set a specific seed27 # n_ctx=2048, # Uncomment to increase the context window28)29 30print("LLM Initialized....")31 32# Cant take too many instructions?33prompt_template = """34system35{context}36user37Give a short summary: {question}38assistant39"""40 41model_name = "BAAI/bge-large-en-v1.5"42model_kwargs = {'device': 'cpu'}43encode_kwargs = {'normalize_embeddings': False}44embeddings = HuggingFaceBgeEmbeddings(45 model_name=model_name,46 model_kwargs=model_kwargs,47 encode_kwargs=encode_kwargs48)49 50 51prompt = PromptTemplate(template=prompt_template, input_variables=['context','question'])52 53url = "http://localhost:6333"54 55client = QdrantClient(56 url=url, prefer_grpc=False57)58 59qdrant_db = Qdrant(client=client, embeddings=embeddings, collection_name="vector_db")60 61retriever = qdrant_db.as_retriever(search_kwargs={"k":1})62 63@app.route('/')64def index():65 return render_template('index.html')66 67@app.route('/get_response', methods=['POST'])68def get_response():69 query = request.form.get('query')70 # Your logic to handle the query71 chain_type_kwargs = {"prompt": prompt}72 qa = RetrievalQA.from_chain_type(73 llm=llm,74 chain_type="stuff",75 retriever=retriever,76 return_source_documents=True,77 chain_type_kwargs=chain_type_kwargs,78 verbose=True79 )80 response = qa(query)81 answer = response['result']82 source_document = response['source_documents'][0].page_content83 doc = response['source_documents'][0].metadata['source']84 response_data = {"answer": answer, "source_document": source_document, "doc": doc}85 86 return jsonify(response_data)87 