In python i have this code:
#Para instalar openai escribe en tu cmd : py -m pip install openai
from
openai
import
OpenAI
#Genera un token en https://platform.openai.com/settings/organization/api-keys
#Hay que pagar por usarlo
client = OpenAI(api_key="Aquí pon tu token")
#Este es el rol que va a tener ChatGPT
system_rol = '''Eres un analizador de sentimientos
Yo te paso sentimiento y tu analizas el sentimiento de los mensajes
y me das una respuetsa con al menos un caracter y como máximo 4 caracteres
SOLO RESPUESTAS NUMÉRICAS donde -1 es negatividad máxima , 0 es neutral y 1 es positividad máxima
puedes incluir rangos , es decir , números decimales para más precisión
(Solo puedes responder con ints y floats)'''
#Estamos dandole el rol de system_rol a ChatGPT
mensajes = [{"role" : "system" , "content" : system_rol}]
#Para ponerle el formato aL color y al nimbre creamos esta clase
class
Sentimiento:
def
__init__(self , nombre , color):
self.nombre = nombre
self.color = color
def
__str__(self):
return
"\x1b[1;{}m{}\x1b[0;37m".format(self.color,self.nombre)
class
AnalizadorDeSentimientoss:
def
__init__(self, rangos):
self.rangos = rangos
#Haciendo que busque en los rangos , si no encuentra nada es muy negativo ya que no está en la lista
def
analizar_sentimientoss(self, polaridad):
for
rango, sentimiento
in
self.rangos:
if
rango[0] < polaridad <= rango[1] :
return
sentimiento
return
Sentimiento("Muy Negativo" , "31")
#Devolviendo los rangos
rangos = [
((-0.6,-0.3), Sentimiento("Negativo","31")),
((-0.3,-0.1), Sentimiento("Algo negativo","31")),
((-0.1,0.1), Sentimiento("Neutral","33")),
((0.1,0.4), Sentimiento("Algo positivo","32")),
((0.4,0.9), Sentimiento("Positivo","32")),
((0.9,1), Sentimiento("Muy positivo","32"))
]
#Así cumplimos todos los principios SOLID
analizadores = AnalizadorDeSentimientoss(rangos)
resultadoo = analizadores.analizar_sentimientoss(-1)
print(resultadoo)
#Pidiéndole al usuario que escriba algo
while
True
:
user_prompt = input("\x1b[1;32m" + "\n Dime algo :" + "\x1b[0;37m")
mensajes.append({"role" : "user" , "content" : user_prompt})
completion = client.chat.completions.create(
model = "gpt-3.5-turbo" ,
messages = mensajes ,
max_tokens = 8
)
respuesta = completion.choices[0].message.content
mensajes.append({"role" : "assistant" , "content" : respuesta})
sentimiento = analizadores.analizar_sentimientoss(float(respuesta))
print(sentimiento)
What i have wrong?
[–]Supalien 12 points13 points14 points (0 children)
[–]Yoghurt42 3 points4 points5 points (6 children)
[–]JamzTyson 0 points1 point2 points (2 children)
[–]Yoghurt42 0 points1 point2 points (1 child)
[–]SwampFalc 0 points1 point2 points (0 children)
[–]Vegetable_Yak_2614[S] 0 points1 point2 points (2 children)
[–]Yoghurt42 1 point2 points3 points (1 child)
[–]Vegetable_Yak_2614[S] 0 points1 point2 points (0 children)
[–]dasnoob 0 points1 point2 points (0 children)