Spaces:
No application file
No application file
Commit ·
4340b50
1
Parent(s): f9888bf
PANEL com ChatGP
Browse files
2_Building_a_ChatGPT-powered_AI_ChatBot.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Data Scientist.: Dr. Eddy Giusepe Chirinos Isidro
|
| 3 |
+
|
| 4 |
+
Crie um chatbot de IA com tecnologia ChatGPT
|
| 5 |
+
============================================
|
| 6 |
+
Como podemos usar a OpenAI ChatGPT para responder mensagens?
|
| 7 |
+
Podemos simplesmente chamar a API OpenAI na função callback.
|
| 8 |
+
|
| 9 |
+
Observe que neste exemplo, adicionamos à função async para
|
| 10 |
+
permitir multitarefa colaborativa em um único thread e permitir
|
| 11 |
+
que tarefas de IO ocorram em segundo plano. Isso garante que nosso
|
| 12 |
+
aplicativo funcione perfeitamente enquanto aguarda as respostas da API OpenAI.
|
| 13 |
+
Async permite a execução simultânea, permitindo-nos realizar outras tarefas
|
| 14 |
+
enquanto esperamos e garantindo uma aplicação responsiva.
|
| 15 |
+
|
| 16 |
+
Executando este script
|
| 17 |
+
----------------------
|
| 18 |
+
|
| 19 |
+
$ panel serve 2_Building_a_ChatGPT-powered_AI_ChatBot.py
|
| 20 |
+
"""
|
| 21 |
+
import openai
|
| 22 |
+
import panel as pn
|
| 23 |
+
|
| 24 |
+
pn.extension()
|
| 25 |
+
|
| 26 |
+
# Substitua sua chave de API OpenAI:
|
| 27 |
+
import openai
|
| 28 |
+
import os
|
| 29 |
+
from dotenv import load_dotenv, find_dotenv
|
| 30 |
+
_ = load_dotenv(find_dotenv()) # read local .env file
|
| 31 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
async def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
|
| 35 |
+
response = openai.ChatCompletion.create(
|
| 36 |
+
model="gpt-3.5-turbo",
|
| 37 |
+
messages=[{"role": "user", "content": contents}],
|
| 38 |
+
stream=True,
|
| 39 |
+
temperature=0.0,
|
| 40 |
+
)
|
| 41 |
+
message = ""
|
| 42 |
+
for chunk in response:
|
| 43 |
+
message += chunk["choices"][0]["delta"].get("content", "")
|
| 44 |
+
yield message
|
| 45 |
+
|
| 46 |
+
chat_interface = pn.chat.ChatInterface(callback=callback, callback_user="ChatGPT")
|
| 47 |
+
chat_interface.send("Envie uma mensagem para obter uma resposta do ChatGPT!", user="System", respond=False)
|
| 48 |
+
chat_interface.servable()
|