File size: 3,024 Bytes
fb2143d
 
 
 
 
 
 
 
 
 
 
8b0d60b
fb2143d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from langchain.document_loaders.csv_loader import CSVLoader
from langchain.vectorstores import FAISS
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.chains import LLMChain
from dotenv import load_dotenv
import streamlit as st
import os


# os.environ['OPENAI_API_KEY'] = ''

load_dotenv()
# 1.矢量化数据
loader = CSVLoader(file_path="updated_magical_book_m3e.csv")
documents = loader.load()

embeddings = HuggingFaceEmbeddings(model_name='sumandeng/my-m3e-base')
# Equivalent to SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

db = FAISS.from_documents(documents,embeddings)


# model = SentenceTransformer("moka-ai/m3e-base")
# embeddings = model.encode(documents)
# text_documents = documents["question"].tolist()


# 2.做相似性搜索
def retrieve_info(query):
    similar_response = db.similarity_search(query,k=3)
    page_contents_array = [doc.page_content for doc in similar_response]
    print(page_contents_array)
    return page_contents_array
# custom_prompt = """
#     我想合作或定制服务,怎么联系?
# """
# results=retrieve_info(custom_prompt)
# print(results)

# 3.设置LLMChain和提示
llm=ChatOpenAI(temperature=0, model='gpt-3.5-turbo-16k-0613')
template = """
    You are an excellent intelligent customer service agent.
    I'm going to share information about a potential customer with you, and you're going to give the best answer that
    Based on past best practices, I should send to this prospect,and follow all the rules below.
    1/ The response should be very similar, if not identical, to past best practices,in terms of length, tone of voice, logical arguments and other details.
    2/ If best practices are irrelevant, try to mimic the style of the best practice to get the prospect's message across.Here's the message I received from the prospect:
    {message}
    Here are best practices for how we typically respond to prospects in similar situations:
    {best_practice}
    Please write the best response I should give to this prospect:

    All replies are in Chinese
"""
prompt=PromptTemplate(
    input_variables=["message","best_practice"],
    template=template
)
chain=LLMChain(llm=llm,prompt=prompt)
# 4.检索生成结果
def generate_response(message):
    best_practice = retrieve_info(message)
    response = chain.run(message=message,best_practice=best_practice)
    return response

message = """
我想联系商务对接
"""
# response=generate_response(message)
# print(response)
# 5.创建一个应用使用streamlit框架
def main():
    st.set_page_config(
        page_title="Customer response generator",page_icon=":bird:")

    st.header("Customer response generator :bird:")
    message = st.text_area("customer message")

    if message:
        st.write("Generating best practice message...")

        result = generate_response(message)
        
        st.info(result)

if __name__ == "__main__":
    main()