Metadata-Version: 2.4
Name: chat-completions-conversation-with-tools
Version: 0.1.0a0
Summary: Chat Completions conversation helper with tool support for Python 2 and Python 3.
Author-email: Jifeng Wu <jifengwu2k@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/jifengwu2k/chat-completions-conversation-with-tools
Project-URL: Bug Tracker, https://github.com/jifengwu2k/chat-completions-conversation-with-tools/issues
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Requires-Python: >=2
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: create-inspect-typeddict
Requires-Dist: get-args-and-origin
Requires-Dist: typing; python_version < "3.5"
Dynamic: license-file

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

```bash
pip install chat-completions-conversation-with-tools
```

## Usage

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

```python
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:

```python
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:

```python
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](LICENSE).
