-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindexing.py
More file actions
102 lines (95 loc) · 3.08 KB
/
Copy pathindexing.py
File metadata and controls
102 lines (95 loc) · 3.08 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
from typing import List
from langchain_core.documents import Document
from langchain_upstage import UpstageEmbeddings
from langchain_elasticsearch import ElasticsearchStore
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain.chains.query_constructor.base import AttributeInfo
from langchain_community.query_constructors.elasticsearch import ElasticsearchTranslator
# Constants
ELASTIC_CLOUD_ID = "************************************************************************************************************************"
ELASTIC_API_KEY = "**************************************"
# Metadata field information
metadata_field_info = [
AttributeInfo(
name="subject",
description="The subject of the email",
type="string",
),
AttributeInfo(
name="cc",
description="The CC (carbon copy) recipients of the email",
type="string",
),
AttributeInfo(
name="from",
description="The sender of the email",
type="string",
),
AttributeInfo(
name="to",
description="The recipient of the email",
type="string",
),
AttributeInfo(
name="year",
description="The year the email was sent",
type="integer",
),
AttributeInfo(
name="month",
description="The month the email was sent",
type="integer",
),
AttributeInfo(
name="day",
description="The day the email was sent",
type="integer",
),
AttributeInfo(
name="hour",
description="The hour the email was sent",
type="integer",
),
AttributeInfo(
name="minute",
description="The minute the email was sent",
type="integer",
),
AttributeInfo(
name="second",
description="The second the email was sent",
type="integer",
),
]
document_content_description = "KAIST academic email data"
def create_vectorstore(documents: List[Document], index_name: str) -> ElasticsearchStore:
"""
Create and return an ElasticsearchStore instance.
"""
return ElasticsearchStore.from_documents(
documents,
UpstageEmbeddings(model="solar-embedding-1-large"),
index_name=index_name,
es_cloud_id=ELASTIC_CLOUD_ID,
es_api_key=ELASTIC_API_KEY,
)
def create_retriever(vectorstore: ElasticsearchStore, llm, query_constructor) -> SelfQueryRetriever:
"""
Create and return a SelfQueryRetriever instance.
"""
return SelfQueryRetriever(
query_constructor=query_constructor,
vectorstore=vectorstore,
structured_query_translator=ElasticsearchTranslator(),
search_kwargs={"k": 10},
)
def setup_indexing(documents: List[Document], llm, query_constructor) -> SelfQueryRetriever:
"""
Set up the indexing and retrieval system.
"""
vectorstore = create_vectorstore(documents, "elasticsearch-self-query_for_demo2")
retriever = create_retriever(vectorstore, llm, query_constructor)
return retriever
# Usage
# retriever = setup_indexing(mails, llm, query_constructor)