Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Fix OpenAI chat message selection

FU-max-boop01b456900a9e
diff --git a/src/python/txtai/api/routers/openai.py b/src/python/txtai/api/routers/openai.py
index 0a793ce1..1b19291a 100644
--- a/src/python/txtai/api/routers/openai.py
+++ b/src/python/txtai/api/routers/openai.py
@@ -44,8 +44,8 @@ def chat(
# Build keyword arguments
kwargs = {key: value for key, value in [("stream", stream), ("maxlength", max_completion_tokens)] if value}
- # Get first message
- message = messages[0]["content"]
+ # Get latest message
+ message = messages[-1]["content"]
# Agent
if model in application.get().agents:
diff --git a/test/python/testapi/testopenai.py b/test/python/testapi/testopenai.py
index 0fba6d40..6fe54276 100644
--- a/test/python/testapi/testopenai.py
+++ b/test/python/testapi/testopenai.py
@@ -122,6 +122,24 @@ class TestOpenAI(unittest.TestCase):
self.assertIsNotNone(response["choices"][0]["message"]["content"])
+ def testChatLatestMessage(self):
+ """
+ Test a chat completion with multiple messages
+ """
+
+ response = self.client.post(
+ "/v1/chat/completions",
+ json={
+ "messages": [
+ {"role": "system", "content": "You are a helpful assistant"},
+ {"role": "user", "content": "Hello"},
+ ],
+ "model": "segmentation",
+ },
+ ).json()
+
+ self.assertEqual(response["choices"][0]["message"]["content"], "Hello")
+
def testChatPipeline(self):
"""
Test a chat completion with a pipeline