Skip to content

Commit 2a64cd4

Browse files
authored
docs: update ChatMessage examples (#9394)
1 parent d8ff508 commit 2a64cd4

File tree

6 files changed

+26
-21
lines changed

6 files changed

+26
-21
lines changed

haystack/components/builders/chat_prompt_builder.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,24 @@ class ChatPromptBuilder:
7575
"template": messages}})
7676
print(res)
7777
78-
>> {'llm': {'replies': [ChatMessage(content="Berlin is the capital city of Germany and one of the most vibrant
78+
>> {'llm': {'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text=
79+
"Berlin is the capital city of Germany and one of the most vibrant
7980
and diverse cities in Europe. Here are some key things to know...Enjoy your time exploring the vibrant and dynamic
80-
capital of Germany!", role=<ChatRole.ASSISTANT: 'assistant'>, name=None, meta={'model': 'gpt-4o-mini',
81+
capital of Germany!")], _name=None, _meta={'model': 'gpt-4o-mini',
8182
'index': 0, 'finish_reason': 'stop', 'usage': {'prompt_tokens': 27, 'completion_tokens': 681, 'total_tokens':
8283
708}})]}}
8384
84-
8585
messages = [system_message, ChatMessage.from_user("What's the weather forecast for {{location}} in the next
8686
{{day_count}} days?")]
8787
8888
res = pipe.run(data={"prompt_builder": {"template_variables": {"location": location, "day_count": "5"},
8989
"template": messages}})
9090
9191
print(res)
92-
>> {'llm': {'replies': [ChatMessage(content="Here is the weather forecast for Berlin in the next 5
92+
>> {'llm': {'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text=
93+
"Here is the weather forecast for Berlin in the next 5
9394
days:\\n\\nDay 1: Mostly cloudy with a high of 22°C (72°F) and...so it's always a good idea to check for updates
94-
closer to your visit.", role=<ChatRole.ASSISTANT: 'assistant'>, name=None, meta={'model': 'gpt-4o-mini',
95+
closer to your visit.")], _name=None, _meta={'model': 'gpt-4o-mini',
9596
'index': 0, 'finish_reason': 'stop', 'usage': {'prompt_tokens': 37, 'completion_tokens': 201,
9697
'total_tokens': 238}})]}}
9798
```

haystack/components/connectors/openapi_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ class OpenAPIServiceConnector:
188188
service_openapi_spec=serperdev_openapi_spec, service_credentials=serper_token)
189189
print(result)
190190
191-
>> {'service_response': [ChatMessage(content='{"searchParameters": {"q": "Why was Sam Altman ousted from OpenAI?",
191+
>> {'service_response': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text=
192+
>> '{"searchParameters": {"q": "Why was Sam Altman ousted from OpenAI?",
192193
>> "type": "search", "engine": "google"}, "answerBox": {"snippet": "Concerns over AI safety and OpenAI\'s role
193194
>> in protecting were at the center of Altman\'s brief ouster from the company."...
194195
```

haystack/components/generators/chat/azure.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ class AzureOpenAIChatGenerator(OpenAIChatGenerator):
5757
5858
```
5959
{'replies':
60-
[ChatMessage(content='Natural Language Processing (NLP) is a branch of artificial intelligence that focuses on
61-
enabling computers to understand, interpret, and generate human language in a way that is useful.',
62-
role=<ChatRole.ASSISTANT: 'assistant'>, name=None,
63-
meta={'model': 'gpt-4o-mini', 'index': 0, 'finish_reason': 'stop',
60+
[ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text=
61+
"Natural Language Processing (NLP) is a branch of artificial intelligence that focuses on
62+
enabling computers to understand, interpret, and generate human language in a way that is useful.")],
63+
_name=None,
64+
_meta={'model': 'gpt-4o-mini', 'index': 0, 'finish_reason': 'stop',
6465
'usage': {'prompt_tokens': 15, 'completion_tokens': 36, 'total_tokens': 51}})]
6566
}
6667
```

haystack/components/generators/chat/hugging_face_local.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ class HuggingFaceLocalChatGenerator:
105105
106106
```
107107
{'replies':
108-
[ChatMessage(content=' Natural Language Processing (NLP) is a subfield of artificial intelligence that deals
108+
[ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text=
109+
"Natural Language Processing (NLP) is a subfield of artificial intelligence that deals
109110
with the interaction between computers and human language. It enables computers to understand, interpret, and
110111
generate human language in a valuable way. NLP involves various techniques such as speech recognition, text
111112
analysis, sentiment analysis, and machine translation. The ultimate goal is to make it easier for computers to
112-
process and derive meaning from human language, improving communication between humans and machines.',
113-
role=<ChatRole.ASSISTANT: 'assistant'>,
114-
name=None,
115-
meta={'finish_reason': 'stop', 'index': 0, 'model':
113+
process and derive meaning from human language, improving communication between humans and machines.")],
114+
_name=None,
115+
_meta={'finish_reason': 'stop', 'index': 0, 'model':
116116
'mistralai/Mistral-7B-Instruct-v0.2',
117117
'usage': {'completion_tokens': 90, 'prompt_tokens': 19, 'total_tokens': 109}})
118118
]

haystack/components/generators/chat/openai.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ class OpenAIChatGenerator:
6767
Output:
6868
```
6969
{'replies':
70-
[ChatMessage(content='Natural Language Processing (NLP) is a branch of artificial intelligence
70+
[ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=
71+
[TextContent(text="Natural Language Processing (NLP) is a branch of artificial intelligence
7172
that focuses on enabling computers to understand, interpret, and generate human language in
72-
a way that is meaningful and useful.',
73-
role=<ChatRole.ASSISTANT: 'assistant'>, name=None,
74-
meta={'model': 'gpt-4o-mini', 'index': 0, 'finish_reason': 'stop',
73+
a way that is meaningful and useful.")],
74+
_name=None,
75+
_meta={'model': 'gpt-4o-mini', 'index': 0, 'finish_reason': 'stop',
7576
'usage': {'prompt_tokens': 15, 'completion_tokens': 36, 'total_tokens': 51}})
7677
]
7778
}

haystack/components/validators/json_schema.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ def run(self, messages: List[ChatMessage]) -> dict:
8181
}
8282
})
8383
print(result)
84-
>> {'schema_validator': {'validated': [ChatMessage(content='\\n{\\n "name": "John",\\n "age": 30\\n}',
85-
role=<ChatRole.ASSISTANT: 'assistant'>, name=None, meta={'model': 'gpt-4-1106-preview', 'index': 0,
84+
>> {'schema_validator': {'validated': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>,
85+
_content=[TextContent(text="\\n{\\n "name": "John",\\n "age": 30\\n}")],
86+
_name=None, _meta={'model': 'gpt-4-1106-preview', 'index': 0,
8687
'finish_reason': 'stop', 'usage': {'completion_tokens': 17, 'prompt_tokens': 20, 'total_tokens': 37}})]}}
8788
```
8889
"""

0 commit comments

Comments
 (0)