forked from marcusschiesser/streamlit-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weaviate.py
73 lines (58 loc) · 1.88 KB
/
weaviate.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
import streamlit as st
import weaviate
from streamlit_examples.utils.cohere import cohere_api_key
@st.cache_resource(show_spinner="Connecting to Weaviate...")
def connect_weaviate():
# Connect to the Weaviate demo database containing 10M wikipedia vectors
# This uses a public READ-ONLY Weaviate API key
auth_config = weaviate.auth.AuthApiKey(
api_key="76320a90-53d8-42bc-b41d-678647c6672e"
)
client = weaviate.Client(
url="https://cohere-demo.weaviate.network/",
auth_client_secret=auth_config,
additional_headers={
"X-Cohere-Api-Key": cohere_api_key,
},
)
client.is_ready()
return client
def search_wikipedia(query, results_lang="en", limit=5):
"""
Query the vectors database and return the top results.
Parameters
----------
query: str
The search query
results_lang: str (optional)
Retrieve results only in the specified language.
The demo dataset has those languages:
en, de, fr, es, it, ja, ar, zh, ko, hi
"""
client = connect_weaviate()
nearText = {"concepts": [query]}
properties = ["text", "title", "url", "views", "lang", "_additional {distance}"]
# To filter by language
if results_lang != "":
where_filter = {
"path": ["lang"],
"operator": "Equal",
"valueString": results_lang,
}
response = (
client.query.get("Articles", properties)
.with_where(where_filter)
.with_near_text(nearText)
.with_limit(limit)
.do()
)
# Search all languages
else:
response = (
client.query.get("Articles", properties)
.with_near_text(nearText)
.with_limit(limit)
.do()
)
result = response["data"]["Get"]["Articles"]
return result