forked from googleapis/langchain-google-spanner-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_spanner_graph_qa.py
215 lines (191 loc) · 7.01 KB
/
test_spanner_graph_qa.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import random
import string
import pytest
from google.cloud import spanner
from langchain.evaluation import load_evaluator
from langchain_community.graphs.graph_document import GraphDocument, Node, Relationship
from langchain_core.documents import Document
from langchain_google_vertexai import ChatVertexAI, VertexAIEmbeddings
from langchain_google_spanner.graph_qa import SpannerGraphQAChain
from langchain_google_spanner.graph_store import SpannerGraphStore
project_id = os.environ["PROJECT_ID"]
instance_id = os.environ["INSTANCE_ID"]
database_id = os.environ["GOOGLE_DATABASE"]
def random_string(num_char=3):
return "".join(random.choice(string.ascii_letters) for _ in range(num_char))
def get_llm():
llm = ChatVertexAI(
model="gemini-1.5-flash-002",
temperature=0,
)
return llm
def get_evaluator():
return load_evaluator(
"embedding_distance",
embeddings=VertexAIEmbeddings(model_name="text-embedding-004"),
)
def get_spanner_graph():
suffix = random_string(num_char=3)
graph_name = "test_graph{}".format(suffix)
graph = SpannerGraphStore(
instance_id=instance_id,
database_id=database_id,
graph_name=graph_name,
client=spanner.Client(project=project_id),
)
return graph
def load_data(graph: SpannerGraphStore):
type_suffix = "_" + random_string(num_char=3)
graph_documents = [
GraphDocument(
nodes=[
Node(
id="Elias Thorne",
type="Person" + type_suffix,
properties={
"name": "Elias Thorne",
"description": "lived in the desert",
},
),
Node(
id="Zephyr",
type="Animal" + type_suffix,
properties={"name": "Zephyr", "description": "pet falcon"},
),
Node(
id="Elara",
type="Person" + type_suffix,
properties={
"name": "Elara",
"description": "resided in the capital city",
},
),
Node(id="Desert", type="Location" + type_suffix, properties={}),
Node(id="Capital City", type="Location" + type_suffix, properties={}),
],
relationships=[
Relationship(
source=Node(
id="Elias Thorne", type="Person" + type_suffix, properties={}
),
target=Node(
id="Desert", type="Location" + type_suffix, properties={}
),
type="LivesIn",
properties={},
),
Relationship(
source=Node(
id="Elias Thorne", type="Person" + type_suffix, properties={}
),
target=Node(
id="Zephyr", type="Animal" + type_suffix, properties={}
),
type="Owns",
properties={},
),
Relationship(
source=Node(id="Elara", type="Person" + type_suffix, properties={}),
target=Node(
id="Capital City", type="Location" + type_suffix, properties={}
),
type="LivesIn",
properties={},
),
Relationship(
source=Node(
id="Elias Thorne", type="Person" + type_suffix, properties={}
),
target=Node(id="Elara", type="Person" + type_suffix, properties={}),
type="Sibling",
properties={},
),
],
source=Document(
metadata={},
page_content=(
"Elias Thorne lived in the desert. He was a skilled craftsman"
" who worked with sandstone. Elias had a pet falcon named"
" Zephyr. His sister, Elara, resided in the capital city and"
" ran a spice shop. They rarely met due to the distance."
),
),
)
]
graph.add_graph_documents(graph_documents)
graph.refresh_schema()
class TestSpannerGraphQAChain:
@pytest.fixture(scope="module")
def setup_db_load_data(self):
graph = get_spanner_graph()
load_data(graph)
yield graph
# teardown
print(graph.get_schema)
graph.cleanup()
@pytest.fixture
def chain(self, setup_db_load_data):
graph = setup_db_load_data
return SpannerGraphQAChain.from_llm(
get_llm(),
graph=graph,
verbose=True,
return_intermediate_steps=True,
allow_dangerous_requests=True,
)
@pytest.fixture
def chain_without_opt_in(self, setup_db_load_data):
graph = setup_db_load_data
return SpannerGraphQAChain.from_llm(
get_llm(),
graph=graph,
verbose=True,
return_intermediate_steps=True,
)
def test_spanner_graph_qa_chain_1(self, chain):
question = "Where does Elias Thorne's sibling live?"
response = chain.invoke("query=" + question)
print(response)
answer = response["result"]
assert (
get_evaluator().evaluate_strings(
prediction=answer,
reference="Elias Thorne's sibling lives in Capital City.\n",
)["score"]
< 0.1
)
def test_spanner_graph_qa_chain_no_answer(self, chain):
question = "Where does Sarah's sibling live?"
response = chain.invoke("query=" + question)
print(response)
answer = response["result"]
assert (
get_evaluator().evaluate_strings(
prediction=answer,
reference="I don't know the answer.\n",
)["score"]
< 0.1
)
def test_spanner_graph_qa_chain_without_opt_in(self, setup_db_load_data):
with pytest.raises(ValueError):
graph = setup_db_load_data
SpannerGraphQAChain.from_llm(
get_llm(),
graph=graph,
verbose=True,
return_intermediate_steps=True,
)