💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Add support for chat messages in LLM pipeline, closes #718
davidmezzetti8bd4d78cf926
diff --git a/setup.py b/setup.pyindex 48d06c58..4ce986f5 100644--- a/setup.py+++ b/setup.py@@ -59,7 +59,7 @@ extras["pipeline-data"] = ["beautifulsoup4>=4.9.3", "nltk>=3.5", "pandas>=1.1.0"extras["pipeline-image"] = ["imagehash>=4.2.1", "pillow>=7.1.2", "timm>=0.4.12"]-extras["pipeline-llm"] = ["litellm>=1.31.2", "llama-cpp-python>=0.2.20"]+extras["pipeline-llm"] = ["litellm>=1.37.16", "llama-cpp-python>=0.2.75"]extras["pipeline-text"] = ["fasttext>=0.9.2", "sentencepiece>=0.1.91"]diff --git a/src/python/txtai/pipeline/llm/generation.py b/src/python/txtai/pipeline/llm/generation.pyindex 4bae2933..aebac73b 100644--- a/src/python/txtai/pipeline/llm/generation.py+++ b/src/python/txtai/pipeline/llm/generation.py@@ -26,7 +26,10 @@ class Generation:def __call__(self, text, maxlength, **kwargs):"""- Generates text using input text+ Generates text. Supports the following input formats:++ - String or list of strings+ - List of dictionaries with `role` and `content` key-values or lists of listsArgs:text: text|list@@ -37,8 +40,8 @@ class Generation:generated text"""- # List of texts- texts = text if isinstance(text, list) else [text]+ # Format inputs+ texts = [text] if isinstance(text, str) or isinstance(text[0], dict) else text# Apply template, if necessaryif self.template:@@ -51,7 +54,8 @@ class Generation:# Clean generated textresults = [self.clean(texts[x], result) for x, result in enumerate(results)]- return results[0] if isinstance(text, str) else results+ # Extract results based on inputs+ return results[0] if isinstance(text, str) or isinstance(text[0], dict) else resultsdef execute(self, texts, maxlength, **kwargs):"""@@ -78,7 +82,7 @@ class Generation:"""# Replace input prompt- text = result.replace(prompt, "")+ text = result.replace(prompt, "") if isinstance(prompt, str) else result# Apply text cleaning rulesreturn text.replace("$=", "<=").strip()diff --git a/src/python/txtai/pipeline/llm/huggingface.py b/src/python/txtai/pipeline/llm/huggingface.pyindex e08e81ce..e288f8f6 100644--- a/src/python/txtai/pipeline/llm/huggingface.py+++ b/src/python/txtai/pipeline/llm/huggingface.py@@ -39,7 +39,10 @@ class HFLLM(HFPipeline):def __call__(self, text, prefix=None, maxlength=512, workers=0, **kwargs):"""- Generates text using input text+ Generates text. Supports the following input formats:++ - String or list of strings+ - List of dictionaries with `role` and `content` key-values or lists of listsArgs:text: text|list@@ -80,7 +83,8 @@ class HFLLM(HFPipeline):# Extract output from list, if necessaryresult = result[0] if isinstance(result, list) else result- return result["generated_text"]+ text = result["generated_text"]+ return text[-1]["content"] if isinstance(text, list) else textdef task(self, path, task, **kwargs):"""diff --git a/src/python/txtai/pipeline/llm/litellm.py b/src/python/txtai/pipeline/llm/litellm.pyindex 5709dd9f..2c08615b 100644--- a/src/python/txtai/pipeline/llm/litellm.py+++ b/src/python/txtai/pipeline/llm/litellm.py@@ -51,23 +51,15 @@ class LiteLLM(Generation):if not LITELLM:raise ImportError('LiteLLM is not available - install "pipeline" extra to enable')- # Register prompt template- self.register(path)-def execute(self, texts, maxlength, **kwargs):results = []for text in texts:- result = api.completion(model=self.path, messages=[{"content": text, "role": "user"}], max_tokens=maxlength, **{**kwargs, **self.kwargs})+ result = api.completion(+ model=self.path,+ messages=[{"content": text, "role": "prompt"}] if isinstance(text, str) else text,+ max_tokens=maxlength,+ **{**kwargs, **self.kwargs}+ )results.append(result["choices"][0]["message"]["content"])return results-- def register(self, path):- """- Registers a custom prompt template for path.-- Args:- path: model path- """-- api.register_prompt_template(model=path, roles={"user": {"pre_message": "", "post_message": ""}})diff --git a/src/python/txtai/pipeline/llm/llama.py b/src/python/txtai/pipeline/llm/llama.pyindex 0509de85..dea052b4 100644--- a/src/python/txtai/pipeline/llm/llama.py+++ b/src/python/txtai/pipeline/llm/llama.py@@ -51,8 +51,7 @@ class LlamaCpp(Generation):def execute(self, texts, maxlength, **kwargs):results = []for text in texts:- result = self.llm(text, max_tokens=maxlength, **kwargs)- results.append(result["choices"][0]["text"])+ results.append(self.messages(text, maxlength, **kwargs) if isinstance(text, list) else self.prompt(text, maxlength, **kwargs))return results@@ -75,3 +74,33 @@ class LlamaCpp(Generation):# Download and cache filereturn hf_hub_download(repo_id="/".join(parts[:repo]), filename="/".join(parts[repo:]))++ def messages(self, messages, maxlength, **kwargs):+ """+ Processes a list of messages.++ Args:+ messages: list of dictionaries with `role` and `content` key-values+ maxlength: maximum sequence length+ kwargs: additional generation keyword arguments++ Returns:+ generated text+ """++ return self.llm.create_chat_completion(messages=messages, max_tokens=maxlength, **kwargs)["choices"][0]["message"]["content"]++ def prompt(self, text, maxlength, **kwargs):+ """+ Processes a prompt.++ Args:+ prompt: prompt text+ maxlength: maximum sequence length+ kwargs: additional generation keyword arguments++ Returns:+ generated text+ """++ return self.llm(text, max_tokens=maxlength, **kwargs)["choices"][0]["text"]diff --git a/src/python/txtai/pipeline/llm/llm.py b/src/python/txtai/pipeline/llm/llm.pyindex c0cb29ed..1a659413 100644--- a/src/python/txtai/pipeline/llm/llm.py+++ b/src/python/txtai/pipeline/llm/llm.py@@ -35,7 +35,10 @@ class LLM(Pipeline):def __call__(self, text, maxlength=512, **kwargs):"""- Generates text using input text+ Generates text. Supports the following input formats:++ - String or list of strings+ - List of dictionaries with `role` and `content` key-values or lists of listsArgs:text: text|listdiff --git a/test/python/testpipeline/testllama.py b/test/python/testpipeline/testllama.pyindex f4318812..da2a6ac2 100644--- a/test/python/testpipeline/testllama.py+++ b/test/python/testpipeline/testllama.py@@ -18,5 +18,11 @@ class TestLlama(unittest.TestCase):"""# Test model generation with llama.cpp- model = LLM("TheBloke/TinyLlama-1.1B-Chat-v0.3-GGUF/tinyllama-1.1b-chat-v0.3.Q2_K.gguf")+ model = LLM("TheBloke/TinyLlama-1.1B-Chat-v0.3-GGUF/tinyllama-1.1b-chat-v0.3.Q2_K.gguf", chat_format="chatml")++ # Test with promptself.assertEqual(model("2 + 2 = ", maxlength=10, seed=0, stop=["."]), "4")++ # Test with list of messages+ messages = [{"role": "system", "content": "You are a helpful assistant. You answer math problems."}, {"role": "user", "content": "2+2?"}]+ self.assertEqual(model(messages, maxlength=10, seed=0, stop=["."]), "4")