Description
Using this requirements.txt file and pip install -r requirements.txt
:
anthropic
python-dotenv
weaviate-client
With the following .env
file:
# weaviate
WCD_URL=<your_wcd_url>
WCD_API_KEY=<your_wcd_key>
# openai
OPENAI_API_KEY=<your_openai_key>
# anthropic
ANTHROPIC_API_KEY=<your_anthropic_key>
Run the following python script:
import os
from dotenv import load_dotenv
from weaviate import WeaviateClient, connect_to_weaviate_cloud
from weaviate.classes.config import Configure
from weaviate.classes.init import Auth
load_dotenv()
def get_client() -> WeaviateClient:
"""Get a weaviate cloud client instance, using WCD_URL, WCD_API_KEY, and OPENAI_API_KEY
environment variables.
**Important:** Note that it is the responsibility of the caller to close the client.
Returns:
WeaviateClient: A configured weaviate cloud client
"""
return connect_to_weaviate_cloud(
cluster_url=os.environ["WCD_URL"],
auth_credentials=Auth.api_key(os.environ["WCD_API_KEY"]),
headers={
"X-Anthropic-Api-Key": os.environ["ANTHROPIC_API_KEY"],
"X-OpenAI-Api-Key": os.environ["OPENAI_API_KEY"],
},
)
if __name__ == "__main__":
with get_client() as client:
print("Creating collection...")
collection_name = "test_collection"
collection = client.collections.create(
name=collection_name,
vectorizer_config=Configure.Vectorizer.text2vec_openai(
),
generative_config=Configure.Generative.anthropic(
# model="claude-3-5-sonnet-20240620",
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
),
)
print("Adding chunk to collection...")
with collection.batch.dynamic() as batch:
batch.add_object({"text": "Most dogs have 4 legs... except those that were amputated."})
print(f"Nb of chunks in collection: {collection.aggregate.over_all().total_count}")
print("Call generate...")
query = "How many legs does a dog have? Answer as a pirate would."
print(f"\tQuery: {query}")
response = collection.generate.near_text(
query="dog",
limit=1,
grouped_task=query,
)
print(f"\tRetrieved chunk: {response.objects[0].properties['text']}")
print(f"\tAnswer: {response.generated}")
print("Delete collection...")
client.collections.delete(collection_name)
You should get the following error:
Creating collection...
Adding chunk to collection...
Nb of chunks in collection: 1
Call generate...
Query: How many legs does a dog have? Answer as a pirate would.
Traceback (most recent call last):
File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/grpc/query.py", line 478, in __call
res = await _Retry(4).with_exponential_backoff(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/grpc/retry.py", line 31, in with_exponential_backoff
raise e
File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/grpc/retry.py", line 28, in with_exponential_backoff
return await f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/grpc/aio/_call.py", line 327, in __await__
raise _create_rpc_error(
grpc.aio._call.AioRpcError: <AioRpcError of RPC that terminated with:
status = StatusCode.UNKNOWN
details = "Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1"
debug_error_string = "UNKNOWN:Error received from peer {created_time:"2025-02-21T16:55:50.85583-05:00", grpc_status:2, grpc_message:"Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1"}"
>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate_generate_issue_main.py", line 56, in <module>
response = collection.generate.near_text(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/syncify.py", line 23, in sync_method
return _EventLoopSingleton.get_instance().run_until_complete(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/event_loop.py", line 42, in run_until_complete
return fut.result()
^^^^^^^^^^^^
File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
raise self._exception
File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/queries/near_text/generate.py", line 101, in near_text
res = await self._query.near_text(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/dtourillon/TouriLogica/Kwerty/dev/weaviate-generate-issue/weaviate-issue-venv/lib/python3.12/site-packages/weaviate/collections/grpc/query.py", line 490, in __call
raise WeaviateQueryError(str(e), "GRPC search") # pyright: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
weaviate.exceptions.WeaviateQueryError: Query call with protocol GRPC search failed with message <AioRpcError of RPC that terminated with:
status = StatusCode.UNKNOWN
details = "Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1"
debug_error_string = "UNKNOWN:Error received from peer {created_time:"2025-02-21T16:55:50.85583-05:00", grpc_status:2, grpc_message:"Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1"}"
>.
Any additional Information
The script above:
- … works using
claude-3-5-sonnet-20240620
, the second last available Claude 3.5 sonnet model - … raises “Anthropic API error: invalid_request_error - max_tokens: must be greater than or equal to 1” using
claude-3-5-sonnet-20241022
, the latest Claude 3.5 Sonnet model… despite max_tokens being set to 1024…