|
1 | | -from couchbase.options import KnownConfigProfiles |
| 1 | +import os |
2 | 2 | from haystack import GeneratedAnswer, Pipeline |
3 | 3 | from haystack.components.builders.answer_builder import AnswerBuilder |
4 | | -from haystack.components.builders.prompt_builder import PromptBuilder |
| 4 | +from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder |
| 5 | +from haystack.dataclasses import ChatMessage |
5 | 6 | from haystack.components.embedders import SentenceTransformersTextEmbedder |
6 | | -from haystack.components.generators import HuggingFaceAPIGenerator |
| 7 | +from haystack.components.generators.chat import HuggingFaceAPIChatGenerator |
| 8 | +from haystack.utils.hf import HFGenerationAPIType |
7 | 9 | from haystack.utils import Secret |
8 | | - |
9 | 10 | from couchbase_haystack import ( |
10 | 11 | CouchbaseClusterOptions, |
11 | 12 | CouchbasePasswordAuthenticator, |
12 | 13 | CouchbaseQueryDocumentStore, |
13 | 14 | CouchbaseQueryEmbeddingRetriever, |
14 | 15 | QueryVectorSearchType, |
15 | 16 | ) |
| 17 | +from couchbase.options import KnownConfigProfiles |
16 | 18 |
|
17 | 19 | # Load HF Token from environment variables. |
18 | 20 | HF_TOKEN = Secret.from_env_var("HF_API_TOKEN") |
|
26 | 28 | # couchbase:enterprise-8.0.0 |
27 | 29 |
|
28 | 30 | document_store = CouchbaseQueryDocumentStore( |
29 | | - cluster_connection_string=Secret.from_token("localhost"), |
30 | | - authenticator=CouchbasePasswordAuthenticator(username=Secret.from_token("username"), password=Secret.from_token("password")), |
| 31 | + cluster_connection_string=Secret.from_env_var("CONNECTION_STRING"), |
| 32 | + authenticator=CouchbasePasswordAuthenticator(username=Secret.from_env_var("USER_NAME"), password=Secret.from_env_var("PASSWORD")), |
31 | 33 | cluster_options=CouchbaseClusterOptions( |
32 | 34 | profile=KnownConfigProfiles.WanDevelopment, |
33 | 35 | ), |
34 | | - bucket="haystack_bucket_name", |
35 | | - scope="haystack_scope_name", |
36 | | - collection="haystack_collection_name", |
| 36 | + bucket=os.getenv("BUCKET_NAME"), |
| 37 | + scope=os.getenv("SCOPE_NAME"), |
| 38 | + collection=os.getenv("COLLECTION_NAME"), |
37 | 39 | search_type=QueryVectorSearchType.ANN, |
38 | 40 | similarity="L2", |
39 | 41 | nprobes=10, |
40 | 42 | ) |
41 | 43 |
|
42 | | -# Build a RAG pipeline with a Retriever to get relevant documents to the query and a HuggingFaceTGIGenerator |
| 44 | +# Build a RAG pipeline with a Retriever to get relevant documents to the query and a HuggingFaceAPIChatGenerator |
43 | 45 | # interacting with LLMs using a custom prompt. |
44 | | -prompt_template = """ |
45 | | -Given these documents, answer the question.\nDocuments: |
| 46 | +prompt_messages = [ |
| 47 | + ChatMessage.from_system("You are a helpful assistant that answers questions based on the provided documents."), |
| 48 | + ChatMessage.from_user("""Given these documents, answer the question. |
| 49 | +Documents: |
46 | 50 | {% for doc in documents %} |
47 | 51 | {{ doc.content }} |
48 | 52 | {% endfor %} |
49 | 53 |
|
50 | | -\nQuestion: {{question}} |
51 | | -\nAnswer: |
52 | | -""" |
| 54 | +Question: {{question}} |
| 55 | +Answer:""") |
| 56 | +] |
53 | 57 | rag_pipeline = Pipeline() |
54 | 58 | rag_pipeline.add_component( |
55 | 59 | "query_embedder", |
56 | 60 | SentenceTransformersTextEmbedder(model="sentence-transformers/all-MiniLM-L6-v2", progress_bar=False), |
57 | 61 | ) |
58 | 62 | rag_pipeline.add_component("retriever", CouchbaseQueryEmbeddingRetriever(document_store=document_store)) |
59 | | -rag_pipeline.add_component("prompt_builder", PromptBuilder(template=prompt_template)) |
60 | | -rag_pipeline.add_component( |
61 | | - "llm", |
62 | | - HuggingFaceAPIGenerator( |
63 | | - api_type="serverless_inference_api", |
64 | | - api_params={"model": "mistralai/Mistral-7B-v0.1"}, |
65 | | - ), |
66 | | -) |
| 63 | +rag_pipeline.add_component("prompt_builder", ChatPromptBuilder(template=prompt_messages, required_variables=["question"])) |
| 64 | +rag_pipeline.add_component("llm", HuggingFaceAPIChatGenerator( |
| 65 | + api_type=HFGenerationAPIType.SERVERLESS_INFERENCE_API, |
| 66 | + api_params={"model": "mistralai/Mistral-7B-Instruct-v0.2"}, |
| 67 | +)) |
67 | 68 | rag_pipeline.add_component("answer_builder", AnswerBuilder()) |
68 | 69 |
|
69 | 70 | rag_pipeline.connect("query_embedder", "retriever.query_embedding") |
70 | | -rag_pipeline.connect("retriever.documents", "prompt_builder.documents") |
71 | | -rag_pipeline.connect("prompt_builder.prompt", "llm.prompt") |
| 71 | +rag_pipeline.connect("retriever", "prompt_builder.documents") |
| 72 | +rag_pipeline.connect("prompt_builder", "llm") |
72 | 73 | rag_pipeline.connect("llm.replies", "answer_builder.replies") |
73 | | -rag_pipeline.connect("llm.meta", "answer_builder.meta") |
74 | 74 | rag_pipeline.connect("retriever", "answer_builder.documents") |
75 | 75 |
|
76 | 76 | # Ask a question on the data you just added. |
|
0 commit comments