Code Monkey home page Code Monkey logo

Comments (5)

thejaminator avatar thejaminator commented on June 22, 2024

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.

rattrayalex avatar rattrayalex commented on June 22, 2024

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.

rattrayalex avatar rattrayalex commented on June 22, 2024

Out of curiosity, can you share why you needed/wanted to instantiate the client in a loop like this?

from anthropic-sdk-python.

rattrayalex avatar rattrayalex commented on June 22, 2024

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.

deter3 avatar deter3 commented on June 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.