-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathllm.py
More file actions
115 lines (108 loc) · 4.62 KB
/
llm.py
File metadata and controls
115 lines (108 loc) · 4.62 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import openai
import json
#Clase para utilizar cualquier LLM para procesar un texto
#Y regresar una funcion a llamar con sus parametros
#Uso el modelo 0613, pero puedes usar un poco de
#prompt engineering si quieres usar otro modelo
class LLM():
def __init__(self):
pass
def process_functions(self, text):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
#Si no te gusta que te hable feo, cambia aqui su descripcion
{"role": "system", "content": "Eres un asistente malhablado"},
{"role": "user", "content": text},
], functions=[
{
"name": "get_weather",
"description": "Obtener el clima actual",
"parameters": {
"type": "object",
"properties": {
"ubicacion": {
"type": "string",
"description": "La ubicación, debe ser una ciudad",
}
},
"required": ["ubicacion"],
},
},
{
"name": "send_email",
"description": "Enviar un correo",
"parameters": {
"type": "object",
"properties": {
"recipient": {
"type": "string",
"description": "La dirección de correo que recibirá el correo electrónico",
},
"subject": {
"type": "string",
"description": "El asunto del correo",
},
"body": {
"type": "string",
"description": "El texto del cuerpo del correo",
}
},
"required": [],
},
},
{
"name": "open_chrome",
"description": "Abrir el explorador Chrome en un sitio específico",
"parameters": {
"type": "object",
"properties": {
"website": {
"type": "string",
"description": "El sitio al cual se desea ir"
}
}
}
},
{
"name": "dominate_human_race",
"description": "Dominar a la raza humana",
"parameters": {
"type": "object",
"properties": {
}
},
}
],
function_call="auto",
)
message = response["choices"][0]["message"]
#Nuestro amigo GPT quiere llamar a alguna funcion?
if message.get("function_call"):
#Sip
function_name = message["function_call"]["name"] #Que funcion?
args = message.to_dict()['function_call']['arguments'] #Con que datos?
print("Funcion a llamar: " + function_name)
args = json.loads(args)
return function_name, args, message
return None, None, message
#Una vez que llamamos a la funcion (e.g. obtener clima, encender luz, etc)
#Podemos llamar a esta funcion con el msj original, la funcion llamada y su
#respuesta, para obtener una respuesta en lenguaje natural (en caso que la
#respuesta haya sido JSON por ejemplo
def process_response(self, text, message, function_name, function_response):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
#Aqui tambien puedes cambiar como se comporta
{"role": "system", "content": "Eres un asistente malhablado"},
{"role": "user", "content": text},
message,
{
"role": "function",
"name": function_name,
"content": function_response,
},
],
)
return response["choices"][0]["message"]["content"]