Spaces:
Build error

File size: 1,769 Bytes
05a1eab
f4512b5
ac69133
 
7d255dc
05a1eab
39570b9
fd3e6e0
05a1eab
fd3e6e0
 
05a1eab
f9b325e
 
 
 
 
05a1eab
 
 
 
 
 
 
dd583c9
39570b9
 
fd3e6e0
05a1eab
 
 
 
 
 
 
d5391c4
05a1eab
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from langchain.chat_models import ChatOpenAI
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationSummaryMemory
from langchain.vectorstores import FAISS
import gradio as gr
import logging

logging.basicConfig(level=logging.INFO)

## setup LLM
llm = ChatOpenAI()

## load vector db
embeddings = OpenAIEmbeddings()
db = FAISS.load_local("faiss_index", embeddings)

## Setup the Q&A chain that stitches LLM, similar results from the vectordb & the prompt template
memory = ConversationSummaryMemory(llm=llm,memory_key="chat_history",return_messages=True, output_key="answer")
retriever = db.as_retriever(search_type="mmr", search_kwargs={'k': 4, 'fetch_k': 5, 'lambda_mult': 0.25})
llm_chain = ConversationalRetrievalChain.from_llm(llm, retriever=retriever, memory=memory, 
                                                  verbose=False, return_source_documents=True)


def blogbot(question, history=None):
  # log what questions are asked
  logging.info('question: ' + question)  
  ## do the prompt against the Q&A chain & display the result
  result = llm_chain(question)
  source_urls = list(set(doc.metadata["source"] for doc in result["source_documents"]))
  answer = f"<strong>Answer:</strong><br><br>{result['answer']}<br><br><strong>Sources:</strong><br>"
  answer += "<br>".join(f'<a href="{url}" target="_blank">{url}</a>' for url in source_urls)
  return answer

gr.ChatInterface(fn=blogbot,chatbot=gr.Chatbot(height=600),
                 examples=["Is the author biased?", "What his opinion on US?", "what is his investment stratergy?"],
                 cache_examples=True, 
                 title="Lisa - Sara's Assistant").queue().launch(share=False)