Spaces:
Runtime error
Runtime error
Download chatbot.py from capsuite/chatbot-yang: direct link, hf CLI and curl.
- Browser
- Download file 1.2 kB
-
https://huggingface.co/spaces/capsuite/chatbot-yang/resolve/main/chatbot.py
- Command line
-
hf download hf://spaces/capsuite/chatbot-yang/chatbot.py
-
curl -L -o chatbot.py https://huggingface.co/spaces/capsuite/chatbot-yang/resolve/main/chatbot.py
1.2 kB
| # %% | |
| import gradio | |
| import openai | |
| from langchain.chains import ConversationalRetrievalChain | |
| from langchain.chat_models import AzureChatOpenAI | |
| from langchain.embeddings import OpenAIEmbeddings | |
| from langchain.vectorstores import FAISS | |
| # %% | |
| embeddings = OpenAIEmbeddings( | |
| deployment="CapSuiteAda", client=openai.Embedding, chunk_size=16 | |
| ) | |
| vectorstore = FAISS.load_local( | |
| folder_path="stores/faiss_vectorstore", embeddings=embeddings | |
| ) | |
| # %% | |
| llm = AzureChatOpenAI(deployment_name="CapSuiteGPT", temperature=0.0) | |
| # %% | |
| qa_chain = ConversationalRetrievalChain.from_llm( | |
| llm=llm, | |
| retriever=vectorstore.as_retriever(), | |
| ) | |
| # %% | |
| def chatbot(question, history=[]): | |
| chat_history = list(map(tuple, history)) | |
| result = qa_chain({"question": question, "chat_history": chat_history}) | |
| return result["answer"] | |
| # %% | |
| chat_interface = gradio.ChatInterface( | |
| fn=chatbot, | |
| examples=[ | |
| "循道衛理楊震社會服務處主要從事什麼工作", | |
| "詳細介紹循道衛理楊震社會服務處", | |
| "循道衛理楊震社會服務處的服務內容", | |
| "牽晴計劃的服務內容", | |
| ], | |
| title="Custom-trained AI Chatbot", | |
| ) | |
| chat_interface.launch() | |
| # %% | |