Astra Assistants
The `astra-assistants` library provides a Pythonic, drop-in replacement for OpenAI's Assistants API, powered by AstraDB. It allows developers to build AI assistants with capabilities like persistent memory, tool use (e.g., web search, code interpreter), and RAG, leveraging AstraDB as the vector database backend. The current version is 2.5.5, with regular releases aligning with OpenAI's API updates.
Common errors
-
ModuleNotFoundError: No module named 'astra-assistants'
cause The 'astra-assistants' package is not installed in the Python environment.fixInstall the package using pip: 'pip install astra-assistants'. -
ImportError: cannot import name 'Chroma' from 'astra-assistants'
cause The 'Chroma' module is not part of the 'astra-assistants' package.fixEnsure you are importing the correct module from 'astra-assistants' as per the official documentation. -
AttributeError: module 'astra_assistants' has no attribute 'patch'
cause The 'patch' function is not available in the 'astra_assistants' module.fixVerify the correct usage of the 'astra_assistants' module and refer to the official documentation for guidance. -
TypeError: 'NoneType' object is not callable
cause Attempting to call a function or method that is None, possibly due to incorrect initialization or missing dependencies.fixCheck the initialization of your objects and ensure all dependencies are correctly installed and configured. -
ValueError: Invalid model specified: 'gpt-4-1106-preview'
cause The specified model name is not recognized or supported by the 'astra-assistants' package.fixRefer to the official documentation for the list of supported models and use a valid model name.
Warnings
- breaking The `astra-assistants` library underwent a complete API overhaul in version 2.0.0 to align with OpenAI's 2024-02-15 API. This introduced new client structures, method calls (e.g., `client.beta.assistants.create`), and response object formats.
- gotcha Astra Assistants requires `ASTRA_DB_APPLICATION_TOKEN` and `ASTRA_DB_API_ENDPOINT` to be set as environment variables or passed explicitly during client initialization. Forgetting either or providing incorrect values will result in authentication errors.
- gotcha When using models like `gpt-4o` or `gpt-3.5-turbo` that are provided by OpenAI, you still need to set your `OPENAI_API_KEY` environment variable, even though you are using the `astra-assistants` wrapper. Astra DB handles the persistence and vector search, but may delegate to OpenAI for the LLM itself.
- gotcha Accessing message content requires navigating through nested objects. The content is typically a list of content blocks, and for text, you'll often need `msg.content[0].text.value`.
Install
-
pip install astra-assistants
Imports
- AstraAssistants
from astra_assistants.assistant import Assistant
from astra_assistants import AstraAssistants
- ThreadMessage
from astra_assistants.models import ThreadMessage
Quickstart
import os
from astra_assistants import AstraAssistants
from astra_assistants.models import ThreadMessage
# --- Authentication Configuration ---
# Ensure these environment variables are set:
# os.environ["ASTRA_DB_APPLICATION_TOKEN"] = "AstraCS:your-token"
# os.environ["ASTRA_DB_API_ENDPOINT"] = "https://<REGION>.aws.a.astra.datastax.com/api/rest"
# Optional: os.environ["OPENAI_API_KEY"] = "sk-..." # Only if using OpenAI models via AstraDB
# Initialize the client. It automatically picks up credentials from environment variables.
client = AstraAssistants()
# 1. Create an assistant
assistant = client.beta.assistants.create(
name="Math Tutor",
instructions="You are a personal math tutor. Answer questions briefly.",
tools=[{"type": "code_interpreter"}], # Example tool. For web_search, use {"type": "web_search"}
model="gpt-4o", # Or a model accessible via your AstraDB setup, e.g., 'gpt-3.5-turbo'
)
print(f"Created Assistant: {assistant.id}")
# 2. Create a thread
thread = client.beta.threads.create()
print(f"Created Thread: {thread.id}")
# 3. Add a message to the thread
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="What is the result of 15 * 3 + 2?",
)
print("Added message to thread.")
# 4. Run the assistant on the thread and poll for completion
run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as 'Student'.",
)
print(f"Run completed with status: {run.status}")
# 5. Retrieve and print the messages
if run.status == "completed":
messages_page = client.beta.threads.messages.list(thread_id=thread.id, order="asc")
for msg in messages_page.data:
# The content can be a list of different block types. For text, access .text.value
if msg.content and hasattr(msg.content[0], 'text'):
print(f"{msg.role}: {msg.content[0].text.value}")
else:
print(f"{msg.role}: {msg.content}") # Fallback for other content types
else:
print(f"Run finished with status {run.status}. Could not retrieve messages.")
# In a real application, you might delete the assistant and thread here.
# client.beta.assistants.delete(assistant.id)
# client.beta.threads.delete(thread.id)