kikocis/llama-stack public
Changesets · Branches · Tags · Compare
| Changeset | 0778f181e3a8ec2d283ba0c38903dda3b75f179cc142626f8c6f6c1e0b2cded0 |
|---|---|
| Author | Hardik Shah <hjshah@meta.com> · 2024-09-06 13:10 -07:00 |
| Committer | GitHub <noreply@github.com> · 2024-09-06 13:10 -07:00 |
| Parents | 355801dfca31 |
3 files changed
modified llama_toolchain/agentic_system/api/api.py +38 −1
| @@ -116,10 +116,47 @@ | ||
| 116 | 116 | ] |
| 117 | 117 | |
| 118 | 118 | |
| 119 | -@json_schema_type | |
| 119 | +class MemoryQueryGenerator(Enum): | |
| 120 | + default = "default" | |
| 121 | + llm = "llm" | |
| 122 | + custom = "custom" | |
| 123 | + | |
| 124 | + | |
| 125 | +class DefaultMemoryQueryGeneratorConfig(BaseModel): | |
| 126 | + type: Literal[MemoryQueryGenerator.default.value] = ( | |
| 127 | + MemoryQueryGenerator.default.value | |
| 128 | + ) | |
| 129 | + sep: str = " " | |
| 130 | + | |
| 131 | + | |
| 132 | +class LLMMemoryQueryGeneratorConfig(BaseModel): | |
| 133 | + type: Literal[MemoryQueryGenerator.llm.value] = MemoryQueryGenerator.llm.value | |
| 134 | + model: str | |
| 135 | + template: str | |
| 136 | + | |
| 137 | + | |
| 138 | +class CustomMemoryQueryGeneratorConfig(BaseModel): | |
| 139 | + type: Literal[MemoryQueryGenerator.custom.value] = MemoryQueryGenerator.custom.value | |
| 140 | + | |
| 141 | + | |
| 142 | +MemoryQueryGeneratorConfig = Annotated[ | |
| 143 | + Union[ | |
| 144 | + DefaultMemoryQueryGeneratorConfig, | |
| 145 | + LLMMemoryQueryGeneratorConfig, | |
| 146 | + CustomMemoryQueryGeneratorConfig, | |
| 147 | + ], | |
| 148 | + Field(discriminator="type"), | |
| 149 | +] | |
| 150 | + | |
| 151 | + | |
| 120 | 152 | class MemoryToolDefinition(ToolDefinitionCommon): |
| 121 | 153 | type: Literal[AgenticSystemTool.memory.value] = AgenticSystemTool.memory.value |
| 122 | 154 | memory_bank_configs: List[MemoryBankConfig] = Field(default_factory=list) |
| 155 | + # This config defines how a query is generated using the messages | |
| 156 | + # for memory bank retrieval. | |
| 157 | + query_generator_config: MemoryQueryGeneratorConfig = Field( | |
| 158 | + default=DefaultMemoryQueryGeneratorConfig() | |
| 159 | + ) | |
| 123 | 160 | max_tokens_in_context: int = 4096 |
| 124 | 161 | max_chunks: int = 10 |
| 125 | 162 |
modified llama_toolchain/agentic_system/meta_reference/agent_instance.py +4 −1
| @@ -31,6 +31,7 @@ | ||
| 31 | 31 | SingleMessageBuiltinTool, |
| 32 | 32 | ) |
| 33 | 33 | |
| 34 | +from .rag.context_retriever import generate_rag_query | |
| 34 | 35 | from .safety import SafetyException, ShieldRunnerMixin |
| 35 | 36 | |
| 36 | 37 | |
| @@ -664,7 +665,9 @@ | ||
| 664 | 665 | # (i.e., no prior turns uploaded an Attachment) |
| 665 | 666 | return None, [] |
| 666 | 667 | |
| 667 | - query = " ".join(m.content for m in messages) | |
| 668 | + query = await generate_rag_query( | |
| 669 | + memory.query_generator_config, messages, inference_api=self.inference_api | |
| 670 | + ) | |
| 668 | 671 | tasks = [ |
| 669 | 672 | self.memory_api.query_documents( |
| 670 | 673 | bank_id=bank_id, |
added llama_toolchain/agentic_system/meta_reference/rag/context_retriever.py +76 −0
| @@ -0,0 +1,76 @@ | ||
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. | |
| 2 | +# All rights reserved. | |
| 3 | +# | |
| 4 | +# This source code is licensed under the terms described in the LICENSE file in | |
| 5 | +# the root directory of this source tree. | |
| 6 | + | |
| 7 | +from typing import List | |
| 8 | + | |
| 9 | +from jinja2 import Template | |
| 10 | +from llama_models.llama3.api import * # noqa: F403 | |
| 11 | + | |
| 12 | + | |
| 13 | +from llama_toolchain.agentic_system.api import ( | |
| 14 | + DefaultMemoryQueryGeneratorConfig, | |
| 15 | + LLMMemoryQueryGeneratorConfig, | |
| 16 | + MemoryQueryGenerator, | |
| 17 | + MemoryQueryGeneratorConfig, | |
| 18 | +) | |
| 19 | +from termcolor import cprint # noqa: F401 | |
| 20 | +from llama_toolchain.inference.api import * # noqa: F403 | |
| 21 | + | |
| 22 | + | |
| 23 | +async def generate_rag_query( | |
| 24 | + config: MemoryQueryGeneratorConfig, | |
| 25 | + messages: List[Message], | |
| 26 | + **kwargs, | |
| 27 | +): | |
| 28 | + """ | |
| 29 | + Generates a query that will be used for | |
| 30 | + retrieving relevant information from the memory bank. | |
| 31 | + """ | |
| 32 | + if config.type == MemoryQueryGenerator.default.value: | |
| 33 | + query = await default_rag_query_generator(config, messages, **kwargs) | |
| 34 | + elif config.type == MemoryQueryGenerator.llm.value: | |
| 35 | + query = await llm_rag_query_generator(config, messages, **kwargs) | |
| 36 | + else: | |
| 37 | + raise NotImplementedError(f"Unsupported memory query generator {config.type}") | |
| 38 | + # cprint(f"Generated query >>>: {query}", color="green") | |
| 39 | + return query | |
| 40 | + | |
| 41 | + | |
| 42 | +async def default_rag_query_generator( | |
| 43 | + config: DefaultMemoryQueryGeneratorConfig, | |
| 44 | + messages: List[Message], | |
| 45 | + **kwargs, | |
| 46 | +): | |
| 47 | + return config.sep.join(interleaved_text_media_as_str(m.content) for m in messages) | |
| 48 | + | |
| 49 | + | |
| 50 | +async def llm_rag_query_generator( | |
| 51 | + config: LLMMemoryQueryGeneratorConfig, | |
| 52 | + messages: List[Message], | |
| 53 | + **kwargs, | |
| 54 | +): | |
| 55 | + assert "inference_api" in kwargs, "LLMRAGQueryGenerator needs inference_api" | |
| 56 | + inference_api = kwargs["inference_api"] | |
| 57 | + | |
| 58 | + m_dict = {"messages": [m.model_dump() for m in messages]} | |
| 59 | + | |
| 60 | + template = Template(config.template) | |
| 61 | + content = template.render(m_dict) | |
| 62 | + | |
| 63 | + model = config.model | |
| 64 | + message = UserMessage(content=content) | |
| 65 | + response = inference_api.chat_completion( | |
| 66 | + ChatCompletionRequest( | |
| 67 | + model=model, | |
| 68 | + messages=[message], | |
| 69 | + stream=False, | |
| 70 | + ) | |
| 71 | + ) | |
| 72 | + | |
| 73 | + async for chunk in response: | |
| 74 | + query = chunk.completion_message.content | |
| 75 | + | |
| 76 | + return query |