Spaces:
No application file
No application file
Commit
·
90a9c4a
1
Parent(s):
4340b50
PANEL, ChatGPT e LangChain
Browse files
3_Build_a_LangChain-powered_AI_chatbot.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Data Scientist.: Dr. Eddy Giusepe Chirinos Isidro 🤓
|
| 3 |
+
|
| 4 |
+
Link do Panel --> https://panel.holoviz.org/reference/chat/PanelCallbackHandler.html
|
| 5 |
+
|
| 6 |
+
Link do ANACONDA --> https://www.anaconda.com/blog/
|
| 7 |
+
|
| 8 |
+
Construa um chatbot de IA com tecnologia LangChain
|
| 9 |
+
==================================================
|
| 10 |
+
O Panel ChatInterface também se integra perfeitamente ao LangChain, aproveitando
|
| 11 |
+
todo o espectro de recursos do LangChain. Aqui está um exemplo de como usamos
|
| 12 |
+
LangChain 'ConversationChain' para armazenar mensagens com 'ConversationBufferMemory'
|
| 13 |
+
e passar mensagens anteriores para a API OpenAI.
|
| 14 |
+
|
| 15 |
+
Observe que antes de mergulharmos no código LangChain, definimos 'callback_handler'
|
| 16 |
+
porque a interface LangChain não tem como transmitir a partir de geradores, então
|
| 17 |
+
precisamos agrupar a interface LangChain com 'pn.chat.langchain.PanelCallbackHandler',
|
| 18 |
+
que herda de `langchain.callbacks.base.BaseCallbackHandler`. Para mais informações, veja
|
| 19 |
+
o link acima.
|
| 20 |
+
|
| 21 |
+
Executando este script
|
| 22 |
+
----------------------
|
| 23 |
+
|
| 24 |
+
$ panel serve 2_Building_a_ChatGPT-powered_AI_ChatBot.py
|
| 25 |
+
"""
|
| 26 |
+
from langchain.chat_models import ChatOpenAI
|
| 27 |
+
from langchain.chains import ConversationChain
|
| 28 |
+
from langchain.memory import ConversationBufferMemory
|
| 29 |
+
|
| 30 |
+
import panel as pn
|
| 31 |
+
|
| 32 |
+
pn.extension()
|
| 33 |
+
|
| 34 |
+
# Substitua sua chave de API OpenAI:
|
| 35 |
+
import openai
|
| 36 |
+
import os
|
| 37 |
+
from dotenv import load_dotenv, find_dotenv
|
| 38 |
+
_ = load_dotenv(find_dotenv()) # read local .env file
|
| 39 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
async def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
|
| 43 |
+
await chain.apredict(input=contents)
|
| 44 |
+
|
| 45 |
+
chat_interface = pn.chat.ChatInterface(callback=callback, callback_user="ChatGPT")
|
| 46 |
+
callback_handler = pn.chat.langchain.PanelCallbackHandler(chat_interface)
|
| 47 |
+
|
| 48 |
+
llm = ChatOpenAI(streaming=True,
|
| 49 |
+
callbacks=[callback_handler],
|
| 50 |
+
model="gpt-3.5-turbo",
|
| 51 |
+
temperature=0.0,
|
| 52 |
+
verbose=True
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
memory = ConversationBufferMemory()
|
| 56 |
+
chain = ConversationChain(llm=llm, memory=memory)
|
| 57 |
+
|
| 58 |
+
chat_interface.send("Envie uma mensagem para obter uma resposta do ChatGPT!", user="System", respond=False)
|
| 59 |
+
chat_interface.servable()
|