-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsample_pipeline.py
28 lines (22 loc) · 1009 Bytes
/
sample_pipeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack.components.generators.chat import HuggingFaceLocalChatGenerator
from haystack import Pipeline
from dotenv import load_dotenv
import os
template = [ChatMessage.from_user("""Please create a summary about the following topic:{{ topic }}""")]
prompt_builder = ChatPromptBuilder(template=template)
llm = HuggingFaceLocalChatGenerator(model="Qwen/Qwen2.5-1.5B-Instruct", generation_kwargs={"max_new_tokens": 150})
# Inititalize pipeline
pipe = Pipeline()
# Add components
pipe.add_component(instance=prompt_builder, name="prompt_builder")
pipe.add_component(instance=llm, name="llm" )
pipe.connect("prompt_builder.prompt", "llm.messages")
response = pipe.run({"prompt_builder": {"topic": "The history of the internet"}})
print(response)
# Serialize the pipeline
yaml_pipeline = pipe.dumps()
# Save the pipeline to a file
with open("chat_pipeline.yaml", "w") as f:
f.write(yaml_pipeline)