Skip to main content

Chat Completions conversation helper with tool support for Python 2 and Python 3.

Project description

Chat Completions conversation helper with tool support for Python 2 and Python 3.

This module provides a small stateful wrapper around a chat-completions-compatible HTTP API. It manages conversation history, serializes tool definitions, supports regular responses and streaming responses, and accumulates streamed tool call deltas into finalized tool calls.

Installation

pip install chat-completions-conversation-with-tools

Usage

Actual tool execution is application-defined. For demonstration purposes, the README mocks the tool result locally.

import json
from typing import TypedDict

from chat_completions_conversation_with_tools import (
    ChatCompletionsConversationWithTools,
    Tool,
)


class WeatherArgs(TypedDict):
    city: str
    units: str


conversation = ChatCompletionsConversationWithTools(
    api_key="...",
    base_url="https://api.openai.com/v1",
    model="gpt-5.4",
    system_prompt=(
        "Call the get_weather tool to look up the weather for a city."
    ),
    tools_by_name={
        "get_weather": Tool(
            "Look up the weather for a city.",
            WeatherArgs,
        ),
    },
)

Non-streaming:

response = conversation.send_and_receive_response(
    "What is the weather in Paris in metric units?"
)
print('Response:', response)

for tool_call in response.tool_calls:
    print("Tool call:", tool_call)
    tool_message = "18 degrees Celsius"
    conversation.append_tool_message(
        tool_call.id,
        tool_message,
    )

final_response = conversation.send_and_receive_response()
print('Final response:', final_response)

Streaming:

def on_content_delta(content):
    print("Content delta:", content)


def on_tool_call_delta(tool_call_delta):
    print("Tool call delta:", tool_call_delta)


response = conversation.send_and_stream_response(
    text="What is the weather in Paris in metric units?",
    on_content_delta=on_content_delta,
    on_tool_call_delta=on_tool_call_delta,
)
print('Response:', response)

for tool_call in response.tool_calls:
    print("Tool call:", tool_call)
    tool_message = "18 degrees Celsius"
    conversation.append_tool_message(
        tool_call.id,
        tool_message,
    )

final_response = conversation.send_and_stream_response(
    on_content_delta=on_content_delta,
    on_tool_call_delta=on_tool_call_delta,
)
print('Final response:', final_response)

Main public API:

  • Tool: wraps a tool description and a TypedDict-based parameter schema
  • ToolCall: stores tool call id, name, and parsed JSON arguments
  • AssistantResponse: stores content and finalized tool_calls as ToolCall objects
  • ChatCompletionsConversationWithTools: manages message history and API requests

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file chat_completions_conversation_with_tools-0.1.0a0.tar.gz.

File metadata

File hashes

Hashes for chat_completions_conversation_with_tools-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 065afaae12bbe69ff1ef458f340310f3323e0f00633d081489e4c29af91ba069
MD5 1b2d75bf98bf6fb45f6f5e12336c51e2
BLAKE2b-256 6dc220bf24b4b78b32b86d7ceb3ff452fb787ccaab5dafc249d170ccedd14ff4

See more details on using hashes here.

File details

Details for the file chat_completions_conversation_with_tools-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for chat_completions_conversation_with_tools-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d9c8523c112965f6de3263a5c5d7c340884b256ee6f4a0ad4b715bfd1a6eb682
MD5 8ec4bbf69097a15033572fcea8dbb2e8
BLAKE2b-256 78bbd224010af2a555640f8f5212e5456a8db3fc4a331a608e2360a3dbd29c6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page