MGLDZM commited on
Commit
19e5163
·
1 Parent(s): eb25aa8
Files changed (4) hide show
  1. logs/function.output.log +18 -0
  2. main.py +3 -5
  3. modules/llm.py +24 -7
  4. modules/settings.py +22 -10
logs/function.output.log ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': 'assistant', 'tool_calls': None}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
2
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 0, 'id': 'call_1lF6wX8Fl9qIDabi0eN1agXL', 'function': {'arguments': '', 'name': 'buscar_google'}, 'type': 'function'}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
3
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': '{"bu', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
4
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': 'squed', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
5
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': 'a": "n', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
6
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': 'otic', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
7
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': 'ias d', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
8
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': 'e hoy"', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
9
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': '}', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
10
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 1, 'id': 'call_Hqn7hcl7lEcD87e22JTyHqVf', 'function': {'arguments': '', 'name': 'buscar_google'}, 'type': 'function'}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
11
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 1, 'id': None, 'function': {'arguments': '{"bu', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
12
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 1, 'id': None, 'function': {'arguments': 'squed', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
13
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 1, 'id': None, 'function': {'arguments': 'a": "ú', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
14
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 1, 'id': None, 'function': {'arguments': 'ltim', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
15
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 1, 'id': None, 'function': {'arguments': 'as no', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
16
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 1, 'id': None, 'function': {'arguments': 'ticias', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
17
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': [{'index': 1, 'id': None, 'function': {'arguments': '"}', 'name': None}, 'type': None}]}, 'finish_reason': None, 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
18
+ {'id': 'chatcmpl-8YgAMqR5wAbKdaFDxrtFsM0CVOaWd', 'choices': [{'delta': {'content': None, 'function_call': None, 'role': None, 'tool_calls': None}, 'finish_reason': 'tool_calls', 'index': 0, 'logprobs': None}], 'created': 1703275930, 'model': 'gpt-3.5-turbo-1106', 'object': 'chat.completion.chunk', 'system_fingerprint': 'fp_772e8125bb'}
main.py CHANGED
@@ -139,17 +139,15 @@ async def get_token(response: Response, user: User_find_from_data):
139
 
140
  @app.post("/chat")
141
  async def chat_async(session: Session_find_from_data):
142
- body = model.Chat(messages = session.data["messages"])
143
- if(len(body.messages) < 1 or body.messages[-1].content==""):
144
  log_module.logger.warning(session.gid, "Mensaje RQ", "error, mensajes vacios")
145
  raise HTTPException(
146
  status_code=status.HTTP_418_IM_A_TEAPOT,
147
  detail= "Nope"
148
  )
149
 
150
- response_async = llm.ejecutar(body, session)
151
-
152
- return StreamingResponse(llm.streamer(response_async, body, session), media_type="application/json")
153
 
154
 
155
  ########################## Static Pages ##########################
 
139
 
140
  @app.post("/chat")
141
  async def chat_async(session: Session_find_from_data):
142
+ chat = model.Chat(messages = session.data["messages"])
143
+ if(len(chat.messages) < 1 or chat.messages[-1].content==""):
144
  log_module.logger.warning(session.gid, "Mensaje RQ", "error, mensajes vacios")
145
  raise HTTPException(
146
  status_code=status.HTTP_418_IM_A_TEAPOT,
147
  detail= "Nope"
148
  )
149
 
150
+ return StreamingResponse(llm.streamer(chat, session), media_type="application/json")
 
 
151
 
152
 
153
  ########################## Static Pages ##########################
modules/llm.py CHANGED
@@ -61,27 +61,42 @@ def manejar_funciones(chunk, response_async, data):
61
  })
62
  return json.dumps(f_cb), ejecutar(data)
63
 
64
- async def streamer(response_async, body, session):
 
 
 
 
65
  yield json.dumps({"comando": "status", "status":{"mensaje":"Cargando", "modo": "reemplazar"}})
66
- message = body.new_msg()
67
 
68
  st = time.time()
69
  st_times = 0
70
-
 
 
 
71
  for chunk in response_async:
 
72
  choise = chunk.choices[0]
73
  delta = choise.delta
74
 
75
  message.tokensOutput += 1
76
- if choise.finish_reason=="stop":
77
  break
78
 
79
- if delta.role:
80
  message.role = delta.role
81
  continue
82
 
83
- if delta.content:
84
  message.content += delta.content
 
 
 
 
 
 
 
85
 
86
  if time.time() - st > 3:
87
  if st_times > 2:
@@ -91,9 +106,11 @@ async def streamer(response_async, body, session):
91
  yield json.dumps({"comando": "status", "status":{"mensaje":".", "modo": "enlinea"}})
92
  st_times += 1
93
  st = time.time()
94
- message.tokensPrompt = body.tokens
95
  session.update_usage(message)
96
 
 
 
97
  yield json.dumps({"comando":"challenge", "challenge": session.challenge} )
98
  yield json.dumps({"comando":"token", "token": session.create_cookie_token() } )
99
  yield json.dumps({"comando":"mensaje", "mensaje": message.model_dump()} )
 
61
  })
62
  return json.dumps(f_cb), ejecutar(data)
63
 
64
+ async def streamer(chat: "model.Chat", session: "model.Session"):
65
+ response_async = ejecutar(chat, session)
66
+
67
+
68
+
69
  yield json.dumps({"comando": "status", "status":{"mensaje":"Cargando", "modo": "reemplazar"}})
70
+ message = chat.new_msg()
71
 
72
  st = time.time()
73
  st_times = 0
74
+ function_exec = {
75
+ "name": "",
76
+ "args": ""
77
+ }
78
  for chunk in response_async:
79
+ print(chunk.model_dump())
80
  choise = chunk.choices[0]
81
  delta = choise.delta
82
 
83
  message.tokensOutput += 1
84
+ if choise.finish_reason in ["stop", "tool_calls"]:
85
  break
86
 
87
+ elif delta.role:
88
  message.role = delta.role
89
  continue
90
 
91
+ elif delta.content:
92
  message.content += delta.content
93
+
94
+ elif delta.tool_calls:
95
+ if(name := delta.tool_calls[0].function.name):
96
+ function_exec["name"] = name
97
+ if(args := delta.tool_calls[0].function.arguments):
98
+ function_exec["args"] += args
99
+
100
 
101
  if time.time() - st > 3:
102
  if st_times > 2:
 
106
  yield json.dumps({"comando": "status", "status":{"mensaje":".", "modo": "enlinea"}})
107
  st_times += 1
108
  st = time.time()
109
+ message.tokensPrompt = chat.tokens
110
  session.update_usage(message)
111
 
112
+
113
+
114
  yield json.dumps({"comando":"challenge", "challenge": session.challenge} )
115
  yield json.dumps({"comando":"token", "token": session.create_cookie_token() } )
116
  yield json.dumps({"comando":"mensaje", "mensaje": message.model_dump()} )
modules/settings.py CHANGED
@@ -1,27 +1,39 @@
1
  import os, json
2
 
3
  from datetime import timezone, timedelta
 
4
  from Crypto.Random import get_random_bytes
5
 
 
 
 
 
 
 
 
6
  TZ = timezone(timedelta(hours=-4))
7
 
8
- GPT_MODEL = "gpt-3.5-turbo-1106"
9
 
10
- MONGO_URL = os.environ.get("MONGO_URL", None)
11
- MONGO_PWD = os.environ.get("MONGO_PWD", None)
12
- MONGO_USR = os.environ.get("MONGO_USR", None)
13
  DB_URI = f"mongodb+srv://{MONGO_USR}:{MONGO_PWD}@{MONGO_URL}/?retryWrites=true&w=majority"
 
14
 
15
- OPENAI_API_KEY=os.environ.get('OPENAI_API_KEY')
16
 
17
- USERS = json.loads(str(os.getenv("USER_KEYS")).replace("\n", ""))
 
18
 
19
- JWT_SECRET = os.environ.get('JWT_SECRET')
20
 
21
  JWT_ALGORITHM = "HS256"
22
  JWT_EXPIRATION_TIME_MINUTES_API = 7*24*60
23
  JWT_EXPIRATION_TIME_MINUTES_VIEW = 7*24*60
24
 
25
- GOOGLE_CLIENT_ID = os.environ.get("GOOGLE_CLIENT_ID")
26
- GOOGLE_CLIENT_SECRET = os.environ.get("GOOGLE_CLIENT_SECRET")
27
- OAUTH_REDIRECT = os.environ.get("OAUTH_REDIRECT")
 
 
 
 
1
  import os, json
2
 
3
  from datetime import timezone, timedelta
4
+ from modules import log_module
5
  from Crypto.Random import get_random_bytes
6
 
7
+ def environ_get(variable, required=False):
8
+ if variable not in os.environ:
9
+ log_module.logger.warning(f" Variable not set {variable}")
10
+ if not required:
11
+ return None
12
+ return os.environ[variable]
13
+
14
  TZ = timezone(timedelta(hours=-4))
15
 
16
+ GPT_MODEL = environ_get("GPT_MODEL", True)
17
 
18
+ MONGO_URL = environ_get("MONGO_URL")
19
+ MONGO_PWD = environ_get("MONGO_PWD")
20
+ MONGO_USR = environ_get("MONGO_USR")
21
  DB_URI = f"mongodb+srv://{MONGO_USR}:{MONGO_PWD}@{MONGO_URL}/?retryWrites=true&w=majority"
22
+ DB_URI = "mongodb://127.0.0.1:27017"
23
 
 
24
 
25
+ OPENAI_API_KEY=environ_get('OPENAI_API_KEY', True)
26
+ USERS = json.loads(str(environ_get("USER_KEYS", True)).replace("\n", ""))
27
 
28
+ JWT_SECRET = environ_get('JWT_SECRET', True)
29
 
30
  JWT_ALGORITHM = "HS256"
31
  JWT_EXPIRATION_TIME_MINUTES_API = 7*24*60
32
  JWT_EXPIRATION_TIME_MINUTES_VIEW = 7*24*60
33
 
34
+ GOOGLE_CLIENT_ID = environ_get("GOOGLE_CLIENT_ID", True)
35
+ GOOGLE_CLIENT_SECRET = environ_get("GOOGLE_CLIENT_SECRET", True)
36
+ GOOGLE_API_KEY = environ_get("GOOGLE_API_KEY")
37
+ GOOGLE_CSE_ID = environ_get("GOOGLE_CSE_ID")
38
+
39
+ OAUTH_REDIRECT = os.environ.get("OAUTH_REDIRECT", True)