Spaces:
Runtime error
Runtime error
fix: split result to return only "helpful answer"
Browse files1234528 unmodified lines343536373839404142434445464748492 unmodified lines525354555657582 unmodified lines616263646566676869707172737475767778import os, textwrapfrom pprint import pprintfrom dotenv import load_dotenvfrom langchain.chains import RetrievalQAfrom langchain_community.vectorstores import FAISS28 unmodified lines# create the retrieverdb_instructEmbedd = FAISS.from_documents(texts, instructor_embeddings)retriever = db_instructEmbedd.as_retriever(search_kwargs={"k": 3})# print("this is the embeddings----------:",db_instructEmbedd,"----------embeddings")# print("this is the retriever----------:",retriever,"----------retriever")# db.save_local("faiss_index")# query = 'What is operating system?'# print('retriever search type:',retriever.search_type) # retriever search type is similarity search# print('retriever search kwargs:',retriever.search_kwargs)# docs = retriever.get_relevant_documents(query)# pprint(docs[0])# pprint(docs[1])# pprint(docs[2])# Initialize the model falcon-7bos.environ["HUGGINGFACEHUB_API_TOKEN"]2 unmodified lines# create the chain to answer questionsqa_chain_instrucEmbed = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True)## Cite sourcesdef wrap_text_preserve_newlines(text, width=110):# Split the input text into lines based on newline characterslines = text.split('\n')2 unmodified lines# Join the wrapped lines back together using newline characterswrapped_text = '\n'.join(wrapped_lines)return wrapped_text# def process_llm_response(llm_response):# print(wrap_text_preserve_newlines(llm_response['result']))# print('\nSources:')# for source in llm_response["source_documents"]:# print(source.metadata['source'])llm_response = qa_chain_instrucEmbed(query)res = wrap_text_preserve_newlines(llm_response['result'])# process_llm_response(llm_response)return res#print(res,"result")if __name__ == '__main__':main()123428 unmodified lines3334353637383940412 unmodified lines4445464748492 unmodified lines5253545556575859606162636465666768import os, textwrapfrom dotenv import load_dotenvfrom langchain.chains import RetrievalQAfrom langchain_community.vectorstores import FAISS28 unmodified lines# create the retrieverdb_instructEmbedd = FAISS.from_documents(texts, instructor_embeddings)retriever = db_instructEmbedd.as_retriever(search_kwargs={"k": 3})# retriever search type is similarity search# query = 'What is operating system?'# Initialize the model falcon-7bos.environ["HUGGINGFACEHUB_API_TOKEN"]2 unmodified lines# create the chain to answer questionsqa_chain_instrucEmbed = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True)def wrap_text_preserve_newlines(text, width=110):# Split the input text into lines based on newline characterslines = text.split('\n')2 unmodified lines# Join the wrapped lines back together using newline characterswrapped_text = '\n'.join(wrapped_lines)return wrapped_textllm_response = qa_chain_instrucEmbed(query)res = wrap_text_preserve_newlines(llm_response['result'])# print(res)index_helpful_answer = res.find("Helpful Answer:")if index_helpful_answer != -1:helpful_answer_text = res[index_helpful_answer + len("Helpful Answer:"):]return(helpful_answer_text.strip())else:return("Error")if __name__ == '__main__':main()
app.py+9-19