ysharma/LangChain_wandbBot
0
1from langchain.llms import OpenAI2from langchain.chains.qa_with_sources import load_qa_with_sources_chain3from langchain.docstore.document import Document4import requests5import pathlib6import subprocess7import tempfile8import os9import gradio as gr10import pickle11 12# using a vector space for our search13from langchain.embeddings.openai import OpenAIEmbeddings14from langchain.vectorstores.faiss import FAISS15from langchain.text_splitter import CharacterTextSplitter16 17#loading FAISS search index from disk18with open("search_index.pickle", "rb") as f:19 search_index = pickle.load(f)20 21#Get GPT3 response using Langchain22def print_answer(question, openai): #openai_embeddings23 #search_index = get_search_index()24 chain = load_qa_with_sources_chain(openai) #(OpenAI(temperature=0)) 25 response = (26 chain(27 {28 "input_documents": search_index.similarity_search(question, k=4),29 "question": question,30 },31 return_only_outputs=True,32 )["output_text"]33 )34 if len(response.split('\n')[-1].split())>2:35 response = response.split('\n')[0] + ', '.join([' <a href="' + response.split('\n')[-1].split()[i] + '" target="_blank"><u>Click Link' + str(i) + '</u></a>' for i in range(1,len(response.split('\n')[-1].split()))])36 else: 37 response = response.split('\n')[0] + ' <a href="' + response.split('\n')[-1].split()[-1] + '" target="_blank"><u>Click Link</u></a>'38 return response39 40 41def chat(message, history, openai_api_key):42 #openai_embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)43 openai = OpenAI(temperature=0, openai_api_key=openai_api_key )44 #os.environ["OPENAI_API_KEY"] = openai_api_key45 history = history or []46 message = message.lower()47 response = print_answer(message, openai) #openai_embeddings48 history.append((message, response))49 return history, history50 51 52with gr.Blocks() as demo:53 gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">54 <div55 style="56 display: inline-flex;57 align-items: center;58 gap: 0.8rem;59 font-size: 1.75rem;60 "61 >62 <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">63 wandb QandA - LangChain Bot64 </h1>65 </div>66 <p style="margin-bottom: 10px; font-size: 94%">67 Hi, I'm a Q and A wandb expert bot, start by typing in your OpenAI API key, questions/issues you are facing in your wandb implementations and then press enter.<br>68 <a href="https://huggingface.co/spaces/ysharma/InstructPix2Pix_Chatbot?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate Space with GPU Upgrade for fast Inference & no queue<br> 69 Built using <a href="https://langchain.readthedocs.io/en/latest/" target="_blank">LangChain</a> and <a href="https://github.com/gradio-app/gradio" target="_blank">Gradio</a> for the wandb Repo70 </p>71 </div>""") 72 with gr.Row():73 question = gr.Textbox(label = 'Type in your questions about wandb here and press Enter!', placeholder = 'What questions do you want to ask about the wandb library?')74 openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")75 state = gr.State()76 chatbot = gr.Chatbot()77 question.submit(chat, [question, state, openai_api_key], [chatbot, state])78 79if __name__ == "__main__":80 demo.launch()