deepset-ai / hayhooks

Deploy Haystack pipelines behind a REST Api.
https://haystack.deepset.ai
Apache License 2.0
30 stars 8 forks source link

API does not work with the ChatMessage #11

Open bmstoyon opened 2 months ago

bmstoyon commented 2 months ago

Hayhooks fails with Chat Messages because the pydantic conversion to a dict remove ChatMessage class.

I had to work around like this:

        dict_local = pipeline_run_req.dict()
        dict_local["prompt_builder"]["prompt_source"] = pipeline_run_req.prompt_builder.prompt_source
        result = pipe.run(data=dict_local)
IronD7 commented 1 month ago

Hey!

I have a similar problem where I send a messages dict to a Multiplexer component in a pipeline. But it is not converted to a List[ChatMessage] but rather a dict is passed on.

So my serialized pipeline looks like this:

chat_pipeline = Pipeline()
chat_pipeline.add_component("msg_receiver", Multiplexer(List[ChatMessage]))
chat_pipeline.add_component("generator", chat_generator)
chat_pipeline.connect("msg_receiver.value", "generator.messages")

and in my API call

  "data": {
    "msg_receiver": {
      "value": [
        {
          "content": "Test",
          "role": "user",
          "name": null
        }
      ]
    },
    "generator": {},

this raises an error in the Generator:

File "/home/anaconda3/envs/hsenv/lib/python3.10/site-packages/haystack/core/pipeline/pipeline.py", line 864, in run res = comp.run(**last_inputs[name]) File "/home/anaconda3/envs/hsenv/lib/python3.10/site-packages/haystack/components/generators/chat/hugging_face_api.py", line 194, in run formatted_messages = [m.to_openai_format() for m in messages] File "/home/anaconda3/envs/hsenv/lib/python3.10/site-packages/haystack/components/generators/chat/hugging_face_api.py", line 194, in formatted_messages = [m.to_openai_format() for m in messages] AttributeError: 'dict' object has no attribute 'to_openai_format

Update:

A custom component included before the multiplexer helped me work around the errors - leaving it here in case somebody else runs into the same issue:

from typing import List, Dict, Any
from haystack import component
from haystack.dataclasses import ChatMessage, ChatRole

@component
class ChatMessageRepairer:

    @component.output_types(messages=List[ChatMessage])
    def run(self, raw_messages: List[Dict[str, Any]]):
        messages = []
        for msg in raw_messages:
            try:
                role = ChatRole(msg["role"])
                messages.append(ChatMessage(content=msg["content"], role=role, name=None))
            except ValueError:
                raise ValueError(f"{role_str} is not a valid ChatRole")

        return {"messages": messages}