|
1 | 1 | import os |
| 2 | + |
| 3 | +from couchbase.options import KnownConfigProfiles |
2 | 4 | from haystack import GeneratedAnswer, Pipeline |
3 | 5 | from haystack.components.builders.answer_builder import AnswerBuilder |
4 | 6 | from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder |
5 | | -from haystack.dataclasses import ChatMessage |
6 | 7 | from haystack.components.embedders import SentenceTransformersTextEmbedder |
7 | 8 | from haystack.components.generators.chat import HuggingFaceAPIChatGenerator |
8 | | -from haystack.utils.hf import HFGenerationAPIType |
| 9 | +from haystack.dataclasses import ChatMessage |
9 | 10 | from haystack.utils import Secret |
| 11 | +from haystack.utils.hf import HFGenerationAPIType |
| 12 | + |
10 | 13 | from couchbase_haystack import ( |
11 | 14 | CouchbaseClusterOptions, |
12 | 15 | CouchbasePasswordAuthenticator, |
13 | 16 | CouchbaseQueryDocumentStore, |
14 | 17 | CouchbaseQueryEmbeddingRetriever, |
15 | 18 | QueryVectorSearchType, |
16 | 19 | ) |
17 | | -from couchbase.options import KnownConfigProfiles |
18 | 20 |
|
19 | 21 | # Load HF Token from environment variables. |
20 | 22 | HF_TOKEN = Secret.from_env_var("HF_API_TOKEN") |
|
29 | 31 |
|
30 | 32 | document_store = CouchbaseQueryDocumentStore( |
31 | 33 | 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")), |
| 34 | + authenticator=CouchbasePasswordAuthenticator( |
| 35 | + username=Secret.from_env_var("USER_NAME"), password=Secret.from_env_var("PASSWORD") |
| 36 | + ), |
33 | 37 | cluster_options=CouchbaseClusterOptions( |
34 | 38 | profile=KnownConfigProfiles.WanDevelopment, |
35 | 39 | ), |
|
45 | 49 | # interacting with LLMs using a custom prompt. |
46 | 50 | prompt_messages = [ |
47 | 51 | 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. |
| 52 | + ChatMessage.from_user( |
| 53 | + """Given these documents, answer the question. |
49 | 54 | Documents: |
50 | 55 | {% for doc in documents %} |
51 | 56 | {{ doc.content }} |
52 | 57 | {% endfor %} |
53 | 58 |
|
54 | 59 | Question: {{question}} |
55 | | -Answer:""") |
| 60 | +Answer:""" |
| 61 | + ), |
56 | 62 | ] |
57 | 63 | rag_pipeline = Pipeline() |
58 | 64 | rag_pipeline.add_component( |
|
61 | 67 | ) |
62 | 68 | rag_pipeline.add_component("retriever", CouchbaseQueryEmbeddingRetriever(document_store=document_store)) |
63 | 69 | 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 | | -)) |
| 70 | +rag_pipeline.add_component( |
| 71 | + "llm", |
| 72 | + HuggingFaceAPIChatGenerator( |
| 73 | + api_type=HFGenerationAPIType.SERVERLESS_INFERENCE_API, |
| 74 | + api_params={"model": "mistralai/Mistral-7B-Instruct-v0.2"}, |
| 75 | + ), |
| 76 | +) |
68 | 77 | rag_pipeline.add_component("answer_builder", AnswerBuilder()) |
69 | 78 |
|
70 | 79 | rag_pipeline.connect("query_embedder", "retriever.query_embedding") |
|
0 commit comments