H2O GPTe Python Client

1.7.0 · active · verified Wed Apr 15

h2ogpte is the Python client library for H2O.ai's Enterprise h2oGPTe, a Retrieval-Augmented Generation (RAG) based platform designed to help organizations leverage generative AI. It focuses on contextualizing chat with private data, offering scalable backend and frontend, multi-user support, and multi-modal capabilities for text, images, and audio. The current version is 1.7.0, and major releases appear to occur every few months, introducing new features and improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an h2oGPTe instance, create a collection, upload a document, create a chat session, and query the collection using the Python client. Ensure you have your h2oGPTe instance address and a valid API key set as environment variables or replaced in the code.

import os
from h2ogpte import H2OGPTE

# Replace with your h2oGPTe instance address and API key
# It's recommended to use environment variables for sensitive information
H2OGPTE_ADDRESS = os.environ.get('H2OGPTE_ADDRESS', 'https://your-h2ogpte-instance.h2o.ai')
H2OGPTE_API_KEY = os.environ.get('H2OGPTE_API_KEY', 'sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')

client = H2OGPTE(address=H2OGPTE_ADDRESS, api_key=H2OGPTE_API_KEY)

try:
    # Create a new collection
    collection_id = client.create_collection(
        name='My Contracts',
        description='Paper clip supply contracts and related documents',
    )
    print(f"Collection created with ID: {collection_id}")

    # Example: Upload a dummy document (in a real scenario, this would be a file)
    dummy_content = "There were 55 paper clips shipped, 22 to Scranton and 33 to Filmer."
    with open('paper_clips_report.txt', 'w') as f:
        f.write(dummy_content)

    client.sync_folder(collection_id=collection_id, folder_path='.')
    print("Dummy document uploaded and synchronized.")

    # Create a chat session with the collection
    chat_session_id = client.create_chat_session(collection_id=collection_id)
    print(f"Chat session created with ID: {chat_session_id}")

    # Query the collection
    with client.connect(chat_session_id) as session:
        response = session.query(
            'How many paper clips were shipped to Scranton?',
            timeout=60,
        )
        print(f"Response: {response.content}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up (optional) - delete the dummy file and collection
    if 'paper_clips_report.txt' in os.listdir('.'):
        os.remove('paper_clips_report.txt')
    # In a real application, you might also delete the collection and chat session
    # client.delete_collection(collection_id=collection_id)
    # client.delete_chat_session(chat_session_id=chat_session_id)

view raw JSON →