-
Notifications
You must be signed in to change notification settings - Fork 269
/
cheshire_cat.py
547 lines (437 loc) · 20.4 KB
/
cheshire_cat.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import time
from copy import deepcopy
import traceback
from typing import Literal, get_args, Dict
import langchain
import os
import asyncio
import langchain
from langchain.llms import Cohere, OpenAI, AzureOpenAI, HuggingFaceTextGenInference, HuggingFaceHub
from langchain.chat_models import ChatOpenAI, AzureChatOpenAI
from langchain.base_language import BaseLanguageModel
import cat.utils as utils
from cat.log import log
from cat.db import crud
from cat.db.database import Database
from cat.rabbit_hole import RabbitHole
from cat.mad_hatter.mad_hatter import MadHatter
from cat.memory.working_memory import WorkingMemoryList, WorkingMemory
from cat.memory.long_term_memory import LongTermMemory
from cat.looking_glass.agent_manager import AgentManager
from cat.looking_glass.callbacks import NewTokenHandler
import cat.factory.llm as llms
import cat.factory.embedder as embedders
from cat.factory.custom_llm import CustomOpenAI
MSG_TYPES = Literal["notification", "chat", "error", "chat_token"]
# main class
class CheshireCat():
"""The Cheshire Cat.
This is the main class that manages everything.
Attributes
----------
ws_messages : list
List of notifications to be sent to the frontend.
"""
# CheshireCat is a singleton, this is the instance
_instance = None
# get instance or create as the constructor is called
def __new__(cls):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
"""Cat initialization.
At init time the Cat executes the bootstrap.
"""
# bootstrap the cat!
# reinstantiate MadHatter (reloads all plugins' hooks and tools)
self.mad_hatter = MadHatter(self)
# allows plugins to do something before cat components are loaded
self.mad_hatter.execute_hook("before_cat_bootstrap")
# load LLM and embedder
self.load_natural_language()
# Load memories (vector collections and working_memory)
self.load_memory()
# After memory is loaded, we can get/create tools embeddings
self.mad_hatter.embed_tools()
# Agent manager instance (for reasoning)
self.agent_manager = AgentManager(self)
# Rabbit Hole Instance
self.rabbit_hole = RabbitHole(self)
# allows plugins to do something after the cat bootstrap is complete
self.mad_hatter.execute_hook("after_cat_bootstrap")
# queue of cat messages not directly related to last user input
# i.e. finished uploading a file
self.ws_messages: Dict[str, asyncio.Queue] = {}
def load_natural_language(self):
"""Load Natural Language related objects.
The method exposes in the Cat all the NLP related stuff. Specifically, it sets the language models
(LLM and Embedder).
Warnings
--------
When using small Language Models it is suggested to turn off the memories and make the main prompt smaller
to prevent them to fail.
See Also
--------
agent_prompt_prefix
"""
# LLM and embedder
self._llm = self.get_language_model()
self.embedder = self.get_language_embedder()
def get_language_model(self) -> BaseLanguageModel:
"""Large Language Model (LLM) selection at bootstrap time.
Returns
-------
llm : BaseLanguageModel
Langchain `BaseLanguageModel` instance of the selected model.
Notes
-----
Bootstrapping is the process of loading the plugins, the natural language objects (e.g. the LLM), the memories,
the *Agent Manager* and the *Rabbit Hole*.
"""
selected_llm = crud.get_setting_by_name(name="llm_selected")
if selected_llm is None:
# return default LLM
llm = llms.LLMDefaultConfig.get_llm_from_config({})
else:
# get LLM factory class
selected_llm_class = selected_llm["value"]["name"]
FactoryClass = getattr(llms, selected_llm_class)
# obtain configuration and instantiate LLM
selected_llm_config = crud.get_setting_by_name(name=selected_llm_class)
try:
llm = FactoryClass.get_llm_from_config(selected_llm_config["value"])
except Exception as e:
import traceback
traceback.print_exc()
llm = llms.LLMDefaultConfig.get_llm_from_config({})
return llm
def get_language_embedder(self) -> embedders.EmbedderSettings:
"""Hook into the embedder selection.
Allows to modify how the Cat selects the embedder at bootstrap time.
Bootstrapping is the process of loading the plugins, the natural language objects (e.g. the LLM),
the memories, the *Agent Manager* and the *Rabbit Hole*.
Parameters
----------
cat: CheshireCat
Cheshire Cat instance.
Returns
-------
embedder : Embeddings
Selected embedder model.
"""
# Embedding LLM
selected_embedder = crud.get_setting_by_name(name="embedder_selected")
if selected_embedder is not None:
# get Embedder factory class
selected_embedder_class = selected_embedder["value"]["name"]
FactoryClass = getattr(embedders, selected_embedder_class)
# obtain configuration and instantiate Embedder
selected_embedder_config = crud.get_setting_by_name(name=selected_embedder_class)
embedder = FactoryClass.get_embedder_from_config(selected_embedder_config["value"])
return embedder
# OpenAI embedder
if type(self._llm) in [OpenAI, ChatOpenAI]:
embedder = embedders.EmbedderOpenAIConfig.get_embedder_from_config(
{
"openai_api_key": self._llm.openai_api_key,
}
)
# Azure
elif type(self._llm) in [AzureOpenAI, AzureChatOpenAI]:
embedder = embedders.EmbedderAzureOpenAIConfig.get_embedder_from_config(
{
"openai_api_key": self._llm.openai_api_key,
"openai_api_type": "azure",
"model": "text-embedding-ada-002",
# Now the only model for embeddings is text-embedding-ada-002
# It is also possible to use the Azure "deployment" name that is user defined
# when the model is deployed to Azure.
# "deployment": "my-text-embedding-ada-002",
"openai_api_base": self._llm.openai_api_base,
# https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#embeddings
# current supported versions 2022-12-01,2023-03-15-preview, 2023-05-15
# Don't mix api versions https://github.com/hwchase17/langchain/issues/4775
"openai_api_version": "2023-05-15",
}
)
# Cohere
elif type(self._llm) in [Cohere]:
embedder = embedders.EmbedderCohereConfig.get_embedder_from_config(
{
"cohere_api_key": self._llm.cohere_api_key,
"model": "embed-multilingual-v2.0",
# Now the best model for embeddings is embed-multilingual-v2.0
}
)
# HuggingFace
elif type(self._llm) in [HuggingFaceHub]:
embedder = embedders.EmbedderHuggingFaceHubConfig.get_embedder_from_config(
{
"huggingfacehub_api_token": self._llm.huggingfacehub_api_token,
"repo_id": "sentence-transformers/all-mpnet-base-v2",
}
)
# Llama-cpp-python
elif type(self._llm) in [CustomOpenAI]:
embedder = embedders.EmbedderLlamaCppConfig.get_embedder_from_config(
{
"url": self._llm.url
}
)
else:
# If no embedder matches vendor, and no external embedder is configured, we use the DumbEmbedder.
# `This embedder is not a model properly trained
# and this makes it not suitable to effectively embed text,
# "but it does not know this and embeds anyway".` - cit. Nicola Corbellini
embedder = embedders.EmbedderDumbConfig.get_embedder_from_config({})
return embedder
def load_memory(self):
"""Load LongTerMemory and WorkingMemory."""
# Memory
vector_memory_config = {"cat": self, "verbose": True}
self.memory = LongTermMemory(vector_memory_config=vector_memory_config)
# List working memory per user
self.working_memory_list = WorkingMemoryList()
# Load default shared working memory user
self.working_memory = self.working_memory_list.get_working_memory()
def recall_relevant_memories_to_working_memory(self, working_memory):
"""Retrieve context from memory.
The method retrieves the relevant memories from the vector collections that are given as context to the LLM.
Recalled memories are stored in the working memory.
Notes
-----
The user's message is used as a query to make a similarity search in the Cat's vector memories.
Five hooks allow to customize the recall pipeline before and after it is done.
See Also
--------
cat_recall_query
before_cat_recalls_memories
before_cat_recalls_episodic_memories
before_cat_recalls_declarative_memories
before_cat_recalls_procedural_memories
after_cat_recalls_memories
"""
user_id = working_memory.get_user_id()
recall_query = working_memory["user_message_json"]["text"]
# We may want to search in memory
recall_query = self.mad_hatter.execute_hook("cat_recall_query", recall_query)
log.info(f'Recall query: "{recall_query}"')
# Embed recall query
recall_query_embedding = self.embedder.embed_query(recall_query)
working_memory["recall_query"] = recall_query
# hook to do something before recall begins
self.mad_hatter.execute_hook("before_cat_recalls_memories")
# Setting default recall configs for each memory
# TODO: can these data structrues become instances of a RecallSettings class?
default_episodic_recall_config = {
"embedding": recall_query_embedding,
"k": 3,
"threshold": 0.7,
"metadata": {"source": user_id},
}
default_declarative_recall_config = {
"embedding": recall_query_embedding,
"k": 3,
"threshold": 0.7,
"metadata": None,
}
default_procedural_recall_config = {
"embedding": recall_query_embedding,
"k": 3,
"threshold": 0.7,
"metadata": None,
}
# hooks to change recall configs for each memory
recall_configs = [
self.mad_hatter.execute_hook("before_cat_recalls_episodic_memories", default_episodic_recall_config),
self.mad_hatter.execute_hook("before_cat_recalls_declarative_memories", default_declarative_recall_config),
self.mad_hatter.execute_hook("before_cat_recalls_procedural_memories", default_procedural_recall_config)
]
memory_types = self.memory.vectors.collections.keys()
for config, memory_type in zip(recall_configs, memory_types):
memory_key = f"{memory_type}_memories"
# recall relevant memories for collection
vector_memory = getattr(self.memory.vectors, memory_type)
memories = vector_memory.recall_memories_from_embedding(**config)
working_memory[memory_key] = memories
# hook to modify/enrich retrieved memories
self.mad_hatter.execute_hook("after_cat_recalls_memories")
def llm(self, prompt: str, chat: bool = False, stream: bool = False) -> str:
"""Generate a response using the LLM model.
This method is useful for generating a response with both a chat and a completion model using the same syntax
Parameters
----------
prompt : str
The prompt for generating the response.
Returns
-------
str
The generated response.
"""
# should we stream the tokens?
callbacks = []
if stream:
callbacks.append(NewTokenHandler(self))
# Check if self._llm is a completion model and generate a response
if isinstance(self._llm, langchain.llms.base.BaseLLM):
return self._llm(prompt, callbacks=callbacks)
# Check if self._llm is a chat model and call it as a completion model
if isinstance(self._llm, langchain.chat_models.base.BaseChatModel):
return self._llm.call_as_llm(prompt, callbacks=callbacks)
def send_ws_message(self, content: str, msg_type: MSG_TYPES = "notification", working_memory: WorkingMemory = None):
"""Send a message via websocket.
This method is useful for sending a message via websocket directly without passing through the LLM
Parameters
----------
working_memory
content : str
The content of the message.
msg_type : str
The type of the message. Should be either `notification`, `chat` or `error`
"""
# no working memory passed, send message to default user
if working_memory is None:
working_memory = self.working_memory_list.get_working_memory()
options = get_args(MSG_TYPES)
if msg_type not in options:
raise ValueError(f"The message type `{msg_type}` is not valid. Valid types: {', '.join(options)}")
if msg_type == "error":
asyncio.run(
working_memory.ws_messages.put(
{
"type": msg_type,
"name": "GenericError",
"description": content
}
)
)
else:
asyncio.run(
working_memory.ws_messages.put(
{
"type": msg_type,
"content": content
}
)
)
def __call__(self, user_message_json):
"""Call the Cat instance.
This method is called on the user's message received from the client.
Parameters
----------
user_message_json : dict
Dictionary received from the Websocket client.
Returns
-------
final_output : dict
Dictionary with the Cat's answer to be sent to the client.
Notes
-----
Here happens the main pipeline of the Cat. Namely, the Cat receives the user's input and recall the memories.
The retrieved context is formatted properly and given in input to the Agent that uses the LLM to produce the
answer. This is formatted in a dictionary to be sent as a JSON via Websocket to the client.
"""
log.info(user_message_json)
# Change working memory based on received user_id
user_id = user_message_json.get('user_id', 'user')
user_message_json['user_id'] = user_id
# ccat class working memory is the default "user" working memory
# self.working_memory = self.working_memory_list.get_working_memory(user_id)
user_working_memory = self.working_memory_list.get_working_memory(user_id)
# hook to modify/enrich user input
user_message_json = self.mad_hatter.execute_hook("before_cat_reads_message", user_message_json)
# store last message in working memory
user_working_memory["user_message_json"] = user_message_json
# recall episodic and declarative memories from vector collections
# and store them in working_memory
try:
self.recall_relevant_memories_to_working_memory(user_working_memory)
except Exception as e:
log.error(e)
traceback.print_exc(e)
err_message = (
"You probably changed Embedder and old vector memory is not compatible. "
"Please delete `core/long_term_memory` folder."
)
return {
"type": "error",
"name": "VectorMemoryError",
"description": err_message,
}
# reply with agent
try:
cat_message = self.agent_manager.execute_agent(user_working_memory)
except Exception as e:
# This error happens when the LLM
# does not respect prompt instructions.
# We grab the LLM output here anyway, so small and
# non instruction-fine-tuned models can still be used.
error_description = str(e)
log.error(error_description)
if not "Could not parse LLM output: `" in error_description:
raise e
unparsable_llm_output = error_description.replace("Could not parse LLM output: `", "").replace("`", "")
cat_message = {
"input": user_working_memory["user_message_json"]["text"],
"intermediate_steps": [],
"output": unparsable_llm_output
}
log.info("cat_message:")
log.info(cat_message)
# update conversation history
user_message = user_working_memory["user_message_json"]["text"]
user_working_memory.update_conversation_history(who="Human", message=user_message)
user_working_memory.update_conversation_history(who="AI", message=cat_message["output"])
# store user message in episodic memory
# TODO: vectorize and store also conversation chunks
# (not raw dialog, but summarization)
_ = self.memory.vectors.episodic.add_texts(
[user_message],
[{"source": user_id, "when": time.time()}],
)
# build data structure for output (response and why with memories)
episodic_report = [dict(d[0]) | {"score": float(d[1]), "id": d[3]} for d in user_working_memory["episodic_memories"]]
declarative_report = [dict(d[0]) | {"score": float(d[1]), "id": d[3]} for d in user_working_memory["declarative_memories"]]
procedural_report = [dict(d[0]) | {"score": float(d[1]), "id": d[3]} for d in user_working_memory["procedural_memories"]]
final_output = {
"type": "chat",
"user_id": user_id,
"content": cat_message.get("output"),
"why": {
"input": cat_message.get("input"),
"intermediate_steps": cat_message.get("intermediate_steps"),
"memory": {
"episodic": episodic_report,
"declarative": declarative_report,
"procedural": procedural_report,
},
},
}
final_output = self.mad_hatter.execute_hook("before_cat_sends_message", final_output)
return final_output
# TODO: remove this method in a few versions, current version 1.2.0
def get_base_url():
"""Allows the Cat exposing the base url."""
log.warning("This method will be removed, import cat.utils tu use it instead.")
return utils.get_base_url()
# TODO: remove this method in a few versions, current version 1.2.0
def get_base_path():
"""Allows the Cat exposing the base path."""
log.warning("This method will be removed, import cat.utils tu use it instead.")
return utils.get_base_path()
# TODO: remove this method in a few versions, current version 1.2.0
def get_plugins_path():
"""Allows the Cat exposing the plugins path."""
log.warning("This method will be removed, import cat.utils tu use it instead.")
return utils.get_plugins_path()
# TODO: remove this method in a few versions, current version 1.2.0
def get_static_url():
"""Allows the Cat exposing the static server url."""
log.warning("This method will be removed, import cat.utils tu usit instead.")
return utils.get_static_url()
# TODO: remove this method in a few versions, current version 1.2.0
def get_static_path():
"""Allows the Cat exposing the static files path."""
log.warning("This method will be removed, import cat.utils tu usit instead.")
return utils.get_static_path()