MCPcopy Index your code
hub / github.com/TaskingAI/TaskingAI / create

Method create

backend/app/operators/assistant/message.py:16–62  ·  view source on GitHub ↗
(
        self,
        create_dict: Dict,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

14
15class MessageModelOperator(PostgresModelOperator):
16 async def create(
17 self,
18 create_dict: Dict,
19 **kwargs,
20 ) -> ModelEntity:
21 from app.services.assistant.chat import update_chat_memory
22
23 # handle kwargs
24 self._check_kwargs(object_id_required=None, **kwargs)
25 assistant_id = kwargs.get("assistant_id")
26 chat_id = kwargs.get("chat_id")
27
28 # attributes
29 request = MessageCreateRequest(**create_dict)
30 role: MessageRole = request.role
31 content: MessageContent = request.content
32 metadata: Dict[str, str] = request.metadata
33
34 # get chat
35 chat = await chat_ops.get(
36 assistant_id=assistant_id,
37 chat_id=chat_id,
38 )
39
40 # count tokens
41 # todo: enable other tokenizer
42 num_tokens = default_tokenizer.count_tokens(content.text)
43
44 # create message
45 message = await super().create(
46 assistant_id=assistant_id,
47 chat_id=chat_id,
48 create_dict={
49 "role": role.value,
50 "content": content.model_dump(),
51 "num_tokens": num_tokens,
52 "metadata": metadata,
53 },
54 )
55
56 # update chat memory
57 updated_chat_memory: ChatMemory = await chat.memory.update_memory(
58 new_message_text=content.text, new_message_token_count=num_tokens, role=role.value
59 )
60 await update_chat_memory(chat=chat, memory=updated_chat_memory)
61
62 return message
63
64
65message_ops = MessageModelOperator(

Callers

nothing calls this directly

Calls 7

update_chat_memoryFunction · 0.90
_check_kwargsMethod · 0.80
count_tokensMethod · 0.80
update_memoryMethod · 0.80
getMethod · 0.45
model_dumpMethod · 0.45

Tested by

no test coverage detected