Skip to main content

Utilities for integration of AI DIAL API with Langchain library

Project description

Overview

PyPI version

The package provides helpers facilitating integration of DIAL API with Langchain library.

Passing DIAL-specific extra fields in Langchain requests/responses

Unlike openai library, langchain-openai library doesn't allow to pass extra request/response parameters to/from the upstream model.

The minimal example highlighting the issue could be found in the example folder:

cd example
python -m venv .venv
source .venv/bin/activate
pip install -q -r requirements.txt
python -m app
Received the following extra fields:
(1) ☑ request.extra_field
(2) ☑ request.tools[0].extra_field
(3) ☐ request.messages[0].extra_field
(4) ☐ response.message.extra_field
(5) ☐ response.extra_field

langchain-openai ignores certain extra fields, meaning that the upstream endpoint won't receive (2) and the client won't receive (4) and (5) if they were sent by the upstream.

Note that top-level request extra fields and tool definition extra fields do actually reach the upstream.

Solution

One way to fix the issue, is to modify the Langchain methods that ignore these extra fields so that they are taken into account.

This is achieved via monkey-patching certain private methods in langchain-openai that do the conversion from the Langchain datatypes to dictionaries and vice versa.

Usage

Install the aidial-integration-langchain package as a dependency in your project:

pip install aidial-integration-langchain

Then import aidial_integration_langchain before importing any Langchain module to apply the patches:

import aidial_integration_langchain.patch # isort:skip  # noqa: F401 # type: ignore

from langchain_openai import AzureChatOpenAI
...

The same example as above, but with the patch applied:

cd example
python -m venv .venv
source .venv/bin/activate
pip install -q -r requirements.txt
pip install -q aidial-integration-langchain
python -m app patch
Received the following extra fields:
(1) ☑ request.extra_field
(2) ☑ request.tools[0].extra_field
(3) ☑ request.messages[0].extra_field
(4) ☑ response.message.extra_field
(5) ☑ response.extra_field

Supported Langchain versions

The following langchain-openai versions have been tested for Python 3.9, 3.10, 3.11, 3.12, and 3.13:

Version Request per-message Response per-message Response top-level
>=0.1.1,<=0.1.22 🟢 🟢 🔴
>=0.1.23,<=0.1.25 🟢 🟢 🟢
>=0.2.0,<=0.2.14 🟢 🟢 🟢
>=0.3.0,<=0.3.18 🟢 🟢 🟢

[!NOTE] The patched langchain-openai<=0.1.22 doesn't support response top-level extra fields, since the structure of the code back then was not very amicable for monkey-patching in this particular respect.

Configuration

The list of extra fields that are allowed to pass-through is controlled by the following environment variables.

Name Default Description
LC_EXTRA_REQUEST_MESSAGE_FIELDS custom_content A comma-separated list of extra message fields allowed to pass-through in chat completion requests.
LC_EXTRA_RESPONSE_MESSAGE_FIELDS custom_content A comma-separated list of extra message fields allowed to pass-through in chat completion responses.
LC_EXTRA_RESPONSE_FIELDS statistics A comma-separated list of extra fields allowed to pass-through on the top-level of the chat completion responses.

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

aidial_integration_langchain-0.2.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

aidial_integration_langchain-0.2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file aidial_integration_langchain-0.2.0.tar.gz.

File metadata

File hashes

Hashes for aidial_integration_langchain-0.2.0.tar.gz
Algorithm Hash digest
SHA256 257b0f93ba26ea1a4ca68b665455675e36ee1ce221271171342d3a213b36e35f
MD5 d80b493a3798fe416276932e3112f1ba
BLAKE2b-256 1e8739a55de3628c9e21a604245be54ac575b2836073067884ee7c7669d0c300

See more details on using hashes here.

File details

Details for the file aidial_integration_langchain-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aidial_integration_langchain-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ff88c24699553e7fb92b16394be5dae3a391f441bfb8ca6d6f640129abbfce7
MD5 f28e66accdea5a1259057449de77e717
BLAKE2b-256 e9ba34ad0210b438d959fea40ebb80a0c43bc8a5f4862b02bf63ccd4ecdc7e0f

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