-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_chatbot.py
More file actions
63 lines (44 loc) · 1.52 KB
/
Copy path02_chatbot.py
File metadata and controls
63 lines (44 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Chatbot con OpenAI GPT-3
"""
import openai
import os
from dotenv import load_dotenv
from colorama import init, Fore
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
openai.api_key = api_key
preguntas_anteriores = []
respuestas_anteriores = []
# Inicializar colorama
init()
def preguntar_chat_gpt(prompt, modelo="text-davinci-002"):
"""
Pregunta a la API de OpenAI GPT-3
"""
respuesta = openai.Completion.create(
engine=modelo,
prompt=prompt,
n=1,
temperature=1,
max_tokens=150
)
return respuesta.choices[0].text.strip()
# Bienvenida
print(Fore.RED + "Bienvenido al chatbot de OpenAI GPT-3." + Fore.RESET)
print(Fore.RED + "Escribe \"salir\" cuando quieras terminar la conversación." + Fore.RESET)
# Loop para controlar el flujo de la conversación
while True:
conversacion_historica = ""
ingreso_usuario = input(Fore.MAGENTA + "Tú: " + Fore.RESET)
if ingreso_usuario == "salir":
break
for pregunta, respuesta in zip(preguntas_anteriores, respuestas_anteriores):
conversacion_historica += f"{Fore.BLUE}Usuario pregunta: {Fore.RESET}{pregunta}"
conversacion_historica += f"{Fore.GREEN}Bot responde: {Fore.RESET}{respuesta}\n"
prompt = f"{Fore.CYAN}Usuario pregunta: {Fore.RESET}{ingreso_usuario}"
conversacion_historica += prompt
respuesta_gpt = preguntar_chat_gpt(conversacion_historica)
print(f"{respuesta_gpt}")
preguntas_anteriores.append(ingreso_usuario)
respuestas_anteriores.append(respuesta_gpt)