Update app.py
Browse files
app.py
CHANGED
@@ -11,64 +11,22 @@ from langchain.schema import AIMessage, HumanMessage
|
|
11 |
|
12 |
|
13 |
import os
|
14 |
-
# os.environ["OPENAI_API_KEY"] = 'sk-U'
|
15 |
-
|
16 |
OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
|
17 |
|
18 |
import pinecone
|
19 |
-
|
20 |
# 初始化 pinecone
|
21 |
pinecone.init(
|
22 |
api_key=os.getenv('pinecone_api_key'),
|
23 |
environment="gcp-starter"
|
24 |
)
|
25 |
|
26 |
-
index_name="
|
27 |
-
|
28 |
-
|
29 |
-
pdf_files = ['./ANSYS_Fluent_Text_Command_List.pdf']
|
30 |
-
raw_text = ''
|
31 |
-
|
32 |
-
for file in pdf_files:
|
33 |
-
reader = PdfReader(file)
|
34 |
-
for i, page in enumerate(reader.pages):
|
35 |
-
|
36 |
-
text = page.extract_text()
|
37 |
-
if text:
|
38 |
-
raw_text += text
|
39 |
-
|
40 |
-
|
41 |
-
text_splitter = CharacterTextSplitter(
|
42 |
-
separator="\n",
|
43 |
-
chunk_size=1000,
|
44 |
-
chunk_overlap=200,
|
45 |
-
length_function=len
|
46 |
-
)
|
47 |
-
|
48 |
-
# texts = text_splitter.split_text(raw_text)
|
49 |
-
|
50 |
-
with open("output.txt", "w", encoding="utf-8") as file:
|
51 |
-
file.write(raw_text)
|
52 |
-
|
53 |
-
texts = text_splitter.split_text(raw_text)
|
54 |
|
55 |
embeddings = OpenAIEmbeddings()
|
56 |
-
|
57 |
-
# 持久化数据
|
58 |
-
#docsearch = Pinecone.from_texts([t.page_content for t in texts], embeddings, index_name=index_name)
|
59 |
-
|
60 |
# 加载数据
|
61 |
docsearch = Pinecone.from_existing_index(index_name, embeddings)
|
62 |
-
|
63 |
-
#embeddings = OpenAIEmbeddings()
|
64 |
-
#embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
|
65 |
-
#docsearch = FAISS.from_texts(texts, embeddings)
|
66 |
chain = load_qa_chain(OpenAI(), chain_type="stuff")
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
#llm = ChatOpenAI(temperature=1.0, model='gpt-3.5-turbo-0613')
|
71 |
-
|
72 |
def predict(message, history):
|
73 |
history_langchain_format = []
|
74 |
for human, ai in history:
|
@@ -84,8 +42,6 @@ def predict(message, history):
|
|
84 |
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
85 |
yield partial_message
|
86 |
|
87 |
-
|
88 |
-
#return response
|
89 |
|
90 |
gr.ChatInterface(predict).queue().launch()
|
91 |
|
|
|
11 |
|
12 |
|
13 |
import os
|
|
|
|
|
14 |
OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
|
15 |
|
16 |
import pinecone
|
|
|
17 |
# 初始化 pinecone
|
18 |
pinecone.init(
|
19 |
api_key=os.getenv('pinecone_api_key'),
|
20 |
environment="gcp-starter"
|
21 |
)
|
22 |
|
23 |
+
index_name="text-index"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
embeddings = OpenAIEmbeddings()
|
|
|
|
|
|
|
|
|
26 |
# 加载数据
|
27 |
docsearch = Pinecone.from_existing_index(index_name, embeddings)
|
|
|
|
|
|
|
|
|
28 |
chain = load_qa_chain(OpenAI(), chain_type="stuff")
|
29 |
|
|
|
|
|
|
|
|
|
30 |
def predict(message, history):
|
31 |
history_langchain_format = []
|
32 |
for human, ai in history:
|
|
|
42 |
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
43 |
yield partial_message
|
44 |
|
|
|
|
|
45 |
|
46 |
gr.ChatInterface(predict).queue().launch()
|
47 |
|