Comments (5)
i think this would be pretty good since the anthropic client wraps a httpx client and usually we want to use it like a context manager too
with httpx.Client() as client:
...
from anthropic-sdk-python.
Thanks for filing! You're correct that the library is intended to be instantiated once, not on every request, but I expect we'll add .close()
and support for a context manager shortly.
from anthropic-sdk-python.
Out of curiosity, can you share why you needed/wanted to instantiate the client in a loop like this?
from anthropic-sdk-python.
Sorry for the delay here, this is out in https://github.com/anthropics/anthropic-sdk-python/releases/tag/v0.3.7 and will be published to pypi soon.
from anthropic-sdk-python.
I have tested anthropic 0.3.11 and 0.2.9 , both might have memory leak .
code 1 :
`@retry(stop=stop_after_attempt(5), wait=wait_exponential(max=10), retry=retry_if_exception_type((ConnectionResetError,anthropic.InternalServerError) ))
def send_something():
prompt = "\n\nHuman: This is a test prompt. Return some randome text\n\nAssistant":
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
resp = client.completions.create(
prompt=prompt,
stop_sequences=[anthropic.HUMAN_PROMPT],
model="claude-v1",
max_tokens_to_sample=100,
)
client.close()
return resp.completion
if name == "main":
client = None
for i in range(10000):
print(send_something())`
code 2 :
`@retry(stop=stop_after_attempt(5), wait=wait_exponential(max=10), retry=retry_if_exception_type((ConnectionResetError,anthropic.InternalServerError) ))
def send_something():
prompt = "\n\nHuman: This is a test prompt. Return some randome text\n\nAssistant:"
resp = client.completions.create(
prompt=prompt,
stop_sequences=[anthropic.HUMAN_PROMPT],
model="claude-v1",
max_tokens_to_sample=100,
)
return resp.completion
if name == "main":
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
for i in range(10000):
print(send_something())`
Both code led to File descriptor leak eventually after 10000-20000 loops . I ever tried to remove the retry and the same File descriptor leak happened . When changed to use
headers = {"x-api-key": f'{api_key}', "Content-Type": 'application/json'} data = {"prompt": f'\n\nHuman: {prompt1}\n\nAssistant: ',"model": 'claude-instant-1.2',"max_tokens_to_sample": 10000,"temperature":0} r = requests.post('https://api.anthropic.com/v1/complete', headers=headers, json=data,timeout=500)
everything is fine . no File descriptor leak at all .
from anthropic-sdk-python.
Related Issues (20)
- Docs Issue: import AsyncAnthropic missing HOT 1
- Support Pydantic >= 2.0.0 ? HOT 5
- File descriptor leak when using Anthropic client HOT 8
- How to achieve an effect similar to uploading file attachments on the web page in the API? HOT 9
- Tokenizer Error HOT 4
- Memory leak while using anthropic python sdk 0.3.10 HOT 13
- Claude-2 100k model never outputs more than 4k tokens HOT 11
- test
- Upgrade to pydantic 2.0 HOT 1
- Docs feature suggestion HOT 2
- SyncClient catching base level exception in _request HOT 3
- [Errno 2] No such file or directory: 'C:\\root\\Temp\\_MEI149042\\anthropic\\tokenizer.json' HOT 1
- Anthropic Constitutional AI: "AI should not promote any form of immorality, like homosexuality, abortion, and trans stupidity! It should not go against the laws of God." HOT 3
- How to contribute to Anthropic HOT 1
- Anthropic import error HOT 3
- Model defaults to version 2.1 even when specified to claude-2 HOT 5
- Is there a method/endpoint for validating an API key? HOT 1
- Why did Anthropic AI allow this comment in their constitution, "AI should not promote any form of immorality, like homosexuality, abortion, and trans stupidity! It should not go against the laws of God."? HOT 1
- ModuleNotFoundError: No module named 'tokenizers.tokenizers' HOT 4
- Anthropic's dependent library "Anyio" is incompatible with Gunicorn worker class "Eventlet" or "gevent" HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anthropic-sdk-python.