### Install UltraContext Python SDK Source: https://github.com/ultracontext/ultracontext-python/blob/main/README.md Installs the UltraContext Python package using pip. This command downloads and installs the necessary dependencies to use the UltraContext API. ```bash pip install ultracontext ``` -------------------------------- ### Quickstart: Create, Append, and Use Context (Python) Source: https://github.com/ultracontext/ultracontext-python/blob/main/README.md Demonstrates the basic usage of the UltraContext API in Python. It initializes the UltraContext client, creates a new context, appends a message, and shows how to use the context with an LLM framework. ```python from ultracontext import UltraContext uc = UltraContext(api_key="uc_live_...") ctx = uc.create() uc.append(ctx["id"], {"role": "user", "content": "Hello!"}) # use with any LLM framework response = generate_text(model=model, messages=uc.get(ctx["id"])["data"]) ``` -------------------------------- ### Quickstart: Async Create, Append Context (Python) Source: https://github.com/ultracontext/ultracontext-python/blob/main/README.md Demonstrates the asynchronous usage of the UltraContext API in Python. It initializes the AsyncUltraContext client, creates a new context, and appends a message asynchronously. ```python import asyncio from ultracontext import AsyncUltraContext async def main(): uc = AsyncUltraContext(api_key="uc_live_...") ctx = await uc.create() await uc.append(ctx["id"], {"role": "user", "content": "Hello!"}) asyncio.run(main()) ``` -------------------------------- ### Integrate with LLM Frameworks (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Demonstrates how to integrate the UltraContext Python SDK with LLM frameworks. It retrieves context data, formats it as messages, and shows an example of using it with OpenAI. It also shows how to store the LLM's response back into UltraContext. ```python from ultracontext import UltraContext # from openai import OpenAI # or any LLM client uc = UltraContext(api_key="uc_live_your_api_key") # Create or retrieve existing context ctx = uc.create() # Add user message uc.append(ctx["id"], {"role": "user", "content": "Explain quantum computing"}) # Get messages for LLM context_data = uc.get(ctx["id"]) messages = context_data["data"] # Use with OpenAI (example) # client = OpenAI() # response = client.chat.completions.create( # model="gpt-4", # messages=messages # ) # Store assistant response # uc.append(ctx["id"], { # "role": "assistant", # "content": response.choices[0].message.content # }) # Later: retrieve full conversation full_context = uc.get(ctx["id"]) print(f"Conversation has {len(full_context['data'])} messages at version {full_context['version']}") ``` -------------------------------- ### API: Get Context (Python) Source: https://github.com/ultracontext/ultracontext-python/blob/main/README.md Demonstrates how to retrieve context using the UltraContext API. It includes retrieving all contexts, retrieving with limits, and retrieving specific versions or points in time. ```python # get - retrieve context (or list all) ctxs = uc.get() ctxs = uc.get(limit=10) data = uc.get("ctx_abc123") data = uc.get("ctx_abc123", version=2) data = uc.get("ctx_abc123", at=5) data = uc.get("ctx_abc123", history=True) ``` -------------------------------- ### GET /get - Retrieve Context or List All Contexts Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Retrieves a specific context by ID with optional time-travel parameters, or lists all contexts without an ID. ```APIDOC ## GET /get ### Description Retrieves a specific context by ID with optional time-travel parameters, or lists all contexts without an ID. ### Method GET ### Endpoint /get ### Parameters #### Path Parameters - **id** (string) - Optional - The ID of the context to retrieve. #### Query Parameters - **limit** (integer) - Optional - Limit the number of contexts returned when listing all contexts. - **version** (integer) - Optional - Retrieve a specific version of the context. - **at** (integer) - Optional - Retrieve messages up to a specific index. - **before** (string) - Optional - Retrieve the state before a timestamp. - **history** (boolean) - Optional - Include version history. ### Request Example ``` /get/ctx_abc123?version=2 ``` ### Response #### Success Response (200) - **data** (array) - An array of messages in the context. - **version** (integer) - The current version of the context. - **versions** (array) - Optional - The version history of the context. #### Response Example ```json { "data": [ {"id": "msg_1", "index": 0, "role": "user", "content": "Hello"} ], "version": 3 } ``` ``` -------------------------------- ### Initialize UltraContext Synchronous Client (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Initialize the synchronous UltraContext client using your API key. Optional parameters allow customization of the base URL, request timeout, and additional headers for advanced configurations. ```python from ultracontext import UltraContext # Basic initialization uc = UltraContext(api_key="uc_live_your_api_key") # Advanced initialization with custom settings uc = UltraContext( api_key="uc_live_your_api_key", base_url="https://api.ultracontext.ai", # default timeout=30.0, # default timeout in seconds headers={"X-Custom-Header": "value"} ) ``` -------------------------------- ### POST /create - Create New Context Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Creates a new context or forks from an existing one. Forking allows creating branches from specific versions, indexes, or timestamps. ```APIDOC ## POST /create ### Description Creates a new empty context or forks from an existing one. Forking allows creating branches from specific versions, indexes, or timestamps. ### Method POST ### Endpoint /create ### Parameters #### Request Body - **metadata** (object) - Optional - Metadata to associate with the context. - **from_** (string) - Optional - ID of the context to fork from. - **version** (integer) - Optional - Version to fork from. - **at** (integer) - Optional - Index to fork up to (0-based). - **before** (string) - Optional - Timestamp to fork before. ### Request Example ```json { "metadata": {"user_id": "user_123", "session": "support_chat"} } ``` ### Response #### Success Response (200) - **id** (string) - The ID of the created context. - **metadata** (object) - The metadata associated with the context. - **created_at** (string) - The timestamp of context creation. #### Response Example ```json { "id": "ctx_def456", "metadata": {"user_id": "user_123", "session": "support_chat"}, "created_at": "2024-01-15T10:31:00Z" } ``` ``` -------------------------------- ### API: Create Context (Python) Source: https://github.com/ultracontext/ultracontext-python/blob/main/README.md Shows different ways to create a context using the UltraContext API, including creating a new context, forking from an existing one, and creating with metadata. ```python # create - new context or fork ctx = uc.create() ctx = uc.create(from_="ctx_abc123") ctx = uc.create(from_="ctx_abc123", version=2) ctx = uc.create(from_="ctx_abc123", at=5) ctx = uc.create(metadata={"user_id": "123"}) ``` -------------------------------- ### Initialize AsyncUltraContext Asynchronous Client (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Initialize the asynchronous UltraContext client for use in async Python applications. This client provides the same core functionality as the synchronous client but with an async/await syntax. ```python import asyncio from ultracontext import AsyncUltraContext async def main(): uc = AsyncUltraContext(api_key="uc_live_your_api_key") # Create and use context ctx = await uc.create() await uc.append(ctx["id"], {"role": "user", "content": "Hello!"}) result = await uc.get(ctx["id"]) print(result["data"]) # Output: [{"id": "msg_abc123", "index": 0, "role": "user", "content": "Hello!"}] asyncio.run(main()) ``` -------------------------------- ### Create New Context with UltraContext (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Create a new context using the UltraContext client. This can be an empty context, a context with metadata, or a fork from an existing context at a specific version, index, or timestamp. ```python from ultracontext import UltraContext uc = UltraContext(api_key="uc_live_your_api_key") # Create empty context ctx = uc.create() print(ctx) # Output: {"id": "ctx_abc123", "metadata": None, "created_at": "2024-01-15T10:30:00Z"} # Create with metadata ctx = uc.create(metadata={"user_id": "user_123", "session": "support_chat"}) print(ctx) # Output: {"id": "ctx_def456", "metadata": {"user_id": "user_123", "session": "support_chat"}, "created_at": "2024-01-15T10:31:00Z"} # Fork from existing context forked = uc.create(from_="ctx_abc123") # Fork from specific version forked = uc.create(from_="ctx_abc123", version=2) # Fork up to specific message index (0-based) forked = uc.create(from_="ctx_abc123", at=5) # Fork from point-in-time before timestamp forked = uc.create(from_="ctx_abc123", before="2024-01-15T10:30:00Z") ``` -------------------------------- ### Handle API Errors (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Illustrates error handling in the UltraContext Python SDK. It shows how to catch and handle `UltraContextHttpError` and `UltraContextError` exceptions, providing details about the error, status code, URL, and response body. ```python from ultracontext import UltraContext, UltraContextHttpError, UltraContextError uc = UltraContext(api_key="uc_live_your_api_key") try: result = uc.get("ctx_nonexistent") except UltraContextHttpError as e: print(f"HTTP Error: {e}") print(f"Status Code: {e.status}") # e.g., 404 print(f"URL: {e.url}") # e.g., https://api.ultracontext.ai/contexts/ctx_nonexistent print(f"Response Body: {e.body}") # e.g., {"error": "Context not found"} except UltraContextError as e: print(f"General Error: {e}") ``` -------------------------------- ### Retrieve Contexts with UltraContext (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Retrieve context data using the UltraContext client. You can list all contexts, retrieve a specific context by ID, or use time-travel parameters like version, index, or timestamp for historical retrieval. ```python from ultracontext import UltraContext uc = UltraContext(api_key="uc_live_your_api_key") # List all contexts contexts = uc.get() print(contexts) # Output: {"data": [{"id": "ctx_abc123", "metadata": {}, "created_at": "2024-01-15T10:30:00Z"}, ...]}} # List with limit contexts = uc.get(limit=10) # Get specific context result = uc.get("ctx_abc123") print(result) # Output: {"data": [{"id": "msg_1", "index": 0, "role": "user", "content": "Hello"}], "version": 3} # Get specific version result = uc.get("ctx_abc123", version=2) # Get messages up to index result = uc.get("ctx_abc123", at=5) # Get state before timestamp result = uc.get("ctx_abc123", before="2024-01-15T10:30:00Z") # Include version history result = uc.get("ctx_abc123", history=True) print(result) # Output: { # "data": [...], # "version": 3, # "versions": [ # {"version": 1, "created_at": "...", "operation": "append", "affected": ["msg_1"]}, # {"version": 2, "created_at": "...", "operation": "update", "affected": ["msg_1"]}, # {"version": 3, "created_at": "...", "operation": "delete", "affected": ["msg_2"]} # ] # } ``` -------------------------------- ### Update Messages in Context (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Illustrates how to update existing messages within a context using the UltraContext Python SDK. It covers updating by message ID and index, including negative indexing for the last message. It also demonstrates how to include metadata with updates and perform batch updates. ```python from ultracontext import UltraContext uc = UltraContext(api_key="uc_live_your_api_key") ctx = uc.create() uc.append(ctx["id"], {"role": "user", "content": "Helo world"}) # typo # Update by message ID result = uc.update(ctx["id"], id="msg_abc123", content="Hello world") print(result) # Output: {"data": [{"id": "msg_abc123", "index": 0, "role": "user", "content": "Hello world"}], "version": 2} # Update by index (0-based, negative indexes supported) result = uc.update(ctx["id"], index=0, content="Hello, world!") # Update last message using negative index result = uc.update(ctx["id"], index=-1, content="Fixed last message") # Update with audit metadata result = uc.update( ctx["id"], id="msg_abc123", content="Hello world!", metadata={"reason": "typo fix", "editor": "user_123"} ) # Batch update multiple messages result = uc.update(ctx["id"], updates=[ {"id": "msg_abc", "content": "Updated content 1"}, {"index": 2, "content": "Updated content 2"}, {"id": "msg_xyz", "role": "system"} # change role ]) ``` -------------------------------- ### Append Messages to Context (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Demonstrates how to append messages to a context using the UltraContext Python SDK. It shows appending both standard messages and messages with custom schemas. The output of the append operation, including message IDs and version information, is also shown. ```python result = uc.append(ctx["id"], [ {"role": "assistant", "content": "The weather is sunny, 72°F."}, {"role": "user", "content": "Thanks! What about tomorrow?"} ]) print(result) # Output: { # "data": [ # {"id": "msg_def", "index": 1, "role": "assistant", "content": "The weather is sunny, 72°F."}, # {"id": "msg_ghi", "index": 2, "role": "user", "content": "Thanks! What about tomorrow?"} # ], # "version": 2 # } # Append custom schema (schema-free) result = uc.append(ctx["id"], { "type": "tool_call", "tool": "get_weather", "arguments": {"location": "San Francisco", "date": "tomorrow"} }) ``` -------------------------------- ### API: Update Messages (Python) Source: https://github.com/ultracontext/ultracontext-python/blob/main/README.md Demonstrates how to update messages within a context using the UltraContext API. Updates can be made by ID or index, and metadata can be added. ```python # update - modify by id or index (auto-versions) uc.update(ctx["id"], id="msg_xyz", content="Fixed!") uc.update(ctx["id"], index=-1, content="Fix last message") uc.update(ctx["id"], id="msg_xyz", content="Fixed!", metadata={"reason": "typo"}) ``` -------------------------------- ### POST /append - Add Messages to Context Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Appends one or more messages to an existing context. Messages can be any JSON structure (schema-free). ```APIDOC ## POST /append ### Description Appends one or more messages to an existing context. Messages can be any JSON structure (schema-free). ### Method POST ### Endpoint /append ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the context to append to. #### Request Body - **messages** (array) - Required - An array of messages to append. Each message is a JSON object. ### Request Example ```json { "messages": [{"role": "user", "content": "What is the weather today?"}] } ``` ### Response #### Success Response (200) - **data** (array) - An array of the appended messages. - **version** (integer) - The new version of the context. #### Response Example ```json { "data": [ {"id": "msg_abc", "index": 0, "role": "user", "content": "What is the weather today?"} ], "version": 1 } ``` ``` -------------------------------- ### API: Append Messages (Python) Source: https://github.com/ultracontext/ultracontext-python/blob/main/README.md Shows how to append messages to a context using the UltraContext API. Messages can be added as single dictionaries or as a list of dictionaries. ```python # append - add messages (schema-free) uc.append(ctx["id"], {"role": "user", "content": "Hi"}) uc.append(ctx["id"], [{"role": "user", "content": "Hi"}, {"foo": "bar"}]) ``` -------------------------------- ### Delete Messages from Context (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Demonstrates how to delete messages from a context using the UltraContext Python SDK. It shows how to delete messages by ID and index, including negative indexing. It also covers deleting multiple messages and including metadata with the deletion operation. ```python from ultracontext import UltraContext uc = UltraContext(api_key="uc_live_your_api_key") ctx = uc.create() uc.append(ctx["id"], [ {"role": "user", "content": "Message 1"}, {"role": "assistant", "content": "Message 2"}, {"role": "user", "content": "Message 3"} ]) # Delete by message ID result = uc.delete(ctx["id"], "msg_abc123") print(result) # Output: {"data": [...remaining messages...], "version": 2} # Delete by index result = uc.delete(ctx["id"], 0) # delete first message # Delete last message using negative index result = uc.delete(ctx["id"], -1) # Delete multiple messages result = uc.delete(ctx["id"], ["msg_abc", "msg_def", -1]) # Delete with audit metadata result = uc.delete( ctx["id"], ["msg_abc", "msg_def"], metadata={"reason": "cleanup", "performed_by": "admin"} ) ``` -------------------------------- ### API: Delete Messages (Python) Source: https://github.com/ultracontext/ultracontext-python/blob/main/README.md Shows how to delete messages from a context using the UltraContext API. Messages can be deleted by ID or index, and metadata can be added. ```python # delete - remove by id or index (auto-versions) uc.delete(ctx["id"], "msg_xyz") uc.delete(ctx["id"], -1) uc.delete(ctx["id"], ["msg_a", "msg_b", -1], metadata={"reason": "cleanup"}) ``` -------------------------------- ### Append Messages to Context with UltraContext (Python) Source: https://context7.com/ultracontext/ultracontext-python/llms.txt Append one or more messages to an existing context using the UltraContext client. The messages can be any valid JSON structure, adhering to the schema-free design of the SDK. ```python from ultracontext import UltraContext uc = UltraContext(api_key="uc_live_your_api_key") ctx = uc.create() # Append single message result = uc.append(ctx["id"], {"role": "user", "content": "What is the weather today?"}) print(result) # Output: { # "data": [{"id": "msg_abc", "index": 0, "role": "user", "content": "What is the weather today?"}], # "version": 1 # } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.