|
| 1 | +import os |
| 2 | +import json |
| 3 | +import weaviate |
| 4 | +from typing import Optional |
| 5 | +from weaviate.classes.config import Configure |
| 6 | +from weaviate.classes.init import Auth |
| 7 | + |
| 8 | +# Required environment variables |
| 9 | +url = os.getenv("WEAVIATE_URL") |
| 10 | +api_key = os.getenv("WEAVIATE_API_KEY") |
| 11 | +openai_key = os.getenv("WEAVIATE_OPENAI_API_KEY") or os.getenv("OPENAI_API_KEY") |
| 12 | + |
| 13 | +if not url: |
| 14 | + raise Exception(( |
| 15 | + "Weaviate URL has not been provided.\n" |
| 16 | + "Did you set the WEAVIATE_URL in your project's .env file?" |
| 17 | + )) |
| 18 | + |
| 19 | +if not api_key: |
| 20 | + raise Exception(( |
| 21 | + "Weaviate API key has not been provided.\n" |
| 22 | + "Did you set the WEAVIATE_API_KEY in your project's .env file?" |
| 23 | + )) |
| 24 | + |
| 25 | +if not openai_key: |
| 26 | + raise Exception(( |
| 27 | + "OpenAI API key has not been provided.\n" |
| 28 | + "Did you set either WEAVIATE_OPENAI_API_KEY or OPENAI_API_KEY in your project's .env file?" |
| 29 | + )) |
| 30 | + |
| 31 | +def search_collection( |
| 32 | + collection_name: str, |
| 33 | + query: str, |
| 34 | + limit: int = 3, |
| 35 | + model: str = "nomic-embed-text" |
| 36 | +) -> str: |
| 37 | + """Search a Weaviate collection using near-text queries. |
| 38 | +
|
| 39 | + Args: |
| 40 | + collection_name: Name of the collection to search |
| 41 | + query: The search query |
| 42 | + limit: Maximum number of results (default: 3) |
| 43 | + model: Text embedding model to use (default: nomic-embed-text) |
| 44 | +
|
| 45 | + Returns: |
| 46 | + str: JSON string containing search results |
| 47 | + """ |
| 48 | + headers = {"X-OpenAI-Api-Key": openai_key} |
| 49 | + vectorizer = Configure.Vectorizer.text2vec_openai(model=model) |
| 50 | + |
| 51 | + client = weaviate.connect_to_weaviate_cloud( |
| 52 | + cluster_url=url, |
| 53 | + auth_credentials=Auth.api_key(api_key), |
| 54 | + headers=headers |
| 55 | + ) |
| 56 | + |
| 57 | + try: |
| 58 | + collection = client.collections.get(collection_name) |
| 59 | + if not collection: |
| 60 | + raise ValueError(f"Collection {collection_name} not found") |
| 61 | + |
| 62 | + response = collection.query.near_text( |
| 63 | + query=query, |
| 64 | + limit=limit |
| 65 | + ) |
| 66 | + |
| 67 | + results = [] |
| 68 | + for obj in response.objects: |
| 69 | + results.append(obj.properties) |
| 70 | + |
| 71 | + return json.dumps(results, indent=2) |
| 72 | + finally: |
| 73 | + client.close() |
| 74 | + |
| 75 | +def create_collection( |
| 76 | + collection_name: str, |
| 77 | + model: str = "nomic-embed-text" |
| 78 | +) -> str: |
| 79 | + """Create a new Weaviate collection. |
| 80 | +
|
| 81 | + Args: |
| 82 | + collection_name: Name of the collection to create |
| 83 | + model: Text embedding model to use (default: nomic-embed-text) |
| 84 | +
|
| 85 | + Returns: |
| 86 | + str: Success message |
| 87 | + """ |
| 88 | + headers = {"X-OpenAI-Api-Key": openai_key} |
| 89 | + vectorizer = Configure.Vectorizer.text2vec_openai(model=model) |
| 90 | + |
| 91 | + client = weaviate.connect_to_weaviate_cloud( |
| 92 | + cluster_url=url, |
| 93 | + auth_credentials=Auth.api_key(api_key), |
| 94 | + headers=headers |
| 95 | + ) |
| 96 | + |
| 97 | + try: |
| 98 | + collection = client.collections.get(collection_name) |
| 99 | + if collection: |
| 100 | + return f"Collection {collection_name} already exists" |
| 101 | + |
| 102 | + client.collections.create( |
| 103 | + name=collection_name, |
| 104 | + vectorizer_config=vectorizer |
| 105 | + ) |
| 106 | + return f"Created collection {collection_name}" |
| 107 | + finally: |
| 108 | + client.close() |
0 commit comments