-
Notifications
You must be signed in to change notification settings - Fork 1
/
flow_client.py
52 lines (39 loc) · 1.48 KB
/
flow_client.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
from typing import List, Optional
import portforward
from docarray import DocList, BaseDoc
from docarray.typing import NdArray
import sys
from jina.clients import Client
ns_name = 'default'
gateway_pod_name = sys.argv[1]
print(gateway_pod_name)
class MyDoc(BaseDoc):
text: str
embedding: Optional[NdArray] = None
class MyDocWithMatches(MyDoc):
matches: DocList[MyDoc] = []
scores: List[float] = []
with portforward.forward(ns_name, gateway_pod_name, 8080, 8080):
client = Client(host='localhost', port=8080)
client.show_progress = True
docs = client.post(
'/index',
inputs=DocList[MyDoc]([MyDoc(text=f'This is document indexed number {i}') for i in range(100)]),
return_type=DocList[MyDoc],
request_size=10
)
print(f'------------ Test 1 ---------------')
print(f'Indexed documents: {len(docs)}')
docs = client.post(
'/search',
inputs=DocList[MyDoc]([MyDoc(text=f'This is document query number {i}') for i in range(10)]),
return_type=DocList[MyDocWithMatches],
request_size=10
)
for doc in docs:
print(f'Query {doc.text} has {len(doc.matches)} matches')
print(f'------------ Test 2 ---------------')
docs = DocList[MyDoc]([MyDoc(text=f'This is document indexed number {i}') for i in range(100)])
queried_docs = client.post("/search", inputs=docs, return_type=DocList[MyDocWithMatches])
matches = queried_docs[0].matches
print(f"Matched documents: {len(matches)}")