{"library":"needle-python","title":"Needle Python Client Library","description":"needle-python is the official client library for the Needle API. It simplifies the process of building Retrieval-Augmented Generation (RAG) pipelines, enabling semantic search and efficient contextualization for Large Language Models (LLMs). The library, currently at version 0.6.0, provides tools for managing collections, uploading files, and performing contextual searches. It is actively maintained with a focus on API integration and RAG pipeline development.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install needle-python"],"cli":null},"imports":["from needle.v1 import NeedleClient","from needle.v1.models import FileToAdd"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom needle.v1 import NeedleClient\nfrom needle.v1.models import FileToAdd\nimport time # For waiting during indexing\n\n# Ensure your API key is set as an environment variable or pass it directly\n# os.environ['NEEDLE_API_KEY'] = 'YOUR_API_KEY_HERE'\nneedle_api_key = os.environ.get('NEEDLE_API_KEY', '')\nif not needle_api_key:\n    print(\"Warning: NEEDLE_API_KEY environment variable not set. Client may fail to authenticate.\")\n\n# Initialize the client\n# You can also pass the api_key directly: needle = NeedleClient(api_key=needle_api_key)\nneedle = NeedleClient()\n\n# Create a new collection\ncollection_name = \"My AI Project Docs\"\nprint(f\"Creating collection: {collection_name}\")\ncollection = needle.collections.create(name=collection_name)\ncollection_id = collection.id\nprint(f\"Collection '{collection.name}' created with ID: {collection_id}\")\n\n# Add files to the collection (e.g., from a URL)\nfile_url = \"https://www.thoughtworks.com/content/dam/thoughtworks/documents/radar/2024/04/tr_technology_radar_vol_30_en.pdf\"\nfile_name = \"tech-radar-30.pdf\"\nprint(f\"Adding file '{file_name}' from {file_url} to collection {collection_id}\")\n\nfiles_to_add = [\n    FileToAdd(name=file_name, url=file_url)\n]\nneedle.collections.files.add(collection_id=collection_id, files=files_to_add)\n\n# Wait for files to be indexed (indexing takes time)\nprint(\"Waiting for files to be indexed...\")\nfor _ in range(10): # Poll for up to 50 seconds\n    current_files = needle.collections.files.list(collection_id)\n    if all(f.status == \"indexed\" for f in current_files):\n        print(\"All files indexed successfully.\")\n        break\n    time.sleep(5)\nelse:\n    print(\"Warning: Not all files indexed within expected time.\")\n\n# Perform a semantic search\nsearch_prompt = \"What techniques moved into adopt in this volume of technology radar?\"\nprint(f\"Searching collection {collection_id} for prompt: '{search_prompt}'\")\nresults = needle.collections.search(collection_id, text=search_prompt)\n\nprint(\"\\nSearch Results (context snippets):\")\nfor r in results:\n    print(f\"- {r.content[:100]}...\")\n\n# Clean up (optional) - delete the collection\n# print(f\"Deleting collection {collection_id}\")\n# needle.collections.delete(collection_id)\n# print(\"Collection deleted.\")\n","lang":"python","description":"This quickstart demonstrates how to initialize the Needle client, create a document collection, add a file via URL, wait for it to be indexed, and then perform a semantic search to retrieve relevant context. It highlights the use of `NEEDLE_API_KEY` for authentication and the typical workflow for building RAG components.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}