Spaces:
Runtime error
Runtime error
Commit
·
2652880
1
Parent(s):
d5f1daa
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
import os
|
7 |
+
os.environ['OPENAI_API_KEY'] = "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"
|
8 |
+
|
9 |
+
|
10 |
+
###Chat model
|
11 |
+
from langchain.schema import (
|
12 |
+
AIMessage,
|
13 |
+
HumanMessage,
|
14 |
+
SystemMessage
|
15 |
+
)
|
16 |
+
from langchain.chat_models import ChatOpenAI
|
17 |
+
|
18 |
+
chat = ChatOpenAI()
|
19 |
+
|
20 |
+
###Memory
|
21 |
+
from langchain.llms import OpenAI
|
22 |
+
from langchain.memory import ConversationSummaryMemory
|
23 |
+
|
24 |
+
llm = OpenAI(temperature=0)
|
25 |
+
memory = ConversationSummaryMemory(llm=llm,memory_key="chat_history",return_messages=True)
|
26 |
+
|
27 |
+
####Retrieval
|
28 |
+
from langchain.document_loaders import DirectoryLoader
|
29 |
+
# from langchain.document_loaders import WebBaseLoader
|
30 |
+
|
31 |
+
# loader = WebBaseLoader("https://www.hdfclife.com/insurance-knowledge-centre/about-life-insurance/health-insurance-meaning-and-types")
|
32 |
+
loader = DirectoryLoader('beshak/', glob="**/*.md")
|
33 |
+
data = loader.load()
|
34 |
+
|
35 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
36 |
+
|
37 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
|
38 |
+
all_splits = text_splitter.split_documents(data)
|
39 |
+
|
40 |
+
from langchain.embeddings import OpenAIEmbeddings
|
41 |
+
from langchain.vectorstores import Chroma
|
42 |
+
|
43 |
+
|
44 |
+
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
|
45 |
+
|
46 |
+
|
47 |
+
from langchain.chains import ConversationalRetrievalChain
|
48 |
+
llm = ChatOpenAI()
|
49 |
+
retriever = vectorstore.as_retriever()
|
50 |
+
qa = ConversationalRetrievalChain.from_llm(llm, retriever=retriever, memory=memory)
|
51 |
+
|
52 |
+
print(qa("You're a helpful AI assistant that answers questions about health insurance.")["answer"])
|
53 |
+
print(qa("What is health insurance?")["answer"])
|
54 |
+
|
55 |
+
import gradio as gr
|
56 |
+
|
57 |
+
def chatbot_response(message, history):
|
58 |
+
return qa(message)["answer"]
|
59 |
+
|
60 |
+
gr.ChatInterface(
|
61 |
+
chatbot_response,
|
62 |
+
chatbot=gr.Chatbot(height=400),
|
63 |
+
textbox=gr.Textbox(placeholder="Ask me question about health insurance", container=False, scale=7),
|
64 |
+
title="Get Simple Health",
|
65 |
+
description="Ask any health insurance related question",
|
66 |
+
theme="soft",
|
67 |
+
examples=["Hello", "What is health insurance?", "What is critical ilness?"],
|
68 |
+
cache_examples=True,
|
69 |
+
retry_btn=None,
|
70 |
+
undo_btn="Delete Previous",
|
71 |
+
clear_btn="Clear",
|
72 |
+
).launch(share=True)
|
73 |
+
|
74 |
+
|
75 |
+
|