forked from agentstack-ai/AgentStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
108 lines (90 loc) · 2.98 KB
/
__init__.py
File metadata and controls
108 lines (90 loc) · 2.98 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
103
104
105
106
107
108
import os
import json
import weaviate
from typing import Optional
from weaviate.classes.config import Configure
from weaviate.classes.init import Auth
# Required environment variables
url = os.getenv("WEAVIATE_URL")
api_key = os.getenv("WEAVIATE_API_KEY")
openai_key = os.getenv("WEAVIATE_OPENAI_API_KEY") or os.getenv("OPENAI_API_KEY")
if not url:
raise Exception((
"Weaviate URL has not been provided.\n"
"Did you set the WEAVIATE_URL in your project's .env file?"
))
if not api_key:
raise Exception((
"Weaviate API key has not been provided.\n"
"Did you set the WEAVIATE_API_KEY in your project's .env file?"
))
if not openai_key:
raise Exception((
"OpenAI API key has not been provided.\n"
"Did you set either WEAVIATE_OPENAI_API_KEY or OPENAI_API_KEY in your project's .env file?"
))
def search_collection(
collection_name: str,
query: str,
limit: int = 3,
model: str = "nomic-embed-text"
) -> str:
"""Search a Weaviate collection using near-text queries.
Args:
collection_name: Name of the collection to search
query: The search query
limit: Maximum number of results (default: 3)
model: Text embedding model to use (default: nomic-embed-text)
Returns:
str: JSON string containing search results
"""
headers = {"X-OpenAI-Api-Key": openai_key}
vectorizer = Configure.Vectorizer.text2vec_openai(model=model)
client = weaviate.connect_to_weaviate_cloud(
cluster_url=url,
auth_credentials=Auth.api_key(api_key),
headers=headers
)
try:
collection = client.collections.get(collection_name)
if not collection:
raise ValueError(f"Collection {collection_name} not found")
response = collection.query.near_text(
query=query,
limit=limit
)
results = []
for obj in response.objects:
results.append(obj.properties)
return json.dumps(results, indent=2)
finally:
client.close()
def create_collection(
collection_name: str,
model: str = "nomic-embed-text"
) -> str:
"""Create a new Weaviate collection.
Args:
collection_name: Name of the collection to create
model: Text embedding model to use (default: nomic-embed-text)
Returns:
str: Success message
"""
headers = {"X-OpenAI-Api-Key": openai_key}
vectorizer = Configure.Vectorizer.text2vec_openai(model=model)
client = weaviate.connect_to_weaviate_cloud(
cluster_url=url,
auth_credentials=Auth.api_key(api_key),
headers=headers
)
try:
collection = client.collections.get(collection_name)
if collection:
return f"Collection {collection_name} already exists"
client.collections.create(
name=collection_name,
vectorizer_config=vectorizer
)
return f"Created collection {collection_name}"
finally:
client.close()