Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API does not work with the ChatMessage #11

Open
bmstoyon opened this issue Apr 15, 2024 · 1 comment
Open

API does not work with the ChatMessage #11

bmstoyon opened this issue Apr 15, 2024 · 1 comment

Comments

@bmstoyon
Copy link

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
Copy link

IronD7 commented May 22, 2024

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}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants