Skip to main content

Needle client library for Python

Project description

Needle Python Library

PyPI - Version PyPI - Python Version

This Python library provides convenient acccess to Needle API. There are various methods and data types which, we believe will help you explore Needle API quickly. There may be some functionality available in REST API earlier than this Python library. In any case, we recommend to take look the the complete documentation. Thank you for flying with us. 🚀

Installation

This library requires Python >3.8 and pip to use. You don't need the sources unless you want to modify it. Install with:

pip install needle-python

Usage ⚡️

To get started, generate an API key for your account in developer settings menu at Needle. Note that your key will be valid until you revoke it. Set the following env variable before you run your code:

export NEEDLE_API_KEY=<your-api-key>

NeedleClient reads the API key from the environment by default. If you like to override this behaviour you can pass it in as a parameter.

Retrieve context from Needle

from needle.v1 import NeedleClient
from needle.v1.models import FileToAdd


ndl = NeedleClient()
collection = ndl.collections.create(name="Tech Trends")

# add file to collection
files = ndl.collections.files.add(
    collection_id=collection_id,
    files=[
        FileToAdd(
            name="tech-radar-30.pdf",
            url="https://www.thoughtworks.com/content/dam/thoughtworks/documents/radar/2024/04/tr_technology_radar_vol_30_en.pdf",
        )
    ],
)

# wait until indexing is complete
files = ndl.collections.files.list(collection_id)
if not all(f.status == "indexed" for f in files):
    time.sleep(5)
    files = ndl.collections.files.list(collection_id)

# retrieve relevant context
prompt = "What techniques moved into adopt in this volume of technology radar?"
results = ndl.collections.search(collection_id, text=prompt)

Needle instantly extracts key points from your files.

Complete your RAG pipeline

Naturally, to compose a human friendly answer use an LLM provider of your choice. For the demo purposes, we used OpenAI in this example:

from openai import OpenAI

system_messages = [{"role": "system", "content": r.content} for r in results] # results from Needle
user_message = {
    "role": "system",
    "content": f"""
        Only answer the question based on the provided results data. 
        If there is no data in the provided data for the question, do not try to generate an answer.
        This is the question: {prompt}
""",
}

openai_client = OpenAI()
answer = openai_client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        *system_messages,
        user_message,
    ],
)

print(answer.choices[0].message.content)
# -> Retrieval-Augmented Generation (RAG) is the technique that moved into "Adopt" in this volume of the Technology Radar.

This is one basic example of a RAG pipeline you can quicklu implement using Needle and OpenAI. Feel free to engineer more precise prompts and explore other prompting techniques such as chain-of-thoughts (CoT), graph of thoughts (GoT) etc.

Needle API helps you with hassle-free contextualization however does not limit you to a certain RAG technique. Let us know what you build in our Discord channel :)

Exceptions 🧨

If a request to Needle API fails, needle.v1.models.Error object will be thrown. There you can see a message and more details about the error.

Support 📞

If you have questions you can contact us in our Discord channel.

License

needle-python is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

needle_python-0.6.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

needle_python-0.6.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file needle_python-0.6.0.tar.gz.

File metadata

  • Download URL: needle_python-0.6.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for needle_python-0.6.0.tar.gz
Algorithm Hash digest
SHA256 6fb2aa99ef56e8f63171add525ab4756c91504dc39b6fd3e33f14cec9ce33b46
MD5 e3720f80c94913e1e0b9897aa1d97084
BLAKE2b-256 6fc4034f43a01d41c7c8d220b1f48a3e471c5fb54ba3092581ee848d98cc167d

See more details on using hashes here.

File details

Details for the file needle_python-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: needle_python-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for needle_python-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5349cd16da8ffb6a6e8593df41a49995f52faab6aed37499cc4c9f6872196c40
MD5 9fb39d57b68d9ffafdcb08bc77df76c7
BLAKE2b-256 51d086aa7949a35e5097e144b24d57e38eea34ee6a13ef0a3a226b25b4473e38

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page