|
| 1 | +--- |
| 2 | +title: "ChatDoubleword integration" |
| 3 | +sidebarTitle: Doubleword |
| 4 | +description: "Integrate with the ChatDoubleword chat model using LangChain Python." |
| 5 | +--- |
| 6 | + |
| 7 | +This will help you get started with Doubleword [chat models](/oss/langchain/models). [Doubleword](https://doubleword.ai/) is an AI model gateway and control layer that provides unified routing, management, and security for inference across multiple model providers. |
| 8 | + |
| 9 | +The `langchain-doubleword` package provides two chat model classes: |
| 10 | + |
| 11 | +- **`ChatDoubleword`**: Real-time chat completions via the Doubleword gateway. |
| 12 | +- **`ChatDoublewordBatch`**: Cost-optimized batched completions using Doubleword's batch API. Uses `autobatcher` to transparently collect concurrent calls into batch submissions. |
| 13 | + |
| 14 | +## Overview |
| 15 | + |
| 16 | +### Integration details |
| 17 | + |
| 18 | +| Class | Package | Serializable | JS/TS Support | Downloads | Latest Version | |
| 19 | +| :--- | :--- | :---: | :---: | :---: | :---: | |
| 20 | +| `ChatDoubleword` | `langchain-doubleword` | beta | ❌ | <a href="https://pypi.org/project/langchain-doubleword/" target="_blank"><img src="https://static.pepy.tech/badge/langchain-doubleword/month" alt="Downloads per month" noZoom height="100" class="rounded" /></a> | <a href="https://pypi.org/project/langchain-doubleword/" target="_blank"><img src="https://img.shields.io/pypi/v/langchain-doubleword?style=flat-square&label=%20&color=orange" alt="PyPI - Latest version" noZoom height="100" class="rounded" /></a> | |
| 21 | + |
| 22 | +### Model features |
| 23 | + |
| 24 | +| [Tool calling](/oss/langchain/tools) | [Structured output](/oss/langchain/structured-output) | [Image input](/oss/langchain/messages#multimodal) | Audio input | Video input | [Token-level streaming](/oss/langchain/streaming/) | Native async | [Token usage](/oss/langchain/models#token-usage) | [Logprobs](/oss/langchain/models#log-probabilities) | |
| 25 | +| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | |
| 26 | +| ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | |
| 27 | + |
| 28 | +## Setup |
| 29 | + |
| 30 | +To access models via Doubleword you'll need a Doubleword account and an API key. Visit the [Doubleword documentation](https://docs.doubleword.ai) to get started. |
| 31 | + |
| 32 | +### Installation |
| 33 | + |
| 34 | +The LangChain Doubleword integration lives in the `langchain-doubleword` package: |
| 35 | + |
| 36 | +<CodeGroup> |
| 37 | + ```bash pip |
| 38 | + pip install -U langchain-doubleword |
| 39 | + ``` |
| 40 | + ```bash uv |
| 41 | + uv add langchain-doubleword |
| 42 | + ``` |
| 43 | +</CodeGroup> |
| 44 | + |
| 45 | +### Credentials |
| 46 | + |
| 47 | +Generate an API key from your Doubleword dashboard and set the `DOUBLEWORD_API_KEY` environment variable: |
| 48 | + |
| 49 | +```python |
| 50 | +import getpass |
| 51 | +import os |
| 52 | + |
| 53 | +if not os.getenv("DOUBLEWORD_API_KEY"): |
| 54 | + os.environ["DOUBLEWORD_API_KEY"] = getpass.getpass("Enter your Doubleword API key: ") |
| 55 | +``` |
| 56 | + |
| 57 | +You can also pass the key directly via the `api_key` parameter or configure it in `~/.dw/credentials.toml`. |
| 58 | + |
| 59 | +To enable automated tracing of your model calls, set your [LangSmith](/langsmith/home) API key: |
| 60 | + |
| 61 | +```python |
| 62 | +os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ") |
| 63 | +os.environ["LANGSMITH_TRACING"] = "true" |
| 64 | +``` |
| 65 | + |
| 66 | +## Instantiation |
| 67 | + |
| 68 | +Now we can instantiate our model object and generate chat completions: |
| 69 | + |
| 70 | +```python |
| 71 | +from langchain_doubleword import ChatDoubleword |
| 72 | + |
| 73 | +model = ChatDoubleword( |
| 74 | + model="gpt-4o", |
| 75 | + temperature=0, |
| 76 | + max_tokens=1024, |
| 77 | + max_retries=2, |
| 78 | + # api_key="...", # if not using DOUBLEWORD_API_KEY env var |
| 79 | +) |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +## Invocation |
| 85 | + |
| 86 | +```python |
| 87 | +messages = [ |
| 88 | + ( |
| 89 | + "system", |
| 90 | + "You are a helpful assistant that translates English to French. Translate the user sentence.", |
| 91 | + ), |
| 92 | + ("human", "I love programming."), |
| 93 | +] |
| 94 | +ai_msg = model.invoke(messages) |
| 95 | +ai_msg.content |
| 96 | +``` |
| 97 | + |
| 98 | +```text |
| 99 | +"J'adore la programmation." |
| 100 | +``` |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## Streaming |
| 105 | + |
| 106 | +```python |
| 107 | +for chunk in model.stream("Write a short poem about the sea."): |
| 108 | + print(chunk.text, end="", flush=True) |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## Tool calling |
| 114 | + |
| 115 | +Doubleword supports OpenAI-compatible tool calling. You can use `bind_tools` to pass Pydantic classes, dict schemas, LangChain tools, or functions. |
| 116 | + |
| 117 | +```python |
| 118 | +from pydantic import BaseModel, Field |
| 119 | + |
| 120 | + |
| 121 | +class GetWeather(BaseModel): |
| 122 | + """Get the current weather in a given location""" |
| 123 | + |
| 124 | + location: str = Field(description="The city and state, e.g. San Francisco, CA") |
| 125 | + |
| 126 | + |
| 127 | +model_with_tools = model.bind_tools([GetWeather]) |
| 128 | +ai_msg = model_with_tools.invoke("What is the weather like in San Francisco?") |
| 129 | +ai_msg.tool_calls |
| 130 | +``` |
| 131 | + |
| 132 | +```text |
| 133 | +[{'name': 'GetWeather', |
| 134 | + 'args': {'location': 'San Francisco, CA'}, |
| 135 | + 'id': 'call_abc123', |
| 136 | + 'type': 'tool_call'}] |
| 137 | +``` |
| 138 | + |
| 139 | +For more on binding tools and tool call outputs, head to the [tool calling](/oss/langchain/tools) docs. |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +## Batch processing |
| 144 | + |
| 145 | +`ChatDoublewordBatch` uses Doubleword's batch API to transparently collect concurrent calls into batch submissions at reduced cost. This is useful for high-throughput workloads where real-time responses are not required. |
| 146 | + |
| 147 | +```python |
| 148 | +from langchain_doubleword import ChatDoublewordBatch |
| 149 | + |
| 150 | +batch_model = ChatDoublewordBatch( |
| 151 | + model="gpt-4o", |
| 152 | + temperature=0, |
| 153 | +) |
| 154 | + |
| 155 | +# Calls are automatically batched behind the scenes |
| 156 | +result = batch_model.invoke("Summarize the theory of relativity in one sentence.") |
| 157 | +result.content |
| 158 | +``` |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## API reference |
| 163 | + |
| 164 | +For detailed documentation and source code, visit the [`langchain-doubleword` GitHub repository](https://github.com/doublewordai/langchain-doubleword). |
0 commit comments