This guide shows you how to set up and use Pinecone Assistant, a service that allows you to upload documents, ask questions, and receive responses that reference your documents.

1. Install the Python SDK

You can use the Assistant API directly or via the Pinecone Python SDK.

To interact with Pinecone Assistant using the Python SDK, install the client and the pinecone-plugin-assistant package as follows:

pip install pinecone
pip install pinecone-plugin-assistant

2. Get an API key

You need an API key to make calls to your assistant.

Create a new API key in the Pinecone console, or use the widget below to generate a key. If you don’t have a Pinecone account, the widget will sign you up for the free Starter plan.

Your generated API key:

"{{YOUR_API_KEY}}"

3. Create an assistant

To create an assistant, use the create_assistant operation as in the following example:

Python
from pinecone import Pinecone

pc = Pinecone(api_key="YOUR_API_KEY")

assistant = pc.assistant.create_assistant(
    assistant_name="example-assistant", 
    instructions="Answer directly and succinctly. Do not provide any additional information.", # Description or directive for the assistant to apply to all responses.
    timeout=30 # Maximum seconds to wait for assistant status to become "Ready" before timing out.
)

4. Upload a file to the assistant

With Pinecone Assistant, you can upload documents, ask questions, and receive responses that reference your documents. This is known as retrieval-augmented generation (RAG).

For this quickstart, download a sample 10-k filing file to your local device.

Next, use the create_file operation to upload the file to your assistant:

Python
from pinecone import Pinecone
pc = Pinecone(api_key="YOUR_API_KEY")

# Get the assistant.
assistant = pc.assistant.Assistant(
    assistant_name="example-assistant", 
)

# Upload a file.
response = assistant.upload_file(
    file_path="/path/to/file/Netflix-10-K-01262024.pdf",
    timeout=None
)

5. Chat with the assistant

With the sample file uploaded, you can now ask the assistant questions about your document.

To chat with a Pinecone assistant, use the chat_assistant endpoint. It returns either a JSON object or a text stream.

The following example requests a JSON response to the message, “How many employees did Netflix have by the end of 2023?”:

Python
from pinecone import Pinecone
from pinecone_plugins.assistant.models.chat import Message

pc = Pinecone(api_key='YOUR_API_KEY')
assistant = pc.assistant.Assistant(assistant_name="example-assistant")

msg = Message(content="How many employees did Netflix have by the end of 2023?")
resp = assistant.chat(messages=[msg])

print(resp)

The example above returns a response like the following:

{'citations': [{'position': 72,
                'references': [{'file': {'created_on': '2024-11-04T18:11:52.243609916Z',
                                         'id': 'b8a4e98e-56e8-4e51-b68c-4e5282770ecb',
                                         'metadata': None,
                                         'name': 'Netflix-10-K-01262024.pdf',
                                         'percent_done': 1.0,
                                         'signed_url': 'https://storage.googleapis.com/knowledge-prod-files/3ed21848-b4a0-4aa9-a292-efd09abba1fa%2F270d1996-e9ad-46aa-b5d5-1dc5e82351b7%2Fb8a4e98e-56e8-4e51-b68c-4e5282770ecb.pdf?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=ke-prod-1%40pc-knowledge-prod.iam.gserviceaccount.com%2F20241104%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20241104T181819Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&response-content-disposition=inline&response-content-type=application%2Fpdf&X-Goog-Signature=827de3a4d68b7a64982bf3eedc735a2225a03170a55866abf82454670556300a9690634e323b9bd50a16035930cd4dc9c12b3f8351f8c8ecb1cbac8b59e0d0665ea8fa0b9b393b4b36a385a260859cc108c5e5f2d91a9b17efe77b1f2cd111c2cb678762fb2a109ac6588d51a503416d8498eacc0571ba4bcb85e4afdd5121dc32d4535d37f66ee0455255029759c8f2bf7de8a3c022c8c21ed30aab62237ac588ad0a55dfcc6c919b6a111235be516a212783f3d438fecd6e778497ad043468c2e6c2caec02f934b28464fe59ddbf11c4fbf594d4259f751f11a6e8aecc88002f7a6c0e332527566ded5ccf86732afd5610f5ea379f36f17e1e206e3ff41199',
                                         'size': 1073470.0,
                                         'status': 'Available',
                                         'updated_on': '2024-11-04T18:12:29.663628217Z'},
                                'pages': [5]}]}],
 'finish_reason': 'stop',
 'id': '0000000000000000591358cd74e3e938',
 'message': {'content': 'By the end of 2023, Netflix had approximately 13,000 '
                        'full-time employees.',
             'role': '"assistant"'},
 'model': 'gpt-4o-2024-05-13',
 'usage': {'completion_tokens': 28,
           'prompt_tokens': 15111,
           'total_tokens': 15139}}

6. Clean up

When you no longer need the example-assistant, use the delete_assistant operation to delete it:

Deleting an assistant also deletes all files uploaded to the assistant.

Python
from pinecone import Pinecone
pc = Pinecone(api_key="YOUR_API_KEY")

pc.assistant.delete_assistant(
    assistant_name="example-assistant", 
)

Next steps