Spaces:
Runtime error
Runtime error
Commit
•
dee7921
0
Parent(s):
Duplicate from mabrow05/sales_qa
Browse filesCo-authored-by: Michael Brown <mabrow05@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +436 -0
- data/mychromadb-20230306T151619Z-001.zip +3 -0
- data/mychromadb/chroma-collections.parquet +3 -0
- data/mychromadb/chroma-embeddings.parquet +3 -0
- data/mychromadb/index/id_to_uuid_0a679074-d94d-491b-afad-d8a5c755a12f.pkl +3 -0
- data/mychromadb/index/id_to_uuid_6a5ebc2c-73d3-4df2-8f71-1e3518ab1b9e.pkl +3 -0
- data/mychromadb/index/id_to_uuid_cae9661f-7a0d-4c52-9c06-95082f5c32b8.pkl +3 -0
- data/mychromadb/index/index_0a679074-d94d-491b-afad-d8a5c755a12f.bin +3 -0
- data/mychromadb/index/index_6a5ebc2c-73d3-4df2-8f71-1e3518ab1b9e.bin +3 -0
- data/mychromadb/index/index_cae9661f-7a0d-4c52-9c06-95082f5c32b8.bin +3 -0
- data/mychromadb/index/index_metadata_0a679074-d94d-491b-afad-d8a5c755a12f.pkl +3 -0
- data/mychromadb/index/index_metadata_6a5ebc2c-73d3-4df2-8f71-1e3518ab1b9e.pkl +3 -0
- data/mychromadb/index/index_metadata_cae9661f-7a0d-4c52-9c06-95082f5c32b8.pkl +3 -0
- data/mychromadb/index/uuid_to_id_0a679074-d94d-491b-afad-d8a5c755a12f.pkl +3 -0
- data/mychromadb/index/uuid_to_id_6a5ebc2c-73d3-4df2-8f71-1e3518ab1b9e.pkl +3 -0
- data/mychromadb/index/uuid_to_id_cae9661f-7a0d-4c52-9c06-95082f5c32b8.pkl +3 -0
- doc_ingest.ipynb +0 -0
- plan_metadata.xlsx +0 -0
- plans.sqlite +0 -0
- requirements.txt +5 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Sales QA Bot
|
3 |
+
emoji: 📈
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.20.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: mabrow05/sales_qa
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,436 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
|
3 |
+
#!pip install gradio
|
4 |
+
#!pip install -U sentence-transformers
|
5 |
+
#!pip install langchain
|
6 |
+
#!pip install openai
|
7 |
+
#!pip install -U chromadb
|
8 |
+
|
9 |
+
import gradio as gr
|
10 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder, util
|
11 |
+
from langchain.llms import OpenAI
|
12 |
+
from langchain.docstore.document import Document
|
13 |
+
from langchain.prompts import PromptTemplate
|
14 |
+
from langchain.chains.question_answering import load_qa_chain
|
15 |
+
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
|
16 |
+
from langchain import LLMMathChain, SQLDatabase, SQLDatabaseChain, LLMChain
|
17 |
+
from langchain.agents import initialize_agent, Tool
|
18 |
+
from langchain.agents import ZeroShotAgent, AgentExecutor
|
19 |
+
from langchain.memory import ConversationBufferWindowMemory
|
20 |
+
|
21 |
+
from langchain.schema import AIMessage, HumanMessage
|
22 |
+
|
23 |
+
import sqlite3
|
24 |
+
import pandas as pd
|
25 |
+
import json
|
26 |
+
|
27 |
+
from functools import partial
|
28 |
+
|
29 |
+
import chromadb
|
30 |
+
import os
|
31 |
+
|
32 |
+
#cxn = sqlite3.connect('./data/mbr.db')
|
33 |
+
|
34 |
+
"""# import models"""
|
35 |
+
|
36 |
+
bi_encoder = SentenceTransformer('multi-qa-MiniLM-L6-cos-v1')
|
37 |
+
bi_encoder.max_seq_length = 256 #Truncate long passages to 256 tokens
|
38 |
+
|
39 |
+
#The bi-encoder will retrieve top_k documents. We use a cross-encoder, to re-rank the results list to improve the quality
|
40 |
+
cross_encoder = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
|
41 |
+
|
42 |
+
"""# setup vector db
|
43 |
+
- chromadb
|
44 |
+
- https://docs.trychroma.com/getting-started
|
45 |
+
"""
|
46 |
+
|
47 |
+
from chromadb.config import Settings
|
48 |
+
|
49 |
+
chroma_client = chromadb.Client(settings=Settings(
|
50 |
+
chroma_db_impl="duckdb+parquet",
|
51 |
+
persist_directory="./data/mychromadb/" # Optional, defaults to .chromadb/ in the current directory
|
52 |
+
))
|
53 |
+
|
54 |
+
#!ls ./data/mychromadb/
|
55 |
+
#collection = chroma_client.create_collection(name="benefit_collection")
|
56 |
+
collection = chroma_client.get_collection(name="plan_collection", embedding_function=bi_encoder)
|
57 |
+
faq_collection = chroma_client.get_collection(name="faq_collection", embedding_function=bi_encoder)
|
58 |
+
"""### vector db search examples"""
|
59 |
+
|
60 |
+
def rtrv(qry, collection, top_k=20):
|
61 |
+
results = collection.query(
|
62 |
+
query_embeddings=[ bi_encoder.encode(qry) ],
|
63 |
+
n_results=top_k,
|
64 |
+
)
|
65 |
+
return results
|
66 |
+
|
67 |
+
def vdb_src(qry, collection, src, top_k=20):
|
68 |
+
results = collection.query(
|
69 |
+
query_embeddings=[ bi_encoder.encode(qry) ],
|
70 |
+
n_results=top_k,
|
71 |
+
where={"source": src},
|
72 |
+
)
|
73 |
+
return results
|
74 |
+
|
75 |
+
def vdb_where(qry, collection, where, top_k=20):
|
76 |
+
results = collection.query(
|
77 |
+
query_embeddings=[ bi_encoder.encode(qry) ],
|
78 |
+
n_results=top_k,
|
79 |
+
where=where,
|
80 |
+
)
|
81 |
+
return results
|
82 |
+
|
83 |
+
def vdb_pretty(qry, collection, top_k=10):
|
84 |
+
results = collection.query(
|
85 |
+
query_embeddings=[ bi_encoder.encode(qry) ],
|
86 |
+
n_results=top_k,
|
87 |
+
include=["metadatas", "documents", "distances","embeddings"]
|
88 |
+
)
|
89 |
+
rslt_pd = pd.DataFrame(results ).explode(['ids','documents', 'metadatas', 'distances', 'embeddings'])
|
90 |
+
rslt_fmt = pd.concat([rslt_pd.drop(['metadatas'], axis=1), rslt_pd['metadatas'].apply(pd.Series)], axis=1 )
|
91 |
+
return rslt_fmt
|
92 |
+
|
93 |
+
# qry = 'Why should I chose Medicare Advantage over traditional Medicare?'
|
94 |
+
# rslt_fmt = vdb_pretty(qry, top_k=10)
|
95 |
+
# rslt_fmt
|
96 |
+
|
97 |
+
# doc_lst = rslt_fmt[['documents']].values.tolist()
|
98 |
+
# len(doc_lst)
|
99 |
+
|
100 |
+
"""# Introduction
|
101 |
+
- example of the kind of question answering that is possible with this tool
|
102 |
+
- assumes we are answering for a member with a Healthy Options Card
|
103 |
+
|
104 |
+
*When will I get my card?*
|
105 |
+
|
106 |
+
# semantic search functions
|
107 |
+
"""
|
108 |
+
|
109 |
+
# choosing to use rerank for this use case as a baseline
|
110 |
+
|
111 |
+
def rernk(query, collection=collection, where=None, top_k=20, top_n = 5):
|
112 |
+
rtrv_rslts = vdb_where(query, collection=collection, where=where, top_k=top_k)
|
113 |
+
rtrv_ids = rtrv_rslts.get('ids')[0]
|
114 |
+
rtrv_docs = rtrv_rslts.get('documents')[0]
|
115 |
+
|
116 |
+
##### Re-Ranking #####
|
117 |
+
cross_inp = [[query, doc] for doc in rtrv_docs]
|
118 |
+
cross_scores = cross_encoder.predict(cross_inp)
|
119 |
+
|
120 |
+
# Sort results by the cross-encoder scores
|
121 |
+
combined = list(zip(rtrv_ids, list(cross_scores)))
|
122 |
+
sorted_tuples = sorted(combined, key=lambda x: x[1], reverse=True)
|
123 |
+
sorted_ids = [t[0] for t in sorted_tuples[:top_n]]
|
124 |
+
predictions = collection.get(ids=sorted_ids, include=["documents","metadatas"])
|
125 |
+
return predictions
|
126 |
+
#return cross_scores
|
127 |
+
|
128 |
+
## version w/o re-rank
|
129 |
+
# def get_text_fmt(qry):
|
130 |
+
# prediction_text = []
|
131 |
+
# predictions = rtrv(qry, top_k = 5)
|
132 |
+
# docs = predictions['documents'][0]
|
133 |
+
# meta = predictions['metadatas'][0]
|
134 |
+
# for i in range(len(docs)):
|
135 |
+
# result = Document(page_content=docs[i], metadata=meta[i])
|
136 |
+
# prediction_text.append(result)
|
137 |
+
# return prediction_text
|
138 |
+
|
139 |
+
def get_text_fmt(qry, collection=collection, where=None):
|
140 |
+
prediction_text = []
|
141 |
+
predictions = rernk(qry, collection=collection, where=where, top_k=20, top_n = 5)
|
142 |
+
docs = predictions['documents']
|
143 |
+
meta = predictions['metadatas']
|
144 |
+
for i in range(len(docs)):
|
145 |
+
result = Document(page_content=docs[i], metadata=meta[i])
|
146 |
+
prediction_text.append(result)
|
147 |
+
return prediction_text
|
148 |
+
|
149 |
+
# get_text_fmt('why should I choose a medicare advantage plan over traditional medicare?')
|
150 |
+
|
151 |
+
"""# LLM based qa functions"""
|
152 |
+
|
153 |
+
llm = OpenAI(temperature=0)
|
154 |
+
# default model
|
155 |
+
# model_name: str = "text-davinci-003"
|
156 |
+
# instruction fine-tuned, sometimes referred to as GPT-3.5
|
157 |
+
|
158 |
+
template = """You are a friendly AI assistant for the insurance company Humana.
|
159 |
+
Given the following extracted parts of a long document and a question, create a succinct final answer.
|
160 |
+
If you don't know the answer, just say that you don't know. Don't try to make up an answer.
|
161 |
+
If the question is not about Humana, politely inform the user that you are tuned to only answer questions about Humana.
|
162 |
+
QUESTION: {question}
|
163 |
+
=========
|
164 |
+
{summaries}
|
165 |
+
=========
|
166 |
+
FINAL ANSWER:"""
|
167 |
+
PROMPT = PromptTemplate(template=template, input_variables=["summaries", "question"])
|
168 |
+
|
169 |
+
chain_qa = load_qa_with_sources_chain(llm=llm, chain_type="stuff", prompt=PROMPT, verbose=False)
|
170 |
+
|
171 |
+
def get_llm_response(message, collection=collection, where=None):
|
172 |
+
mydocs = get_text_fmt(message, collection, where)
|
173 |
+
responses = chain_qa({"input_documents":mydocs, "question":message})
|
174 |
+
return responses
|
175 |
+
|
176 |
+
|
177 |
+
get_llm_response_humana = partial(get_llm_response, where={'company':'humana'})
|
178 |
+
get_llm_response_essence = partial(get_llm_response, where={'company':'essence'})
|
179 |
+
get_llm_response_faq = partial(get_llm_response, collection=faq_collection)
|
180 |
+
|
181 |
+
# rslt = get_llm_response('can I buy shrimp?')
|
182 |
+
# rslt['output_text']
|
183 |
+
|
184 |
+
# for d in rslt['input_documents']:
|
185 |
+
# print(d.page_content)
|
186 |
+
# print(d.metadata['url'])
|
187 |
+
|
188 |
+
# rslt['output_text']
|
189 |
+
|
190 |
+
"""# Database query"""
|
191 |
+
## setup member database
|
192 |
+
## only do this once
|
193 |
+
# d = {'mbr_fname':['bruce'],
|
194 |
+
# 'mbr_lname':['broussard'],
|
195 |
+
# 'mbr_id':[456] ,
|
196 |
+
# 'policy_id':['H1036-236'],
|
197 |
+
# 'accumulated_out_of_pocket':[3800],
|
198 |
+
# 'accumulated_routine_footcare_visits':[6],
|
199 |
+
# 'accumulated_trasportation_trips':[22],
|
200 |
+
# 'accumulated_drug_cost':[7500],
|
201 |
+
# }
|
202 |
+
# df = pd.DataFrame(data=d, columns=['mbr_fname', 'mbr_lname', 'mbr_id', 'policy_id', 'accumulated_out_of_pocket', 'accumulated_routine_footcare_visits', 'accumulated_trasportation_trips','accumulated_drug_cost'])
|
203 |
+
# df.to_sql(name='mbr_details', con=cxn, if_exists='replace')
|
204 |
+
|
205 |
+
# # sample db query
|
206 |
+
# qry = '''select accumulated_routine_footcare_visits
|
207 |
+
# from mbr_details'''
|
208 |
+
# foot_det = pd.read_sql(qry, cxn)
|
209 |
+
# foot_det.values[0][0]
|
210 |
+
|
211 |
+
#db = SQLDatabase.from_uri("sqlite:///./data/mbr.db")
|
212 |
+
|
213 |
+
#db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True, return_intermediate_steps=True)
|
214 |
+
|
215 |
+
#def db_qry(qry):
|
216 |
+
# responses = db_chain('my mbr_id is 456 ;'+str(qry) ) ############### hardcode mbr id 456 for demo
|
217 |
+
# return responses
|
218 |
+
|
219 |
+
"""# Math
|
220 |
+
- default version
|
221 |
+
"""
|
222 |
+
|
223 |
+
llm_math_chain = LLMMathChain(llm=llm, verbose=True)
|
224 |
+
|
225 |
+
# llm_math_chain.run('what is the square root of 49?')
|
226 |
+
|
227 |
+
"""# Greeting"""
|
228 |
+
|
229 |
+
template = """You are an AI assistant for the insurance company Humana.
|
230 |
+
Your name is Jarvis and you were created on February 13, 2023.
|
231 |
+
Offer polite, friendly greetings and brief small talk.
|
232 |
+
Respond to thanks with, 'Glad to help.'
|
233 |
+
If the question is not about Humana, politely guide the user to ask questions about Humana insurance benefits
|
234 |
+
QUESTION: {question}
|
235 |
+
=========
|
236 |
+
FINAL ANSWER:"""
|
237 |
+
greet_prompt = PromptTemplate(template=template, input_variables=["question"])
|
238 |
+
|
239 |
+
greet_llm = LLMChain(prompt=greet_prompt, llm=llm, verbose=True)
|
240 |
+
|
241 |
+
# greet_llm.run('will it snow in Lousiville tomorrow')
|
242 |
+
|
243 |
+
# greet_llm.run('Thanks, that was great')
|
244 |
+
|
245 |
+
"""# MRKL Chain"""
|
246 |
+
|
247 |
+
tools = [
|
248 |
+
Tool(
|
249 |
+
name = "Humana Plans",
|
250 |
+
func=get_llm_response_humana,
|
251 |
+
description='''Useful for confirming benefits of Humana plans.
|
252 |
+
Useful for answering questions about Humana insurance plans.
|
253 |
+
You should ask targeted questions.'''
|
254 |
+
),
|
255 |
+
Tool(
|
256 |
+
name = "Essence Plans",
|
257 |
+
func=get_llm_response_essence,
|
258 |
+
description='''Useful for confirming benefits of Essence Healthcare plans.
|
259 |
+
Useful for answering questions about Essence Healthcare plans.
|
260 |
+
You should ask targeted questions.'''
|
261 |
+
),
|
262 |
+
Tool(
|
263 |
+
name = "FAQ",
|
264 |
+
func=get_llm_response_faq,
|
265 |
+
description='''Useful for answering general health insurance questions. Useful for answering questions about Medicare and
|
266 |
+
Medicare Advantage. '''
|
267 |
+
),
|
268 |
+
Tool(
|
269 |
+
name="Calculator",
|
270 |
+
func=llm_math_chain.run,
|
271 |
+
description="""Only useful for when you need to answer questions about math, like subtracting two numbers or dividing numbers.
|
272 |
+
This tool should not be used to look up facts."""
|
273 |
+
),
|
274 |
+
#Tool(
|
275 |
+
# name = "Search",
|
276 |
+
# func=search.run,
|
277 |
+
# description="Useful for when you need to answer questions than can not be answered using the other tools. This tool is a last resort."
|
278 |
+
#),
|
279 |
+
Tool(
|
280 |
+
name="Greeting",
|
281 |
+
func=greet_llm.run,
|
282 |
+
return_direct=True,
|
283 |
+
description="useful for when you need to respond to greetings, thanks, make small talk or answer questions about yourself"
|
284 |
+
),
|
285 |
+
]
|
286 |
+
|
287 |
+
|
288 |
+
##### Create Agent
|
289 |
+
|
290 |
+
#mrkl = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False, return_intermediate_steps=True, max_iterations=5, early_stopping_method="generate")
|
291 |
+
|
292 |
+
prefix = """Answer the following question as best as you can. You should not make up any answers. To answer the question, use the following
|
293 |
+
tools:"""
|
294 |
+
suffix = """If the question is not about healthcare or Humana,
|
295 |
+
you should use the "Greeting" tool and pass it the question being asked.
|
296 |
+
If you are not confident in which tool to use,
|
297 |
+
you should use the "Greeting" tool and pass it the question being asked.
|
298 |
+
Remember, only answer using the information output from the
|
299 |
+
tools! Begin!"
|
300 |
+
|
301 |
+
{chat_history}
|
302 |
+
Question: {input}
|
303 |
+
{agent_scratchpad}"""
|
304 |
+
|
305 |
+
prompt = ZeroShotAgent.create_prompt(
|
306 |
+
tools,
|
307 |
+
prefix=prefix,
|
308 |
+
suffix=suffix,
|
309 |
+
input_variables=["input", "chat_history", "agent_scratchpad"]
|
310 |
+
)
|
311 |
+
|
312 |
+
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
313 |
+
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
|
314 |
+
agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True,
|
315 |
+
max_iterations=5, early_stopping_method="generate",
|
316 |
+
return_intermediate_steps=True)
|
317 |
+
|
318 |
+
|
319 |
+
def make_memory_buffer(history, mem_len=2):
|
320 |
+
mem = ConversationBufferWindowMemory(k=mem_len, memory_key="chat_history", output_key="output")
|
321 |
+
hist = []
|
322 |
+
for user,ai in history:
|
323 |
+
hist+=[HumanMessage(content=user), AIMessage(content=ai)]
|
324 |
+
|
325 |
+
mem.chat_memory.messages = hist
|
326 |
+
return mem
|
327 |
+
|
328 |
+
def agent_rspnd(qry, history, agent=agent_chain):
|
329 |
+
agent.memory = make_memory_buffer(history)
|
330 |
+
response = agent({"input":str(qry) })
|
331 |
+
return response
|
332 |
+
|
333 |
+
def make_memory_buffer(history, mem_len=2):
|
334 |
+
|
335 |
+
hist = []
|
336 |
+
for user,ai in history:
|
337 |
+
hist+=[HumanMessage(content=user), AIMessage(content=ai)]
|
338 |
+
|
339 |
+
mem = ConversationBufferWindowMemory(k=mem_len, memory_key="chat_history", output_key="output")
|
340 |
+
mem.chat_memory.messages = hist
|
341 |
+
return mem
|
342 |
+
|
343 |
+
def agent_rspnd(qry, history):
|
344 |
+
agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True,
|
345 |
+
memory=make_memory_buffer(history),
|
346 |
+
max_iterations=5, early_stopping_method="generate",
|
347 |
+
return_intermediate_steps=True)
|
348 |
+
response = agent_chain({"input":str(qry) })
|
349 |
+
return response
|
350 |
+
|
351 |
+
def mrkl_rspnd(qry):
|
352 |
+
response = mrkl({"input":str(qry) })
|
353 |
+
return response
|
354 |
+
|
355 |
+
# r = mrkl_rspnd("can I buy fish with the card?")
|
356 |
+
# print(r['output'])
|
357 |
+
|
358 |
+
# print(json.dumps(r['intermediate_steps'], indent=2))
|
359 |
+
|
360 |
+
#r['intermediate_steps']
|
361 |
+
|
362 |
+
# from IPython.core.display import display, HTML
|
363 |
+
|
364 |
+
def get_cot(r):
|
365 |
+
cot = '<p>'
|
366 |
+
try:
|
367 |
+
intermedObj = r['intermediate_steps']
|
368 |
+
cot +='<b>Input:</b> '+r['input']+'<br>'
|
369 |
+
for agnt_action, obs in intermedObj:
|
370 |
+
al = '<br> '.join(agnt_action.log.split('\n') )
|
371 |
+
cot += '<b>AI chain of thought:</b> '+ al +'<br>'
|
372 |
+
if type(obs) is dict:
|
373 |
+
if obs.get('input_documents') is not None: #### this criteria doesn't work
|
374 |
+
for d in obs['input_documents']:
|
375 |
+
cot += ' '+'<i>- '+str(d.page_content)+'</i>'+' <a href="'+ str(d.metadata['url']) +'">'+str(d.metadata['page'])+'</a> '+'<br>'
|
376 |
+
cot += '<b>Observation:</b> '+str(obs['output_text']) +'<br><br>'
|
377 |
+
elif obs.get('intermediate_steps') is not None:
|
378 |
+
cot += '<b>Query:</b> '+str(obs.get('intermediate_steps')) +'<br><br>'
|
379 |
+
else:
|
380 |
+
pass
|
381 |
+
else:
|
382 |
+
cot += '<b>Observation:</b> '+str(obs) +'<br><br>'
|
383 |
+
except:
|
384 |
+
pass
|
385 |
+
cot += '</p>'
|
386 |
+
return cot
|
387 |
+
|
388 |
+
# cot = get_cot(r)
|
389 |
+
# display(HTML(cot))
|
390 |
+
|
391 |
+
"""# chat example"""
|
392 |
+
|
393 |
+
def chat(message, history):
|
394 |
+
history = history or []
|
395 |
+
#message = message.lower()
|
396 |
+
|
397 |
+
response = agent_rspnd(message, history)
|
398 |
+
cot = get_cot(response)
|
399 |
+
history.append((message, response['output']))
|
400 |
+
return history, history, cot
|
401 |
+
|
402 |
+
css=".gradio-container {background-color: whitesmoke}"
|
403 |
+
|
404 |
+
xmpl_list = ["How does Humana's transportation benefit compare to Essence's?",
|
405 |
+
"Why should I choose a Medicare Advantage plan over Traditional Medicare?",
|
406 |
+
"What is the difference between a Medicare Advantage HMO plan and a PPO plan?",
|
407 |
+
"What is a low income subsidy plan and do I qualify for one of these plans?",
|
408 |
+
"Are my medications covered on a low income subsidy plan?"]
|
409 |
+
|
410 |
+
with gr.Blocks(css=css) as demo:
|
411 |
+
history_state = gr.State()
|
412 |
+
response_state = gr.State()
|
413 |
+
gr.Markdown('# Sales QA Bot')
|
414 |
+
gr.Markdown("""You are a **Louisville, KY** resident who currently has **Medicare Advantage** through an insurer called
|
415 |
+
**Essence Healthcare**. You don't know a lot about Medicare Advantage or your current benefits, so you may have questions about
|
416 |
+
how Humana's plans compare. This bot is here to help you learn about what **Humana has to offer** while answering any
|
417 |
+
other questions you might have. Welcome!""")
|
418 |
+
with gr.Row():
|
419 |
+
chatbot = gr.Chatbot()
|
420 |
+
with gr.Accordion(label='Show AI chain of thought: ', open=False,):
|
421 |
+
ai_cot = gr.HTML(show_label=False)
|
422 |
+
with gr.Row():
|
423 |
+
message = gr.Textbox(label='Input your question here:',
|
424 |
+
placeholder='Why should I choose Medicare Advantage?',
|
425 |
+
lines=1)
|
426 |
+
submit = gr.Button(value='Send',
|
427 |
+
variant='secondary').style(full_width=False)
|
428 |
+
submit.click(chat,
|
429 |
+
inputs=[message, history_state],
|
430 |
+
outputs=[chatbot, history_state, ai_cot])
|
431 |
+
gr.Examples(
|
432 |
+
examples=xmpl_list,
|
433 |
+
inputs=message
|
434 |
+
)
|
435 |
+
|
436 |
+
demo.launch()
|
data/mychromadb-20230306T151619Z-001.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:509bf8ec0648c517fd60f0c5e9214cfec4de1293e5c97fb58618e7dc91e0c3a2
|
3 |
+
size 4738160
|
data/mychromadb/chroma-collections.parquet
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:85bb2965764ace8535b9e89502ee065094995ac52c62c9e2e9797e4452ff37c1
|
3 |
+
size 721
|
data/mychromadb/chroma-embeddings.parquet
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:696babdca342c483071e6876bc23f55396f044d63486e367b68cbbc5acb95c4c
|
3 |
+
size 8588858
|
data/mychromadb/index/id_to_uuid_0a679074-d94d-491b-afad-d8a5c755a12f.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:62a155376d9dd9fd29ae34624ca53e60ebb5d7e894f09ecedaa495bf01a536cd
|
3 |
+
size 18262
|
data/mychromadb/index/id_to_uuid_6a5ebc2c-73d3-4df2-8f71-1e3518ab1b9e.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:de55bcaa0ac0dcd981f3fb054118d82cd4df9d465469e91470d492e212f77e6a
|
3 |
+
size 50372
|
data/mychromadb/index/id_to_uuid_cae9661f-7a0d-4c52-9c06-95082f5c32b8.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ac4c9d3c83a880ef575f8877a9da8fc29bd6a595cc462292b0a371bc31275f13
|
3 |
+
size 44948
|
data/mychromadb/index/index_0a679074-d94d-491b-afad-d8a5c755a12f.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d4de5cff6f21aa7977cedfbde7999af2dcb46a955958587d97d60716f77b4849
|
3 |
+
size 958668
|
data/mychromadb/index/index_6a5ebc2c-73d3-4df2-8f71-1e3518ab1b9e.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cfb27f50cadd8b3d5c50a4c674021f923d2474072c52a1a1a3b111479dd00b7f
|
3 |
+
size 2621248
|
data/mychromadb/index/index_cae9661f-7a0d-4c52-9c06-95082f5c32b8.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:eac82921b0d3e3af113ce6fda8545b4b33b2f85901ef8ddf549508ebc0b0f2c3
|
3 |
+
size 2341688
|
data/mychromadb/index/index_metadata_0a679074-d94d-491b-afad-d8a5c755a12f.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ff8860fe0b626f1ba5ef392a8f5972aac7405bc174e9969638ae90d1ffd10d23
|
3 |
+
size 74
|
data/mychromadb/index/index_metadata_6a5ebc2c-73d3-4df2-8f71-1e3518ab1b9e.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2306024ca09ce327169c751742438568e4867a628aeec4d87cf9946baf7c3014
|
3 |
+
size 74
|
data/mychromadb/index/index_metadata_cae9661f-7a0d-4c52-9c06-95082f5c32b8.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c9c04162c316440379c3a029d96bcc4bfa42c748effe242338a15b9bec9756c9
|
3 |
+
size 74
|
data/mychromadb/index/uuid_to_id_0a679074-d94d-491b-afad-d8a5c755a12f.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1b1560aafd6f73cb420c4ff376e920c9a62ec2a29776295b3985db118e5221eb
|
3 |
+
size 21382
|
data/mychromadb/index/uuid_to_id_6a5ebc2c-73d3-4df2-8f71-1e3518ab1b9e.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5cc32f95d05290aabd669ac7f1e83531f45ea14f4929c8e088a2eed7ee0cadef
|
3 |
+
size 58890
|
data/mychromadb/index/uuid_to_id_cae9661f-7a0d-4c52-9c06-95082f5c32b8.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bb30f0032f296206a5d4fa6c8300a28df4aedfd3c39cf4c9305f0d33f49c872f
|
3 |
+
size 52582
|
doc_ingest.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
plan_metadata.xlsx
ADDED
Binary file (5.38 kB). View file
|
|
plans.sqlite
ADDED
Binary file (12.3 kB). View file
|
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sentence-transformers==2.2.2
|
2 |
+
openai==0.27.2
|
3 |
+
langchain==0.0.109
|
4 |
+
chromadb==0.3.11
|
5 |
+
gradio
|