Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: expose resource cleanup methods #444

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ParthSareen
Copy link
Contributor

@ParthSareen ParthSareen commented Feb 13, 2025

  • added support for context manager protocol
  • added explicit .close() method to manage clients

Previously a ResourceWarning was getting thrown:

<sys>:0: ResourceWarning: unclosed <socket.socket fd=3, family=2, type=1, proto=6, laddr=('127.0.0.1', 57073), raddr=('127.0.0.1', 11434)>

This can now be prevented with the client.close() method or by using Python's context manager protocol.
Users can now also use client with context manager protocol as:

with Client() as client:
    messages = [
      {
        'role': 'user',
        'content': 'Why is the sky blue?',
      },
    ]

    response = client.chat('llama3.2', messages=messages)
    print(response['message']['content'])

Closes: #441

@ParthSareen ParthSareen force-pushed the parth/client-resource-cleanup branch from 37c7817 to 1a047b0 Compare February 14, 2025 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to close connections
2 participants