S0ham075/gitbot
0
1from langchain.chains import RetrievalQA2from langchain.prompts import PromptTemplate3from langchain_together import Together4from langchain_community.vectorstores import Qdrant5from main import get_repo_name6import qdrant_client7import os 8 9from langchain_community.embeddings.fastembed import FastEmbedEmbeddings10embeddings = FastEmbedEmbeddings(model_name="BAAI/bge-small-en-v1.5")11 12client = qdrant_client.QdrantClient(13 os.getenv("QDRANT_HOST"),14 api_key=os.getenv("QDRANT_API_KEY")15)16 17 18B_INST, E_INST = "[INST]", "[/INST]"19B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"20 21def get_prompt(instruction, new_system_prompt ):22 SYSTEM_PROMPT = B_SYS + new_system_prompt + E_SYS23 prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST24 return prompt_template25 26sys_prompt = """You are a helpful, smart and intelligent coding assistant. Always answer as helpfully as possible using the context code provided. Your answers should only answer the question once, you can provide code snippets but make sure you explain them thoroughly27 28If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. """29 30instruction = """CONTEXT CODE:/n/n {context}/n31 32Question: {question}"""33 34 35prompt_template = get_prompt(instruction, sys_prompt)36 37llama_prompt = PromptTemplate(38 template=prompt_template, input_variables=["context", "question"]39)40 41llama2_llm = Together(42 model="togethercomputer/llama-2-70b-chat",43 temperature=0.7,44 max_tokens=1024,45 together_api_key="d8ec7106bd0c268bf4672dba83272b86054fbe849eba82f3f75ceb17e6d57eb0"46)47 48 49def process_llm_response(llm_response):50 response = " "51 response += llm_response['result'] + "\n\nSources\n"52 for source in llm_response['source_documents']:53 response +="Source - "+source.metadata['source'] +"\n"54 55 return response56 57def answer_query(query,url):58 vectorstore = Qdrant(59 client=client, 60 collection_name=get_repo_name(url),61 embeddings=embeddings62 )63 qa_chain = RetrievalQA.from_chain_type(llm= llama2_llm, chain_type_kwargs = {"prompt": llama_prompt},chain_type="stuff",retriever= vectorstore.as_retriever(),return_source_documents = True)64 return process_llm_response(qa_chain(query))65 66 