Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,9 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from langchain.chains import ConversationalRetrievalChain
|
| 4 |
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
from langchain.llms import ChatOpenAI
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
# 加載環境變量
|
|
@@ -22,7 +21,7 @@ openai_api_key = api_key
|
|
| 22 |
|
| 23 |
# 將聊天歷史轉換為適合 LangChain 的二元組格式
|
| 24 |
def transform_history_for_langchain(history):
|
| 25 |
-
return [(chat[0], chat[1]) for chat in history if chat[0]]
|
| 26 |
|
| 27 |
# 將 Gradio 的歷史紀錄轉換為 OpenAI 格式
|
| 28 |
def transform_history_for_openai(history):
|
|
@@ -67,7 +66,7 @@ if 'vectordb' not in globals():
|
|
| 67 |
def handle_query(user_message, temperature, chat_history):
|
| 68 |
try:
|
| 69 |
if not user_message:
|
| 70 |
-
return chat_history
|
| 71 |
|
| 72 |
# 使用 LangChain 的 ConversationalRetrievalChain 處理查詢
|
| 73 |
preface = """
|
|
@@ -94,7 +93,7 @@ def handle_query(user_message, temperature, chat_history):
|
|
| 94 |
return chat_history + [("系統", "抱歉,出現了一個錯誤。")]
|
| 95 |
|
| 96 |
# 更新對話歷史中的 AI 回應
|
| 97 |
-
chat_history[-1] = (user_message, result["answer"])
|
| 98 |
|
| 99 |
return chat_history
|
| 100 |
|
|
@@ -116,14 +115,14 @@ with gr.Blocks() as demo:
|
|
| 116 |
|
| 117 |
# 用戶輸入後立即顯示提問文字,不添加回應部分
|
| 118 |
def user_input(user_message, history):
|
| 119 |
-
history.append((user_message, ""))
|
| 120 |
-
return history, history
|
| 121 |
|
| 122 |
# 處理 AI 回應,更新回應部分
|
| 123 |
def bot_response(history):
|
| 124 |
-
user_message = history[-1][0]
|
| 125 |
-
history = handle_query(user_message, 0.7, history)
|
| 126 |
-
return history, history
|
| 127 |
|
| 128 |
# 先顯示提問文字,然後處理 AI 回應
|
| 129 |
submit_btn.click(user_input, [txt, state], [chatbot, state], queue=False).then(
|
|
@@ -136,4 +135,4 @@ with gr.Blocks() as demo:
|
|
| 136 |
)
|
| 137 |
|
| 138 |
# 啟動 Gradio 應用
|
| 139 |
-
demo.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
from langchain.chains import ConversationalRetrievalChain
|
| 4 |
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
+
from langchain_community.document_loaders import PyPDFLoader, Docx2txtLoader, TextLoader
|
| 6 |
+
from langchain_community.vectorstores import Chroma
|
| 7 |
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
|
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
# 加載環境變量
|
|
|
|
| 21 |
|
| 22 |
# 將聊天歷史轉換為適合 LangChain 的二元組格式
|
| 23 |
def transform_history_for_langchain(history):
|
| 24 |
+
return [(chat[0], chat[1]) for chat in history if chat[0]] # 使用整數索引來訪問元組中的元素
|
| 25 |
|
| 26 |
# 將 Gradio 的歷史紀錄轉換為 OpenAI 格式
|
| 27 |
def transform_history_for_openai(history):
|
|
|
|
| 66 |
def handle_query(user_message, temperature, chat_history):
|
| 67 |
try:
|
| 68 |
if not user_message:
|
| 69 |
+
return chat_history # 返回不變的聊天記錄
|
| 70 |
|
| 71 |
# 使用 LangChain 的 ConversationalRetrievalChain 處理查詢
|
| 72 |
preface = """
|
|
|
|
| 93 |
return chat_history + [("系統", "抱歉,出現了一個錯誤。")]
|
| 94 |
|
| 95 |
# 更新對話歷史中的 AI 回應
|
| 96 |
+
chat_history[-1] = (user_message, result["answer"]) # 更新最後一個記錄,配對用戶輸入和 AI 回應
|
| 97 |
|
| 98 |
return chat_history
|
| 99 |
|
|
|
|
| 115 |
|
| 116 |
# 用戶輸入後立即顯示提問文字,不添加回應部分
|
| 117 |
def user_input(user_message, history):
|
| 118 |
+
history.append((user_message, "")) # 顯示提問文字,回應部分為空字符串
|
| 119 |
+
return history, history # 立即更新 chatbot 顯示
|
| 120 |
|
| 121 |
# 處理 AI 回應,更新回應部分
|
| 122 |
def bot_response(history):
|
| 123 |
+
user_message = history[-1][0] # 獲取最新的用戶輸入
|
| 124 |
+
history = handle_query(user_message, 0.7, history) # 調用處理函數
|
| 125 |
+
return history, history # 返回更新後的聊天記錄
|
| 126 |
|
| 127 |
# 先顯示提問文字,然後處理 AI 回應
|
| 128 |
submit_btn.click(user_input, [txt, state], [chatbot, state], queue=False).then(
|
|
|
|
| 135 |
)
|
| 136 |
|
| 137 |
# 啟動 Gradio 應用
|
| 138 |
+
demo.launch()
|