├── .gitattributes ├── app.py ├── requirements.txt └── test_server.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | from langchain.document_loaders.csv_loader import CSVLoader 3 | from langchain.vectorstores import FAISS 4 | from langchain.embeddings.openai import OpenAIEmbeddings 5 | from langchain.prompts import PromptTemplate 6 | from langchain.chat_models import ChatOpenAI 7 | from langchain.chains import LLMChain 8 | from dotenv import load_dotenv 9 | 10 | load_dotenv() 11 | 12 | # 1. Vectorise the sales response csv data 13 | loader = CSVLoader(file_path="sales_response.csv") 14 | documents = loader.load() 15 | 16 | embeddings = OpenAIEmbeddings() 17 | db = FAISS.from_documents(documents, embeddings) 18 | 19 | # 2. Function for similarity search 20 | 21 | 22 | def retrieve_info(query): 23 | similar_response = db.similarity_search(query, k=3) 24 | 25 | page_contents_array = [doc.page_content for doc in similar_response] 26 | 27 | # print(page_contents_array) 28 | 29 | return page_contents_array 30 | 31 | 32 | # 3. Setup LLMChain & prompts 33 | llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k-0613") 34 | 35 | template = """ 36 | You are a world class business development representative. 37 | I will share a prospect's message with you and you will give me the best answer that 38 | I should send to this prospect based on past best practies, 39 | and you will follow ALL of the rules below: 40 | 41 | 1/ Response should be very similar or even identical to the past best practies, 42 | in terms of length, ton of voice, logical arguments and other details 43 | 44 | 2/ If the best practice are irrelevant, then try to mimic the style of the best practice to prospect's message 45 | 46 | Below is a message I received from the prospect: 47 | {message} 48 | 49 | Here is a list of best practies of how we normally respond to prospect in similar scenarios: 50 | {best_practice} 51 | 52 | Please write the best response that I should send to this prospect: 53 | """ 54 | 55 | prompt = PromptTemplate( 56 | input_variables=["message", "best_practice"], 57 | template=template 58 | ) 59 | 60 | chain = LLMChain(llm=llm, prompt=prompt) 61 | 62 | 63 | # 4. Retrieval augmented generation 64 | def generate_response(message): 65 | best_practice = retrieve_info(message) 66 | response = chain.run(message=message, best_practice=best_practice) 67 | return response 68 | 69 | 70 | # 5. Build an app with streamlit 71 | def main(): 72 | st.set_page_config( 73 | page_title="Customer response generator", page_icon=":bird:") 74 | 75 | st.header("Customer response generator :bird:") 76 | message = st.text_area("customer message") 77 | 78 | if message: 79 | st.write("Generating best practice message...") 80 | 81 | result = generate_response(message) 82 | 83 | st.info(result) 84 | 85 | 86 | if __name__ == '__main__': 87 | main() 88 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | fastapi[all] 2 | openai 3 | python-dotenv 4 | pydantic==1.* 5 | langchain 6 | faiss-cpu 7 | tiktoken -------------------------------------------------------------------------------- /test_server.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | print( 4 | requests.post( 5 | "http://0.0.0.0:10000", 6 | json={ 7 | "message": "Would be very interesting in having a conversation. Where are you based?" 8 | } 9 | ).json() 10 | ) 11 | --------------------------------------------------------------------------------