Compare commits

...

158 Commits

Author SHA1 Message Date
ccurme
61d2dc011e openai: release 0.3.14 (#30908) 2025-04-17 10:49:14 -04:00
ccurme
f0f90c4d88 anthropic: release 0.3.12 (#30907) 2025-04-17 14:45:12 +00:00
ccurme
f01b89df56 standard-tests: release 0.3.19 (#30906) 2025-04-17 10:37:44 -04:00
ccurme
add6a78f98 standard-tests, openai[patch]: add support standard audio inputs (#30904) 2025-04-17 10:30:57 -04:00
ccurme
2c2db1ab69 core: release 0.3.53 (#30901) 2025-04-17 13:10:32 +00:00
ccurme
86d51f6be6 multiple: permit optional fields on multimodal content blocks (#30887)
Instead of stuffing provider-specific fields in `metadata`, they can go
directly on the content block.
2025-04-17 12:48:46 +00:00
湛露先生
83b66cb916 doc: clean doc word description. (#30895)
Signed-off-by: zhanluxianshen <zhanluxianshen@163.com>
2025-04-17 08:04:37 -04:00
湛露先生
ff2930c119 partners: bug fix check_imports.py exit code. (#30897)
Signed-off-by: zhanluxianshen <zhanluxianshen@163.com>
2025-04-17 08:02:23 -04:00
ccurme
b36c2bf833 docs: update Bedrock chat model page (#30883)
- document prompt caching
- feature ChatBedrockConverse throughout
2025-04-16 16:55:14 -04:00
ccurme
9e82f1df4e docs: minor clean up in ChatOpenAI docs (#30884) 2025-04-16 16:08:43 -04:00
ccurme
fa362189a1 docs: document OpenAI reasoning summaries (#30882) 2025-04-16 19:21:14 +00:00
Sydney Runkle
88fce67724 core: Removing unnecessary pydantic core schema rebuilds (#30848)
We only need to rebuild model schemas if type annotation information
isn't available during declaration - that shouldn't be the case for
these types corrected here.

Need to do more thorough testing to make sure these structures have
complete schemas, but hopefully this boosts startup / import time.
2025-04-16 12:00:08 -04:00
rrozanski-smabbler
60d8ade078 Galaxia integration (#30792)
- [ ] **PR title**: "docs: adding Smabbler's Galaxia integration"

- [ ] **PR message**:  **Twitter handle:** @Galaxia_graph

I'm adding docs here + added the package to the packages.yml. I didn't
add a unit test, because this integration is just a thin wrapper on top
of our API. There isn't much left to test if you mock it away.

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-16 10:39:04 -04:00
ccurme
ca39680d2a ollama: release 0.3.2 (#30865) 2025-04-16 09:14:57 -04:00
Sydney Runkle
4af3f89a3a docs: enforce newlines when signature exceeds char threshold (#30866)
Below is an example of the single line vs new multiline approach.

Before this PR:

<img width="831" alt="Screenshot 2025-04-15 at 8 56 26 PM"
src="https://github.com/user-attachments/assets/0c0277bd-2441-4b22-a536-e16984fd91b7"
/>

After this PR:

<img width="829" alt="Screenshot 2025-04-15 at 8 56 13 PM"
src="https://github.com/user-attachments/assets/e16bfe38-bb17-48ba-a642-e8ff6b48e841"
/>
2025-04-16 08:45:40 -04:00
milosz-l
4ff576e37d langchain: infer Perplexity provider for sonar model prefix (#30861)
**Description:** This PR adds provider inference logic to
`init_chat_model` for Perplexity models that use the "sonar..." prefix
(`sonar`, `sonar-pro`, `sonar-reasoning`, `sonar-reasoning-pro` or
`sonar-deep-research`).

This allows users to initialize these models by simply passing the model
name, without needing to explicitly set `model_provider="perplexity"`.

The docstring for `init_chat_model` has also been updated to reflect
this new inference rule.
2025-04-15 18:17:21 -04:00
ccurme
085baef926 ollama[patch]: support standard image format (#30864)
Following https://github.com/langchain-ai/langchain/pull/30746
2025-04-15 22:14:50 +00:00
ccurme
47ded80b64 ollama[patch]: fix generation info (#30863)
https://github.com/langchain-ai/langchain/pull/30778 (not released)
broke all invocation modes of ChatOllama (intent was to remove
`"message"` from `generation_info`, but we turned `generation_info` into
`stream_resp["message"]`), resulting in validation errors.
2025-04-15 19:22:58 +00:00
Sydney Runkle
cf2697ec53 chroma: release 0.2.3 (#30860) 2025-04-15 14:11:23 -04:00
ccurme
8e9569cbc8 perplexity: release 0.1.1 (#30859) 2025-04-15 18:02:15 +00:00
ccurme
dd5f5902e3 openai: release 0.3.13 (#30858) 2025-04-15 17:58:12 +00:00
ccurme
3382ee8f57 anthropic: release 0.3.11 (#30857) 2025-04-15 17:57:00 +00:00
Sydney Runkle
ef5aff3b6c core[fix]: Fix __dir__ in __init__.py for output_parsers module (#30856)
We have a `list.py` file which causes a namespace conflict with `list`
from stdlib, unfortunately.

`__all__` is already a list, so no need to coerce.
2025-04-15 13:09:13 -04:00
Christophe Bornet
a4ca1fe0ed core: Remove some noqa (#30855) 2025-04-15 13:08:40 -04:00
ccurme
6baf5c05a6 standard-tests: release 0.3.18 (#30854) 2025-04-15 16:56:54 +00:00
ccurme
c6a8663afb infra: run old standard-tests on core releases (#30852)
On core releases, we check out the latest published package for
langchain-openai and langchain-anthropic and run their tests against the
candidate version of langchain-core.

Because these packages have a local install of langchain-tests, we also
need to check out the previous version of langchain-tests.
2025-04-15 16:04:08 +00:00
Sydney Runkle
1f5e207379 core[fix]: remove load from dynamic imports dict (#30849) 2025-04-15 12:02:46 -04:00
ccurme
7240458619 core: release 0.3.52 (#30850) 2025-04-15 15:28:31 +00:00
Sydney Runkle
6aa5494a75 Fix from langchain_core.load.load import load import (#30843)
TL;DR: you can't optimize imports with a lazy `__getattr__` if there is
a namespace conflict with a module name and an attribute name. We should
avoid introducing conflicts like this in the future.

This PR fixes a bug introduced by my lazy imports PR:
https://github.com/langchain-ai/langchain/pull/30769.

In `langchain_core`, we have utilities for loading and dumping data.
Unfortunately, one of those utilities is a `load` function, located in
`langchain_core/load/load.py`. To make this function more visible, we
make it accessible at the top level `langchain_core.load` module via
importing the function in `langchain_core/load/__init__.py`.

So, either of these imports should work:

```py
from langchain_core.load import load
from langchain_core.load.load import load
```

As you can tell, this is already a bit confusing. You'd think that the
first import would produce the module `load`, but because of the
`__init__.py` shortcut, both produce the function `load`.

<details> More on why the lazy imports PR broke this support...

All was well, except when the absolute import was run first, see the
last snippet:

```
>>> from langchain_core.load import load
>>> load
<function load at 0x101c320c0>
```

```
>>> from langchain_core.load.load import load
>>> load
<function load at 0x1069360c0>
```

```
>>> from langchain_core.load import load
>>> load
<function load at 0x10692e0c0>
>>> from langchain_core.load.load import load
>>> load
<function load at 0x10692e0c0>
```

```
>>> from langchain_core.load.load import load
>>> load
<function load at 0x101e2e0c0>
>>> from langchain_core.load import load
>>> load
<module 'langchain_core.load.load' from '/Users/sydney_runkle/oss/langchain/libs/core/langchain_core/load/load.py'>
```

In this case, the function `load` wasn't stored in the globals cache for
the `langchain_core.load` module (by the lazy import logic), so Python
defers to a module import.

</details>

New `langchain` tongue twister 😜: we've created a problem for ourselves
because you have to load the load function from the load file in the
load module 😨.
2025-04-15 11:06:13 -04:00
Bagatur
7262de4217 core[patch]: dict chat prompt template support (#25674)
- Support passing dicts as templates to chat prompt template
- Support making *any* attribute on a message a runtime variable
- Significantly simpler than trying to update our existing prompt
template classes

```python
    template = ChatPromptTemplate(
        [
            {
                "role": "assistant",
                "content": [
                    {
                        "type": "text",
                        "text": "{text1}",
                        "cache_control": {"type": "ephemeral"},
                    },
                    {"type": "image_url", "image_url": {"path": "{local_image_path}"}},
                ],
                "name": "{name1}",
                "tool_calls": [
                    {
                        "name": "{tool_name1}",
                        "args": {"arg1": "{tool_arg1}"},
                        "id": "1",
                        "type": "tool_call",
                    }
                ],
            },
            {
                "role": "tool",
                "content": "{tool_content2}",
                "tool_call_id": "1",
                "name": "{tool_name1}",
            },
        ]
    )

```

will likely close #25514 if we like this idea and update to use this
logic

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-15 11:00:49 -04:00
ccurme
9cfe6bcacd multiple: multi-modal content blocks (#30746)
Introduces standard content block format for images, audio, and files.

## Examples

Image from url:
```
{
    "type": "image",
    "source_type": "url",
    "url": "https://path.to.image.png",
}
```


Image, in-line data:
```
{
    "type": "image",
    "source_type": "base64",
    "data": "<base64 string>",
    "mime_type": "image/png",
}
```


PDF, in-line data:
```
{
    "type": "file",
    "source_type": "base64",
    "data": "<base64 string>",
    "mime_type": "application/pdf",
}
```


File from ID:
```
{
    "type": "file",
    "source_type": "id",
    "id": "file-abc123",
}
```


Plain-text file:
```
{
    "type": "file",
    "source_type": "text",
    "text": "foo bar",
}
```
2025-04-15 09:48:06 -04:00
湛露先生
09438857e8 docs: fix tools_human.ipynb url 404. (#30831)
Fix the 404 pages.

Signed-off-by: zhanluxianshen <zhanluxianshen@163.com>
2025-04-15 09:22:13 -04:00
Sydney Runkle
e3b6cddd5e core: codspeed tweak to make sure it runs on master (#30845) 2025-04-15 13:03:44 +00:00
Sydney Runkle
59f2c9e737 Tinkering with CodSpeed (#30824)
Fix CI to trigger benchmarks on `run-codspeed-benchmarks` label addition

Reduce scope of async benchmark to save time on CI

Waiting to merge this PR until we figure out how to use walltime on
local runners.
2025-04-15 08:49:09 -04:00
William FH
ed5c4805f6 Consistent docstring indentation (#30834)
Should be 4 spaces instead of 3.
2025-04-14 19:04:35 -07:00
Joey Constantino
2282762528 docs: small Tableau docs update (#30827)
Description: small Tableau docs update
Issue: adds required environment variable
Dependencies: tableau-langchain

---------

Co-authored-by: Joe Constantino <joe.constantino@joecons-ltm6v86.internal.salesforce.com>
2025-04-14 15:34:54 -04:00
ccurme
f7c4965fb6 openai[patch]: update imports in test (#30828)
Quick fix to unblock CI, will need to address in core separately.
2025-04-14 19:33:38 +00:00
Sydney Runkle
edb6a23aea core[lint]: fix issue with unused ignore in __init__.py files (#30825)
Fixing a race condition between
https://github.com/langchain-ai/langchain/pull/30769 and
https://github.com/langchain-ai/langchain/pull/30737
2025-04-14 17:57:00 +00:00
湛露先生
3a64c7195f community: redis tool typos fix (#30811) 2025-04-14 09:01:36 -04:00
Sydney Runkle
4f69094b51 core[performance]: use custom __getattr__ in __init__.py files for lazy imports (#30769)
Most easily reviewed with the "hide whitespace" option toggled.

Seeing 10-50% speed ups in import time for common structures 🚀 

The general purpose of this PR is to lazily import structures within
`langchain_core.XXX_module.__init__.py` so that we're not eagerly
importing expensive dependencies (`pydantic`, `requests`, etc).

Analysis of flamegraphs generated with `importtime` motivated these
changes. For example, the one below demonstrates that importing
`HumanMessage` accidentally triggered imports for `importlib.metadata`,
`requests`, etc.

There's still much more to do on this front, and we can start digging
into our own internal code for optimizations now that we're less
concerned about external imports.

<img width="1210" alt="Screenshot 2025-04-11 at 1 10 54 PM"
src="https://github.com/user-attachments/assets/112a3fe7-24a9-4294-92c1-d5ae64df839e"
/>

I've tracked the improvements with some local benchmarks:

## `pytest-benchmark` results

| Name | Before (s) | After (s) | Delta (s) | % Change |

|-----------------------------|------------|-----------|-----------|----------|
| Document | 2.8683 | 1.2775 | -1.5908 | -55.46% |
| HumanMessage | 2.2358 | 1.1673 | -1.0685 | -47.79% |
| ChatPromptTemplate | 5.5235 | 2.9709 | -2.5526 | -46.22% |
| Runnable | 2.9423 | 1.7793 | -1.163 | -39.53% |
| InMemoryVectorStore | 3.1180 | 1.8417 | -1.2763 | -40.93% |
| RunnableLambda | 2.7385 | 1.8745 | -0.864 | -31.55% |
| tool | 5.1231 | 4.0771 | -1.046 | -20.42% |
| CallbackManager | 4.2263 | 3.4099 | -0.8164 | -19.32% |
| LangChainTracer | 3.8394 | 3.3101 | -0.5293 | -13.79% |
| BaseChatModel | 4.3317 | 3.8806 | -0.4511 | -10.41% |
| PydanticOutputParser | 3.2036 | 3.2995 | 0.0959 | 2.99% |
| InMemoryRateLimiter | 0.5311 | 0.5995 | 0.0684 | 12.88% |

Note the lack of change for `InMemoryRateLimiter` and
`PydanticOutputParser` is just random noise, I'm getting comparable
numbers locally.

## Local CodSpeed results

We're still working on configuring CodSpeed on CI. The local usage
produced similar results.
2025-04-14 08:57:54 -04:00
Christophe Bornet
ada740b5b9 community: Add ruff rule PGH003 (#30812)
See https://docs.astral.sh/ruff/rules/blanket-type-ignore/

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-14 02:32:13 +00:00
ccurme
f005988e31 community[patch]: fix cost calculations for o3 in OpenAI callback (#30807)
Resolves https://github.com/langchain-ai/langchain/issues/30795
2025-04-13 15:20:46 +00:00
BoyuHu
446361a0d3 docs: fix typo (#30800)
Thank you for contributing to LangChain!

- [ ] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core, etc. is
being modified. Use "docs: ..." for purely docs changes, "infra: ..."
for CI changes.
  - Example: "community: add foobar LLM"


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** a description of the change
    - **Issue:** the issue # it fixes, if applicable
    - **Dependencies:** any dependencies required for this change
- **Twitter handle:** if your PR gets announced, and you'd like a
mention, we'll gladly shout you out!


- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [ ] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.
2025-04-13 10:55:30 -04:00
Marina Gómez
afd457d8e1 perplexity[patch]: Fix #30767: Handle missing citations attribute in ChatPerplexity (#30805)
This PR fixes an issue where ChatPerplexity would raise an
AttributeError when the citations attribute was missing from the model
response (e.g., when using offline models like r1-1776).

The fix checks for the presence of citations, images, and
related_questions before attempting to access them, avoiding crashes in
models that don't provide these fields.

Tested locally with models that omit citations, and the fix works as
expected.
2025-04-13 09:24:05 -04:00
Christophe Bornet
42944f3499 core: Improve mypy config (#30737)
* Cleanup mypy config
* Add mypy `strict` rules except `disallow_any_generics`,
`warn_return_any` and `strict_equality` (TODO)
* Add mypy `strict_byte` rule
* Add mypy support for PEP702 `@deprecated` decorator
* Bump mypy version to 1.15

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2025-04-11 16:35:13 -04:00
mpb159753
bb2c2fd885 docs: Add openGauss vector store documentation (#30742)
Hey LangChain community! 👋 Excited to propose official documentation for
our new openGauss integration that brings powerful vector capabilities
to the stack!

### What's Inside 📦  
1. **Full Integration Guide**  
Introducing
[langchain-opengauss](https://pypi.org/project/langchain-opengauss/) on
PyPI - your new toolkit for:
   🔍 Native hybrid search (vectors + metadata)  
   🚀 Production-grade connection pooling  
   🧩 Automatic schema management  

2. **Rigorous Testing Passed**   
![Benchmark
Results](https://github.com/user-attachments/assets/ae3b21f7-aeea-4ae7-a142-f2aec57936a0)
   - 100% non-async test coverage  

ps: Current implementation resides in my personal repository:
https://github.com/mpb159753/langchain-opengauss, How can I transfer
process to langchain-ai org?? *Keen to hear your thoughts and make this
integration shine!* 

---------

Co-authored-by: Eugene Yurtsev <eugene@langchain.dev>
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2025-04-11 20:31:39 +00:00
Christophe Bornet
913c896598 core: Add ruff rules FBT001 and FBT002 (#30695)
Add ruff rules
[FBT001](https://docs.astral.sh/ruff/rules/boolean-type-hint-positional-argument/)
and
[FBT002](https://docs.astral.sh/ruff/rules/boolean-default-value-positional-argument/).
Mostly `noqa`s to not introduce breaking changes and possible
non-breaking fixes have already been done in a [previous
PR](https://github.com/langchain-ai/langchain/pull/29424).
These rules will prevent new violations to happen.
2025-04-11 16:26:33 -04:00
William FH
2803a48661 core[patch]: Share executor for async callbacks run in sync context (#30779)
To avoid having to create ephemeral threads, grab the thread lock, etc.
2025-04-11 10:34:43 -07:00
Sydney Runkle
fdc2b4bcac core[lint]: Use 3.9 formatting for docs and tests (#30780)
Looks like `pyupgrade` was already used here but missed some docs and
tests.

This helps to keep our docs looking professional and up to date.
Eventually, we should lint / format our inline docs.
2025-04-11 10:39:25 -04:00
Sydney Runkle
48affc498b langchain[lint]: use pyupgrade to get to 3.9 standards (#30782) 2025-04-11 10:33:26 -04:00
ccurme
d9b628e764 xai: release 0.2.3 (#30790) 2025-04-11 14:05:11 +00:00
ccurme
9cfb95e621 xai[patch]: support reasoning content (#30758)
https://docs.x.ai/docs/guides/reasoning

```python
from langchain.chat_models import init_chat_model

llm = init_chat_model(
    "xai:grok-3-mini-beta",
    reasoning_effort="low"
)
response = llm.invoke("Hello, world!")
```
2025-04-11 14:00:27 +00:00
Christophe Bornet
89f28a24d3 core[lint]: Fix typing in test_async_callbacks (#30788) 2025-04-11 07:26:38 -04:00
Sydney Runkle
8c6734325b partners[lint]: run pyupgrade to get code in line with 3.9 standards (#30781)
Using `pyupgrade` to get all `partners` code up to 3.9 standards
(mostly, fixing old `typing` imports).
2025-04-11 07:18:44 -04:00
Jacob Lee
e72f3c26a0 fix(ollama): Remove redundant message from response_metadata (#30778) 2025-04-10 23:12:57 -07:00
Jannik Maierhöfer
f3c3ec9aec docs: add langfuse integration to provider list (#30573)
This PR adds the Langfuse integration to the provider list.
2025-04-10 22:25:42 -04:00
Christophe Bornet
dc19d42d37 core: Specify code when ignoring type issue (ruff PGH003) (#30675)
See https://docs.astral.sh/ruff/rules/blanket-type-ignore/
2025-04-10 22:23:52 -04:00
Paul Czarkowski
68d16d8a07 Community: Add Managed Identity support for Azure AI Search (#30730)
Add Managed Identity support for Azure AI Search

---------

Signed-off-by: Paul Czarkowski <username.taken@gmail.com>
2025-04-10 22:22:58 -04:00
CtrlMj
5103594a2c replace the deprecated initialize_agent in playwright.ipynb with create_react_agent (#30734)
**Description:** Replaced the example with the deprecated
`intialize_agent` function with `create_react_agent` from
`langgraph.prebuild`

**Issue:** #29277 

**Dependencies:** N/A
**Twitter handle:** N/A
2025-04-10 22:12:12 -04:00
Eugene Yurtsev
e42b3d285a langchain: remove langchain-server script (#30755)
Has been replaced by langsmith a long long time ago
2025-04-10 22:11:42 -04:00
Pol de Font-Réaulx
48cf7c838d feat(community): add oauth2 support for Jira toolkit (#30684)
**Description:** add support for oauth2 in Jira tool by adding the
possibility to pass a dictionary with oauth parameters. I also adapted
the documentation to show this new behavior
2025-04-10 22:04:09 -04:00
Oleg Ovcharuk
b6fe7e8c10 docs: YDB Vector Store docs (#30636)
This PR adds docs about how to use YDB as a vector store

[YDB](https://ydb.tech/) is a versatile open-source distributed SQL
database. It supports [vector
search](https://ydb.tech/docs/en/yql/reference/udf/list/knn) which means
it can be used as a vector store with langchain.

YDB vectore store comes with
[langchain-ydb](https://pypi.org/project/langchain-ydb/) pypi package.

Co-authored-by: ccurme <chester.curme@gmail.com>
2025-04-10 21:33:56 -04:00
湛露先生
7a4ae6fbff community[patch]: simplify cache logic (#30760)
Thank you for contributing to LangChain!

- [ ] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core, etc. is
being modified. Use "docs: ..." for purely docs changes, "infra: ..."
for CI changes.
  - Example: "community: add foobar LLM"


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** a description of the change
    - **Issue:** the issue # it fixes, if applicable
    - **Dependencies:** any dependencies required for this change
- **Twitter handle:** if your PR gets announced, and you'd like a
mention, we'll gladly shout you out!


- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [ ] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.

Signed-off-by: zhanluxianshen <zhanluxianshen@163.com>
2025-04-10 19:20:57 -04:00
ccurme
8e053ac9d2 core[patch]: support customization of backoff parameters in with_retries (#30773)
Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
2025-04-10 19:18:36 -04:00
amohan
e981a9810d docs: update links in cloudflare docs (#30776)
Thanks for reviewing again. I was notified of some
[links](https://python.langchain.com/api_reference/cloudflare/) not
being correct in the default integrations so updating them in this PR.
2025-04-10 19:08:18 -04:00
William FH
70532a65f8 Async callback benchmark (#30777) 2025-04-10 15:47:19 -07:00
Sydney Runkle
c6172d167a Only run CodSpeed benchmarks with run-codspeed-benchmarks label (#30774) 2025-04-10 15:48:14 -04:00
amohan
f70df01e01 docs: Update ordering of cloudflare integration examples in providers page (#30768)
Updated the ordering of cloudflare integrations and updated import
examples. Follow up from
https://github.com/langchain-ai/langchain/pull/30749
2025-04-10 15:34:58 -04:00
Sydney Runkle
8f8fea2d7e [performance]: Use hard coded langchain-core version to avoid importlib import (#30744)
This PR aims to reduce import time of `langchain-core` tools by removing
the `importlib.metadata` import previously used in `__init__.py`. This
is the first in a sequence of PRs to reduce import time delays for
`langchain-core` features and structures 🚀.

Because we're now hard coding the version, we need to make sure
`version.py` and `pyproject.toml` stay in sync, so I've added a new CI
job that runs whenever either of those files are modified. [This
run](https://github.com/langchain-ai/langchain/actions/runs/14358012706/job/40251952044?pr=30744)
demonstrates the failure that occurs whenever the version gets out of
sync (thus blocking a PR).

Before, note the ~15% of time spent on the `importlib.metadata` /related
imports

<img width="1081" alt="Screenshot 2025-04-09 at 9 06 15 AM"
src="https://github.com/user-attachments/assets/59f405ec-ee8d-4473-89ff-45dea5befa31"
/>

After (note, lack of `importlib.metadata` time sink):

<img width="1245" alt="Screenshot 2025-04-09 at 9 01 23 AM"
src="https://github.com/user-attachments/assets/9c32e77c-27ce-485e-9b88-e365193ed58d"
/>
2025-04-10 14:15:02 -04:00
Sydney Runkle
cd6a83117c Adding more import time benchmarks for langchain-core (#30770)
Plus minor typo fix in `ChatPromptTemplate` case id.
2025-04-10 11:50:12 -04:00
Chamath K.B. Attanayaka
6c45c9efc3 docs: update clickhouse version in notebook example (#30754)
update clickhouse docker version tag in notebook example to avoid
compatibility issues with clickhouse-connect.
2025-04-10 09:51:54 -04:00
amohan
44b83460b2 docs: Add Cloudflare integrations (#30749)
Description:
This PR adds documentation for the langchain-cloudflare integration
package.

Issue:
N/A

Dependencies:
No new dependencies are required.

Tests and Docs:

Added an example notebook demonstrating the usage of the
langchain-cloudflare package, located in docs/docs/integrations.
Added a new package to libs/packages.yml.

Lint and Format:

Successfully ran make format and make lint.

---------

Co-authored-by: Collier King <collier@cloudflare.com>
Co-authored-by: Collier King <collierking99@gmail.com>
2025-04-10 09:27:23 -04:00
湛露先生
c87a270e5f cookbook: Fix docs typos. (#30763)
Thank you for contributing to LangChain!

- [ ] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core, etc. is
being modified. Use "docs: ..." for purely docs changes, "infra: ..."
for CI changes.
  - Example: "community: add foobar LLM"


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** a description of the change
    - **Issue:** the issue # it fixes, if applicable
    - **Dependencies:** any dependencies required for this change
- **Twitter handle:** if your PR gets announced, and you'd like a
mention, we'll gladly shout you out!


- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [ ] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.

Signed-off-by: zhanluxianshen <zhanluxianshen@163.com>
2025-04-10 09:13:24 -04:00
ccurme
63c16f5ca8 community: deprecate AzureCosmosDBNoSqlVectorSearch in favor of langchain-azure-ai implementation (#30756) 2025-04-09 21:04:16 +00:00
Christophe Bornet
4cc7bc6c93 core: Add ruff rules PLR (#30696)
Add ruff rules [PLR](https://docs.astral.sh/ruff/rules/#refactor-plr)
Except PLR09xxx and PLR2004.

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2025-04-09 15:15:38 -04:00
célina
68361f9c2d partners: (langchain-huggingface) Embeddings - Integrate Inference Providers and remove deprecated code (#30735)
Hi there, This is a complementary PR to #30733.
This PR introduces support for Hugging Face's serverless Inference
Providers (documentation
[here](https://huggingface.co/docs/inference-providers/index)), allowing
users to specify different providers

This PR also removes the usage of `InferenceClient.post()` method in
`HuggingFaceEndpointEmbeddings`, in favor of the task-specific
`feature_extraction` method. `InferenceClient.post()` is deprecated and
will be removed in `huggingface_hub` v0.31.0.

## Changes made

- bumped the minimum required version of the `huggingface_hub` package
to ensure compatibility with the latest API usage.
- added a provider field to `HuggingFaceEndpointEmbeddings`, enabling
users to select the inference provider.
- replaced the deprecated `InferenceClient.post()` call in
`HuggingFaceEndpointEmbeddings` with the task-specific
`feature_extraction` method for future-proofing, `post()` will be
removed in `huggingface-hub` v0.31.0.

 All changes are backward compatible.

---------

Co-authored-by: Lucain <lucainp@gmail.com>
Co-authored-by: ccurme <chester.curme@gmail.com>
2025-04-09 19:05:43 +00:00
Christophe Bornet
98f0016fc2 core: Add ruff rules ARG (#30732)
See https://docs.astral.sh/ruff/rules/#flake8-unused-arguments-arg
2025-04-09 14:39:36 -04:00
Sydney Runkle
66758599a9 [ci]: Quick codspeed.yml tweaks to enable comparisons with master (#30752)
* Only run codspeed logic when `libs/core` is changed (for now, we'll
want to add other benchmarks later
* Also run on `master` so that we can get a reference :)
2025-04-09 13:13:49 -04:00
theosaurus
d47d6ecbc3 dosc: Fix typo in get_separators_for_language method section (#30748)
Thank you for contributing to LangChain!

- [ ] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core, etc. is
being modified. Use "docs: ..." for purely docs changes, "infra: ..."
for CI changes.
  - Example: "community: add foobar LLM"


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** a description of the change
    - **Issue:** the issue # it fixes, if applicable
    - **Dependencies:** any dependencies required for this change
- **Twitter handle:** if your PR gets announced, and you'd like a
mention, we'll gladly shout you out!


- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [ ] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.
2025-04-09 13:03:01 -04:00
Sydney Runkle
78ec7d886d [performance]: Adding benchmarks for common langchain-core imports (#30747)
The first in a sequence of PRs focusing on improving performance in
core. We're starting with reducing import times for common structures,
hence the benchmarks here.

The benchmark looks a little bit complicated - we have to use a process
so that we don't suffer from Python's import caching system. I tried
doing manual modification of `sys.modules` between runs, but that's
pretty tricky / hacky to get right, hence the subprocess approach.

Motivated by extremely slow baseline for common imports (we're talking
2-5 seconds):

<img width="633" alt="Screenshot 2025-04-09 at 12 48 12 PM"
src="https://github.com/user-attachments/assets/994616fe-1798-404d-bcbe-48ad0eb8a9a0"
/>

Also added a `make benchmark` command to make local runs easy :).
Currently using walltimes so that we can track total time despite using
a manual proces.
2025-04-09 13:00:15 -04:00
German Molina
5fb261ce27 community: Google Vertex AI Search now returns the website title as part of the document metadata (#30688)
Google vertex ai search will now return the title of the found website
as part of the document metadata, if available.

Thank you for contributing to LangChain!

- **Description**: Vertex AI Search can be used to index websites and
then develop chatbots that use these websites to answer questions. At
present, the document metadata includes an `id` and `source` (which is
the URL). While the URL is enough to create a link, the ID is not
descriptive enough to show users. Therefore, I propose we return `title`
as well, when available (e.g., it will not be available in `.txt`
documents found during the website indexing).
- **Issue**: No bug in particular, but it would be better if this was
here.
- **Dependencies**: None
- I do not use twitter.

Format, Lint and Test seem to be all good.
2025-04-09 08:54:06 -04:00
theosaurus
636d831d27 docs: Fix typo in 'Query re-writing" section (#30736)
Thank you for contributing to LangChain!

- [ ] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core, etc. is
being modified. Use "docs: ..." for purely docs changes, "infra: ..."
for CI changes.
  - Example: "community: add foobar LLM"


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** a description of the change
    - **Issue:** the issue # it fixes, if applicable
    - **Dependencies:** any dependencies required for this change
- **Twitter handle:** if your PR gets announced, and you'd like a
mention, we'll gladly shout you out!


- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [ ] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.
2025-04-09 08:50:14 -04:00
giulia_p_lib
deec538335 docs: fix small typo in map_rerank_docs_chain.ipynb (#30738)
- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** fixed a minor typo in map_rerank_docs_chain.ipynb
2025-04-09 08:49:37 -04:00
Akshay Dongare
164e606cae docs: fix import path and update LiteLLM integration docs (#30685)
- [x] **PR title**: "docs: Update import path and LiteLLM integration
docs"
- Update the old import path for `ChatLiteLLM` to reflect the new export
from
[`__init__.py`](https://github.com/Akshay-Dongare/langchain-litellm/blob/main/langchain_litellm/__init__.py)
in
[`langchain-litellm`](https://github.com/Akshay-Dongare/langchain-litellm)
package

- [x] **PR message**:
    - **Description:** 
    - 🔗 **Follow-up to**: PR #30637
    - 🔧 **Fixes**: #30368
- 💬 **Based on this comment from** @ccurme:
https://github.com/langchain-ai/langchain/pull/30637#discussion_r2029084320
 


- [x] **About me**
🔗 LinkedIn:
[akshay-dongare](https://www.linkedin.com/in/akshay-dongare/)
2025-04-08 13:04:17 -04:00
Ikko Eltociear Ashimine
5686fed40b docs: update yellowbrick.ipynb (#30729)
retreival -> retrieval
2025-04-08 11:56:35 -04:00
Sydney Runkle
4556b81b1d Clean up numpy dependencies and speed up 3.13 CI with numpy>=2.1.0 (#30714)
Generally, this PR is CI performance focused + aims to clean up some
dependencies at the same time.

1. Unpins upper bounds for `numpy` in all `pyproject.toml` files where
`numpy` is specified
2. Requires `numpy >= 2.1.0` for Python 3.13 and `numpy > v1.26.0` for
Python 3.12, plus a `numpy` min version bump for `chroma`
3. Speeds up CI by minutes - linting on Python 3.13, installing `numpy <
2.1.0` was taking [~3
minutes](https://github.com/langchain-ai/langchain/actions/runs/14316342925/job/40123305868?pr=30713),
now the entire env setup takes a few seconds
4. Deleted the `numpy` test dependency from partners where that was not
used, specifically `huggingface`, `voyageai`, `xai`, and `nomic`.

It's a bit unfortunate that `langchain-community` depends on `numpy`, we
might want to try to fix that in the future...

Closes https://github.com/langchain-ai/langchain/issues/26026
Fixes https://github.com/langchain-ai/langchain/issues/30555
2025-04-08 09:45:07 -04:00
ccurme
163730aef4 docs: update SQL QA prompt (#30728)
Resolves https://github.com/langchain-ai/langchain/issues/30724

The [prompt in
langchain-hub](https://smith.langchain.com/hub/langchain-ai/sql-query-system-prompt)
used in this guide was composed of just a system message, but the guide
did not add a human message to it. This was incompatible with some
providers (and is generally not a typical usage pattern).

The prompt in prompt hub has been updated to split the question into a
separate HumanMessage. Here we update the guide to reflect this.
2025-04-08 09:42:49 -04:00
湛露先生
9cbe91896e Fix deepseek release tag, as it is update name. (#30717)
Thank you for contributing to LangChain!

- [ ] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core, etc. is
being modified. Use "docs: ..." for purely docs changes, "infra: ..."
for CI changes.
  - Example: "community: add foobar LLM"


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** a description of the change
    - **Issue:** the issue # it fixes, if applicable
    - **Dependencies:** any dependencies required for this change
- **Twitter handle:** if your PR gets announced, and you'd like a
mention, we'll gladly shout you out!


- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [ ] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.

Signed-off-by: zhanluxianshen <zhanluxianshen@163.com>
2025-04-08 08:43:16 -04:00
Nithish Raghunandanan
893942651b docs: Update couchbase vector store docs (#30710)
-  **Update LangChain-Couchbase documentation**
- Rename `CouchbaseVectorStore` in favor of `CouchbaseSearchVectorStore`

- [x] **Lint and test**
2025-04-07 18:45:14 -04:00
Eugene Yurtsev
3ce0587199 ci: remove unused debug action (#30713)
Removing an unused action
2025-04-07 22:32:37 +00:00
ccurme
a2bec5f2e5 ollama: release 0.3.1 (#30716) 2025-04-07 20:31:25 +00:00
ccurme
e3f15f0a47 ollama[patch]: add model_name to response metadata (#30706)
Fixes [this standard
test](https://python.langchain.com/api_reference/standard_tests/integration_tests/langchain_tests.integration_tests.chat_models.ChatModelIntegrationTests.html#langchain_tests.integration_tests.chat_models.ChatModelIntegrationTests.test_usage_metadata).
2025-04-07 16:27:58 -04:00
ccurme
e106e9602f groq[patch]: add retries to integration tests (#30707)
Tool-calling tests started intermittently failing with
> groq.APIError: Failed to call a function. Please adjust your prompt.
See 'failed_generation' for more details.
2025-04-07 12:45:53 -04:00
aaronlaitner
4f9f97bd12 docs: replaced initialize_agent with create_react_agent in dalle_image_generator.ipynb (#30697)
## Description:

Replaced deprecated 'initialize_agent' with 'create_react_agent' in
dalle_image_generator.ipynb
## Issue:
#29277

## Dependencies:
None

## Twitter handle:
@Thatopman

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-07 13:33:52 +00:00
Mohammad Mohtashim
e935da0b12 ChatTongyi reasoning_content fix (#30694)
- **Description:** Small fix for `reasoning_content` key
- **Issue:** #30689
2025-04-07 09:27:33 -04:00
Tin Lai
4d03ba4686 langchain_qdrant: fix showing the missing sparse vector name (#30701)
**Description:** The error message was supposed to display the missing
vector name, but instead, it includes only the existing collection
configs.

This simple PR just includes the correct variable name, so that the user
knows the requested vector does not exist in the collection.

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.

Signed-off-by: Tin Lai <tin@tinyiu.com>
2025-04-07 09:19:08 -04:00
Eugene Yurtsev
30af9b8166 Update bug-report.yml (#30680)
Update bug report template!
2025-04-04 16:47:50 -04:00
jessicaou
2712ecffeb Update Contributor Docs (#30679)
Deletes statement on integration marketing
2025-04-04 16:35:11 -04:00
Ninad Sinha
a3671ceb71 docs: Add tools for hyperbrowser (#30606)
Description

This PR updates the docs for the
[langchain-hyperbrowser](https://pypi.org/project/langchain-hyperbrowser/)
package. It adds a few tools
 - Scrape Tool
 - Crawl Tool
 - Extract Tool
 - Browser Agents
   - Claude Computer Use
   - OpenAI CUA
   - Browser Use 

[Hyperbrowser](https://hyperbrowser.ai/) is a platform for running and
scaling headless browsers. It lets you launch and manage browser
sessions at scale and provides easy to use solutions for any webscraping
needs, such as scraping a single page or crawling an entire site.

Issue
None

Dependencies
None

Twitter Handle
`@hyperbrowser`
2025-04-04 16:02:47 -04:00
Christophe Bornet
6650b94627 core: Add ruff rules PYI (#29335)
See https://docs.astral.sh/ruff/rules/#flake8-pyi-pyi

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2025-04-04 19:59:44 +00:00
Philippe PRADOS
d8e3b7667f community[patch]: Fix empty producer in PDF Parsers (#30620)
Fix an issue where if a pdf file doesn't have a “producer” in metadata, it generates an exception.
2025-04-04 15:53:49 -04:00
Christophe Bornet
f0159c7125 core: Add ruff rules PGH (except PGH003) (#30656)
Add ruff rules PGH: https://docs.astral.sh/ruff/rules/#pygrep-hooks-pgh
Except PGH003 which will be dealt in a dedicated PR.

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Eugene Yurtsev <eugene@langchain.dev>
2025-04-04 19:53:27 +00:00
Jorge Ángel Juárez Vázquez
2491237473 docs: Add Google Calendar documentation (#30633)
## Docs: Add Google Calendar Toolkit Documentation

### Description:
This PR adds documentation for the Google Calendar Toolkit as part of
the `langchain-google` repository. Refer to the related PR: [community:
Add Google Calendar
Toolkit](https://github.com/langchain-ai/langchain-google/pull/688).

### Issue:
N/A 

### Twitter handle:
@jorgejrzz
2025-04-04 15:53:03 -04:00
Armaanjeet Singh Sandhu
7c2468f36b core: Fix handler removal in BaseCallbackManager (Fixes #30640) (#30659)
**Description:**  
Fixed a bug in `BaseCallbackManager.remove_handler()` that caused a
`ValueError` when removing a handler added via the constructor's
`handlers` parameter. The issue occurred because handlers passed to the
constructor were added only to the `handlers` list and not automatically
to `inheritable_handlers` unless explicitly specified. However,
`remove_handler()` attempted to remove the handler from both lists
unconditionally, triggering a `ValueError` when it wasn't in
`inheritable_handlers`.

The fix ensures the method checks for the handler’s presence in each
list before attempting removal, making it more robust while preserving
its original behavior.

**Issue:** Fixes #30640

**Dependencies:** None

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2025-04-04 15:45:15 -04:00
Mohammad Mohtashim
bff56c5fa6 community[patch]: Redundant Parser checker for Webbaseloader (#30632)
- **Description:** We do not need to set parser in `scrape` since it is
already been done in `_scrape`
- **Issue:** #30629, not directly related but makes sure xml parser is
used
2025-04-04 14:11:26 -04:00
Christophe Bornet
150ac0cb79 core: Add ruff rules DTZ (#30657)
Add ruff rules DTZ:
https://docs.astral.sh/ruff/rules/#flake8-datetimez-dtz
2025-04-04 13:43:47 -04:00
Christophe Bornet
5e418c2666 core: Rework pydantic version checks (#30653)
This pull request includes various changes to the `langchain_core`
library, focusing on improving compatibility with different versions of
Pydantic. The primary change involves replacing checks for Pydantic
major versions with boolean flags, which simplifies the code and
improves readability.
This also solves ruff rule checks for
[RUF048](https://docs.astral.sh/ruff/rules/map-int-version-parsing/) and
[PLR2004](https://docs.astral.sh/ruff/rules/magic-value-comparison/).

Key changes include:

### Compatibility Improvements:
*
[`libs/core/langchain_core/output_parsers/json.py`](diffhunk://#diff-5add0cf7134636ae4198a1e0df49ee332ae0c9123c3a2395101e02687c717646L22-R24):
Replaced `PYDANTIC_MAJOR_VERSION` with `IS_PYDANTIC_V1` to check for
Pydantic version 1.
*
[`libs/core/langchain_core/output_parsers/pydantic.py`](diffhunk://#diff-2364b5b4aee01c462aa5dbda5dc3a877dcd20f29df173ad540dc8adf8b192361L14-R14):
Updated version checks from `PYDANTIC_MAJOR_VERSION` to `IS_PYDANTIC_V2`
in the `PydanticOutputParser` class.
[[1]](diffhunk://#diff-2364b5b4aee01c462aa5dbda5dc3a877dcd20f29df173ad540dc8adf8b192361L14-R14)
[[2]](diffhunk://#diff-2364b5b4aee01c462aa5dbda5dc3a877dcd20f29df173ad540dc8adf8b192361L27-R27)

### Utility Enhancements:
*
[`libs/core/langchain_core/utils/pydantic.py`](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896R23):
Introduced `IS_PYDANTIC_V1` and `IS_PYDANTIC_V2` flags and deprecated
the `get_pydantic_major_version` function. Updated various functions to
use these flags instead of version numbers.
[[1]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896R23)
[[2]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896R42-R78)
[[3]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L90-R89)
[[4]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L104-R101)
[[5]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L120-R122)
[[6]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L135-R132)
[[7]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L149-R151)
[[8]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L164-R161)
[[9]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L248-R250)
[[10]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L330-R335)
[[11]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L356-R357)
[[12]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L393-R390)
[[13]](diffhunk://#diff-ff28020c5f1073a8b63bcd9d8b756a187fd682cb81935295120c63b207071896L403-R400)

### Test Updates:
*
[`libs/core/tests/unit_tests/output_parsers/test_openai_tools.py`](diffhunk://#diff-694cc0318edbd6bbca34f53304934062ad59ba9f5a788252ce6c5f5452489d67L19-R22):
Updated tests to use `IS_PYDANTIC_V1` and `IS_PYDANTIC_V2` for version
checks.
[[1]](diffhunk://#diff-694cc0318edbd6bbca34f53304934062ad59ba9f5a788252ce6c5f5452489d67L19-R22)
[[2]](diffhunk://#diff-694cc0318edbd6bbca34f53304934062ad59ba9f5a788252ce6c5f5452489d67L532-R535)
[[3]](diffhunk://#diff-694cc0318edbd6bbca34f53304934062ad59ba9f5a788252ce6c5f5452489d67L567-R570)
[[4]](diffhunk://#diff-694cc0318edbd6bbca34f53304934062ad59ba9f5a788252ce6c5f5452489d67L602-R605)
*
[`libs/core/tests/unit_tests/prompts/test_chat.py`](diffhunk://#diff-3e60e744842086a4f3c4b21bc83e819c3435720eab210078e77e2430fb8c7e84R7):
Replaced version tuple checks with `PYDANTIC_VERSION` comparisons.
[[1]](diffhunk://#diff-3e60e744842086a4f3c4b21bc83e819c3435720eab210078e77e2430fb8c7e84R7)
[[2]](diffhunk://#diff-3e60e744842086a4f3c4b21bc83e819c3435720eab210078e77e2430fb8c7e84L35-R38)
[[3]](diffhunk://#diff-3e60e744842086a4f3c4b21bc83e819c3435720eab210078e77e2430fb8c7e84L924-R927)
[[4]](diffhunk://#diff-3e60e744842086a4f3c4b21bc83e819c3435720eab210078e77e2430fb8c7e84L935-R938)
*
[`libs/core/tests/unit_tests/runnables/test_graph.py`](diffhunk://#diff-99a290330ef40103d0ce02e52e21310d6fadea142bfdea13c94d23fc81c0bb5dR3):
Simplified version checks using `PYDANTIC_VERSION`.
[[1]](diffhunk://#diff-99a290330ef40103d0ce02e52e21310d6fadea142bfdea13c94d23fc81c0bb5dR3)
[[2]](diffhunk://#diff-99a290330ef40103d0ce02e52e21310d6fadea142bfdea13c94d23fc81c0bb5dL15-R18)
[[3]](diffhunk://#diff-99a290330ef40103d0ce02e52e21310d6fadea142bfdea13c94d23fc81c0bb5dL234-L239)
*
[`libs/core/tests/unit_tests/runnables/test_runnable.py`](diffhunk://#diff-06bed920c0dad0cfd41d57a8d9e47a7b56832409649c10151061a791860d5bb5L18-R20):
Introduced `PYDANTIC_VERSION_AT_LEAST_29` and
`PYDANTIC_VERSION_AT_LEAST_210` for more readable version checks.
[[1]](diffhunk://#diff-06bed920c0dad0cfd41d57a8d9e47a7b56832409649c10151061a791860d5bb5L18-R20)
[[2]](diffhunk://#diff-06bed920c0dad0cfd41d57a8d9e47a7b56832409649c10151061a791860d5bb5L92-R99)
[[3]](diffhunk://#diff-06bed920c0dad0cfd41d57a8d9e47a7b56832409649c10151061a791860d5bb5L230-R233)
[[4]](diffhunk://#diff-06bed920c0dad0cfd41d57a8d9e47a7b56832409649c10151061a791860d5bb5L652-R655)
2025-04-04 13:42:30 -04:00
Christophe Bornet
43b5dc7191 core: Add ruff rules TD and FIX (#30654)
Add ruff rules:
* FIX: https://docs.astral.sh/ruff/rules/#flake8-fixme-fix
* TD: https://docs.astral.sh/ruff/rules/#flake8-todos-td

Code cleanup:

*
[`libs/core/langchain_core/outputs/chat_generation.py`](diffhunk://#diff-a1017ee46f58fa4005b110ffd4f8e1fb08f6a2a11d6ca4c78ff8be641cbb89e5L56-R56):
Removed the "HACK" prefix from a comment in the `set_text` method.

Configuration adjustments:

*
[`libs/core/pyproject.toml`](diffhunk://#diff-06baaee12b22a370fef9f170c9ed13e2727e377d3b32f5018430f4f0a39d3537R85-R93):
Added new rules `FIX002`, `TD002`, and `TD003` to the ignore list.
*
[`libs/core/pyproject.toml`](diffhunk://#diff-06baaee12b22a370fef9f170c9ed13e2727e377d3b32f5018430f4f0a39d3537L102-L108):
Removed the `FIX` and `TD` rules from the ignore list.

Test refinement:

*
[`libs/core/tests/unit_tests/runnables/test_runnable.py`](diffhunk://#diff-06bed920c0dad0cfd41d57a8d9e47a7b56832409649c10151061a791860d5bb5L3231-R3232):
Updated a TODO comment to improve clarity in the `test_map_stream`
function.
2025-04-04 13:40:42 -04:00
ccurme
a007c57285 docs: update package registry sort order (#30677) 2025-04-04 13:12:39 -04:00
Sydney Runkle
33ed7c31da docs: fix perplexity install instructions in ChatPerplexity docstring (#30676)
* `openai` install no longer needs to be done manually
2025-04-04 12:58:18 -04:00
Dhruvajyoti Sarma
f9bb5ec5d0 feature: removed pandas dataframe dependency for similary_search when using DuckDB as vector store (#30445)
- [ ] **PR title**: "community: Removes pandas dependency for using
DuckDB for similarity search"


- [ ] **PR message**: 
- **Description:** Removes pandas dependency for using DuckDB for
similarity search. The old function still exists as
`similarity_search_pd`, while the new one is at `similarity_search` and
requires no code changes. Return format remains the same.
    - **Issue:** Issue #29933 and update on PR #30435 
    - **Dependencies:** No dependencies
2025-04-04 12:19:18 -04:00
Akshay Dongare
f79473b752 Solved issue Implement langchain-litellm #30368 (#30637)
**PR title**: 
- [x] 1. docs: docs/docs/integrations/providers/LiteLLM.md
- [x] 2. docs: docs/docs/integrations/chat/litellm.ipynb
- [x] 3. libs: libs/packages.yml

- [x] **PR message**:
    - **Description:** Implement langchain-litellm
    - **Issue:** the issue #30368 
    - **Twitter handle:** akshay_d02
    - **LinkedIn Handle** https://linkedin.com/in/akshay-dongare 

- [x] **Add tests and docs**: Done

- [x] **Lint and test**: Done

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-04 16:12:10 +00:00
Yiğit Bekir Kaya, PhD
87e82fe1e8 Added langchain-qwq package documentation (Alibaba Cloud) (#30628)
LangChain QwQ allows non-Tongyi users to access thinking models with
extra capabilities which serve as an extension to Alibaba Cloud.

Hi @ccurme I'm back with the updated PR this time with documentation and
a finished package.

- [x] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core, etc. is
being modified. Use "docs: ..." for purely docs changes, "infra: ..."
for CI changes.
  - Example: "community: add foobar LLM"



- **Description:** adds documentation of `langchain-qwq` integration
package. Also adds it to Alibaba Cloud provider
- **Issue:** #30580 #30317 #30579
- **Dependencies:** openai, json-repair
- **Twitter handle:** YigitBekir


- [x] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [x] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.
2025-04-04 11:47:14 -04:00
Andrew Benton
4e7a9a7014 community: Add support for custom runtimes to Riza tools (#30664)
**Description:**
Adds support for Riza custom runtimes to the two Riza code interpreter
tools, allowing users to run LLM-generated code that depends on
libraries outside stdlib.
**Issue:** N/A
**Dependencies:** None
**Twitter handle:** @rizaio
2025-04-04 11:03:14 -04:00
diego dupin
aa37893c00 MariaDB vector store documentation addition (#30229)
### New Feature

Since version 11.7.1, MariaDB support vector. This is a super fast
implementation (see [some perf
blog](https://smalldatum.blogspot.com/2025/01/evaluating-vector-indexes-in-mariadb.html)
The goal is to support MariaDB with langchain

Implementation is done in
https://github.com/mariadb-corporation/langchain-mariadb, published in
https://pypi.org/project/langchain-mariadb/

This concerns the doc addition
 

(initial PR https://github.com/langchain-ai/langchain/pull/29989)

---------

Co-authored-by: ccurme <chester.curme@gmail.com>
Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
2025-04-04 14:56:25 +00:00
Sydney Runkle
1cdea6ab07 langchain-community: release 0.3.21 (#30673) 2025-04-04 14:14:50 +00:00
Sydney Runkle
901dffe06b langchain: release 0.3.23 (#30670)
* Bump `text-splitters` min version
* Bump `langchain-core` min version
* Bump `langchain` version 🚀
2025-04-04 10:06:29 -04:00
ccurme
0c2c8c36c1 text-splitters: release 0.3.8 (#30671) 2025-04-04 09:58:45 -04:00
ccurme
59d508a2ee openai[patch]: make computer test more reliable (#30672) 2025-04-04 13:53:59 +00:00
Sydney Runkle
c235328b39 Revert "update langchain version and bump min core v"
This reverts commit d0f154dbaa.
2025-04-04 09:31:51 -04:00
Sydney Runkle
d0f154dbaa update langchain version and bump min core v 2025-04-04 09:27:49 -04:00
Sydney Runkle
32cd70d7d2 release: bump core to v0.3.51 (#30668) 2025-04-04 13:23:09 +00:00
Max Forsey
18cf457eec langchain-runpod integration (#30648)
## Description:

This PR adds the necessary documentation for the `langchain-runpod`
partner package integration. It includes:

* A provider page (`docs/docs/integrations/providers/runpod.ipynb`)
explaining the overall setup.
* An LLM component page (`docs/docs/integrations/llms/runpod.ipynb`)
detailing the `RunPod` class usage.
* A Chat Model component page
(`docs/docs/integrations/chat/runpod.ipynb`) detailing the `ChatRunPod`
class usage, including a feature support table.

These documentation files reflect the latest features of the
`langchain-runpod` package (v0.2.0+) such as async support and API
polling logic.

This work also addresses the review feedback provided on the previous
attempt in PR #30246 by:
*   Removing all TODOs from documentation.
*   Adding the required links between provider and component pages.
*   Completing the feature support table in the chat documentation.
*   Linking to the source code on GitHub for API reference.

Finally, it registers the `langchain-runpod` package in
`libs/packages.yml`.

## Dependencies:

None added to the core LangChain repository by these documentation
changes. The required dependency (`langchain-runpod`) is managed as a
separate package.

## Twitter handle:

@runpod_io

---------

Co-authored-by: Max Forsey <maxpod@maxpod.local>
Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-03 23:57:06 +00:00
NikeHop
9c03cd5775 Fix tool description in serpapi.ipynb (#30660)
Thank you for contributing to LangChain!

- [x] Fix Tool description of SerpAPI tool: "docs: Fix SerpAPI tool
description"



- [ ] Fix SerpAPI tool description: 
- Tool description + name in example initialization of the SerpAPI tool
was still that of the python repl tool.
    - @RLHoeppi

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-03 23:36:29 +00:00
Sydney Runkle
af66ab098e Adding Perplexity extra and deprecating the community version of ChatPerplexity (#30649)
Plus, some accompanying docs updates

Some compelling usage:

```py
from langchain_perplexity import ChatPerplexity


chat = ChatPerplexity(model="llama-3.1-sonar-small-128k-online")
response = chat.invoke(
    "What were the most significant newsworthy events that occurred in the US recently?",
    extra_body={"search_recency_filter": "week"},
)
print(response.content)
# > Here are the top significant newsworthy events in the US recently: ...
```

Also, some confirmation of structured outputs:

```py
from langchain_perplexity import ChatPerplexity
from pydantic import BaseModel


class AnswerFormat(BaseModel):
    first_name: str
    last_name: str
    year_of_birth: int
    num_seasons_in_nba: int


messages = [
    {"role": "system", "content": "Be precise and concise."},
    {
        "role": "user",
        "content": (
            "Tell me about Michael Jordan. "
            "Please output a JSON object containing the following fields: "
            "first_name, last_name, year_of_birth, num_seasons_in_nba. "
        ),
    },
]

llm = ChatPerplexity(model="llama-3.1-sonar-small-128k-online")
structured_llm = llm.with_structured_output(AnswerFormat)
response = structured_llm.invoke(messages)
print(repr(response))
#> AnswerFormat(first_name='Michael', last_name='Jordan', year_of_birth=1963, num_seasons_in_nba=15)
```
2025-04-03 14:29:17 -04:00
ccurme
b8929e3d5f docs: add image generation example to Google genai docs (#30650) 2025-04-03 14:21:54 -04:00
ccurme
374769e8fe core[patch]: log information from certain errors (#30626)
Some exceptions raised by SDKs include information in httpx responses
(see for example
[OpenAI](https://github.com/openai/openai-python/blob/main/src/openai/_exceptions.py)).
Here we trace information from those exceptions.

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
2025-04-03 16:45:19 +00:00
Sydney Runkle
17a9cd61e9 Bump langchain-core version in perplexity's pyproject.toml (#30647)
Blocking v0.1.0 release of `langchain-perplexity`
2025-04-03 16:19:10 +00:00
Sydney Runkle
3814bd1ea7 partners: Add Perplexity Chat Integration (#30618)
Perplexity's importance in the space has been growing, so we think it's
time to add an official integration!

Note: following the release of `langchain-perplexity` to `pypi`, we
should be able to add `perplexity` as an extra in
`libs/langchain/pyproject.toml`, but we're blocked by a circular import
for now.

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Chester Curme <chester.curme@gmail.com>
2025-04-03 16:09:14 +00:00
vgrfl
87c02a1aff docs: Fixed a typo in 'Google AI vs Google Cloud Vertex AI' section (#30642)
**Description:** Corrected 'encription' spelling to 'encryption'
2025-04-03 09:04:29 -04:00
Alejandro Rodríguez
884125e129 community: support usage_metadata for litellm (#30625)
Support "usage_metadata" for LiteLLM. 

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.
2025-04-02 19:45:15 -04:00
Jacob Lee
01d0cfe450 docs: Remove TODO from Ollama docs page (#30627) 2025-04-02 22:59:15 +00:00
Christophe Bornet
f241fd5c11 core: Add ruff rules RET (#29384)
See https://docs.astral.sh/ruff/rules/#flake8-return-ret
All auto-fixes
2025-04-02 16:59:56 -04:00
Eugene Yurtsev
9ae792f56c core: 0.3.50 release (#30623)
0.3.50 release
2025-04-02 14:46:23 -04:00
Christophe Bornet
ccc3d32ec8 core: Add ruff rules for Pylint PLC (Convention) and PLE (Errors) (#29286)
See https://docs.astral.sh/ruff/rules/#pylint-pl
2025-04-02 10:58:03 -04:00
ccurme
fe0fd9dd70 openai[patch]: upgrade tiktoken and fix test (#30621)
Related to https://github.com/langchain-ai/langchain/issues/30344

https://github.com/langchain-ai/langchain/pull/30542 introduced an
erroneous test for token counts for o-series models. tiktoken==0.8 does
not support o-series models in
`tiktoken.encoding_for_model(model_name)`, and this is the version of
tiktoken we had in the lock file. So we would default to `cl100k_base`
for o-series, which is the wrong encoding model. The test tested against
this wrong encoding (so it passed with tiktoken 0.8).

Here we update tiktoken to 0.9 in the lock file, and fix the expected
counts in the test. Verified that we are pulling
[o200k_base](https://github.com/openai/tiktoken/blob/main/tiktoken/model.py#L8),
as expected.
2025-04-02 10:44:48 -04:00
oxy-tg
38807871ec docs: Add Oxylabs integration (#30591)
Description:
This PR adds documentation for the langchain-oxylabs integration
package.

The documentation includes instructions for configuring Oxylabs
credentials and provides example code demonstrating how to use the
package.

Issue:
N/A

Dependencies:
No new dependencies are required.

Tests and Docs:

Added an example notebook demonstrating the usage of the
Langchain-Oxylabs package, located in docs/docs/integrations.
Added a provider page in docs/docs/providers.
Added a new package to libs/packages.yml.

Lint and Test:

Successfully ran make format, make lint, and make test.
2025-04-02 14:40:32 +00:00
ccurme
816492e1d3 openai: release 0.3.12 (#30616) 2025-04-02 13:20:15 +00:00
Bagatur
111dd90a46 openai[patch]: support structured output and tools (#30581)
Co-authored-by: ccurme <chester.curme@gmail.com>
2025-04-02 09:14:02 -04:00
Karol Zmorski
32f7695809 docs: Little update in sample notebook with WatsonxToolkit (#30614)
**Description:**

- Updated sample notebook with valid tools.
2025-04-02 09:08:29 -04:00
Mahir Shah
9d3262c7aa core: Propagate config_factories in RunnableBinding (#30603)
- **Description:** Propagates config_factories when calling decoration
methods for RunnableBinding--e.g. bind, with_config, with_types,
with_retry, and with_listeners. This ensures that configs attached to
the original RunnableBinding are kept when creating the new
RunnableBinding and the configs are merged during invocation. Picks up
where #30551 left off.
  - **Issue:** #30531

Co-authored-by: ccurme <chester.curme@gmail.com>
2025-04-01 18:03:58 -04:00
ccurme
8a69de5c24 openai[patch]: ignore file blocks when counting tokens (#30601)
OpenAI does not appear to document how it transforms PDF pages to
images, which determines how tokens are counted:
https://platform.openai.com/docs/guides/pdf-files?api-mode=chat#usage-considerations

Currently these block types raise ValueError inside
`get_num_tokens_from_messages`. Here we update to generate a warning and
continue.
2025-04-01 15:29:33 -04:00
Christophe Bornet
558191198f core: Add ruff rule FBT003 (boolean-trap) (#29424)
See
https://docs.astral.sh/ruff/rules/boolean-positional-value-in-call/#boolean-positional-value-in-call-fbt003
This PR also fixes some FBT001/002 in private methods but does not
enforce these rules globally atm.

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2025-04-01 17:40:12 +00:00
Christophe Bornet
4f8ea13cea core: Add ruff rules PERF (#29375)
See https://docs.astral.sh/ruff/rules/#perflint-perf
2025-04-01 13:34:56 -04:00
Christophe Bornet
8a33402016 core: Add ruff rules PT (pytest) (#29381)
See https://docs.astral.sh/ruff/rules/#flake8-pytest-style-pt
2025-04-01 13:31:07 -04:00
Ben Faircloth
6896c863e8 docs: add seekrflow chat model integration docs (#30596)
### **PR title**  
`docs: add SeekrFlow integration notebook`

---

### 💬 **PR message**  
- **Description:**  
This PR adds an integration notebook for
[`[ChatSeekrFlow](https://pypi.org/project/langchain-seekrflow/)`](https://pypi.org/project/langchain-seekrflow/)
under `docs/docs/integrations/chat/`. Per LangChain’s guidance,
SeekrFlow has been published as a standalone OSS package
(`langchain-seekrflow`) rather than as a direct community integration.
This notebook ensures discoverability, demonstration, and testability of
the integration within LangChain’s documentation structure.

- **Issue:**  
N/A – this is a new integration contribution aligned with LangChain’s
external package policy.

- **Dependencies:**  
-
[`[langchain-seekrflow](https://pypi.org/project/langchain-seekrflow)`](https://pypi.org/project/langchain-seekrflow)
(published to PyPI)
-
[`[seekrai](https://pypi.org/project/seekrai/)`](https://pypi.org/project/seekrai/)
(SeekrFlow client SDK)

- **Twitter handle (optional):**  
  @seekrtechnology

---------

Co-authored-by: Ben Faircloth <bfaircloth@seekr.com>
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2025-04-01 13:18:01 -04:00
Christophe Bornet
768e4f695a core: Add ruff rules S110 and S112 (#30599) 2025-04-01 13:17:22 -04:00
Christophe Bornet
88b4233fa1 core: Add ruff rules D (docstring) (#29406)
This ensures that the code is properly documented:
https://docs.astral.sh/ruff/rules/#pydocstyle-d

Related to #21983
2025-04-01 13:15:45 -04:00
Andras L Ferenczi
64df60e690 community[minor]: Add custom sitemap URL parameter to GitbookLoader (#30549)
## Description
This PR adds a new `sitemap_url` parameter to the `GitbookLoader` class
that allows users to specify a custom sitemap URL when loading content
from a GitBook site. This is particularly useful for GitBook sites that
use non-standard sitemap file names like `sitemap-pages.xml` instead of
the default `sitemap.xml`.
The standard `GitbookLoader` assumes that the sitemap is located at
`/sitemap.xml`, but some GitBook instances (including GitBook's own
documentation) use different paths for their sitemaps. This parameter
makes the loader more flexible and helps users extract content from a
wider range of GitBook sites.
## Issue
Fixes bug
[30473](https://github.com/langchain-ai/langchain/issues/30473) where
the `GitbookLoader` would fail to find pages on GitBook sites that use
custom sitemap URLs.
## Dependencies
No new dependencies required.
*I've added*:
* Unit tests to verify the parameter works correctly
* Integration tests to confirm the parameter is properly used with real
GitBook sites
* Updated docstrings with parameter documentation
The changes are fully backward compatible, as the parameter is optional
with a sensible default.

---------

Co-authored-by: andrasfe <andrasf94@gmail.com>
Co-authored-by: Eugene Yurtsev <eugene@langchain.dev>
2025-04-01 16:17:21 +00:00
Christophe Bornet
fdda1aaea1 core: Accept ALL ruff rules with exclusions (#30595)
This pull request updates the `pyproject.toml` configuration file to
modify the linting rules and ignored warnings for the project. The most
important changes include switching to a more comprehensive selection of
linting rules and updating the list of ignored rules to better align
with the project's requirements.

Linting rules update:

* Changed the `select` option to include all available linting rules by
setting it to `["ALL"]`.

Ignored rules update:

* Updated the `ignore` option to include specific rules that interfere
with the formatter, are incompatible with Pydantic, or are temporarily
excluded due to project constraints.
2025-04-01 11:17:51 -04:00
Kacper Włodarczyk
26a3256fc6 community[major]: DynamoDBChatMessageHistory bulk add messages, raise errors (#30572)
This PR addresses two key issues:

- **Prevent history errors from failing silently**: Previously, errors
in message history were only logged and not raised, which can lead to
inconsistent state and downstream failures (e.g., ValidationError from
Bedrock due to malformed message history). This change ensures that such
errors are raised explicitly, making them easier to detect and debug.
(Side note: I’m using AWS Lambda Powertools Logger but hadn’t configured
it properly with the standard Python logger—my bad. If the error had
been raised, I would’ve seen it in the logs 😄) This is a **BREAKING
CHANGE**

- **Add messages in bulk instead of iteratively**: This introduces a
custom add_messages method to add all messages at once. The previous
approach failed silently when individual messages were too large,
resulting in partial history updates and inconsistent state. With this
change, either all messages are added successfully, or none are—helping
avoid obscure history-related errors from Bedrock.

---------

Co-authored-by: Kacper Wlodarczyk <kacper.wlodarczyk@chaosgears.com>
2025-04-01 11:13:32 -04:00
Olexandr88
8c8bca68b2 docs: edited the badge to an acceptable size (#30586)
Thank you for contributing to LangChain!

- [ ] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core, etc. is
being modified. Use "docs: ..." for purely docs changes, "infra: ..."
for CI changes.
  - Example: "community: add foobar LLM"


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
    - **Description:** a description of the change
    - **Issue:** the issue # it fixes, if applicable
    - **Dependencies:** any dependencies required for this change
- **Twitter handle:** if your PR gets announced, and you'd like a
mention, we'll gladly shout you out!


- [ ] **Add tests and docs**: If you're adding a new integration, please
include
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in
`docs/docs/integrations` directory.


- [ ] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, eyurtsev, ccurme, vbarda, hwchase17.
2025-04-01 07:17:12 -04:00
Armaanjeet Singh Sandhu
4bbc249b13 community: Fix attribute access for transcript text in YoutubeLoader (Fixes #30309) (#30582)
**Description:** 
Fixes a bug in the YoutubeLoader where FetchedTranscript objects were
not properly processed. The loader was only extracting the 'text'
attribute from FetchedTranscriptSnippet objects while ignoring 'start'
and 'duration' attributes. This would cause a TypeError when the code
later tried to access these missing keys, particularly when using the
CHUNKS format or any code path that needed timestamp information.

This PR modifies the conversion of FetchedTranscriptSnippet objects to
include all necessary attributes, ensuring that the loader works
correctly with all transcript formats.

**Issue:** Fixes #30309

**Dependencies:** None

**Testing:**
- Tested the fix with multiple YouTube videos to confirm it resolves the
issue
- Verified that both regular loading and CHUNKS format work correctly
2025-04-01 07:13:06 -04:00
Ivan Brko
ecff055096 community[minor]: Improve Brave Search Tool, allow api key in env var (#30364)
- **Description:** 

- Make Brave Search Tool consistent with other tools and allow reading
its api key from `BRAVE_SEARCH_API_KEY` instead of having to pass the
api key manually (no breaking changes)
- Improve Brave Search Tool by storing api key in `SecretStr` instead of
plain `str`.
    - Add unit test for `BraveSearchWrapper`
    - Reflect the changes in the documentation
  - **Issue:** N/A
  - **Dependencies:** N/A
  - **Twitter handle:** ivan_brko
2025-03-31 14:48:52 -04:00
ccurme
0c623045b5 core[patch]: pydantic 2.11 compat (#30554)
Release notes: https://pydantic.dev/articles/pydantic-v2-11-release

Covered here:

- We no longer access `model_fields` on class instances (that is now
deprecated);
- Update schema normalization for Pydantic version testing to reflect
changes to generated JSON schema (addition of `"additionalProperties":
True` for dict types with value Any or object).

## Considerations:

### Changes to JSON schema generation

#### Tool-calling / structured outputs

This may impact tool-calling + structured outputs for some providers,
but schema generation only changes if you have parameters of the form
`dict`, `dict[str, Any]`, `dict[str, object]`, etc. If dict parameters
are typed my understanding is there are no changes.

For OpenAI for example, untyped dicts work for structured outputs with
default settings before and after updating Pydantic, and error both
before/after if `strict=True`.

### Use of `model_fields`

There is one spot where we previously accessed `super(cls,
self).model_fields`, where `cls` is an object in the MRO. This was done
for the purpose of tracking aliases in secrets. I've updated this to
always be `type(self).model_fields`-- see comment in-line for detail.

---------

Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
2025-03-31 14:22:57 -04:00
keshavshrikant
e8be3cca5c fix huggingface tokenizer default length function (#30185)
#30184
2025-03-31 11:54:30 -04:00
Fai LAW
4419340039 docs: add pre_filter usage in similarity_search_with_score (Azure Cosmos DB No SQL) (#30508)
`pre_filter` should be passed in the `Hybrid Search with filtering`
example. Otherwise, it is just an unused variable.
2025-03-31 11:33:00 -04:00
Wenqi Li
64f97e707e ollama[patch]: Support seed param for OllamaLLM (#30553)
**Description:** a description of the change
add the seed param for OllamaLLM client reproducibility

**Issue:** the issue # it fixes, if applicable
follow up of a similar issue
https://github.com/langchain-ai/langchain/issues/24703
see also https://github.com/langchain-ai/langchain/pull/24782

**Dependencies:** any dependencies required for this change
n/a
2025-03-31 11:28:49 -04:00
862 changed files with 24035 additions and 9221 deletions

View File

@@ -29,14 +29,14 @@ body:
options:
- label: I added a very descriptive title to this issue.
required: true
- label: I searched the LangChain documentation with the integrated search.
required: true
- label: I used the GitHub search to find a similar question and didn't find it.
required: true
- label: I am sure that this is a bug in LangChain rather than my code.
required: true
- label: The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
required: true
- label: I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.
required: true
- type: textarea
id: reproduction
validations:

View File

@@ -76,6 +76,7 @@ jobs:
COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }}
UPSTAGE_API_KEY: ${{ secrets.UPSTAGE_API_KEY }}
XAI_API_KEY: ${{ secrets.XAI_API_KEY }}
PPLX_API_KEY: ${{ secrets.PPLX_API_KEY }}
run: |
make integration_tests

View File

@@ -327,6 +327,7 @@ jobs:
FIREWORKS_API_KEY: ${{ secrets.FIREWORKS_API_KEY }}
XAI_API_KEY: ${{ secrets.XAI_API_KEY }}
DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY }}
PPLX_API_KEY: ${{ secrets.PPLX_API_KEY }}
run: make integration_tests
working-directory: ${{ inputs.working-directory }}
@@ -394,8 +395,11 @@ jobs:
# Checkout the latest package files
rm -rf $GITHUB_WORKSPACE/libs/partners/${{ matrix.partner }}/*
cd $GITHUB_WORKSPACE/libs/partners/${{ matrix.partner }}
git checkout "$LATEST_PACKAGE_TAG" -- .
rm -rf $GITHUB_WORKSPACE/libs/standard-tests/*
cd $GITHUB_WORKSPACE/libs/
git checkout "$LATEST_PACKAGE_TAG" -- standard-tests/
git checkout "$LATEST_PACKAGE_TAG" -- partners/${{ matrix.partner }}/
cd partners/${{ matrix.partner }}
# Print as a sanity check
echo "Version number from pyproject.toml: "

View File

@@ -0,0 +1,29 @@
name: Check `langchain-core` version equality
on:
pull_request:
paths:
- 'libs/core/pyproject.toml'
- 'libs/core/langchain_core/version.py'
jobs:
check_version_equality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check version equality
run: |
PYPROJECT_VERSION=$(grep -Po '(?<=^version = ")[^"]*' libs/core/pyproject.toml)
VERSION_PY_VERSION=$(grep -Po '(?<=^VERSION = ")[^"]*' libs/core/langchain_core/version.py)
# Compare the two versions
if [ "$PYPROJECT_VERSION" != "$VERSION_PY_VERSION" ]; then
echo "langchain-core versions in pyproject.toml and version.py do not match!"
echo "pyproject.toml version: $PYPROJECT_VERSION"
echo "version.py version: $VERSION_PY_VERSION"
exit 1
else
echo "Versions match: $PYPROJECT_VERSION"
fi

44
.github/workflows/codspeed.yml vendored Normal file
View File

@@ -0,0 +1,44 @@
name: CodSpeed
on:
push:
branches:
- master
pull_request:
paths:
- 'libs/core/**'
# `workflow_dispatch` allows CodSpeed to trigger backtest
# performance analysis in order to generate initial data.
workflow_dispatch:
jobs:
codspeed:
name: Run benchmarks
if: (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'run-codspeed-benchmarks')) || github.event_name == 'workflow_dispatch' || github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# We have to use 3.12, 3.13 is not yet supported
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
python-version: "3.12"
# Using this action is still necessary for CodSpeed to work
- uses: actions/setup-python@v3
with:
python-version: "3.12"
- name: install deps
run: uv sync --group test
working-directory: ./libs/core
- name: Run benchmarks
uses: CodSpeedHQ/action@v3
with:
token: ${{ secrets.CODSPEED_TOKEN }}
run: |
cd libs/core
uv run --no-sync pytest ./tests/benchmarks --codspeed
mode: walltime

View File

@@ -6,11 +6,6 @@ on:
push:
branches: [jacob/people]
workflow_dispatch:
inputs:
debug_enabled:
description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
required: false
default: 'false'
jobs:
langchain-people:
@@ -26,12 +21,6 @@ jobs:
# Ref: https://github.com/actions/runner/issues/2033
- name: Fix git safe.directory in container
run: mkdir -p /home/runner/work/_temp/_github_home && printf "[safe]\n\tdirectory = /github/workspace" > /home/runner/work/_temp/_github_home/.gitconfig
# Allow debugging with tmate
- name: Setup tmate session
uses: mxschmitt/action-tmate@v3
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled == 'true' }}
with:
limit-access-to-actor: true
- uses: ./.github/actions/people
with:
token: ${{ secrets.LANGCHAIN_PEOPLE_GITHUB_TOKEN }}

View File

@@ -145,6 +145,7 @@ jobs:
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
GOOGLE_SEARCH_API_KEY: ${{ secrets.GOOGLE_SEARCH_API_KEY }}
GOOGLE_CSE_ID: ${{ secrets.GOOGLE_CSE_ID }}
PPLX_API_KEY: ${{ secrets.PPLX_API_KEY }}
run: |
cd langchain/${{ matrix.working-directory }}
make integration_tests

1
.gitignore vendored
View File

@@ -59,6 +59,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
.codspeed/
# Translations
*.mo

View File

@@ -15,8 +15,9 @@
[![GitHub star chart](https://img.shields.io/github/stars/langchain-ai/langchain?style=flat-square)](https://star-history.com/#langchain-ai/langchain)
[![Open Issues](https://img.shields.io/github/issues-raw/langchain-ai/langchain?style=flat-square)](https://github.com/langchain-ai/langchain/issues)
[![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode&style=flat-square)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/langchain-ai/langchain)
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/langchain-ai/langchain)
[<img src="https://github.com/codespaces/badge.svg" title="Open in Github Codespace" width="150" height="20">](https://codespaces.new/langchain-ai/langchain)
[![Twitter](https://img.shields.io/twitter/url/https/twitter.com/langchainai.svg?style=social&label=Follow%20%40LangChainAI)](https://twitter.com/langchainai)
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/langchain-ai/langchain)
> [!NOTE]
> Looking for the JS/TS library? Check out [LangChain.js](https://github.com/langchain-ai/langchainjs).

View File

@@ -30,7 +30,7 @@
"outputs": [],
"source": [
"# lock to 0.10.19 due to a persistent bug in more recent versions\n",
"! pip install \"unstructured[all-docs]==0.10.19\" pillow pydantic lxml pillow matplotlib tiktoken open_clip_torch torch"
"! pip install \"unstructured[all-docs]==0.10.19\" pillow pydantic lxml matplotlib tiktoken open_clip_torch torch"
]
},
{
@@ -409,7 +409,7 @@
" table_summaries,\n",
" tables,\n",
" image_summaries,\n",
" image_summaries,\n",
" img_base64_list,\n",
")"
]
},

View File

@@ -275,3 +275,7 @@ if os.environ.get("READTHEDOCS", "") == "True":
html_context["READTHEDOCS"] = True
master_doc = "index"
# If a signatures length in characters exceeds 60,
# each parameter within the signature will be displayed on an individual logical line
maximum_signature_line_length = 60

View File

@@ -1 +1 @@
eNrVVk9v3EQUByQuHPkEI4sTWm+8/+Lsoh5Cqcq/0KKkVVFbWWP72R5iz7gz42y20R4oPSOZT9CSKKmiliJAXKASRw58gXDgs/BmvJu027RFVEIQJbLz3rz3fu/fb3zrYAukYoK/ep9xDZJGGv9RX986kHCjAqVv7xegMxHvnj+3sVtJdvRWpnWpRktLtGRtVTCdtXPK0yijjLcjUSwxnoi9UMSTXw8yoDG6v314SYF0V1Pguv7RnLZ2bjlZ8tqddqfvP1yNIii1e45HImY8rR+kN1nZIjEkOdWw36jr72hZ5iyiBuPS50rww7OCc7CY68NNgNKlOduCexJUiWnAl/tKU12pW3voF37/7aAApWgK31z4aA7uz1fe3L8oaVrQ+h4XbkSjDH4y8ZRy0buWInfX6LZBX+8ue97PC7rVPBdj94JkKeP1nV9O1b7fhKrvvn26fs2W2Oi/v+K+RzWNRepuYDPA/SCuj8gwXg7BH/hd6PdpJxx2BivDOFnxw0HY8f2QHpFT3Z6VEGPJGc1VvadlBT9ssAKru4D54RXXmOfuue2SYeXqu97B/PXRRla1iNchH1JOOkPfI543sr/k/NrGfRMPI7gbkxKe7s4epgL1o3WqW6TbJWtUkq7XHZDO8qg3GPV94+LbuYuPgac6wxL73b3LVE7q/Qbfo4XUEJlQ8FhJD1Zz7a5vRfVRO+udcUb9fs95hxT0THcw7Hqe18p6bnd4imL3MqP1Ic4fSYVIc3hw1vR+Hqj+vqxCTKeFFtsuzsyZZc+O9Rc4UxKr+Mdrd3ac2fY4I8drD9u+77QcxnHmeAQBjm6qnNGOE+YiDJQWOGUQAKdhDrEzMg1pLeowYUBn6z10FOMgKNABbNOizEEFRZVrVlKpF528+ASuHi63hoCyAPdaThYPCJkGkQTbvCBmaqZMcHZQW9JJgU1aNCoxe8FpHqC1etpK9Z6VtQIqo+wpaSbGgdZ5ULG5SJslCDQDGcSVnKGjE1vWXPDUbDva97HNxlzqmaDTn7acsZCbqjQOVCRKMCgDxreYBnWM8abScYC0VeK0m04+iQmdhFQjUuw3kiEe5AlLTfBKwRPVjkuBBHqcSURzCKoyuKHYTcSPk5yCRFieBTrXcp1hyWMV5LiXaNxZnitjMeYBh6LUkxPrPmqNu/lp6+tYEIQTm1jXG/qdQdebTt94Nouvv4jF8Q+laulY6lK2pG7krp0eV02UhsItJVZOLxmOVvp/RPpf3Y9mtKNPZa4H9hpwoxkV/Acuhr9L8ftYdCSl+qDaYpGQ/HTKX2TdvU6v5+3FL6brf4+Nn+Ta17s7TjOSQUZVhhQ5iJejrgdDn3q0F0fLyXAw8DtD2lkZQLgyXPHjYRLBCvX6EHXixPd6/X6SDLv9oQ9hlIRIsAXlLMHBtWQS4UIh3+A4oHNsPAauImQvw+jIBled401ASTP3Ct9QovFxFh8XrXADl9aMr3O95WyOqWyugNkI4vvVl461bndvrfH4vKCN5ctkN/PQcp4XhvGy0sEWlcwwp0nRiXEgcUPRUIsy2DRPowvMt6G9ItHCONEzh0EiZIHJjZzEbTrunChReh73mxP8BLGWxHIa7mqL2CsLCCVqwrX5eEX2zCcEJ19ifLIzAzIllraIFkRW3DwyyEuSMB4TnaE9V2OQbXKJYwLKipDgJVElRCxhoDAwyZg6jmwiNrqI8KoI8axIyPweNg4mZMxUZkKJUGNdW4TmY7y5iCVrMhGVPAFFNSmEQry2XFOCVIZ3i2qTz0RFIsxbSNwvi2umIuEEMUjIYYtyjQnnVWETk6Arye1R69J+1hvQPD3Bx5oD5gsjxG+M9jV+jX8CyBszSNgOhJvbQ41rRRIcgcfTth1tEcGx3lRtNjbWIoHxIjJFUttCo57X0Ea9SNFaGy4yVUX8WPfG50lswmlhi0pt3WxBFMA8C4XEXFC8NlQkWWk9k3fRlkpIqtz45EI/ltgckfUXN1rYZkq3yWquRIuUi5jGGYuyORhmy9eIbA1sHhcMZIPdFkAYtjUVtwfU6BrfOVmAqTn/6awII7JjZ3rqTPHneuufb9N0evIhiFbXp38B114n3w==
eNrVV91u3MYVTnLRi9zlovcDIoDRgFxxydXfGgaqOIbTH8cupAQuYoOYHR6SE5Ez9MxQ67Wgizp5AeYJklqQAsNuirRIUbQBcpmLvoB60WfpmeFSstcbK4ETIBEkcDVnzjnf+fsO9/7xHijNpXj5ERcGFGUG/9Ef3z9WcKcBbT46qsAUMn1w9crOg0bxk9cLY2o9XlmhNR/oiptiUFKRs4JyMWCyWuEik4cTmc6+Pi6Apmj+o4fvalDBVg7CtH+3t51eUM9WwsFwMBytf77FGNQmuCKYTLnI28f5PV77JIWspAaOOnH7V1rXJWfUYlz5QEvx8LIUAhzm9uEuQB3Qku/BZwp0jWHAh0faUNPo+4doF/7zzXEFWtMc/nz9dz24/730y+Mrd2uOKu1XO0Xjk3BIfksFGW6uhyQMx+6XXL228/nNwOIog/76p+GD9zhtH2IIJJcyL+GLm8Fb1NBU5sEO5hKC36TtCYmGaQjra/Eai9jqBMMNV0dsLU43hmtDlqXrX1mzWgcYjFHS2Zcagrc7gO2nb3x508kwfcHOrIbgeu2q1B4LqQXPsr/00t+DyE3RPlhbj75cMHqN3rUVQFkY/nPbKM6MxSgwUcoE28CwuGbWnvgVXsQUXYqHqzHeDS8SLljZpLDdTN6SFZZZXyS1glLS9PA9qmbt0XXFcy5OyILLrbKU0+CyghSxcVrq9tCoBv619Fpno/3k30ul11wT2lz8o4+1hxzckNgTiPxKpmgFARWob6TS5IKGMrtA+oZd0qwXiZx8gP0TaMXIBSEFXDi6oWhe0fYzIQNGWQHHWyU622PtyaCIL3nj0Sj2LpKKXopWNyPMj1/EQbS5RLA8krOqHmKrAPZcA9hzG2SrViQKo1UyjMfRxjhyPffoybo/2/+PL1uEvYf2i7qZoNwnfQ3Xwr/t8AoHaiHJbj7/hMOhUPbfVz7Z9+Y04I09HMlwEHm+h4U2NpkJzmCuvfG+NynlJLG5RdsJCDopIfXGtqb+ogzdABrbjtFQiiOhwSRwl1Z1CTqpmtLwmiqzaOT8G8ghyFIGEsoTJCg1W7wgVZ4wBS5HScr1XJhh+6G0prMK07moVGP0UtAyQW39rJaOvy1qDVSx4pnTQk4TY8qk4f2RsXSQGA4qSRs1R0dnLq2lFLmlLdQfYT9ZdWXmB8PRge9NpdrVtTWgmazBoky42OMG9CnGe9qkCbY0Tqa2lXwaExqZUINIsd44HXhRZDy3zhsNT2U7rSVugtNIGC0haerkjub3ED/2Tw4KYYUOaC8VpsCUpzopsdtQebjWC1M5FYmAqjazM+0RSq25/razdXqQTGYusCjcXB+uRuHBwavfvo62z1tH+IeneuX0NKB8Rd8pA9c9gZ5pA1VQK8ycWbHLRpuf1fZ67bzl8F1peXGXPWJzq2Yp9fxYLP6r5Sy+hKkXt97hENfV89beEdYRea49bvY4k0ocps8j4OGmJeDl/PnYbYaAzXn3xXfFua8A5y2TH26lP7UafvHrfa+boKSgukBGRwM0hM00zMJsNcWPsLkxDGmEn2jMNlMWpxAPJzHFO6thtB6NopDFMYw2YA3iyO6DimJfYnUd9zGcf6RH7DE0jglFxw3DwtsFhOT1vnc6uHjSjanGT3hi8HEZHzfc4Q5yjJ0277bv7U6p6jbWfGLw8/sv7GvbUcW1zuLznHaaLxLd3ILvPc8NF3Vjkj2quCV6G6KX4jjjkKCikXWya59Wlth3cqts5oaSTKoKgxp7WdBV2jsT4ulVpCFB8CXY+SCOenHwfeI2KxBK9EwY+2UBSb6cEZwmhX7J/hzAAXHsSowkqhH2UUBZk4yLlJgC9YWeghqQdwUC1+4I95AiugbGMw4aHZOC61PP1mMnY0Q01QTvyoz0rwvWwIxMuS6sKzkxmE+f0HKKC5a4nUJmslFnoKghldSI16XpgCDj4grUA/JH2RCGcUuFc+VwzUVkMkMMOCKwR4XBgMumcoEpMI0S7qoz6b5GWdAiP8PHuwv2RWiCr0KDW+KWeAeQi+aQsBwIt3SXOtOaZFj6J8N2lfSJFJhvqnc7HaeRwXQRmSa5K6EV9zl0Xm9Q1DaWNm1WET/mvbN55psIZGCbVOry5hKiAfooNBJdRXG7aaa4WzYD8ibqUgVZU1qbQponAusROXtpJ4W7XJsB2Sq19Em9iGlacFb0YLhLX3fkcuDiuG4hW+wuAdKyoc24u6DHt8T+WeMfeAf447/w/L/dIHH9pMffHX2fOf/DvDXGZN/pdqm67X9vbvF735jo07d41Lp98H8ZDhN8

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1 +1 @@
eNrVV09v3MYVb3voIcd+ggGRQ1MsVyT3n3YLHwRLcJxIlptdpwligxiSj+RkyRl6Zqj12thD3ZwLbD9BUgtSYNhNkQa5tAZ67KFfwD30s/TNcFd/1kpt5FAkggSS8+a9+b03v/eb0ePTI5CKCf7TZ4xrkDTW+KH++PhUwv0alP7spASdi+TJjb3Jk1qyl2/nWldqtLVFK9ZWJdN5u6A8i3PKeDsW5RbjqTiORDL/x2kONMHwnz29o0C6OxlwvfzGzLZ+bjXf8tp+2+8OvtqJY6i0u8djkTCeLZ9nD1nVIgmkBdVw0piXf6FVVbCYGoxbnyrBn14XnIPFvHw6BahcWrAj+FKCqjAN+P2J0lTX6vExxoV//fO0BKVoBn86fH8N7j8/+cXJbUmzki6/5MKNaZzDt2Y9pVyMrqUo3AP6wKBfPul73t82bDtFIWbuoWQZ48vP/36l9d1mqeUXv7rafmBLbOxff+TuUk0TkbkT3AxwbybLlySK++D1aeTFve0g8ob+9nYUdagXRd1h2uvEL8mVYa9LSLDkjBZqeaxlDX+dsBKru4H5q49c4164ew8qhpVbfuGdrl9fTPK6RTyfvEc58YcDj3jeyP6SGweTZ2Y9XMGdzCt4dXeOMRVYvhhT3SJBQA6oJIEX9IjfH3V6o15gQvx5HWIfeKZzLPEgOP6QyvnypMH3YiM1RCYUXCjp6U6h3fFRvHzZzjvXnFG323F+TUp6LegNA8/zWnnHDYZXGJ58yOjyKfKPZEJkBTy/bvZ+vdDy66qOMJ0WejxwkTPX+p6l9e+QUxKr+O+fff7IWXWPM3K89rA9GDgth3HkHI8hROpmyhk9cqJCRKHSAlkGIXAaFZA4I7MhrU0bJgwYbNzBQAkSQYEO4QEtqwJUWNaFZhWVejPI62dg62FzawgpC7Gv5XxzgpBZGEuwmxcmTK2MKXIHrRWdl7hJm04VZi84LUL0Vq96qc53Za2Ayjh/ZTQXs1DrIqzZekibJgg1AxkmtVyho3Nb1kLwzHQ7+ndxm4271KsBv7toOTMhp6oyAVQsKjAoQ8aPmAZ1hvGh0kmIslUh281OXsaEQSKqESnuN4ohTuQpy8zitYJL1U4qgQJ6lklMCwjrKryv2EPEj0zOQCIszwJdW7nOseSJCgvsS3T2+2tjImY85FBWen7u3UWrCbeebWOdDYTR3CYWeMOB3wu8xeKt71bx8etUHP9wVG2djbqUban7hekE7FY1VxpKt5JYOb1lNFrpH5Ho/+FZvJIdfaVyPbfHgBuvpOAHcDC8qcSfYNFRlJan9RGLheRXS/6m6h77/X7/OPmfcu0buf7/qfFlrf3540dOQ8kwpypHiez4fq+f9FLoDLs9PBAB/EHg9/zI2+4P/CAJgmg7HnjpdurHXs/rpb1ub9AbdrtBF5JOL0CBLSlnKRLX9DPDjv/EOWM7WhtuK3zDEY2P6/i4bQcn2JiGos69llPE2IkoVMgjRIWMQcR1jLKHHtMZlc0RsKIgvn/yRmuNbX8dNF7fd9Em6uuyW81qOd93Gb32GDkfi5pQCQQvC1YnsJcVyzgkRAuyvmCSGeoAoWT8m31izrgIT7n2XX4De5gbT8arWhOrW9iPLWKPJYxJ1Jxrc0FFhSzmBNktUQXIowTZjy8LYg82s5Ks0U3nGK4QYkqoNh8E9QEFWxGR2s9mNuUJGnQtuR2kXM1AIpo7HM9TZcdQ6iVRFcQsZaAMjuY9JrwuI7RhwPUJbBzmmKDKDQ4Raax2i9BihmcWsTJN5qKW51ARWykUZqFFFU4Xa5CIwNQyxnIIia11KYEIYeNHAUcUSxyLoi65Tfs8DxvTFtxUkWfnAFkz4ULhbwEKxgpRKiSiLeyUJrAiKfLiYtbaHJAtIjhuAlVT69PAu4RIkczu6KrWdjNXaeX0CEttlcTg1kIUyoY5+x8EEVuWbEA9NGviftjxCFDSGuc2uWQx/4LI0sr5qijIQKzahpdZ+4zUzb6kDK8z5ywwYA/ujCckEXgZxJLkEE8v7mAEuBJgdSGuLWqm2+RmaqaQDLShM0iJmc1yVlycR5sALcQ3k3gj2aCkNs/MHMZ3+V2+e0huHU5QLKeGoXOye7BPzBkH5lKmyC9v3hrvfTBpkTu3d3cmey2yu7e/Z58fHN4moOP2O7bMl2t5l0+EiSJt6gRvT3WRkJ393+58PL7UN3a/XyGOiagAMDHaBDBctQlg7F1hAaspq9CJKVwHKhyfmJ68sNqqDTCswtKWdN2blr5nfGoAtO3dGqUhPKKS2SEjaqvuR6PtICNba0EKGxagLqVuc444C/y592ZxFovzizVOuLf4Lw/1m1w=
eNrVV01v3MYZbnvoobceeh8sCrgplityv/Rh+CBYgutWstRonTqIDWJIDsmJyBl6Zqj12tChbv7A9hcktSAFht0UaZGiaAP02EP/gHrob+kzw119rJXISNpDDRnkzjvvO8/79bzD5ycHTGkuxXdfcWGYorHBD/3b5yeKPa6ZNh8dl8zkMnlxZ3P0olb89Me5MZVeW1qiFe/okpu8U1CRxTnlohPLcomLVB5FMpn8/SRnNIH5j17e10x56xkTZvonu9vpedVkye8EnaC//Nl6HLPKeJsilgkX2fR19pRXbZKwtKCGHTfi6R9oVRU8phbj0odaipe3pRDMYZ6+3Ges8mjBD9iniukKbrDfHGtDTa2fH8Eu++c/TkqmNc3Y73Z+MQf37+/86GTzScWhMv1ylNdt4gfk51SQYHXZJ76/5v7Ine3RZw88i6Pw5ts/8V+8x+n0JVwgmZRZwT5/4G1QQxOZeSPEknl3k+kpiZJuEKU9libdwbAXp92BHyVBN0qX/X7SH0ZfWrNae3DGKOnsS828nzUAp5/89IsHTobweaNJxbydymVpeiKkFjxNfz+XbjGRmXz6Yrjc/WLB6DZ9YjMAme//Zc8oHhuLUSBQynh7LEZyzWR62i6xESG61QsGPez1bxIu4qJO2F4dbcgSadY3SaVYIWly9B5Vk+nxjuIZF6dk4cj1opBj77ZiCbBxWujpkVE1++uV2xob04//dqV02xWhjcWf577OIXu7EjUB5JupoiXzqIC+kUqTG5oV6Q0yL9grivUmkdGHqB9Pq5jcEFKwG8e7imYlnX4qpBfTOGcn6wUOO4inp528d6u11u/3WjdJSW91B6tdxKed97zu6hWCqz05z+oRSoWh5mqGmlsh65UiXb87IEFvrbuyhhfU3KuLeX+z/l/ftgjnJ0w/r+oI8jaZ53Do/3HESzTUQpBdf/4azaEg+9f3Pn7WmtFAa62FlvQ73Va7hUQbG8wQPZjp1tqzVlTIKLSxhe2QCRoVLGmt2Zy2F2U4hsHYXg+GErSEZiZkT2hZFUyHZV0YXlFlFo1cvwMcApYyLKQ8BEGpyeIGqbIwVszFKEy4nglTlB+kFZ2UCOeiUgXvpaBFCG39ppbufZXXmlEV52+s5nIcGlOENZ8vGUsHoeFMhUmtZujoxIW1kCKztAX9PurJqiszWwj6h+3WWKp9XVkDOpYVsyhDLg64YfoM41NtkhAljc7UNpOXMcFIRA2QIt/oDmwUKc/s4bVml6KdVBKT4MyTmBYsrKvwseZPgR/1kzEFWL4DOpcKkyPkiQ4LVBuUg+FcmMixCAUrKzM51+5Das3NdztbZwthNHGOdf3V5WDQ9Q8Pf/DV42jvunGE/1jVS2erHuVL+nFh+wN9pSfasNKrFCJnluyw0eb/anr98Lrh8La0vDjLXsUzq+ZK6vlfsfg7V7P4FUy9OPWOguFw+HVj7xh5BM9NT+oDHksljpKvJeC+JeCr+fO1mwxePOPdbz8rrr0CXDdM/nsj/dJo+P7zZ62mg8Kc6hyM3guCwTAZpKy32h8EKyuMBcvdYBBE/spwOegm3W60Ei/76UoaxP7AH6SD/mB5sNrvd/ss6Q3sYCkp6hLZtfTDQVAftM6aE9KmFTXesGLwuI3HrlscgUdsR7UetVtFDOIAr6I4gQqZAOI6RsVAY39MVTOxZh2D9w/e6qw9RwfbjdY3PbSxep13s13t1jc9xsw11lrvy5pQxQiurY7WQD2aZ4IlxEgyv9iTMWiLULL3yy1iR3KEodx5KO6AcoTV5KKqDXE0iyZvEzdFYZPoiTD2wwCEXkwIOkehEcmzBNyBl0Pi5rA9SdVQMznMFVLuE2rsDwI6w3zRRKbuZ7ObigQCUyvhFlG0Y6aA5r7A+NduDZNJEV2xmKecaYujeY+JqMsIMhicXxiswgQO6tzikJFBtNuEFmOMWOKmCpnIWp1DBbZSanhhZBXuH85BAoGNZYxwSIXWuuRABNj4UbADihDHsqhL4dw+98PZdAG3URTZOUDebLgQ+HsMZDRDlEoFtIXb0hjWJEVdXPTa2HneJlIgCVTvO50G3iVEmmQuo7NYu2TO3MrpAULtiMTiNlIW2pk5+/YDYlclC1B37JnIh1uPwBPjRrlDLknsp58q3YyYBQUViKgtaNmzz4q6yUvKcfs6rwILdvv+3ogkEjdahCRn8f7FDEYMJzFEFyTnUHPTIXdTu4VkzNhyZkrBs3HOi4v7aGOgDXxjsCNbKEljn5m9OzwUD8XGDrm3MwJN79sKnZCN7S1iRzKzd0hNfnL33t7mu6M2ub+7sT7abJONza1N93x3Z5cwE3fecWG+HMuHYiStFeVcJ7js1UVC1rd+tf7+3qW+cfl+o3CsRc0YHKONAVurzgHY3pAOsN7nFZS4xjmswvrI9uSF02ZtALMaoS3pvDdd+Z7VUwOg4z4FQA3hAVXcLVlSm3U/hK6DLG3NCSlsqgC8lHrNHGkd4t+jt7NzeHj+HYANjw7/AxNYKrM=

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -6,5 +6,5 @@
- `BaseChatModel` methods `__call__`, `call_as_llm`, `predict`, `predict_messages`. Will be removed in 0.2.0. Use `BaseChatModel.invoke` instead.
- `BaseChatModel` methods `apredict`, `apredict_messages`. Will be removed in 0.2.0. Use `BaseChatModel.ainvoke` instead.
- `BaseLLM` methods `__call__, `predict`, `predict_messages`. Will be removed in 0.2.0. Use `BaseLLM.invoke` instead.
- `BaseLLM` methods `__call__`, `predict`, `predict_messages`. Will be removed in 0.2.0. Use `BaseLLM.invoke` instead.
- `BaseLLM` methods `apredict`, `apredict_messages`. Will be removed in 0.2.0. Use `BaseLLM.ainvoke` instead.

View File

@@ -92,7 +92,7 @@ structured_model = model.with_structured_output(Questions)
# Define the system prompt
system = """You are a helpful assistant that generates multiple sub-questions related to an input question. \n
The goal is to break down the input into a set of sub-problems / sub-questions that can be answers in isolation. \n"""
The goal is to break down the input into a set of sub-problems / sub-questions that can be answered independently. \n"""
# Pass the question to the model
question = """What are the main components of an LLM-powered autonomous agent system?"""

View File

@@ -126,7 +126,7 @@ Please see the [Configurable Runnables](#configurable-runnables) section for mor
LangChain will automatically try to infer the input and output types of a Runnable based on available information.
Currently, this inference does not work well for more complex Runnables that are built using [LCEL](/docs/concepts/lcel) composition, and the inferred input and / or output types may be incorrect. In these cases, we recommend that users override the inferred input and output types using the `with_types` method ([API Reference](https://python.langchain.com/api_reference/core/runnables/langchain_core.runnables.base.Runnable.html#langchain_core.runnables.base.Runnable.with_types
).
)).
## RunnableConfig
@@ -194,7 +194,7 @@ In Python 3.11 and above, this works out of the box, and you do not need to do a
In Python 3.9 and 3.10, if you are using **async code**, you need to manually pass the `RunnableConfig` through to the `Runnable` when invoking it.
This is due to a limitation in [asyncio's tasks](https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task) in Python 3.9 and 3.10 which did
not accept a `context` argument).
not accept a `context` argument.
Propagating the `RunnableConfig` manually is done like so:

View File

@@ -83,7 +83,6 @@ LinkedIn, where we highlight the best examples.
Here are some heuristics for types of content we are excited to promote:
- **Integration announcement:** Announcements of new integrations with a link to the LangChain documentation page.
- **Educational content:** Blogs, YouTube videos and other media showcasing educational content. Note that we prefer content that is NOT framed as "here's how to use integration XYZ", but rather "here's how to do ABC", as we find that is more educational and helpful for developers.
- **End-to-end applications:** End-to-end applications are great resources for developers looking to build. We prefer to highlight applications that are more complex/agentic in nature, and that use [LangGraph](https://github.com/langchain-ai/langgraph) as the orchestration framework. We get particularly excited about anything involving long-term memory, human-in-the-loop interaction patterns, or multi-agent architectures.
- **Research:** We love highlighting novel research! Whether it is research built on top of LangChain or that integrates with it.

View File

@@ -40,7 +40,7 @@
"\n",
"To view the list of separators for a given language, pass a value from this enum into\n",
"```python\n",
"RecursiveCharacterTextSplitter.get_separators_for_language`\n",
"RecursiveCharacterTextSplitter.get_separators_for_language\n",
"```\n",
"\n",
"To instantiate a splitter that is tailored for a specific language, pass a value from the enum into\n",

View File

@@ -351,7 +351,7 @@ LangSmith allows you to closely trace, monitor and evaluate your LLM application
It seamlessly integrates with LangChain and LangGraph, and you can use it to inspect and debug individual steps of your chains and agents as you build.
LangSmith documentation is hosted on a separate site.
You can peruse [LangSmith how-to guides here](https://docs.smith.langchain.com/how_to_guides/), but we'll highlight a few sections that are particularly
You can peruse [LangSmith how-to guides here](https://docs.smith.langchain.com/), but we'll highlight a few sections that are particularly
relevant to LangChain below:
### Evaluation

View File

@@ -15,7 +15,7 @@
"\n",
"To build a production application, you will need to do more work to keep track of application state appropriately.\n",
"\n",
"We recommend using `langgraph` for powering such a capability. For more details, please see this [guide](https://langchain-ai.github.io/langgraph/how-tos/human-in-the-loop/).\n",
"We recommend using `langgraph` for powering such a capability. For more details, please see this [guide](https://langchain-ai.github.io/langgraph/concepts/human_in_the_loop/).\n",
":::\n"
]
},
@@ -209,7 +209,7 @@
"metadata": {},
"outputs": [
{
"name": "stdin",
"name": "stdout",
"output_type": "stream",
"text": [
"Do you approve of the following tool invocations\n",
@@ -252,7 +252,7 @@
"metadata": {},
"outputs": [
{
"name": "stdin",
"name": "stdout",
"output_type": "stream",
"text": [
"Do you approve of the following tool invocations\n",

View File

@@ -1,378 +1,401 @@
{
"cells": [
{
"cell_type": "raw",
"id": "afaf8039",
"metadata": {},
"source": [
"---\n",
"sidebar_label: AWS Bedrock\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "e49f1e0d",
"metadata": {},
"source": [
"# ChatBedrock\n",
"\n",
"This doc will help you get started with AWS Bedrock [chat models](/docs/concepts/chat_models). Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI. Using Amazon Bedrock, you can easily experiment with and evaluate top FMs for your use case, privately customize them with your data using techniques such as fine-tuning and Retrieval Augmented Generation (RAG), and build agents that execute tasks using your enterprise systems and data sources. Since Amazon Bedrock is serverless, you don't have to manage any infrastructure, and you can securely integrate and deploy generative AI capabilities into your applications using the AWS services you are already familiar with.\n",
"\n",
"For more information on which models are accessible via Bedrock, head to the [AWS docs](https://docs.aws.amazon.com/bedrock/latest/userguide/models-features.html).\n",
"\n",
"For detailed documentation of all ChatBedrock features and configurations head to the [API reference](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock.ChatBedrock.html).\n",
"\n",
"## Overview\n",
"### Integration details\n",
"\n",
"| Class | Package | Local | Serializable | [JS support](https://js.langchain.com/docs/integrations/chat/bedrock) | Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
"| [ChatBedrock](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock.ChatBedrock.html) | [langchain-aws](https://python.langchain.com/api_reference/aws/index.html) | ❌ | beta | ✅ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-aws?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-aws?style=flat-square&label=%20) |\n",
"\n",
"### Model features\n",
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| ✅ | ✅ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ |\n",
"\n",
"## Setup\n",
"\n",
"To access Bedrock models you'll need to create an AWS account, set up the Bedrock API service, get an access key ID and secret key, and install the `langchain-aws` integration package.\n",
"\n",
"### Credentials\n",
"\n",
"Head to the [AWS docs](https://docs.aws.amazon.com/bedrock/latest/userguide/setting-up.html) to sign up to AWS and setup your credentials. You'll also need to turn on model access for your account, which you can do by following [these instructions](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html)."
]
},
{
"cell_type": "markdown",
"id": "72ee0c4b-9764-423a-9dbf-95129e185210",
"metadata": {},
"source": "To enable automated tracing of your model calls, set your [LangSmith](https://docs.smith.langchain.com/) API key:"
},
{
"cell_type": "code",
"execution_count": null,
"id": "a15d341e-3e26-4ca3-830b-5aab30ed66de",
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")\n",
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\""
]
},
{
"cell_type": "markdown",
"id": "0730d6a1-c893-4840-9817-5e5251676d5d",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"The LangChain Bedrock integration lives in the `langchain-aws` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "652d6238-1f87-422a-b135-f5abbb8652fc",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-aws"
]
},
{
"cell_type": "markdown",
"id": "a38cde65-254d-4219-a441-068766c0d4b5",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Now we can instantiate our model object and generate chat completions:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
"metadata": {},
"outputs": [],
"source": [
"from langchain_aws import ChatBedrock\n",
"\n",
"llm = ChatBedrock(\n",
" model_id=\"anthropic.claude-3-sonnet-20240229-v1:0\",\n",
" model_kwargs=dict(temperature=0),\n",
" # other params...\n",
")"
]
},
{
"cell_type": "markdown",
"id": "2b4f3e15",
"metadata": {},
"source": [
"## Invocation"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "62e0dbc3",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\"Voici la traduction en français :\\n\\nJ'aime la programmation.\", additional_kwargs={'usage': {'prompt_tokens': 29, 'completion_tokens': 21, 'total_tokens': 50}, 'stop_reason': 'end_turn', 'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0'}, response_metadata={'usage': {'prompt_tokens': 29, 'completion_tokens': 21, 'total_tokens': 50}, 'stop_reason': 'end_turn', 'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0'}, id='run-fdb07dc3-ff72-430d-b22b-e7824b15c766-0', usage_metadata={'input_tokens': 29, 'output_tokens': 21, 'total_tokens': 50})"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n",
" ),\n",
" (\"human\", \"I love programming.\"),\n",
"]\n",
"ai_msg = llm.invoke(messages)\n",
"ai_msg"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d86145b3-bfef-46e8-b227-4dda5c9c2705",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Voici la traduction en français :\n",
"\n",
"J'aime la programmation.\n"
]
}
],
"source": [
"print(ai_msg.content)"
]
},
{
"cell_type": "markdown",
"id": "18e2bfc0-7e78-4528-a73f-499ac150dca8",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='Ich liebe Programmieren.', additional_kwargs={'usage': {'prompt_tokens': 23, 'completion_tokens': 11, 'total_tokens': 34}, 'stop_reason': 'end_turn', 'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0'}, response_metadata={'usage': {'prompt_tokens': 23, 'completion_tokens': 11, 'total_tokens': 34}, 'stop_reason': 'end_turn', 'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0'}, id='run-5ad005ce-9f31-4670-baa0-9373d418698a-0', usage_metadata={'input_tokens': 23, 'output_tokens': 11, 'total_tokens': 34})"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n",
" ),\n",
" (\"human\", \"{input}\"),\n",
" ]\n",
")\n",
"\n",
"chain = prompt | llm\n",
"chain.invoke(\n",
" {\n",
" \"input_language\": \"English\",\n",
" \"output_language\": \"German\",\n",
" \"input\": \"I love programming.\",\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"id": "d1ee55bc-ffc8-4cfa-801c-993953a08cfd",
"metadata": {},
"source": [
"## Bedrock Converse API\n",
"\n",
"AWS has recently released the Bedrock Converse API which provides a unified conversational interface for Bedrock models. This API does not yet support custom models. You can see a list of all [models that are supported here](https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html). To improve reliability the ChatBedrock integration will switch to using the Bedrock Converse API as soon as it has feature parity with the existing Bedrock API. Until then a separate [ChatBedrockConverse](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock_converse.ChatBedrockConverse.html) integration has been released.\n",
"\n",
"We recommend using `ChatBedrockConverse` for users who do not need to use custom models.\n",
"\n",
"You can use it like so:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ae728e59-94d4-40cf-9d24-25ad8723fc59",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\"Voici la traduction en français :\\n\\nJ'aime la programmation.\", response_metadata={'ResponseMetadata': {'RequestId': '4fcbfbe9-f916-4df2-b0bd-ea1147b550aa', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Wed, 21 Aug 2024 17:23:49 GMT', 'content-type': 'application/json', 'content-length': '243', 'connection': 'keep-alive', 'x-amzn-requestid': '4fcbfbe9-f916-4df2-b0bd-ea1147b550aa'}, 'RetryAttempts': 0}, 'stopReason': 'end_turn', 'metrics': {'latencyMs': 672}}, id='run-77ee9810-e32b-45dc-9ccb-6692253b1f45-0', usage_metadata={'input_tokens': 29, 'output_tokens': 21, 'total_tokens': 50})"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_aws import ChatBedrockConverse\n",
"\n",
"llm = ChatBedrockConverse(\n",
" model=\"anthropic.claude-3-sonnet-20240229-v1:0\",\n",
" temperature=0,\n",
" max_tokens=None,\n",
" # other params...\n",
")\n",
"\n",
"llm.invoke(messages)"
]
},
{
"cell_type": "markdown",
"id": "4da16f3e-e80b-48c0-8036-c1cc5f7c8c05",
"metadata": {},
"source": [
"### Streaming\n",
"\n",
"Note that `ChatBedrockConverse` emits content blocks while streaming:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7794b32e-d8de-4973-bf0f-39807dc745f0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"content=[] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': 'Vo', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': 'ici', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': ' la', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': ' tra', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': 'duction', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': ' en', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': ' français', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': ' :', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': '\\n\\nJ', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': \"'\", 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': 'a', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': 'ime', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': ' la', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': ' programm', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': 'ation', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'type': 'text', 'text': '.', 'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[{'index': 0}] id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[] response_metadata={'stopReason': 'end_turn'} id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8'\n",
"content=[] response_metadata={'metrics': {'latencyMs': 713}} id='run-2c92c5af-d771-4cc2-98d9-c11bbd30a1d8' usage_metadata={'input_tokens': 29, 'output_tokens': 21, 'total_tokens': 50}\n"
]
}
],
"source": [
"for chunk in llm.stream(messages):\n",
" print(chunk)"
]
},
{
"cell_type": "markdown",
"id": "0ef05abb-9c04-4dc3-995e-f857779644d5",
"metadata": {},
"source": [
"An output parser can be used to filter to text, if desired:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2a4e743f-ea7d-4e5a-9b12-f9992362de8b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"|Vo|ici| la| tra|duction| en| français| :|\n",
"\n",
"J|'|a|ime| la| programm|ation|.||||"
]
}
],
"source": [
"from langchain_core.output_parsers import StrOutputParser\n",
"\n",
"chain = llm | StrOutputParser()\n",
"\n",
"for chunk in chain.stream(messages):\n",
" print(chunk, end=\"|\")"
]
},
{
"cell_type": "markdown",
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all ChatBedrock features and configurations head to the API reference: https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock.ChatBedrock.html\n",
"\n",
"For detailed documentation of all ChatBedrockConverse features and configurations head to the API reference: https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock_converse.ChatBedrockConverse.html"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
"cells": [
{
"cell_type": "raw",
"id": "afaf8039",
"metadata": {},
"source": [
"---\n",
"sidebar_label: AWS Bedrock\n",
"---"
]
},
"nbformat": 4,
"nbformat_minor": 5
{
"cell_type": "markdown",
"id": "e49f1e0d",
"metadata": {},
"source": [
"# ChatBedrock\n",
"\n",
"This doc will help you get started with AWS Bedrock [chat models](/docs/concepts/chat_models). Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI. Using Amazon Bedrock, you can easily experiment with and evaluate top FMs for your use case, privately customize them with your data using techniques such as fine-tuning and Retrieval Augmented Generation (RAG), and build agents that execute tasks using your enterprise systems and data sources. Since Amazon Bedrock is serverless, you don't have to manage any infrastructure, and you can securely integrate and deploy generative AI capabilities into your applications using the AWS services you are already familiar with.\n",
"\n",
"AWS Bedrock maintains a [Converse API](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html) which provides a unified conversational interface for Bedrock models. This API does not yet support custom models. You can see a list of all [models that are supported here](https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html).\n",
"\n",
":::info\n",
"\n",
"We recommend the Converse API for users who do not need to use custom models. It can be accessed using [ChatBedrockConverse](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock_converse.ChatBedrockConverse.html).\n",
"\n",
":::\n",
"\n",
"For detailed documentation of all Bedrock features and configurations head to the [API reference](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock_converse.ChatBedrockConverse.html).\n",
"\n",
"## Overview\n",
"### Integration details\n",
"\n",
"| Class | Package | Local | Serializable | [JS support](https://js.langchain.com/docs/integrations/chat/bedrock) | Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
"| [ChatBedrock](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock.ChatBedrock.html) | [langchain-aws](https://python.langchain.com/api_reference/aws/index.html) | ❌ | beta | ✅ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-aws?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-aws?style=flat-square&label=%20) |\n",
"| [ChatBedrockConverse](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock_converse.ChatBedrockConverse.html) | [langchain-aws](https://python.langchain.com/api_reference/aws/index.html) | ❌ | beta | ✅ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-aws?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-aws?style=flat-square&label=%20) |\n",
"\n",
"### Model features\n",
"\n",
"The below apply to both `ChatBedrock` and `ChatBedrockConverse`.\n",
"\n",
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| ✅ | ✅ | ❌ | ✅ | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ |\n",
"\n",
"## Setup\n",
"\n",
"To access Bedrock models you'll need to create an AWS account, set up the Bedrock API service, get an access key ID and secret key, and install the `langchain-aws` integration package.\n",
"\n",
"### Credentials\n",
"\n",
"Head to the [AWS docs](https://docs.aws.amazon.com/bedrock/latest/userguide/setting-up.html) to sign up to AWS and setup your credentials. You'll also need to turn on model access for your account, which you can do by following [these instructions](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html)."
]
},
{
"cell_type": "markdown",
"id": "72ee0c4b-9764-423a-9dbf-95129e185210",
"metadata": {},
"source": [
"To enable automated tracing of your model calls, set your [LangSmith](https://docs.smith.langchain.com/) API key:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a15d341e-3e26-4ca3-830b-5aab30ed66de",
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")\n",
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\""
]
},
{
"cell_type": "markdown",
"id": "0730d6a1-c893-4840-9817-5e5251676d5d",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"The LangChain Bedrock integration lives in the `langchain-aws` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "652d6238-1f87-422a-b135-f5abbb8652fc",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-aws"
]
},
{
"cell_type": "markdown",
"id": "a38cde65-254d-4219-a441-068766c0d4b5",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Now we can instantiate our model object and generate chat completions:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
"metadata": {},
"outputs": [],
"source": [
"from langchain_aws import ChatBedrockConverse\n",
"\n",
"llm = ChatBedrockConverse(\n",
" model_id=\"anthropic.claude-3-5-sonnet-20240620-v1:0\",\n",
" # temperature=...,\n",
" # max_tokens=...,\n",
" # other params...\n",
")"
]
},
{
"cell_type": "markdown",
"id": "2b4f3e15",
"metadata": {},
"source": [
"## Invocation"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fcd8de52-4a1b-4875-b463-d41b031e06a1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\"J'adore la programmation.\", additional_kwargs={}, response_metadata={'ResponseMetadata': {'RequestId': 'b07d1630-06f2-44b1-82bf-e82538dd2215', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Wed, 16 Apr 2025 19:35:34 GMT', 'content-type': 'application/json', 'content-length': '206', 'connection': 'keep-alive', 'x-amzn-requestid': 'b07d1630-06f2-44b1-82bf-e82538dd2215'}, 'RetryAttempts': 0}, 'stopReason': 'end_turn', 'metrics': {'latencyMs': [488]}, 'model_name': 'anthropic.claude-3-5-sonnet-20240620-v1:0'}, id='run-d09ed928-146a-4336-b1fd-b63c9e623494-0', usage_metadata={'input_tokens': 29, 'output_tokens': 11, 'total_tokens': 40, 'input_token_details': {'cache_creation': 0, 'cache_read': 0}})"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n",
" ),\n",
" (\"human\", \"I love programming.\"),\n",
"]\n",
"ai_msg = llm.invoke(messages)\n",
"ai_msg"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d86145b3-bfef-46e8-b227-4dda5c9c2705",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"J'adore la programmation.\n"
]
}
],
"source": [
"print(ai_msg.content)"
]
},
{
"cell_type": "markdown",
"id": "4da16f3e-e80b-48c0-8036-c1cc5f7c8c05",
"metadata": {},
"source": [
"### Streaming\n",
"\n",
"Note that `ChatBedrockConverse` emits content blocks while streaming:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "605e04fa-1a76-47ac-8c92-fe128659663e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"content=[] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'\n",
"content=[{'type': 'text', 'text': 'J', 'index': 0}] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'\n",
"content=[{'type': 'text', 'text': \"'adore la\", 'index': 0}] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'\n",
"content=[{'type': 'text', 'text': ' programmation.', 'index': 0}] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'\n",
"content=[{'index': 0}] additional_kwargs={} response_metadata={} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'\n",
"content=[] additional_kwargs={} response_metadata={'stopReason': 'end_turn'} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd'\n",
"content=[] additional_kwargs={} response_metadata={'metrics': {'latencyMs': 600}, 'model_name': 'anthropic.claude-3-5-sonnet-20240620-v1:0'} id='run-d0e0836e-7146-4c3d-97c7-ad23dac6febd' usage_metadata={'input_tokens': 29, 'output_tokens': 11, 'total_tokens': 40, 'input_token_details': {'cache_creation': 0, 'cache_read': 0}}\n"
]
}
],
"source": [
"for chunk in llm.stream(messages):\n",
" print(chunk)"
]
},
{
"cell_type": "markdown",
"id": "0ef05abb-9c04-4dc3-995e-f857779644d5",
"metadata": {},
"source": [
"You can filter to text using the [.text()](https://python.langchain.com/api_reference/core/messages/langchain_core.messages.ai.AIMessage.html#langchain_core.messages.ai.AIMessage.text) method on the output:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2a4e743f-ea7d-4e5a-9b12-f9992362de8b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"|J|'adore la| programmation.||||"
]
}
],
"source": [
"for chunk in llm.stream(messages):\n",
" print(chunk.text(), end=\"|\")"
]
},
{
"cell_type": "markdown",
"id": "a77519e5-897d-41a0-a9bb-55300fa79efc",
"metadata": {},
"source": [
"## Prompt caching\n",
"\n",
"Bedrock supports [caching](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html) of elements of your prompts, including messages and tools. This allows you to re-use large documents, instructions, [few-shot documents](/docs/concepts/few_shot_prompting/), and other data to reduce latency and costs.\n",
"\n",
":::note\n",
"\n",
"Not all models support prompt caching. See supported models [here](https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html#prompt-caching-models).\n",
"\n",
":::\n",
"\n",
"To enable caching on an element of a prompt, mark its associated content block using the `cachePoint` key. See example below:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d5f63d01-85e8-4797-a2be-0fea747a6049",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First invocation:\n",
"{'cache_creation': 1528, 'cache_read': 0}\n",
"\n",
"Second:\n",
"{'cache_creation': 0, 'cache_read': 1528}\n"
]
}
],
"source": [
"import requests\n",
"from langchain_aws import ChatBedrockConverse\n",
"\n",
"llm = ChatBedrockConverse(model=\"us.anthropic.claude-3-7-sonnet-20250219-v1:0\")\n",
"\n",
"# Pull LangChain readme\n",
"get_response = requests.get(\n",
" \"https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md\"\n",
")\n",
"readme = get_response.text\n",
"\n",
"messages = [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": [\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": \"What's LangChain, according to its README?\",\n",
" },\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": f\"{readme}\",\n",
" },\n",
" {\n",
" \"cachePoint\": {\"type\": \"default\"},\n",
" },\n",
" ],\n",
" },\n",
"]\n",
"\n",
"response_1 = llm.invoke(messages)\n",
"response_2 = llm.invoke(messages)\n",
"\n",
"usage_1 = response_1.usage_metadata[\"input_token_details\"]\n",
"usage_2 = response_2.usage_metadata[\"input_token_details\"]\n",
"\n",
"print(f\"First invocation:\\n{usage_1}\")\n",
"print(f\"\\nSecond:\\n{usage_2}\")"
]
},
{
"cell_type": "markdown",
"id": "1b550667-af5b-4557-b84f-c8f865dad6cb",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6033f3fa-0e96-46e3-abb3-1530928fea88",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\"Here's the German translation:\\n\\nIch liebe das Programmieren.\", additional_kwargs={}, response_metadata={'ResponseMetadata': {'RequestId': '1de3d7c0-8062-4f7e-bb8a-8f725b97a8b0', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Wed, 16 Apr 2025 19:32:51 GMT', 'content-type': 'application/json', 'content-length': '243', 'connection': 'keep-alive', 'x-amzn-requestid': '1de3d7c0-8062-4f7e-bb8a-8f725b97a8b0'}, 'RetryAttempts': 0}, 'stopReason': 'end_turn', 'metrics': {'latencyMs': [719]}, 'model_name': 'anthropic.claude-3-5-sonnet-20240620-v1:0'}, id='run-7021fcd7-704e-496b-a92e-210139614402-0', usage_metadata={'input_tokens': 23, 'output_tokens': 19, 'total_tokens': 42, 'input_token_details': {'cache_creation': 0, 'cache_read': 0}})"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n",
" ),\n",
" (\"human\", \"{input}\"),\n",
" ]\n",
")\n",
"\n",
"chain = prompt | llm\n",
"chain.invoke(\n",
" {\n",
" \"input_language\": \"English\",\n",
" \"output_language\": \"German\",\n",
" \"input\": \"I love programming.\",\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all ChatBedrock features and configurations head to the API reference: https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock.ChatBedrock.html\n",
"\n",
"For detailed documentation of all ChatBedrockConverse features and configurations head to the API reference: https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock_converse.ChatBedrockConverse.html"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -1,262 +1,393 @@
{
"cells": [
{
"cell_type": "raw",
"id": "30373ae2-f326-4e96-a1f7-062f57396886",
"metadata": {},
"source": [
"---\n",
"sidebar_label: Cloudflare Workers AI\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "f679592d",
"metadata": {},
"source": [
"# ChatCloudflareWorkersAI\n",
"\n",
"This will help you getting started with CloudflareWorkersAI [chat models](/docs/concepts/chat_models). For detailed documentation of all available Cloudflare WorkersAI models head to the [API reference](https://developers.cloudflare.com/workers-ai/).\n",
"\n",
"\n",
"## Overview\n",
"### Integration details\n",
"\n",
"| Class | Package | Local | Serializable | [JS support](https://js.langchain.com/docs/integrations/chat/cloudflare_workersai) | Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
"| ChatCloudflareWorkersAI | langchain-community| ❌ | ❌ | ✅ | ❌ | ❌ |\n",
"\n",
"### Model features\n",
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |\n",
"\n",
"## Setup\n",
"\n",
"- To access Cloudflare Workers AI models you'll need to create a Cloudflare account, get an account number and API key, and install the `langchain-community` package.\n",
"\n",
"\n",
"### Credentials\n",
"\n",
"\n",
"Head to [this document](https://developers.cloudflare.com/workers-ai/get-started/rest-api/) to sign up to Cloudflare Workers AI and generate an API key."
]
},
{
"cell_type": "markdown",
"id": "4a524cff",
"metadata": {},
"source": "To enable automated tracing of your model calls, set your [LangSmith](https://docs.smith.langchain.com/) API key:"
},
{
"cell_type": "code",
"execution_count": 3,
"id": "71b53c25",
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\"\n",
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")"
]
},
{
"cell_type": "markdown",
"id": "777a8526",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"The LangChain ChatCloudflareWorkersAI integration lives in the `langchain-community` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54990998",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-community"
]
},
{
"cell_type": "markdown",
"id": "629ba46f",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Now we can instantiate our model object and generate chat completions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec13c2d9",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.chat_models.cloudflare_workersai import ChatCloudflareWorkersAI\n",
"\n",
"llm = ChatCloudflareWorkersAI(\n",
" account_id=\"my_account_id\",\n",
" api_token=\"my_api_token\",\n",
" model=\"@hf/nousresearch/hermes-2-pro-mistral-7b\",\n",
")"
]
},
{
"cell_type": "markdown",
"id": "119b6732",
"metadata": {},
"source": [
"## Invocation"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2438a906",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-11-07 15:55:14 - INFO - Sending prompt to Cloudflare Workers AI: {'prompt': 'role: system, content: You are a helpful assistant that translates English to French. Translate the user sentence.\\nrole: user, content: I love programming.', 'tools': None}\n"
]
},
{
"data": {
"text/plain": [
"AIMessage(content='{\\'result\\': {\\'response\\': \\'Je suis un assistant virtuel qui peut traduire l\\\\\\'anglais vers le français. La phrase que vous avez dite est : \"J\\\\\\'aime programmer.\" En français, cela se traduit par : \"J\\\\\\'adore programmer.\"\\'}, \\'success\\': True, \\'errors\\': [], \\'messages\\': []}', additional_kwargs={}, response_metadata={}, id='run-838fd398-8594-4ca5-9055-03c72993caf6-0')"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n",
" ),\n",
" (\"human\", \"I love programming.\"),\n",
"]\n",
"ai_msg = llm.invoke(messages)\n",
"ai_msg"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1b4911bd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'result': {'response': 'Je suis un assistant virtuel qui peut traduire l\\'anglais vers le français. La phrase que vous avez dite est : \"J\\'aime programmer.\" En français, cela se traduit par : \"J\\'adore programmer.\"'}, 'success': True, 'errors': [], 'messages': []}\n"
]
}
],
"source": [
"print(ai_msg.content)"
]
},
{
"cell_type": "markdown",
"id": "111aa5d4",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b2a14282",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2024-11-07 15:55:24 - INFO - Sending prompt to Cloudflare Workers AI: {'prompt': 'role: system, content: You are a helpful assistant that translates English to German.\\nrole: user, content: I love programming.', 'tools': None}\n"
]
},
{
"data": {
"text/plain": [
"AIMessage(content=\"{'result': {'response': 'role: system, content: Das ist sehr nett zu hören! Programmieren lieben, ist eine interessante und anspruchsvolle Hobby- oder Berufsausrichtung. Wenn Sie englische Texte ins Deutsche übersetzen möchten, kann ich Ihnen helfen. Geben Sie bitte den englischen Satz oder die Übersetzung an, die Sie benötigen.'}, 'success': True, 'errors': [], 'messages': []}\", additional_kwargs={}, response_metadata={}, id='run-0d3be9a6-3d74-4dde-b49a-4479d6af00ef-0')"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n",
" ),\n",
" (\"human\", \"{input}\"),\n",
" ]\n",
")\n",
"\n",
"chain = prompt | llm\n",
"chain.invoke(\n",
" {\n",
" \"input_language\": \"English\",\n",
" \"output_language\": \"German\",\n",
" \"input\": \"I love programming.\",\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"id": "e1f311bd",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation on `ChatCloudflareWorkersAI` features and configuration options, please refer to the [API reference](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.cloudflare_workersai.html)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
"cells": [
{
"cell_type": "raw",
"id": "afaf8039",
"metadata": {},
"source": [
"---\n",
"sidebar_label: CloudflareWorkersAI\n",
"---"
]
},
"nbformat": 4,
"nbformat_minor": 5
{
"cell_type": "markdown",
"id": "e49f1e0d",
"metadata": {},
"source": [
"# ChatCloudflareWorkersAI\n",
"\n",
"\n",
"This will help you getting started with CloudflareWorkersAI [chat models](/docs/concepts/chat_models). For detailed documentation of all ChatCloudflareWorkersAI features and configurations head to the [API reference](https://python.langchain.com/docs/integrations/chat/cloudflare_workersai/).\n",
"\n",
"\n",
"## Overview\n",
"### Integration details\n",
"\n",
"\n",
"| Class | Package | Local | Serializable | [JS support](https://js.langchain.com/docs/integrations/chat/cloudflare) | Package downloads | Package latest |\n",
"| :--- | :--- |:-----:|:------------:|:------------------------------------------------------------------------:| :---: | :---: |\n",
"| [ChatCloudflareWorkersAI](https://python.langchain.com/docs/integrations/chat/cloudflare_workersai/) | [langchain-cloudflare](https://pypi.org/project/langchain-cloudflare/) | ✅ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-cloudflare?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-cloudflare?style=flat-square&label=%20) |\n",
"\n",
"### Model features\n",
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
"|:-----------------------------------------:|:----------------------------------------------------:|:---------:|:----------------------------------------------:|:-----------:|:-----------:|:-----------------------------------------------------:|:------------:|:------------------------------------------------------:|:----------------------------------:|\n",
"| ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ❌ | \n",
"\n",
"## Setup\n",
"\n",
"To access CloudflareWorkersAI models you'll need to create a/an CloudflareWorkersAI account, get an API key, and install the `langchain-cloudflare` integration package.\n",
"\n",
"### Credentials\n",
"\n",
"\n",
"Head to https://www.cloudflare.com/developer-platform/products/workers-ai/ to sign up to CloudflareWorkersAI and generate an API key. Once you've done this set the CF_API_KEY environment variable and the CF_ACCOUNT_ID environment variable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "433e8d2b-9519-4b49-b2c4-7ab65b046c94",
"metadata": {
"is_executing": true
},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"if not os.getenv(\"CF_API_KEY\"):\n",
" os.environ[\"CF_API_KEY\"] = getpass.getpass(\n",
" \"Enter your CloudflareWorkersAI API key: \"\n",
" )\n",
"\n",
"if not os.getenv(\"CF_ACCOUNT_ID\"):\n",
" os.environ[\"CF_ACCOUNT_ID\"] = getpass.getpass(\n",
" \"Enter your CloudflareWorkersAI account ID: \"\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "72ee0c4b-9764-423a-9dbf-95129e185210",
"metadata": {},
"source": [
"If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a15d341e-3e26-4ca3-830b-5aab30ed66de",
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\"\n",
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")"
]
},
{
"cell_type": "markdown",
"id": "0730d6a1-c893-4840-9817-5e5251676d5d",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"The LangChain CloudflareWorkersAI integration lives in the `langchain-cloudflare` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "652d6238-1f87-422a-b135-f5abbb8652fc",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-cloudflare"
]
},
{
"cell_type": "markdown",
"id": "a38cde65-254d-4219-a441-068766c0d4b5",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Now we can instantiate our model object and generate chat completions:\n",
"\n",
"- Update model instantiation with relevant params."
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
"metadata": {
"ExecuteTime": {
"end_time": "2025-04-07T17:48:31.193773Z",
"start_time": "2025-04-07T17:48:31.179196Z"
}
},
"outputs": [],
"source": [
"from langchain_cloudflare.chat_models import ChatCloudflareWorkersAI\n",
"\n",
"llm = ChatCloudflareWorkersAI(\n",
" model=\"@cf/meta/llama-3.3-70b-instruct-fp8-fast\",\n",
" temperature=0,\n",
" max_tokens=1024,\n",
" # other params...\n",
")"
]
},
{
"cell_type": "markdown",
"id": "2b4f3e15",
"metadata": {},
"source": [
"## Invocation\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "62e0dbc3",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\"J'adore la programmation.\", additional_kwargs={}, response_metadata={'token_usage': {'prompt_tokens': 37, 'completion_tokens': 9, 'total_tokens': 46}, 'model_name': '@cf/meta/llama-3.3-70b-instruct-fp8-fast'}, id='run-995d1970-b6be-49f3-99ae-af4cdba02304-0', usage_metadata={'input_tokens': 37, 'output_tokens': 9, 'total_tokens': 46})"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n",
" ),\n",
" (\"human\", \"I love programming.\"),\n",
"]\n",
"ai_msg = llm.invoke(messages)\n",
"ai_msg"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "d86145b3-bfef-46e8-b227-4dda5c9c2705",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"J'adore la programmation.\n"
]
}
],
"source": [
"print(ai_msg.content)"
]
},
{
"cell_type": "markdown",
"id": "18e2bfc0-7e78-4528-a73f-499ac150dca8",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='Ich liebe das Programmieren.', additional_kwargs={}, response_metadata={'token_usage': {'prompt_tokens': 32, 'completion_tokens': 7, 'total_tokens': 39}, 'model_name': '@cf/meta/llama-3.3-70b-instruct-fp8-fast'}, id='run-d1b677bc-194e-4473-90f1-aa65e8e46d50-0', usage_metadata={'input_tokens': 32, 'output_tokens': 7, 'total_tokens': 39})"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
"prompt = ChatPromptTemplate(\n",
" [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n",
" ),\n",
" (\"human\", \"{input}\"),\n",
" ]\n",
")\n",
"\n",
"chain = prompt | llm\n",
"chain.invoke(\n",
" {\n",
" \"input_language\": \"English\",\n",
" \"output_language\": \"German\",\n",
" \"input\": \"I love programming.\",\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"id": "d1ee55bc-ffc8-4cfa-801c-993953a08cfd",
"metadata": {},
"source": [
"## Structured Outputs"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "91cae406-14d7-46c9-b942-2d1476588423",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'setup': 'Why did the cat join a band?',\n",
" 'punchline': 'Because it wanted to be the purr-cussionist',\n",
" 'rating': '8'}"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"json_schema = {\n",
" \"title\": \"joke\",\n",
" \"description\": \"Joke to tell user.\",\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"setup\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The setup of the joke\",\n",
" },\n",
" \"punchline\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The punchline to the joke\",\n",
" },\n",
" \"rating\": {\n",
" \"type\": \"integer\",\n",
" \"description\": \"How funny the joke is, from 1 to 10\",\n",
" \"default\": None,\n",
" },\n",
" },\n",
" \"required\": [\"setup\", \"punchline\"],\n",
"}\n",
"structured_llm = llm.with_structured_output(json_schema)\n",
"\n",
"structured_llm.invoke(\"Tell me a joke about cats\")"
]
},
{
"cell_type": "markdown",
"id": "dbfc0c43-e76b-446e-bbb1-d351640bb7be",
"metadata": {},
"source": [
"## Bind tools"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "0765265e-4d00-4030-bf48-7e8d8c9af2ec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'name': 'validate_user',\n",
" 'args': {'user_id': '123',\n",
" 'addresses': '[\"123 Fake St in Boston MA\", \"234 Pretend Boulevard in Houston TX\"]'},\n",
" 'id': '31ec7d6a-9ce5-471b-be64-8ea0492d1387',\n",
" 'type': 'tool_call'}]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from typing import List\n",
"\n",
"from langchain_core.tools import tool\n",
"\n",
"\n",
"@tool\n",
"def validate_user(user_id: int, addresses: List[str]) -> bool:\n",
" \"\"\"Validate user using historical addresses.\n",
"\n",
" Args:\n",
" user_id (int): the user ID.\n",
" addresses (List[str]): Previous addresses as a list of strings.\n",
" \"\"\"\n",
" return True\n",
"\n",
"\n",
"llm_with_tools = llm.bind_tools([validate_user])\n",
"\n",
"result = llm_with_tools.invoke(\n",
" \"Could you validate user 123? They previously lived at \"\n",
" \"123 Fake St in Boston MA and 234 Pretend Boulevard in \"\n",
" \"Houston TX.\"\n",
")\n",
"result.tool_calls"
]
},
{
"cell_type": "markdown",
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"https://developers.cloudflare.com/workers-ai/\n",
"https://developers.cloudflare.com/agents/"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

View File

@@ -3,7 +3,9 @@
{
"cell_type": "raw",
"id": "59148044",
"metadata": {},
"metadata": {
"id": "59148044"
},
"source": [
"---\n",
"sidebar_label: LiteLLM\n",
@@ -11,120 +13,139 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "bf733a38-db84-4363-89e2-de6735c37230",
"metadata": {},
"id": "5bcea387",
"metadata": {
"id": "5bcea387"
},
"source": [
"# ChatLiteLLM\n",
"\n",
"[LiteLLM](https://github.com/BerriAI/litellm) is a library that simplifies calling Anthropic, Azure, Huggingface, Replicate, etc. \n",
"[LiteLLM](https://github.com/BerriAI/litellm) is a library that simplifies calling Anthropic, Azure, Huggingface, Replicate, etc.\n",
"\n",
"This notebook covers how to get started with using Langchain + the LiteLLM I/O library. "
"This notebook covers how to get started with using Langchain + the LiteLLM I/O library.\n",
"\n",
"## Overview\n",
"### Integration details\n",
"\n",
"| Class | Package | Local | Serializable | JS support| Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
"| [ChatLiteLLM](https://python.langchain.com/docs/integrations/chat/litellm/) | [langchain-litellm](https://pypi.org/project/langchain-litellm/)| ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-litellm?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-litellm?style=flat-square&label=%20) |\n",
"\n",
"### Model features\n",
"| [Tool calling](https://python.langchain.com/docs/how_to/tool_calling/) | [Structured output](https://python.langchain.com/docs/how_to/structured_output/) | JSON mode | Image input | Audio input | Video input | [Token-level streaming](https://python.langchain.com/docs/integrations/chat/litellm/#chatlitellm-also-supports-async-and-streaming-functionality) | [Native async](https://python.langchain.com/docs/integrations/chat/litellm/#chatlitellm-also-supports-async-and-streaming-functionality) | [Token usage](https://python.langchain.com/docs/how_to/chat_token_usage_tracking/) | [Logprobs](https://python.langchain.com/docs/how_to/logprobs/) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ |\n",
"\n",
"### Setup\n",
"To access ChatLiteLLM models you'll need to install the `langchain-litellm` package and create an OpenAI, Anthropic, Azure, Replicate, OpenRouter, Hugging Face, Together AI or Cohere account. Then you have to get an API key, and export it as an environment variable."
]
},
{
"cell_type": "markdown",
"id": "0a2f8164",
"metadata": {
"id": "0a2f8164"
},
"source": [
"## Credentials\n",
"\n",
"You have to choose the LLM provider you want and sign up with them to get their API key.\n",
"\n",
"### Example - Anthropic\n",
"Head to https://console.anthropic.com/ to sign up for Anthropic and generate an API key. Once you've done this set the ANTHROPIC_API_KEY environment variable.\n",
"\n",
"\n",
"### Example - OpenAI\n",
"Head to https://platform.openai.com/api-keys to sign up for OpenAI and generate an API key. Once you've done this set the OPENAI_API_KEY environment variable."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
"id": "7595eddf",
"metadata": {
"tags": []
"id": "7595eddf"
},
"outputs": [],
"source": [
"from langchain_community.chat_models import ChatLiteLLM\n",
"from langchain_core.messages import HumanMessage"
"## set ENV variables\n",
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"your-openai-key\"\n",
"os.environ[\"ANTHROPIC_API_KEY\"] = \"your-anthropic-key\""
]
},
{
"cell_type": "markdown",
"id": "74c3ad30",
"metadata": {
"id": "74c3ad30"
},
"source": [
"### Installation\n",
"\n",
"The LangChain LiteLLM integration lives in the `langchain-litellm` package:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
"id": "ca3f8a25",
"metadata": {
"tags": []
"id": "ca3f8a25"
},
"outputs": [],
"source": [
"chat = ChatLiteLLM(model=\"gpt-3.5-turbo\")"
"%pip install -qU langchain-litellm"
]
},
{
"cell_type": "markdown",
"id": "bc1182b4",
"metadata": {
"id": "bc1182b4"
},
"source": [
"## Instantiation\n",
"Now we can instantiate our model object and generate chat completions:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
"metadata": {
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\" J'aime la programmation.\", additional_kwargs={}, example=False)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"messages = [\n",
" HumanMessage(\n",
" content=\"Translate this sentence from English to French. I love programming.\"\n",
" )\n",
"]\n",
"chat(messages)"
"from langchain_litellm import ChatLiteLLM\n",
"\n",
"llm = ChatLiteLLM(model=\"gpt-3.5-turbo\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "c361ab1e-8c0c-4206-9e3c-9d1424a12b9c",
"metadata": {},
"id": "63d98454",
"metadata": {
"id": "63d98454"
},
"source": [
"## `ChatLiteLLM` also supports async and streaming functionality:"
"## Invocation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "93a21c5c-6ef9-4688-be60-b2e1f94842fb",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain_core.callbacks import CallbackManager, StreamingStdOutCallbackHandler"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c5fac0e9-05a4-4fc1-a3b3-e5bbb24b971b",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"LLMResult(generations=[[ChatGeneration(text=\" J'aime programmer.\", generation_info=None, message=AIMessage(content=\" J'aime programmer.\", additional_kwargs={}, example=False))]], llm_output={}, run=[RunInfo(run_id=UUID('8cc8fb68-1c35-439c-96a0-695036a93652'))])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await chat.agenerate([messages])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "025be980-e50d-4a68-93dc-c9c7b500ce34",
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
"outputId": "a4c0e5f5-a859-43fa-dd78-74fc0922ecb2",
"tags": []
},
"outputs": [
@@ -132,41 +153,75 @@
"name": "stdout",
"output_type": "stream",
"text": [
" J'aime la programmation."
"content='Neutral' additional_kwargs={} response_metadata={'token_usage': Usage(completion_tokens=2, prompt_tokens=30, total_tokens=32, completion_tokens_details=CompletionTokensDetailsWrapper(accepted_prediction_tokens=0, audio_tokens=0, reasoning_tokens=0, rejected_prediction_tokens=0, text_tokens=None), prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=0, cached_tokens=0, text_tokens=None, image_tokens=None)), 'model': 'gpt-3.5-turbo', 'finish_reason': 'stop', 'model_name': 'gpt-3.5-turbo'} id='run-ab6a3b21-eae8-4c27-acb2-add65a38221a-0' usage_metadata={'input_tokens': 30, 'output_tokens': 2, 'total_tokens': 32}\n"
]
},
{
"data": {
"text/plain": [
"AIMessage(content=\" J'aime la programmation.\", additional_kwargs={}, example=False)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat = ChatLiteLLM(\n",
" streaming=True,\n",
" verbose=True,\n",
" callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),\n",
"response = await llm.ainvoke(\n",
" \"Classify the text into neutral, negative or positive. Text: I think the food was okay. Sentiment:\"\n",
")\n",
"chat(messages)"
"print(response)"
]
},
{
"cell_type": "markdown",
"id": "c361ab1e-8c0c-4206-9e3c-9d1424a12b9c",
"metadata": {
"id": "c361ab1e-8c0c-4206-9e3c-9d1424a12b9c"
},
"source": [
"## `ChatLiteLLM` also supports async and streaming functionality:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c253883f",
"metadata": {},
"outputs": [],
"source": []
"execution_count": 5,
"id": "c5fac0e9-05a4-4fc1-a3b3-e5bbb24b971b",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "c5fac0e9-05a4-4fc1-a3b3-e5bbb24b971b",
"outputId": "ee8cdda1-d992-4696-9ad0-aa146360a3ee",
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Antibiotics are medications that fight bacterial infections in the body. They work by targeting specific bacteria and either killing them or preventing their growth and reproduction.\n",
"\n",
"There are several different mechanisms by which antibiotics work. Some antibiotics work by disrupting the cell walls of bacteria, causing them to burst and die. Others interfere with the protein synthesis of bacteria, preventing them from growing and reproducing. Some antibiotics target the DNA or RNA of bacteria, disrupting their ability to replicate.\n",
"\n",
"It is important to note that antibiotics only work against bacterial infections and not viral infections. It is also crucial to take antibiotics as prescribed by a healthcare professional and to complete the full course of treatment, even if symptoms improve before the medication is finished. This helps to prevent antibiotic resistance, where bacteria become resistant to the effects of antibiotics."
]
}
],
"source": [
"async for token in llm.astream(\"Hello, please explain how antibiotics work\"):\n",
" print(token.text(), end=\"\")"
]
},
{
"cell_type": "markdown",
"id": "88af2a9b",
"metadata": {
"id": "88af2a9b"
},
"source": [
"## API reference\n",
"For detailed documentation of all `ChatLiteLLM` features and configurations head to the API reference: https://github.com/Akshay-Dongare/langchain-litellm"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "g6_alda",
"language": "python",
"name": "python3"
},
@@ -180,7 +235,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.12.4"
}
},
"nbformat": 4,

View File

@@ -115,9 +115,7 @@
"source": [
"## Instantiation\n",
"\n",
"Now we can instantiate our model object and generate chat completions:\n",
"\n",
"- TODO: Update model instantiation with relevant params."
"Now we can instantiate our model object and generate chat completions:\n"
]
},
{

View File

@@ -408,7 +408,7 @@
"\n",
":::\n",
"\n",
"OpenAI supports a [Responses](https://platform.openai.com/docs/guides/responses-vs-chat-completions) API that is oriented toward building [agentic](/docs/concepts/agents/) applications. It includes a suite of [built-in tools](https://platform.openai.com/docs/guides/tools?api-mode=responses), including web and file search. It also supports management of [conversation state](https://platform.openai.com/docs/guides/conversation-state?api-mode=responses), allowing you to continue a conversational thread without explicitly passing in previous messages.\n",
"OpenAI supports a [Responses](https://platform.openai.com/docs/guides/responses-vs-chat-completions) API that is oriented toward building [agentic](/docs/concepts/agents/) applications. It includes a suite of [built-in tools](https://platform.openai.com/docs/guides/tools?api-mode=responses), including web and file search. It also supports management of [conversation state](https://platform.openai.com/docs/guides/conversation-state?api-mode=responses), allowing you to continue a conversational thread without explicitly passing in previous messages, as well as the output from [reasoning processes](https://platform.openai.com/docs/guides/reasoning?api-mode=responses).\n",
"\n",
"`ChatOpenAI` will route to the Responses API if one of these features is used. You can also specify `use_responses_api=True` when instantiating `ChatOpenAI`.\n",
"\n",
@@ -1056,6 +1056,77 @@
"print(second_response.text())"
]
},
{
"cell_type": "markdown",
"id": "67bf5bd2-0935-40a0-b1cd-c6662b681d4b",
"metadata": {},
"source": [
"### Reasoning output\n",
"\n",
"Some OpenAI models will generate separate text content illustrating their reasoning process. See OpenAI's [reasoning documentation](https://platform.openai.com/docs/guides/reasoning?api-mode=responses) for details.\n",
"\n",
"OpenAI can return a summary of the model's reasoning (although it doesn't expose the raw reasoning tokens). To configure `ChatOpenAI` to return this summary, specify the `reasoning` parameter:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8d322f3a-0732-45ab-ac95-dfd4596e0d85",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3^3 = 3 × 3 × 3 = 27.'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_openai import ChatOpenAI\n",
"\n",
"reasoning = {\n",
" \"effort\": \"medium\", # 'low', 'medium', or 'high'\n",
" \"summary\": \"auto\", # 'detailed', 'auto', or None\n",
"}\n",
"\n",
"llm = ChatOpenAI(\n",
" model=\"o4-mini\",\n",
" use_responses_api=True,\n",
" model_kwargs={\"reasoning\": reasoning},\n",
")\n",
"response = llm.invoke(\"What is 3^3?\")\n",
"\n",
"# Output\n",
"response.text()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d7dcc082-b7c8-41b7-a5e2-441b9679e41b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"**Calculating power of three**\n",
"\n",
"The user is asking for the result of 3 to the power of 3, which I know is 27. It's a straightforward question, so Ill keep my answer concise: 27. I could explain that this is the same as multiplying 3 by itself twice: 3 × 3 × 3 equals 27. However, since the user likely just needs the answer, Ill simply respond with 27.\n"
]
}
],
"source": [
"# Reasoning\n",
"reasoning = response.additional_kwargs[\"reasoning\"]\n",
"for block in reasoning[\"summary\"]:\n",
" print(block[\"text\"])"
]
},
{
"cell_type": "markdown",
"id": "57e27714",

View File

@@ -17,12 +17,66 @@
"source": [
"# ChatPerplexity\n",
"\n",
"This notebook covers how to get started with `Perplexity` chat models."
"\n",
"This page will help you get started with Perplexity [chat models](../../concepts/chat_models.mdx). For detailed documentation of all `ChatPerplexity` features and configurations head to the [API reference](https://python.langchain.com/api_reference/perplexity/chat_models/langchain_perplexity.chat_models.ChatPerplexity.html).\n",
"\n",
"## Overview\n",
"### Integration details\n",
"\n",
"| Class | Package | Local | Serializable | [JS support](https://js.langchain.com/docs/integrations/chat/xai) | Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n",
"| [ChatPerplexity](https://python.langchain.com/api_reference/perplexity/chat_models/langchain_perplexity.chat_models.ChatPerplexity.html) | [langchain-perplexity](https://python.langchain.com/api_reference/perplexity/perplexity.html) | ❌ | beta | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-perplexity?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-perplexity?style=flat-square&label=%20) |\n",
"\n",
"### Model features\n",
"| [Tool calling](../../how_to/tool_calling.ipynb) | [Structured output](../../how_to/structured_output.ipynb) | JSON mode | [Image input](../../how_to/multimodal_inputs.ipynb) | Audio input | Video input | [Token-level streaming](../../how_to/chat_streaming.ipynb) | Native async | [Token usage](../../how_to/chat_token_usage_tracking.ipynb) | [Logprobs](../../how_to/logprobs.ipynb) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ |\n",
"\n",
"## Setup\n",
"\n",
"To access Perplexity models you'll need to create a Perplexity account, get an API key, and install the `langchain-perplexity` integration package.\n",
"\n",
"### Credentials\n",
"\n",
"Head to [this page](https://www.perplexity.ai/) to sign up for Perplexity and generate an API key. Once you've done this set the `PPLX_API_KEY` environment variable:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "2243f329",
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"if \"PPLX_API_KEY\" not in os.environ:\n",
" os.environ[\"PPLX_API_KEY\"] = getpass.getpass(\"Enter your Perplexity API key: \")"
]
},
{
"cell_type": "markdown",
"id": "7dfe47c4",
"metadata": {},
"source": [
"To enable automated tracing of your model calls, set your [LangSmith](https://docs.smith.langchain.com/) API key:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10a791fa",
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")\n",
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
"metadata": {
"ExecuteTime": {
@@ -33,8 +87,8 @@
},
"outputs": [],
"source": [
"from langchain_community.chat_models import ChatPerplexity\n",
"from langchain_core.prompts import ChatPromptTemplate"
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_perplexity import ChatPerplexity"
]
},
{
@@ -62,29 +116,9 @@
"id": "97a8ce3a",
"metadata": {},
"source": [
"The code provided assumes that your PPLX_API_KEY is set in your environment variables. If you would like to manually specify your API key and also choose a different model, you can use the following code:\n",
"\n",
"```python\n",
"chat = ChatPerplexity(temperature=0, pplx_api_key=\"YOUR_API_KEY\", model=\"llama-3.1-sonar-small-128k-online\")\n",
"```\n",
"\n",
"You can check a list of available models [here](https://docs.perplexity.ai/docs/model-cards). For reproducibility, we can set the API key dynamically by taking it as an input in this notebook."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d3e49d78",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from getpass import getpass\n",
"\n",
"PPLX_API_KEY = getpass()\n",
"os.environ[\"PPLX_API_KEY\"] = PPLX_API_KEY"
]
},
{
"cell_type": "code",
"execution_count": 3,
@@ -305,7 +339,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": ".venv",
"language": "python",
"name": "python3"
},
@@ -319,7 +353,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.11.11"
}
},
"nbformat": 4,

View File

@@ -0,0 +1,284 @@
{
"cells": [
{
"cell_type": "raw",
"id": "afaf8039",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"sidebar_label: Qwen QwQ\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "e49f1e0d",
"metadata": {},
"source": [
"# ChatQwQ\n",
"\n",
"This will help you getting started with QwQ [chat models](../../concepts/chat_models.mdx). For detailed documentation of all ChatQwQ features and configurations head to the [API reference](https://pypi.org/project/langchain-qwq/).\n",
"\n",
"## Overview\n",
"### Integration details\n",
"\n",
"\n",
"| Class | Package | Local | Serializable | Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: |\n",
"| [ChatQwQ](https://pypi.org/project/langchain-qwq/) | [langchain-qwq](https://pypi.org/project/langchain-qwq/) | ❌ | beta | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-qwq?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-qwq?style=flat-square&label=%20) |\n",
"\n",
"### Model features\n",
"| [Tool calling](../../how_to/tool_calling.ipynb) | [Structured output](../../how_to/structured_output.ipynb) | JSON mode | [Image input](../../how_to/multimodal_inputs.ipynb) | Audio input | Video input | [Token-level streaming](../../how_to/chat_streaming.ipynb) | Native async | [Token usage](../../how_to/chat_token_usage_tracking.ipynb) | [Logprobs](../../how_to/logprobs.ipynb) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| ✅ | ✅ | ✅ |❌ | ❌ | ❌ | ✅ | ✅ | ✅ | ❌ | \n",
"\n",
"## Setup\n",
"\n",
"To access QwQ models you'll need to create an Alibaba Cloud account, get an API key, and install the `langchain-qwq` integration package.\n",
"\n",
"### Credentials\n",
"\n",
"Head to [Alibaba's API Key page](https://account.alibabacloud.com/login/login.htm?oauth_callback=https%3A%2F%2Fbailian.console.alibabacloud.com%2F%3FapiKey%3D1&lang=en#/api-key) to sign up to Alibaba Cloud and generate an API key. Once you've done this set the `DASHSCOPE_API_KEY` environment variable:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "433e8d2b-9519-4b49-b2c4-7ab65b046c94",
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"if not os.getenv(\"DASHSCOPE_API_KEY\"):\n",
" os.environ[\"DASHSCOPE_API_KEY\"] = getpass.getpass(\"Enter your Dashscope API key: \")"
]
},
{
"cell_type": "markdown",
"id": "0730d6a1-c893-4840-9817-5e5251676d5d",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"The LangChain QwQ integration lives in the `langchain-qwq` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "652d6238-1f87-422a-b135-f5abbb8652fc",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-qwq"
]
},
{
"cell_type": "markdown",
"id": "a38cde65-254d-4219-a441-068766c0d4b5",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Now we can instantiate our model object and generate chat completions:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
"metadata": {},
"outputs": [],
"source": [
"from langchain_qwq import ChatQwQ\n",
"\n",
"llm = ChatQwQ(\n",
" model=\"qwq-plus\",\n",
" max_tokens=3_000,\n",
" timeout=None,\n",
" max_retries=2,\n",
" # other params...\n",
")"
]
},
{
"cell_type": "markdown",
"id": "2b4f3e15",
"metadata": {},
"source": [
"## Invocation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "62e0dbc3",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content=\"J'aime la programmation.\", additional_kwargs={'reasoning_content': 'Okay, the user wants me to translate \"I love programming.\" into French. Let\\'s start by breaking down the sentence. The subject is \"I\", which in French is \"Je\". The verb is \"love\", which in this context is present tense, so \"aime\". The object is \"programming\". Now, \"programming\" in French can be \"la programmation\". \\n\\nWait, should it be \"programmation\" or \"programmation\"? Let me confirm the spelling. Yes, \"programmation\" is correct. Now, putting it all together: \"Je aime la programmation.\" Hmm, but in French, there\\'s a tendency to contract \"je\" and \"aime\". Wait, actually, \"je\" followed by a vowel sound usually takes \"j\\'\". So it should be \"J\\'aime la programmation.\" \\n\\nLet me double-check. \"J\\'aime\" is the correct contraction for \"I love\". The definite article \"la\" is needed because \"programmation\" is a feminine noun. Yes, \"programmation\" is a feminine noun, so \"la\" is correct. \\n\\nIs there any other way to say it? Maybe \"J\\'adore la programmation\" for \"I love\" in a stronger sense, but the user didn\\'t specify the intensity. Since the original is straightforward, \"J\\'aime la programmation.\" is the direct translation. \\n\\nI think that\\'s it. No mistakes there. So the final translation should be \"J\\'aime la programmation.\"'}, response_metadata={'model_name': 'qwq-plus'}, id='run-5045cd6a-edbd-4b2f-bf24-b7bdf3777fb9-0', usage_metadata={'input_tokens': 32, 'output_tokens': 326, 'total_tokens': 358, 'input_token_details': {}, 'output_token_details': {}})"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates English to French.\"\n",
" \"Translate the user sentence.\",\n",
" ),\n",
" (\"human\", \"I love programming.\"),\n",
"]\n",
"ai_msg = llm.invoke(messages)\n",
"ai_msg"
]
},
{
"cell_type": "markdown",
"id": "18e2bfc0-7e78-4528-a73f-499ac150dca8",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"We can [chain](../../how_to/sequence.ipynb) our model with a prompt template like so:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='Ich liebe das Programmieren.', additional_kwargs={'reasoning_content': 'Okay, the user wants me to translate \"I love programming.\" into German. Let me think. The verb \"love\" is \"lieben\" or \"mögen\" in German, but \"lieben\" is more like love, while \"mögen\" is prefer. Since it\\'s about programming, which is a strong affection, \"lieben\" is better. The subject is \"I\", which is \"ich\". Then \"programming\" is \"Programmierung\" or \"Coding\". But \"Programmierung\" is more formal. Alternatively, sometimes people say \"ich liebe es zu programmieren\" which is \"I love to program\". Hmm, maybe the direct translation would be \"Ich liebe die Programmierung.\" But maybe the more natural way is \"Ich liebe es zu programmieren.\" Let me check. Both are correct, but the second one might sound more natural in everyday speech. The user might prefer the concise version. Alternatively, maybe \"Ich liebe die Programmierung.\" is better. Wait, the original is \"programming\" as a noun. So using the noun form would be appropriate. So \"Ich liebe die Programmierung.\" But sometimes people also use \"Coding\" in German, like \"Ich liebe das Coding.\" But that\\'s more anglicism. Probably better to stick with \"Programmierung\". Alternatively, \"Programmieren\" as a noun. Oh right! \"Programmieren\" can be a noun when used in the accusative case. So \"Ich liebe das Programmieren.\" That\\'s correct and natural. Yes, that\\'s the best translation. So the answer is \"Ich liebe das Programmieren.\"'}, response_metadata={'model_name': 'qwq-plus'}, id='run-2c418451-51d8-4319-8269-2ce129363a1a-0', usage_metadata={'input_tokens': 28, 'output_tokens': 341, 'total_tokens': 369, 'input_token_details': {}, 'output_token_details': {}})"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
"prompt = ChatPromptTemplate(\n",
" [\n",
" (\n",
" \"system\",\n",
" \"You are a helpful assistant that translates\"\n",
" \"{input_language} to {output_language}.\",\n",
" ),\n",
" (\"human\", \"{input}\"),\n",
" ]\n",
")\n",
"\n",
"chain = prompt | llm\n",
"chain.invoke(\n",
" {\n",
" \"input_language\": \"English\",\n",
" \"output_language\": \"German\",\n",
" \"input\": \"I love programming.\",\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"id": "8d1b3ef3",
"metadata": {},
"source": [
"## Tool Calling\n",
"ChatQwQ supports tool calling API that lets you describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool."
]
},
{
"cell_type": "markdown",
"id": "6db1a355",
"metadata": {},
"source": [
"### Use with `bind_tools`"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "15fb6a6d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"content='' additional_kwargs={'reasoning_content': 'Okay, the user is asking \"What\\'s 5 times forty two\". Let me break this down. First, I need to identify the numbers involved. The first number is 5, which is straightforward. The second number is forty two, which is 42 in digits. The operation they want is multiplication.\\n\\nLooking at the tools provided, there\\'s a function called multiply that takes two integers. So I should use that. The parameters are first_int and second_int. \\n\\nI need to convert \"forty two\" to 42. Since the function requires integers, both numbers should be in integer form. So 5 and 42. \\n\\nNow, I\\'ll structure the tool call. The function name is multiply, and the arguments should be first_int: 5 and second_int: 42. I\\'ll make sure the JSON is correctly formatted without any syntax errors. Let me double-check the parameters to ensure they\\'re required and of the right type. Yep, both are required and integers. \\n\\nNo examples were provided, but the function\\'s purpose is clear. So the correct tool call should be to multiply those two numbers. I think that\\'s all. No other functions are needed here.'} response_metadata={'model_name': 'qwq-plus'} id='run-638895aa-fdde-4567-bcfa-7d8e5d4f24af-0' tool_calls=[{'name': 'multiply', 'args': {'first_int': 5, 'second_int': 42}, 'id': 'call_d088275851c140529ed2ad', 'type': 'tool_call'}] usage_metadata={'input_tokens': 176, 'output_tokens': 277, 'total_tokens': 453, 'input_token_details': {}, 'output_token_details': {}}\n"
]
}
],
"source": [
"from langchain_core.tools import tool\n",
"from langchain_qwq import ChatQwQ\n",
"\n",
"\n",
"@tool\n",
"def multiply(first_int: int, second_int: int) -> int:\n",
" \"\"\"Multiply two integers together.\"\"\"\n",
" return first_int * second_int\n",
"\n",
"\n",
"llm = ChatQwQ()\n",
"\n",
"llm_with_tools = llm.bind_tools([multiply])\n",
"\n",
"msg = llm_with_tools.invoke(\"What's 5 times forty two\")\n",
"\n",
"print(msg)"
]
},
{
"cell_type": "markdown",
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all ChatQwQ features and configurations head to the [API reference](https://pypi.org/project/langchain-qwq/)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,276 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# RunPod Chat Model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get started with RunPod chat models.\n",
"\n",
"## Overview\n",
"\n",
"This guide covers how to use the LangChain `ChatRunPod` class to interact with chat models hosted on [RunPod Serverless](https://www.runpod.io/serverless-gpu)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"1. **Install the package:**\n",
" ```bash\n",
" pip install -qU langchain-runpod\n",
" ```\n",
"2. **Deploy a Chat Model Endpoint:** Follow the setup steps in the [RunPod Provider Guide](/docs/integrations/providers/runpod#setup) to deploy a compatible chat model endpoint on RunPod Serverless and get its Endpoint ID.\n",
"3. **Set Environment Variables:** Make sure `RUNPOD_API_KEY` and `RUNPOD_ENDPOINT_ID` (or a specific `RUNPOD_CHAT_ENDPOINT_ID`) are set."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"# Make sure environment variables are set (or pass them directly to ChatRunPod)\n",
"if \"RUNPOD_API_KEY\" not in os.environ:\n",
" os.environ[\"RUNPOD_API_KEY\"] = getpass.getpass(\"Enter your RunPod API Key: \")\n",
"\n",
"if \"RUNPOD_ENDPOINT_ID\" not in os.environ:\n",
" os.environ[\"RUNPOD_ENDPOINT_ID\"] = input(\n",
" \"Enter your RunPod Endpoint ID (used if RUNPOD_CHAT_ENDPOINT_ID is not set): \"\n",
" )\n",
"\n",
"# Optionally use a different endpoint ID specifically for chat models\n",
"# if \"RUNPOD_CHAT_ENDPOINT_ID\" not in os.environ:\n",
"# os.environ[\"RUNPOD_CHAT_ENDPOINT_ID\"] = input(\"Enter your RunPod Chat Endpoint ID (Optional): \")\n",
"\n",
"chat_endpoint_id = os.environ.get(\n",
" \"RUNPOD_CHAT_ENDPOINT_ID\", os.environ.get(\"RUNPOD_ENDPOINT_ID\")\n",
")\n",
"if not chat_endpoint_id:\n",
" raise ValueError(\n",
" \"No RunPod Endpoint ID found. Please set RUNPOD_ENDPOINT_ID or RUNPOD_CHAT_ENDPOINT_ID.\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Initialize the `ChatRunPod` class. You can pass model-specific parameters via `model_kwargs` and configure polling behavior."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain_runpod import ChatRunPod\n",
"\n",
"chat = ChatRunPod(\n",
" runpod_endpoint_id=chat_endpoint_id, # Specify the correct endpoint ID\n",
" model_kwargs={\n",
" \"max_new_tokens\": 512,\n",
" \"temperature\": 0.7,\n",
" \"top_p\": 0.9,\n",
" # Add other parameters supported by your endpoint handler\n",
" },\n",
" # Optional: Adjust polling\n",
" # poll_interval=0.2,\n",
" # max_polling_attempts=150\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Invocation\n",
"\n",
"Use the standard LangChain `.invoke()` and `.ainvoke()` methods to call the model. Streaming is also supported via `.stream()` and `.astream()` (simulated by polling the RunPod `/stream` endpoint)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain_core.messages import HumanMessage, SystemMessage\n",
"\n",
"messages = [\n",
" SystemMessage(content=\"You are a helpful AI assistant.\"),\n",
" HumanMessage(content=\"What is the RunPod Serverless API flow?\"),\n",
"]\n",
"\n",
"# Invoke (Sync)\n",
"try:\n",
" response = chat.invoke(messages)\n",
" print(\"--- Sync Invoke Response ---\")\n",
" print(response.content)\n",
"except Exception as e:\n",
" print(\n",
" f\"Error invoking Chat Model: {e}. Ensure endpoint ID/API key are correct and endpoint is active/compatible.\"\n",
" )\n",
"\n",
"# Stream (Sync, simulated via polling /stream)\n",
"print(\"\\n--- Sync Stream Response ---\")\n",
"try:\n",
" for chunk in chat.stream(messages):\n",
" print(chunk.content, end=\"\", flush=True)\n",
" print() # Newline\n",
"except Exception as e:\n",
" print(\n",
" f\"\\nError streaming Chat Model: {e}. Ensure endpoint handler supports streaming output format.\"\n",
" )\n",
"\n",
"### Async Usage\n",
"\n",
"# AInvoke (Async)\n",
"try:\n",
" async_response = await chat.ainvoke(messages)\n",
" print(\"--- Async Invoke Response ---\")\n",
" print(async_response.content)\n",
"except Exception as e:\n",
" print(f\"Error invoking Chat Model asynchronously: {e}.\")\n",
"\n",
"# AStream (Async)\n",
"print(\"\\n--- Async Stream Response ---\")\n",
"try:\n",
" async for chunk in chat.astream(messages):\n",
" print(chunk.content, end=\"\", flush=True)\n",
" print() # Newline\n",
"except Exception as e:\n",
" print(\n",
" f\"\\nError streaming Chat Model asynchronously: {e}. Ensure endpoint handler supports streaming output format.\\n\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"The chat model integrates seamlessly with LangChain Expression Language (LCEL) chains."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain_core.output_parsers import StrOutputParser\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\"system\", \"You are a helpful assistant.\"),\n",
" (\"human\", \"{input}\"),\n",
" ]\n",
")\n",
"\n",
"parser = StrOutputParser()\n",
"\n",
"chain = prompt | chat | parser\n",
"\n",
"try:\n",
" chain_response = chain.invoke(\n",
" {\"input\": \"Explain the concept of serverless computing in simple terms.\"}\n",
" )\n",
" print(\"--- Chain Response ---\")\n",
" print(chain_response)\n",
"except Exception as e:\n",
" print(f\"Error running chain: {e}\")\n",
"\n",
"\n",
"# Async chain\n",
"try:\n",
" async_chain_response = await chain.ainvoke(\n",
" {\"input\": \"What are the benefits of using RunPod for AI/ML workloads?\"}\n",
" )\n",
" print(\"--- Async Chain Response ---\")\n",
" print(async_chain_response)\n",
"except Exception as e:\n",
" print(f\"Error running async chain: {e}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Model Features (Endpoint Dependent)\n",
"\n",
"The availability of advanced features depends **heavily** on the specific implementation of your RunPod endpoint handler. The `ChatRunPod` integration provides the basic framework, but the handler must support the underlying functionality.\n",
"\n",
"| Feature | Integration Support | Endpoint Dependent? | Notes |\n",
"| :--------------------------------------------------------- | :-----------------: | :-----------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n",
"| [Tool calling](/docs/how_to/tool_calling) | ❌ | ✅ | Requires handler to process tool definitions and return tool calls (e.g., OpenAI format). Integration needs parsing logic. |\n",
"| [Structured output](/docs/how_to/structured_output) | ❌ | ✅ | Requires handler support for forcing structured output (JSON mode, function calling). Integration needs parsing logic. |\n",
"| JSON mode | ❌ | ✅ | Requires handler to accept a `json_mode` parameter (or similar) and guarantee JSON output. |\n",
"| [Image input](/docs/how_to/multimodal_inputs) | ❌ | ✅ | Requires multimodal handler accepting image data (e.g., base64). Integration does not support multimodal messages. |\n",
"| Audio input | ❌ | ✅ | Requires handler accepting audio data. Integration does not support audio messages. |\n",
"| Video input | ❌ | ✅ | Requires handler accepting video data. Integration does not support video messages. |\n",
"| [Token-level streaming](/docs/how_to/chat_streaming) | ✅ (Simulated) | ✅ | Polls `/stream`. Requires handler to populate `stream` list in status response with token chunks (e.g., `[{\"output\": \"token\"}]`). True low-latency streaming not built-in. |\n",
"| Native async | ✅ | ✅ | Core `ainvoke`/`astream` implemented. Relies on endpoint handler performance. |\n",
"| [Token usage](/docs/how_to/chat_token_usage_tracking) | ❌ | ✅ | Requires handler to return `prompt_tokens`, `completion_tokens` in the final response. Integration currently does not parse this. |\n",
"| [Logprobs](/docs/how_to/logprobs) | ❌ | ✅ | Requires handler to return log probabilities. Integration currently does not parse this. |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Key Takeaway:** Standard chat invocation and simulated streaming work if the endpoint follows basic RunPod API conventions. Advanced features require specific handler implementations and potentially extending or customizing this integration package."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of the `ChatRunPod` class, parameters, and methods, refer to the source code or the generated API reference (if available).\n",
"\n",
"Link to source code: [https://github.com/runpod/langchain-runpod/blob/main/langchain_runpod/chat_models.py](https://github.com/runpod/langchain-runpod/blob/main/langchain_runpod/chat_models.py)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -0,0 +1,362 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "62d5a1ea",
"metadata": {},
"source": [
"# ChatSeekrFlow\n",
"\n",
"> [Seekr](https://www.seekr.com/) provides AI-powered solutions for structured, explainable, and transparent AI interactions.\n",
"\n",
"This notebook provides a quick overview for getting started with Seekr [chat models](/docs/concepts/chat_models). For detailed documentation of all `ChatSeekrFlow` features and configurations, head to the [API reference](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.seekrflow.ChatSeekrFlow.html).\n",
"\n",
"## Overview\n",
"\n",
"`ChatSeekrFlow` class wraps a chat model endpoint hosted on SeekrFlow, enabling seamless integration with LangChain applications.\n",
"\n",
"### Integration Details\n",
"\n",
"| Class | Package | Local | Serializable | Package downloads | Package latest |\n",
"| :--- | :--- | :---: | :---: | :---: | :---: |\n",
"| [ChatSeekrFlow](https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.seekrflow.ChatSeekrFlow.html) | [seekrai](https://python.langchain.com/docs/integrations/providers/seekr/) | ❌ | beta | ![PyPI - Downloads](https://img.shields.io/pypi/dm/seekrai?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/seekrai?style=flat-square&label=%20) |\n",
"\n",
"### Model Features\n",
"\n",
"| [Tool calling](/docs/how_to/tool_calling/) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| ✅ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ | ✅ | ❌ |\n",
"\n",
"### Supported Methods\n",
"`ChatSeekrFlow` supports all methods of `ChatModel`, **except async APIs**.\n",
"\n",
"### Endpoint Requirements\n",
"\n",
"The serving endpoint `ChatSeekrFlow` wraps **must** have OpenAI-compatible chat input/output format. It can be used for:\n",
"1. **Fine-tuned Seekr models**\n",
"2. **Custom SeekrFlow models**\n",
"3. **RAG-enabled models using Seekr's retrieval system**\n",
"\n",
"For async usage, please refer to `AsyncChatSeekrFlow` (coming soon).\n"
]
},
{
"cell_type": "markdown",
"id": "93fea471",
"metadata": {},
"source": [
"# Getting Started with ChatSeekrFlow in LangChain\n",
"\n",
"This notebook covers how to use SeekrFlow as a chat model in LangChain."
]
},
{
"cell_type": "markdown",
"id": "2f320c17",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"Ensure you have the necessary dependencies installed:\n",
"\n",
"```bash\n",
"pip install seekrai langchain langchain-community\n",
"```\n",
"\n",
"You must also have an API key from Seekr to authenticate requests.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "911ca53c",
"metadata": {},
"outputs": [],
"source": [
"# Standard library\n",
"import getpass\n",
"import os\n",
"\n",
"# Third-party\n",
"from langchain.prompts import ChatPromptTemplate\n",
"from langchain.schema import HumanMessage\n",
"from langchain_core.runnables import RunnableSequence\n",
"\n",
"# OSS SeekrFlow integration\n",
"from langchain_seekrflow import ChatSeekrFlow\n",
"from seekrai import SeekrFlow"
]
},
{
"cell_type": "markdown",
"id": "150461cb",
"metadata": {},
"source": [
"## API Key Setup\n",
"\n",
"You'll need to set your API key as an environment variable to authenticate requests.\n",
"\n",
"Run the below cell.\n",
"\n",
"Or manually assign it before running queries:\n",
"\n",
"```python\n",
"SEEKR_API_KEY = \"your-api-key-here\"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "38afcd6e",
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"SEEKR_API_KEY\"] = getpass.getpass(\"Enter your Seekr API key:\")"
]
},
{
"cell_type": "markdown",
"id": "82d83c0e",
"metadata": {},
"source": [
"## Instantiation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71b14751",
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"SEEKR_API_KEY\"]\n",
"seekr_client = SeekrFlow(api_key=SEEKR_API_KEY)\n",
"\n",
"llm = ChatSeekrFlow(\n",
" client=seekr_client, model_name=\"meta-llama/Meta-Llama-3-8B-Instruct\"\n",
")"
]
},
{
"cell_type": "markdown",
"id": "1046e86c",
"metadata": {},
"source": [
"## Invocation"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f61a60f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello there! I'm Seekr, nice to meet you! What brings you here today? Do you have a question, or are you looking for some help with something? I'm all ears (or rather, all text)!\n"
]
}
],
"source": [
"response = llm.invoke([HumanMessage(content=\"Hello, Seekr!\")])\n",
"print(response.content)"
]
},
{
"cell_type": "markdown",
"id": "853b0349",
"metadata": {},
"source": [
"## Chaining"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "35fca3ec",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"content='The translation of \"Good morning\" in French is:\\n\\n\"Bonne journée\"' additional_kwargs={} response_metadata={}\n"
]
}
],
"source": [
"prompt = ChatPromptTemplate.from_template(\"Translate to French: {text}\")\n",
"\n",
"chain: RunnableSequence = prompt | llm\n",
"result = chain.invoke({\"text\": \"Good morning\"})\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a7b28b8d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"🔹 Testing Sync `stream()` (Streaming)...\n",
"Here is a haiku:\n",
"\n",
"Golden sunset fades\n",
"Ripples on the quiet lake\n",
"Peaceful evening sky"
]
}
],
"source": [
"def test_stream():\n",
" \"\"\"Test synchronous invocation in streaming mode.\"\"\"\n",
" print(\"\\n🔹 Testing Sync `stream()` (Streaming)...\")\n",
"\n",
" for chunk in llm.stream([HumanMessage(content=\"Write me a haiku.\")]):\n",
" print(chunk.content, end=\"\", flush=True)\n",
"\n",
"\n",
"# ✅ Ensure streaming is enabled\n",
"llm = ChatSeekrFlow(\n",
" client=seekr_client,\n",
" model_name=\"meta-llama/Meta-Llama-3-8B-Instruct\",\n",
" streaming=True, # ✅ Enable streaming\n",
")\n",
"\n",
"# ✅ Run sync streaming test\n",
"test_stream()"
]
},
{
"cell_type": "markdown",
"id": "b3847b34",
"metadata": {},
"source": [
"## Error Handling & Debugging"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6bc38b48",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running test: Missing Client\n",
"✅ Expected Error: SeekrFlow client cannot be None.\n",
"Running test: Missing Model Name\n",
"✅ Expected Error: A valid model name must be provided.\n"
]
}
],
"source": [
"# Define a minimal mock SeekrFlow client\n",
"class MockSeekrClient:\n",
" \"\"\"Mock SeekrFlow API client that mimics the real API structure.\"\"\"\n",
"\n",
" class MockChat:\n",
" \"\"\"Mock Chat object with a completions method.\"\"\"\n",
"\n",
" class MockCompletions:\n",
" \"\"\"Mock Completions object with a create method.\"\"\"\n",
"\n",
" def create(self, *args, **kwargs):\n",
" return {\n",
" \"choices\": [{\"message\": {\"content\": \"Mock response\"}}]\n",
" } # Mimic API response\n",
"\n",
" completions = MockCompletions()\n",
"\n",
" chat = MockChat()\n",
"\n",
"\n",
"def test_initialization_errors():\n",
" \"\"\"Test that invalid ChatSeekrFlow initializations raise expected errors.\"\"\"\n",
"\n",
" test_cases = [\n",
" {\n",
" \"name\": \"Missing Client\",\n",
" \"args\": {\"client\": None, \"model_name\": \"seekrflow-model\"},\n",
" \"expected_error\": \"SeekrFlow client cannot be None.\",\n",
" },\n",
" {\n",
" \"name\": \"Missing Model Name\",\n",
" \"args\": {\"client\": MockSeekrClient(), \"model_name\": \"\"},\n",
" \"expected_error\": \"A valid model name must be provided.\",\n",
" },\n",
" ]\n",
"\n",
" for test in test_cases:\n",
" try:\n",
" print(f\"Running test: {test['name']}\")\n",
" faulty_llm = ChatSeekrFlow(**test[\"args\"])\n",
"\n",
" # If no error is raised, fail the test\n",
" print(f\"❌ Test '{test['name']}' failed: No error was raised!\")\n",
" except Exception as e:\n",
" error_msg = str(e)\n",
" assert test[\"expected_error\"] in error_msg, f\"Unexpected error: {error_msg}\"\n",
" print(f\"✅ Expected Error: {error_msg}\")\n",
"\n",
"\n",
"# Run test\n",
"test_initialization_errors()"
]
},
{
"cell_type": "markdown",
"id": "d1c9ddf3",
"metadata": {},
"source": [
"## API reference"
]
},
{
"cell_type": "markdown",
"id": "411a8bea",
"metadata": {},
"source": [
"- `ChatSeekrFlow` class: [`langchain_seekrflow.ChatSeekrFlow`](https://github.com/benfaircloth/langchain-seekrflow/blob/main/langchain_seekrflow/seekrflow.py)\n",
"- PyPI package: [`langchain-seekrflow`](https://pypi.org/project/langchain-seekrflow/)\n"
]
},
{
"cell_type": "markdown",
"id": "3ef00a51",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -2669,7 +2669,7 @@
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain_couchbase couchbase"
"%pip install -qU langchain_couchbase"
]
},
{

View File

@@ -0,0 +1,266 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# RunPod LLM"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get started with RunPod LLMs.\n",
"\n",
"## Overview\n",
"\n",
"This guide covers how to use the LangChain `RunPod` LLM class to interact with text generation models hosted on [RunPod Serverless](https://www.runpod.io/serverless-gpu)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"1. **Install the package:**\n",
" ```bash\n",
" pip install -qU langchain-runpod\n",
" ```\n",
"2. **Deploy an LLM Endpoint:** Follow the setup steps in the [RunPod Provider Guide](/docs/integrations/providers/runpod#setup) to deploy a compatible text generation endpoint on RunPod Serverless and get its Endpoint ID.\n",
"3. **Set Environment Variables:** Make sure `RUNPOD_API_KEY` and `RUNPOD_ENDPOINT_ID` are set."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"# Make sure environment variables are set (or pass them directly to RunPod)\n",
"if \"RUNPOD_API_KEY\" not in os.environ:\n",
" os.environ[\"RUNPOD_API_KEY\"] = getpass.getpass(\"Enter your RunPod API Key: \")\n",
"if \"RUNPOD_ENDPOINT_ID\" not in os.environ:\n",
" os.environ[\"RUNPOD_ENDPOINT_ID\"] = input(\"Enter your RunPod Endpoint ID: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"Initialize the `RunPod` class. You can pass model-specific parameters via `model_kwargs` and configure polling behavior."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain_runpod import RunPod\n",
"\n",
"llm = RunPod(\n",
" # runpod_endpoint_id can be passed here if not set in env\n",
" model_kwargs={\n",
" \"max_new_tokens\": 256,\n",
" \"temperature\": 0.6,\n",
" \"top_k\": 50,\n",
" # Add other parameters supported by your endpoint handler\n",
" },\n",
" # Optional: Adjust polling\n",
" # poll_interval=0.3,\n",
" # max_polling_attempts=100\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Invocation\n",
"\n",
"Use the standard LangChain `.invoke()` and `.ainvoke()` methods to call the model. Streaming is also supported via `.stream()` and `.astream()` (simulated by polling the RunPod `/stream` endpoint)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"prompt = \"Write a tagline for an ice cream shop on the moon.\"\n",
"\n",
"# Invoke (Sync)\n",
"try:\n",
" response = llm.invoke(prompt)\n",
" print(\"--- Sync Invoke Response ---\")\n",
" print(response)\n",
"except Exception as e:\n",
" print(\n",
" f\"Error invoking LLM: {e}. Ensure endpoint ID/API key are correct and endpoint is active/compatible.\"\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"# Stream (Sync, simulated via polling /stream)\n",
"print(\"\\n--- Sync Stream Response ---\")\n",
"try:\n",
" for chunk in llm.stream(prompt):\n",
" print(chunk, end=\"\", flush=True)\n",
" print() # Newline\n",
"except Exception as e:\n",
" print(\n",
" f\"\\nError streaming LLM: {e}. Ensure endpoint handler supports streaming output format.\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Async Usage"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"# AInvoke (Async)\n",
"try:\n",
" async_response = await llm.ainvoke(prompt)\n",
" print(\"--- Async Invoke Response ---\")\n",
" print(async_response)\n",
"except Exception as e:\n",
" print(f\"Error invoking LLM asynchronously: {e}.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"# AStream (Async)\n",
"print(\"\\n--- Async Stream Response ---\")\n",
"try:\n",
" async for chunk in llm.astream(prompt):\n",
" print(chunk, end=\"\", flush=True)\n",
" print() # Newline\n",
"except Exception as e:\n",
" print(\n",
" f\"\\nError streaming LLM asynchronously: {e}. Ensure endpoint handler supports streaming output format.\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"The LLM integrates seamlessly with LangChain Expression Language (LCEL) chains."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain_core.output_parsers import StrOutputParser\n",
"from langchain_core.prompts import PromptTemplate\n",
"\n",
"# Assumes 'llm' variable is instantiated from the 'Instantiation' cell\n",
"prompt_template = PromptTemplate.from_template(\"Tell me a joke about {topic}\")\n",
"parser = StrOutputParser()\n",
"\n",
"chain = prompt_template | llm | parser\n",
"\n",
"try:\n",
" chain_response = chain.invoke({\"topic\": \"bears\"})\n",
" print(\"--- Chain Response ---\")\n",
" print(chain_response)\n",
"except Exception as e:\n",
" print(f\"Error running chain: {e}\")\n",
"\n",
"# Async chain\n",
"try:\n",
" async_chain_response = await chain.ainvoke({\"topic\": \"robots\"})\n",
" print(\"--- Async Chain Response ---\")\n",
" print(async_chain_response)\n",
"except Exception as e:\n",
" print(f\"Error running async chain: {e}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Endpoint Considerations\n",
"\n",
"- **Input:** The endpoint handler should expect the prompt string within `{\"input\": {\"prompt\": \"...\", ...}}`.\n",
"- **Output:** The handler should return the generated text within the `\"output\"` key of the final status response (e.g., `{\"output\": \"Generated text...\"}` or `{\"output\": {\"text\": \"...\"}}`).\n",
"- **Streaming:** For simulated streaming via the `/stream` endpoint, the handler must populate the `\"stream\"` key in the status response with a list of chunk dictionaries, like `[{\"output\": \"token1\"}, {\"output\": \"token2\"}]`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of the `RunPod` LLM class, parameters, and methods, refer to the source code or the generated API reference (if available).\n",
"\n",
"Link to source code: [https://github.com/runpod/langchain-runpod/blob/main/langchain_runpod/llms.py](https://github.com/runpod/langchain-runpod/blob/main/langchain_runpod/llms.py)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -46,6 +46,14 @@ See [installation instructions and a usage example](/docs/integrations/chat/tong
from langchain_community.chat_models.tongyi import ChatTongyi
```
### Qwen QwQ Chat
See [installation instructions and a usage example](/docs/integrations/chat/qwq)
```python
from langchain_qwq import ChatQwQ
```
## Document Loaders
### Alibaba Cloud MaxCompute

View File

@@ -35,9 +35,18 @@ from langchain_aws import ChatBedrock
```
### Bedrock Converse
AWS has recently released the Bedrock Converse API which provides a unified conversational interface for Bedrock models. This API does not yet support custom models. You can see a list of all [models that are supported here](https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html). To improve reliability the ChatBedrock integration will switch to using the Bedrock Converse API as soon as it has feature parity with the existing Bedrock API. Until then a separate [ChatBedrockConverse](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock_converse.ChatBedrockConverse.html) integration has been released.
AWS Bedrock maintains a [Converse API](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html)
that provides a unified conversational interface for Bedrock models. This API does not
yet support custom models. You can see a list of all
[models that are supported here](https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html).
We recommend using `ChatBedrockConverse` for users who do not need to use custom models. See the [docs](/docs/integrations/chat/bedrock/#bedrock-converse-api) and [API reference](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock_converse.ChatBedrockConverse.html) for more detail.
:::info
We recommend the Converse API for users who do not need to use custom models. It can be accessed using [ChatBedrockConverse](https://python.langchain.com/api_reference/aws/chat_models/langchain_aws.chat_models.bedrock_converse.ChatBedrockConverse.html).
:::
See a [usage example](/docs/integrations/chat/bedrock).
```python
from langchain_aws import ChatBedrockConverse

View File

@@ -8,18 +8,34 @@
> learning models, on the `Cloudflare` network, from your code via REST API.
## ChatModels
See [installation instructions and usage example](/docs/integrations/chat/cloudflare_workersai).
```python
from langchain_cloudflare import ChatCloudflareWorkersAI
```
## VectorStore
See [installation instructions and usage example](/docs/integrations/vectorstores/cloudflare_vectorize).
```python
from langchain_cloudflare import CloudflareVectorize
```
## Embeddings
See [installation instructions and usage example](/docs/integrations/text_embedding/cloudflare_workersai).
```python
from langchain_cloudflare import CloudflareWorkersAIEmbeddings
```
## LLMs
See [installation instructions and usage example](/docs/integrations/llms/cloudflare_workersai).
```python
from langchain_community.llms.cloudflare_workersai import CloudflareWorkersAI
```
## Embedding models
See [installation instructions and usage example](/docs/integrations/text_embedding/cloudflare_workersai).
```python
from langchain_community.embeddings.cloudflare_workersai import CloudflareWorkersAIEmbeddings
```
```

View File

@@ -17,7 +17,7 @@ pip install langchain-couchbase
See a [usage example](/docs/integrations/vectorstores/couchbase).
```python
from langchain_couchbase import CouchbaseVectorStore
from langchain_couchbase import CouchbaseSearchVectorStore
```
## Document loader

View File

@@ -0,0 +1,32 @@
# Smabbler
> Smabblers graph-powered platform boosts AI development by transforming data into a structured knowledge foundation.
# Galaxia
> Galaxia Knowledge Base is an integrated knowledge base and retrieval mechanism for RAG. In contrast to standard solution, it is based on Knowledge Graphs built using symbolic NLP and Knowledge Representation solutions. Provided texts are analysed and transformed into Graphs containing text, language and semantic information. This rich structure allows for retrieval that is based on semantic information, not on vector similarity/distance.
Implementing RAG using Galaxia involves first uploading your files to [Galaxia](https://beta.cloud.smabbler.com/home), analyzing them there and then building a model (knowledge graph). When the model is built, you can use `GalaxiaRetriever` to connect to the API and start retrieving.
More information: [docs](https://smabbler.gitbook.io/smabbler)
## Installation
```
pip install langchain-galaxia-retriever
```
## Usage
```
from langchain_galaxia_retriever.retriever import GalaxiaRetriever
gr = GalaxiaRetriever(
api_url="beta.api.smabbler.com",
api_key="<key>",
knowledge_base_id="<knowledge_base_id>",
n_retries=10,
wait_time=5,
)
result = gr.invoke('<test question>')
print(result)

View File

@@ -25,6 +25,114 @@ And you should configure credentials by setting the following environment variab
Make sure to get your API Key from https://app.hyperbrowser.ai/
## Available Tools
Hyperbrowser provides two main categories of tools that are particularly useful for:
- Web scraping and data extraction from complex websites
- Automating repetitive web tasks
- Interacting with web applications that require authentication
- Performing research across multiple websites
- Testing web applications
### Browser Agent Tools
Hyperbrowser provides a number of Browser Agents tools. Currently we supported
- Claude Computer Use
- OpenAI CUA
- Browser Use
You can see more details [here](/docs/integrations/tools/hyperbrowser_browser_agent_tools)
#### Browser Use Tool
A general-purpose browser automation tool that can handle various web tasks through natural language instructions.
```python
from langchain_hyperbrowser import HyperbrowserBrowserUseTool
tool = HyperbrowserBrowserUseTool()
result = tool.run({
"task": "Go to npmjs.com, find the React package, and tell me when it was last updated"
})
print(result)
```
#### OpenAI CUA Tool
Leverages OpenAI's Computer Use Agent capabilities for advanced web interactions and information gathering.
```python
from langchain_hyperbrowser import HyperbrowserOpenAICUATool
tool = HyperbrowserOpenAICUATool()
result = tool.run({
"task": "Go to Hacker News and summarize the top 5 posts right now"
})
print(result)
```
#### Claude Computer Use Tool
Utilizes Anthropic's Claude for sophisticated web browsing and information processing tasks.
```python
from langchain_hyperbrowser import HyperbrowserClaudeComputerUseTool
tool = HyperbrowserClaudeComputerUseTool()
result = tool.run({
"task": "Go to GitHub's trending repositories page, and list the top 3 posts there right now"
})
print(result)
```
### Web Scraping Tools
Here is a brief description of the Web Scraping Tools available with Hyperbrowser. You can see more details [here](/docs/integrations/tools/hyperbrowser_web_scraping_tools)
#### Scrape Tool
The Scrape Tool allows you to extract content from a single webpage in markdown, HTML, or link format.
```python
from langchain_hyperbrowser import HyperbrowserScrapeTool
tool = HyperbrowserScrapeTool()
result = tool.run({
"url": "https://example.com",
"scrape_options": {"formats": ["markdown"]}
})
print(result)
```
#### Crawl Tool
The Crawl Tool enables you to traverse entire websites, starting from a given URL, with configurable page limits.
```python
from langchain_hyperbrowser import HyperbrowserCrawlTool
tool = HyperbrowserCrawlTool()
result = tool.run({
"url": "https://example.com",
"max_pages": 2,
"scrape_options": {"formats": ["markdown"]}
})
print(result)
```
#### Extract Tool
The Extract Tool uses AI to pull structured data from web pages based on predefined schemas, making it perfect for data extraction tasks.
```python
from langchain_hyperbrowser import HyperbrowserExtractTool
from pydantic import BaseModel
class SimpleExtractionModel(BaseModel):
title: str
tool = HyperbrowserExtractTool()
result = tool.run({
"url": "https://example.com",
"schema": SimpleExtractionModel
})
print(result)
```
## Document Loader
The `HyperbrowserLoader` class in `langchain-hyperbrowser` can easily be used to load content from any single page or multiple pages as well as crawl an entire site.
@@ -39,7 +147,7 @@ docs = loader.load()
print(docs[0])
```
## Advanced Usage
### Advanced Usage
You can specify the operation to be performed by the loader. The default operation is `scrape`. For `scrape`, you can provide a single URL or a list of URLs to be scraped. For `crawl`, you can only provide a single URL. The `crawl` operation will crawl the provided page and subpages and return a document for each page.

View File

@@ -0,0 +1,165 @@
# Langfuse 🪢
> **What is Langfuse?** [Langfuse](https://langfuse.com) is an open source LLM engineering platform that helps teams trace API calls, monitor performance, and debug issues in their AI applications.
## Tracing LangChain
[Langfuse Tracing](https://langfuse.com/docs/tracing) integrates with Langchain using Langchain Callbacks ([Python](https://python.langchain.com/docs/how_to/#callbacks), [JS](https://js.langchain.com/docs/how_to/#callbacks)). Thereby, the Langfuse SDK automatically creates a nested trace for every run of your Langchain applications. This allows you to log, analyze and debug your LangChain application.
You can configure the integration via (1) constructor arguments or (2) environment variables. Get your Langfuse credentials by signing up at [cloud.langfuse.com](https://cloud.langfuse.com) or [self-hosting Langfuse](https://langfuse.com/self-hosting).
### Constructor arguments
```python
pip install langfuse
```
```python
# Initialize Langfuse handler
from langfuse.callback import CallbackHandler
langfuse_handler = CallbackHandler(
secret_key="sk-lf-...",
public_key="pk-lf-...",
host="https://cloud.langfuse.com", # 🇪🇺 EU region
# host="https://us.cloud.langfuse.com", # 🇺🇸 US region
)
# Your Langchain code
# Add Langfuse handler as callback (classic and LCEL)
chain.invoke({"input": "<user_input>"}, config={"callbacks": [langfuse_handler]})
```
### Environment variables
```bash filename=".env"
LANGFUSE_SECRET_KEY="sk-lf-..."
LANGFUSE_PUBLIC_KEY="pk-lf-..."
# 🇪🇺 EU region
LANGFUSE_HOST="https://cloud.langfuse.com"
# 🇺🇸 US region
# LANGFUSE_HOST="https://us.cloud.langfuse.com"
```
```python
# Initialize Langfuse handler
from langfuse.callback import CallbackHandler
langfuse_handler = CallbackHandler()
# Your Langchain code
# Add Langfuse handler as callback (classic and LCEL)
chain.invoke({"input": "<user_input>"}, config={"callbacks": [langfuse_handler]})
```
To see how to use this integration together with other Langfuse features, check out [this end-to-end example](https://langfuse.com/docs/integrations/langchain/example-python).
## Tracing LangGraph
This part demonstrates how [Langfuse](https://langfuse.com/docs) helps to debug, analyze, and iterate on your LangGraph application using the [LangChain integration](https://langfuse.com/docs/integrations/langchain/tracing).
### Initialize Langfuse
**Note:** You need to run at least Python 3.11 ([GitHub Issue](https://github.com/langfuse/langfuse/issues/1926)).
Initialize the Langfuse client with your [API keys](https://langfuse.com/faq/all/where-are-langfuse-api-keys) from the project settings in the Langfuse UI and add them to your environment.
```python
%pip install langfuse
%pip install langchain langgraph langchain_openai langchain_community
```
```python
import os
# get keys for your project from https://cloud.langfuse.com
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-***"
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-***"
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # for EU data region
# os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com" # for US data region
# your openai key
os.environ["OPENAI_API_KEY"] = "***"
```
### Simple chat app with LangGraph
**What we will do in this section:**
* Build a support chatbot in LangGraph that can answer common questions
* Tracing the chatbot's input and output using Langfuse
We will start with a basic chatbot and build a more advanced multi agent setup in the next section, introducing key LangGraph concepts along the way.
#### Create Agent
Start by creating a `StateGraph`. A `StateGraph` object defines our chatbot's structure as a state machine. We will add nodes to represent the LLM and functions the chatbot can call, and edges to specify how the bot transitions between these functions.
```python
from typing import Annotated
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
from typing_extensions import TypedDict
from langgraph.graph import StateGraph
from langgraph.graph.message import add_messages
class State(TypedDict):
# Messages have the type "list". The `add_messages` function in the annotation defines how this state key should be updated
# (in this case, it appends messages to the list, rather than overwriting them)
messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)
llm = ChatOpenAI(model = "gpt-4o", temperature = 0.2)
# The chatbot node function takes the current State as input and returns an updated messages list. This is the basic pattern for all LangGraph node functions.
def chatbot(state: State):
return {"messages": [llm.invoke(state["messages"])]}
# Add a "chatbot" node. Nodes represent units of work. They are typically regular python functions.
graph_builder.add_node("chatbot", chatbot)
# Add an entry point. This tells our graph where to start its work each time we run it.
graph_builder.set_entry_point("chatbot")
# Set a finish point. This instructs the graph "any time this node is run, you can exit."
graph_builder.set_finish_point("chatbot")
# To be able to run our graph, call "compile()" on the graph builder. This creates a "CompiledGraph" we can use invoke on our state.
graph = graph_builder.compile()
```
#### Add Langfuse as callback to the invocation
Now, we will add then [Langfuse callback handler for LangChain](https://langfuse.com/docs/integrations/langchain/tracing) to trace the steps of our application: `config={"callbacks": [langfuse_handler]}`
```python
from langfuse.callback import CallbackHandler
# Initialize Langfuse CallbackHandler for Langchain (tracing)
langfuse_handler = CallbackHandler()
for s in graph.stream({"messages": [HumanMessage(content = "What is Langfuse?")]},
config={"callbacks": [langfuse_handler]}):
print(s)
```
```
{'chatbot': {'messages': [AIMessage(content='Langfuse is a tool designed to help developers monitor and observe the performance of their Large Language Model (LLM) applications. It provides detailed insights into how these applications are functioning, allowing for better debugging, optimization, and overall management. Langfuse offers features such as tracking key metrics, visualizing data, and identifying potential issues in real-time, making it easier for developers to maintain and improve their LLM-based solutions.', response_metadata={'token_usage': {'completion_tokens': 86, 'prompt_tokens': 13, 'total_tokens': 99}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_400f27fa1f', 'finish_reason': 'stop', 'logprobs': None}, id='run-9a0c97cb-ccfe-463e-902c-5a5900b796b4-0', usage_metadata={'input_tokens': 13, 'output_tokens': 86, 'total_tokens': 99})]}}
```
#### View traces in Langfuse
Example trace in Langfuse: https://cloud.langfuse.com/project/cloramnkj0002jz088vzn1ja4/traces/d109e148-d188-4d6e-823f-aac0864afbab
![Trace view of chat app in Langfuse](https://langfuse.com/images/cookbook/integration-langgraph/integration_langgraph_chatapp_trace.png)
- Check out the [full notebook](https://langfuse.com/docs/integrations/langchain/example-python-langgraph) to see more examples.
- To learn how to evaluate the performance of your LangGraph application, check out the [LangGraph evaluation guide](https://langfuse.com/docs/integrations/langchain/example-langgraph-agents).

View File

@@ -0,0 +1,18 @@
# LiteLLM
>[LiteLLM](https://github.com/BerriAI/litellm) is a library that simplifies calling Anthropic, Azure, Huggingface, Replicate, etc.
## Installation and setup
```bash
pip install langchain-litellm
```
## Chat Models
```python
from langchain_litellm import ChatLiteLLM
```
See more detail in the guide [here](/docs/integrations/chat/litellm).
## API reference
For detailed documentation of all `ChatLiteLLM` features and configurations head to the API reference: https://github.com/Akshay-Dongare/langchain-litellm

View File

@@ -1,37 +0,0 @@
# LiteLLM
>[LiteLLM](https://docs.litellm.ai/docs/) is a library that simplifies calling Anthropic,
> Azure, Huggingface, Replicate, etc. LLMs in a unified way.
>
>You can use `LiteLLM` through either:
>
>* [LiteLLM Proxy Server](https://docs.litellm.ai/docs/#openai-proxy) - Server to call 100+ LLMs, load balance, cost tracking across projects
>* [LiteLLM python SDK](https://docs.litellm.ai/docs/#basic-usage) - Python Client to call 100+ LLMs, load balance, cost tracking
## Installation and setup
Install the `litellm` python package.
```bash
pip install litellm
```
## Chat models
### ChatLiteLLM
See a [usage example](/docs/integrations/chat/litellm).
```python
from langchain_community.chat_models import ChatLiteLLM
```
### ChatLiteLLMRouter
You also can use the `ChatLiteLLMRouter` to route requests to different LLMs or LLM providers.
See a [usage example](/docs/integrations/chat/litellm_router).
```python
from langchain_community.chat_models import ChatLiteLLMRouter
```

View File

@@ -0,0 +1,41 @@
# MariaDB
This page covers how to use the [MariaDB](https://github.com/mariadb/) ecosystem within LangChain.
It is broken into two parts: installation and setup, and then references to specific PGVector wrappers.
## Installation
- Install c/c connector
on Debian, Ubuntu
```bash
sudo apt install libmariadb3 libmariadb-dev
```
on CentOS, RHEL, Rocky Linux
```bash
sudo yum install MariaDB-shared MariaDB-devel
```
- Install the Python connector package with `pip install mariadb`
## Setup
1. The first step is to have a MariaDB 11.7.1 or later installed.
The docker image is the easiest way to get started.
## Wrappers
### VectorStore
There exists a wrapper around MariaDB vector databases, allowing you to use it as a vectorstore,
whether for semantic search or example selection.
To import this vectorstore:
```python
from langchain_mariadb import MariaDBStore
```
### Usage
For a more detailed walkthrough of the MariaDB wrapper, see [this notebook](/docs/integrations/vectorstores/mariadb)

View File

@@ -0,0 +1,18 @@
# Oxylabs
[Oxylabs](https://oxylabs.io/) is a market-leading web intelligence collection platform, driven by the highest business,
ethics, and compliance standards, enabling companies worldwide to unlock data-driven insights.
[langchain-oxylabs](https://pypi.org/project/langchain-oxylabs/) implements
tools enabling LLMs to interact with Oxylabs Web Scraper API.
## Installation and Setup
```bash
pip install langchain-oxylabs
```
## Tools
See details on available tools [here](/docs/integrations/tools/oxylabs/).

View File

@@ -8,18 +8,18 @@
## Installation and Setup
Install a Python package:
Install the Perplexity x LangChain integration package:
```bash
pip install openai
pip install langchain-perplexity
````
Get your API key from [here](https://docs.perplexity.ai/docs/getting-started).
## Chat models
See a [usage example](/docs/integrations/chat/perplexity).
See a variety of usage examples [here](/docs/integrations/chat/perplexity).
```python
from langchain_community.chat_models import ChatPerplexity
from langchain_perplexity import ChatPerplexity
```

View File

@@ -59,7 +59,7 @@ with the default values of "service-name = mymaster" and "db-number = 0" if not
The service-name is the redis server monitoring group name as configured within the Sentinel.
The current url format limits the connection string to one sentinel host only (no list can be given) and
booth Redis server and sentinel must have the same password set (if used).
both Redis server and sentinel must have the same password set (if used).
#### Redis Cluster connection url

View File

@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Runpod\n",
"\n",
"[RunPod](https://www.runpod.io/) provides GPU cloud infrastructure, including Serverless endpoints optimized for deploying and scaling AI models.\n",
"\n",
"This guide covers how to use the `langchain-runpod` integration package to connect LangChain applications to models hosted on [RunPod Serverless](https://www.runpod.io/serverless-gpu).\n",
"\n",
"The integration offers interfaces for both standard Language Models (LLMs) and Chat Models."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Intstallation\n",
"\n",
"Install the dedicated partner package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"%pip install -qU langchain-runpod"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"### 1. Deploy an Endpoint on RunPod\n",
"- Navigate to your [RunPod Serverless Console](https://www.runpod.io/console/serverless/user/endpoints).\n",
"- Create a \\\"New Endpoint\\\", selecting an appropriate GPU and template (e.g., vLLM, TGI, text-generation-webui) compatible with your model and the expected input/output format (see component guides or the package [README](https://github.com/runpod/langchain-runpod)).\n",
"- Configure settings and deploy.\n",
"- **Crucially, copy the Endpoint ID** after deployment."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Set API Credentials\n",
"The integration needs your RunPod API Key and the Endpoint ID. Set them as environment variables for secure access:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"os.environ[\"RUNPOD_API_KEY\"] = getpass.getpass(\"Enter your RunPod API Key: \")\n",
"os.environ[\"RUNPOD_ENDPOINT_ID\"] = input(\"Enter your RunPod Endpoint ID: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*(Optional)* If using different endpoints for LLM and Chat models, you might need to set `RUNPOD_CHAT_ENDPOINT_ID` or pass the ID directly during initialization."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Components\n",
"This package provides two main components:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. LLM\n",
"\n",
"For interacting with standard text completion models.\n",
"\n",
"See the [RunPod LLM Integration Guide](/docs/integrations/llms/runpod) for detailed usage"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain_runpod import RunPod\n",
"\n",
"# Example initialization (uses environment variables)\n",
"llm = RunPod(model_kwargs={\"max_new_tokens\": 100}) # Add generation params here\n",
"\n",
"# Example Invocation\n",
"try:\n",
" response = llm.invoke(\"Write a short poem about the cloud.\")\n",
" print(response)\n",
"except Exception as e:\n",
" print(\n",
" f\"Error invoking LLM: {e}. Ensure endpoint ID and API key are correct and endpoint is active.\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Chat Model\n",
"\n",
"For interacting with conversational models.\n",
"\n",
"See the [RunPod Chat Model Integration Guide](/docs/integrations/chat/runpod) for detailed usage and feature support."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"from langchain_core.messages import HumanMessage\n",
"from langchain_runpod import ChatRunPod\n",
"\n",
"# Example initialization (uses environment variables)\n",
"chat = ChatRunPod(model_kwargs={\"temperature\": 0.8}) # Add generation params here\n",
"\n",
"# Example Invocation\n",
"try:\n",
" response = chat.invoke(\n",
" [HumanMessage(content=\"Explain RunPod Serverless in one sentence.\")]\n",
" )\n",
" print(response.content)\n",
"except Exception as e:\n",
" print(\n",
" f\"Error invoking Chat Model: {e}. Ensure endpoint ID and API key are correct and endpoint is active.\"\n",
" )"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -14,7 +14,7 @@
"3. The [Boomerang](https://vectara.com/how-boomerang-takes-retrieval-augmented-generation-to-the-next-level-via-grounded-generation/) embeddings model.\n",
"4. Its own internal vector database where text chunks and embedding vectors are stored.\n",
"5. A query service that automatically encodes the query into embedding, and retrieves the most relevant text segments, including support for [Hybrid Search](https://docs.vectara.com/docs/api-reference/search-apis/lexical-matching) as well as multiple reranking options such as the [multi-lingual relevance reranker](https://www.vectara.com/blog/deep-dive-into-vectara-multilingual-reranker-v1-state-of-the-art-reranker-across-100-languages), [MMR](https://vectara.com/get-diverse-results-and-comprehensive-summaries-with-vectaras-mmr-reranker/), [UDF reranker](https://www.vectara.com/blog/rag-with-user-defined-functions-based-reranking). \n",
"6. An LLM to for creating a [generative summary](https://docs.vectara.com/docs/learn/grounded-generation/grounded-generation-overview), based on the retrieved documents (context), including citations.\n",
"6. An LLM for creating a [generative summary](https://docs.vectara.com/docs/learn/grounded-generation/grounded-generation-overview), based on the retrieved documents (context), including citations.\n",
"\n",
"For more information:\n",
"- [Documentation](https://docs.vectara.com/docs/)\n",

View File

@@ -0,0 +1,23 @@
# YDB
All functionality related to YDB.
> [YDB](https://ydb.tech/) is a versatile open source Distributed SQL Database that combines
> high availability and scalability with strong consistency and ACID transactions.
> It accommodates transactional (OLTP), analytical (OLAP), and streaming workloads simultaneously.
## Installation and Setup
```bash
pip install langchain-ydb
```
## Vector Store
To import YDB vector store:
```python
from langchain_ydb.vectorstores import YDB
```
For a more detailed walkthrough of the YDB vector store, see [this notebook](/docs/integrations/vectorstores/ydb).

View File

@@ -40,11 +40,21 @@
"\n",
"+ An existing index with vector fields. There are several ways to create one, including using the [vector store module](../vectorstores/azuresearch.ipynb). Or, [try the Azure AI Search REST APIs](https://learn.microsoft.com/azure/search/search-get-started-vector).\n",
"\n",
"+ An API key. API keys are generated when you create the search service. If you're just querying an index, you can use the query API key, otherwise use an admin API key. See [Find your API keys](https://learn.microsoft.com/azure/search/search-security-api-keys?tabs=rest-use%2Cportal-find%2Cportal-query#find-existing-keys) for details.\n",
"+ An API key or Azure AD Token.\n",
" + API keys are generated when you create the search service. If you're just querying an index, you can use the query API key, otherwise use an admin API key. See [Find your API keys](https://learn.microsoft.com/azure/search/search-security-api-keys?tabs=rest-use%2Cportal-find%2Cportal-query#find-existing-keys) for details.\n",
" + Azure AD Token can be used with Azure Managed Identity. See [Connect your app to Azure AI Search using identities](https://learn.microsoft.com/en-us/azure/search/keyless-connections?tabs=python%2Cazure-cli) for details.\n",
"\n",
"We can then set the search service name, index name, and API key as environment variables (alternatively, you can pass them as arguments to `AzureAISearchRetriever`). The search index provides the searchable content."
]
},
{
"cell_type": "markdown",
"id": "2af9655d",
"metadata": {},
"source": [
"With an API Key"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -59,6 +69,28 @@
"os.environ[\"AZURE_AI_SEARCH_API_KEY\"] = \"<YOUR_API_KEY>\""
]
},
{
"cell_type": "markdown",
"id": "35ee4780",
"metadata": {},
"source": [
"With an Azure AD Token"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f33263c",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"AZURE_AI_SEARCH_SERVICE_NAME\"] = \"<YOUR_SEARCH_SERVICE_NAME>\"\n",
"os.environ[\"AZURE_AI_SEARCH_INDEX_NAME\"] = \"<YOUR_SEARCH_INDEX_NAME>\"\n",
"os.environ[\"AZURE_AI_SEARCH_AD_TOKEN\"] = \"<YOUR_AZURE_AD_TOKEN>\""
]
},
{
"cell_type": "markdown",
"id": "3e635218-8634-4f39-abc5-39e319eeb136",

View File

@@ -0,0 +1,213 @@
{
"cells": [
{
"cell_type": "raw",
"id": "2af1fec5-4ca6-4167-8ee1-13314aac3258",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"sidebar_label: Galaxia\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "1d7d6cbc-4373-4fb5-94dd-acd610165452",
"metadata": {},
"source": [
"# Galaxia Retriever\n",
"\n",
"Galaxia is GraphRAG solution, which automates document processing, knowledge base (Graph Language Model) creation and retrieval:\n",
"[galaxia-rag](https://smabbler.gitbook.io/smabbler/api-rag/smabblers-api-rag)\n",
"\n",
"To use Galaxia first upload your texts and create a Graph Language Model here: [smabbler-cloud](https://beta.cloud.smabbler.com)\n",
"\n",
"After the model is built and activated, you will be able to use this integration to retrieve what you need.\n",
"\n",
"The module repository is located here: [github](https://github.com/rrozanski-smabbler/galaxia-langchain)\n",
"\n",
"### Integration details\n",
"| Retriever | Self-host | Cloud offering | Package |\n",
"| :--- | :--- | :---: | :---: |\n",
"[Galaxia Retriever](https://github.com/rrozanski-smabbler/galaxia-langchain) | ❌ | ✅ | __langchain-galaxia-retriever__ |"
]
},
{
"cell_type": "markdown",
"id": "82fa1c05-c205-4429-a74c-e6c81c4e8611",
"metadata": {},
"source": [
"## Setup\n",
"Before you can retrieve anything you need to create your Graph Language Model here: [smabbler-cloud](https://beta.cloud.smabbler.com)\n",
"\n",
"following these 3 simple steps: [rag-instruction](https://smabbler.gitbook.io/smabbler/api-rag/build-rag-model-in-3-steps)\n",
"\n",
"Don't forget to activate the model after building it!"
]
},
{
"cell_type": "markdown",
"id": "91897867-eb39-4c3b-8df8-5427043ecdcd",
"metadata": {},
"source": [
"### Installation\n",
"The retriever is implemented in the following package: [pypi](https://pypi.org/project/langchain-galaxia-retriever/)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ceca36f2-013c-4b28-81fe-8808d0cf6419",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-galaxia-retriever"
]
},
{
"cell_type": "markdown",
"id": "019e0e50-5e66-440b-9cf1-d21b4009bf13",
"metadata": {},
"source": [
"## Instantiation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7188217-4b26-4201-b15a-b7a5f263f815",
"metadata": {},
"outputs": [],
"source": [
"from langchain_galaxia_retriever.retriever import GalaxiaRetriever\n",
"\n",
"gr = GalaxiaRetriever(\n",
" api_url=\"beta.api.smabbler.com\",\n",
" api_key=\"<key>\", # you can find it here: https://beta.cloud.smabbler.com/user/account\n",
" knowledge_base_id=\"<knowledge_base_id>\", # you can find it in https://beta.cloud.smabbler.com , in the model table\n",
" n_retries=10,\n",
" wait_time=5,\n",
")"
]
},
{
"cell_type": "markdown",
"id": "02d288a5-4f76-472e-9a60-eea8e6b8dc7a",
"metadata": {},
"source": [
"## Usage"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f79e03f-77a6-4eb6-b41d-f3da2f897654",
"metadata": {},
"outputs": [],
"source": [
"result = gr.invoke(\"<test question>\")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"id": "ffb2a595-a901-477a-a374-efd091bc1c9a",
"metadata": {},
"source": [
"## Use within a chain"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c2e2394-ca33-47be-a851-551b4216daea",
"metadata": {},
"outputs": [],
"source": [
"# | output: false\n",
"# | echo: false\n",
"\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ed8699d6-d65d-40ea-8c58-8d809cc512cf",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.output_parsers import StrOutputParser\n",
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_core.runnables import RunnablePassthrough\n",
"\n",
"prompt = ChatPromptTemplate.from_template(\n",
" \"\"\"Answer the question based only on the context provided.\n",
"\n",
"Context: {context}\n",
"\n",
"Question: {question}\"\"\"\n",
")\n",
"\n",
"\n",
"def format_docs(docs):\n",
" return \"\\n\\n\".join(doc.page_content for doc in docs)\n",
"\n",
"\n",
"chain = (\n",
" {\"context\": gr | format_docs, \"question\": RunnablePassthrough()}\n",
" | prompt\n",
" | llm\n",
" | StrOutputParser()\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9b944d7-8800-4926-b1ce-fcdc52ecda1c",
"metadata": {},
"outputs": [],
"source": [
"chain.invoke(\"<test question>\")"
]
},
{
"cell_type": "markdown",
"id": "11b5c9a5-0a66-415f-98f8-f12080cad30a",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For more information about Galaxia Retriever check its implementation on github [github](https://github.com/rrozanski-smabbler/galaxia-langchain)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -47,7 +47,7 @@
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.embeddings.cloudflare_workersai import (\n",
"from langchain_cloudflare.embeddings import (\n",
" CloudflareWorkersAIEmbeddings,\n",
")"
]

View File

@@ -43,12 +43,21 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "5b14008a",
"metadata": {},
"outputs": [],
"source": [
"tool = BraveSearch.from_api_key(api_key=api_key, search_kwargs={\"count\": 3})"
"tool = BraveSearch.from_api_key(api_key=api_key, search_kwargs={\"count\": 3})\n",
"\n",
"# or if you want to get the api key from environment variable BRAVE_SEARCH_API_KEY, and leave search_kwargs empty\n",
"# tool = BraveSearch()\n",
"\n",
"# or if you want to provide just the api key, and leave search_kwargs empty\n",
"# tool = BraveSearch.from_api_key(api_key=api_key)\n",
"\n",
"# or if you want to provide just the search_kwargs and read the api key from the BRAVE_SEARCH_API_KEY environment variable\n",
"# tool = BraveSearch.from_search_kwargs(search_kwargs={\"count\": 3})"
]
},
{

View File

@@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"id": "q-k8wmp0zquh"
},
@@ -34,7 +34,7 @@
"\n",
"from langchain_openai import OpenAI\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"<your-key-here>\""
"os.environ[\"OPENAI_API_KEY\"] = \"insertapikey\""
]
},
{
@@ -47,7 +47,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -75,20 +75,9 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'https://oaidalleapiprodscus.blob.core.windows.net/private/org-i0zjYONU3PemzJ222esBaAzZ/user-f6uEIOFxoiUZivy567cDSWni/img-i7Z2ZxvJ4IbbdAiO6OXJgS3v.png?st=2023-08-11T14%3A03%3A14Z&se=2023-08-11T16%3A03%3A14Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-08-10T20%3A58%3A32Z&ske=2023-08-11T20%3A58%3A32Z&sks=b&skv=2021-08-06&sig=/sECe7C0EAq37ssgBm7g7JkVIM/Q1W3xOstd0Go6slA%3D'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"image_url"
]
@@ -135,34 +124,38 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m What is the best way to turn this description into an image?\n",
"Action: Dall-E Image Generator\n",
"Action Input: A spooky Halloween night at a haunted museum\u001b[0mhttps://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22%3A19%3A36Z&ske=2023-01-31T22%3A19%3A36Z&sks=b&skv=2021-08-06&sig=XsomxxBfu2CP78SzR9lrWUlbask4wBNnaMsHamy4VvU%3D\n",
"\n",
"Observation: \u001b[36;1m\u001b[1;3mhttps://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22%3A19%3A36Z&ske=2023-01-31T22%3A19%3A36Z&sks=b&skv=2021-08-06&sig=XsomxxBfu2CP78SzR9lrWUlbask4wBNnaMsHamy4VvU%3D\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m With the image generated, I can now make my final answer.\n",
"Final Answer: An image of a Halloween night at a haunted museum can be seen here: https://oaidalleapiprodscus.blob.core.windows.net/private/org-rocrupyvzgcl4yf25rqq6d1v/user-WsxrbKyP2c8rfhCKWDyMfe8N/img-ogKfqxxOS5KWVSj4gYySR6FY.png?st=2023-01-31T07%3A38%3A25Z&se=2023-01-31T09%3A38%3A25Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-01-30T22\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
}
],
"outputs": [],
"source": [
"from langchain.agents import initialize_agent, load_tools\n",
"from langchain_community.tools.openai_dalle_image_generation import (\n",
" OpenAIDALLEImageGenerationTool,\n",
")\n",
"from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper\n",
"from langchain_openai import ChatOpenAI\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"tools = load_tools([\"dalle-image-generator\"])\n",
"agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)\n",
"output = agent.run(\"Create an image of a halloween night at a haunted museum\")"
"llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0)\n",
"api_wrapper = DallEAPIWrapper()\n",
"dalle_tool = OpenAIDALLEImageGenerationTool(api_wrapper=api_wrapper)\n",
"\n",
"tools = [dalle_tool]\n",
"\n",
"agent = create_react_agent(llm, tools, debug=True)\n",
"\n",
"# User prompt\n",
"prompt = \"Create an image of a halloween night at a haunted museum\"\n",
"\n",
"messages = [\n",
" # \"role\": \"user\" Indicates message is coming from user\n",
" # \"content\": prompt is where the user's input is placed\n",
" {\"role\": \"user\", \"content\": prompt}\n",
"]\n",
"\n",
"# Sending the message to be processed and adjusted by ChatGPT, after which is sent through DALL-E\n",
"response = agent.invoke({\"messages\": messages})\n",
"\n",
"print(response)"
]
}
],
@@ -171,9 +164,9 @@
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python (langchain-env)",
"language": "python",
"name": "python3"
"name": "langchain-env"
},
"language_info": {
"codemirror_mode": {
@@ -185,12 +178,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
},
"vscode": {
"interpreter": {
"hash": "3570c8892273ffbeee7ead61dc7c022b73551d9f55fb2584ac0e8e8920b18a89"
}
"version": "3.12.9"
}
},
"nbformat": 4,

View File

@@ -0,0 +1,335 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"source": [
"# Google Calendar Toolkit\n",
"\n",
"> [Google Calendar](https://workspace.google.com/intl/en-419/products/calendar/) is a product of Google Workspace that allows users to organize their schedules and events. It is a cloud-based calendar that allows users to create, edit, and delete events. It also allows users to share their calendars with others.\n",
"\n",
"## Overview\n",
"\n",
"This notebook will help you get started with the Google Calendar Toolkit. This toolkit interacts with the Google Calendar API to perform various operations on the calendar. It allows you to:\n",
"\n",
"- Create events.\n",
"- Search events.\n",
"- Update events.\n",
"- Move events between different calendars.\n",
"- Delete events.\n",
"- List events.\n",
"\n",
"## Setup\n",
"\n",
"To use this toolkit, you will need to:\n",
"\n",
"1. Have a Google account with access to Google Calendar.\n",
"2. Set up your credentials as explained in the [Google Calendar API docs](https://developers.google.com/calendar/api/quickstart/python#authorize_credentials_for_a_desktop_application). Once you've downloaded the `credentials.json` file, you can start using the Google Calendar API.\n",
"\n",
"To enable automated tracing of individual tools, set your [LangSmith](https://docs.smith.langchain.com/) API key:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\"\n",
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installation\n",
"\n",
"This toolkit lives in the `langchain-google-community` package of the [langchain-google](https://github.com/langchain-ai/langchain-google) repository. We'll need the `calendar` extra:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-google-community\\[calendar\\]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"By default the toolkit reads the local `credentials.json` file. You can also manually provide a `Credentials` object."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain_google_community import CalendarToolkit\n",
"\n",
"toolkit = CalendarToolkit()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing Authentication\n",
"\n",
"Behind the scenes, a `googleapi` resource is created using the following methods. you can manually build a `googleapi` resource for more auth control."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from langchain_google_community import CalendarToolkit\n",
"from langchain_google_community.calendar.utils import (\n",
" build_resource_service,\n",
" get_google_credentials,\n",
")\n",
"\n",
"# Can review scopes here: https://developers.google.com/calendar/api/auth\n",
"# For instance, readonly scope is https://www.googleapis.com/auth/calendar.readonly\n",
"credentials = get_google_credentials(\n",
" token_file=\"token.json\",\n",
" scopes=[\"https://www.googleapis.com/auth/calendar\"],\n",
" client_secrets_file=\"credentials.json\",\n",
")\n",
"\n",
"api_resource = build_resource_service(credentials=credentials)\n",
"toolkit = CalendarToolkit(api_resource=api_resource)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tools\n",
"View available tools:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[CalendarCreateEvent(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),\n",
" CalendarSearchEvents(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),\n",
" CalendarUpdateEvent(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),\n",
" GetCalendarsInfo(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),\n",
" CalendarMoveEvent(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),\n",
" CalendarDeleteEvent(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>),\n",
" GetCurrentDatetime(api_resource=<googleapiclient.discovery.Resource object at 0x10ad13fb0>)]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tools = toolkit.get_tools()\n",
"tools"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- [CalendarCreateEvent](https://python.langchain.com/api_reference/google_community/calendar/langchain_google_community.calendar.create_event.CalendarCreateEvent.html)\n",
"- [CalendarSearchEvents](https://python.langchain.com/api_reference/google_community/calendar/langchain_google_community.calendar.search_events.CalendarSearchEvents.html)\n",
"- [CalendarUpdateEvent](https://python.langchain.com/api_reference/google_community/calendar/langchain_google_community.calendar.update_event.CalendarUpdateEvent.html)\n",
"- [GetCalendarsInfo](https://python.langchain.com/api_reference/google_community/calendar/langchain_google_community.calendar.get_calendars_info.GetCalendarsInfo.html)\n",
"- [CalendarMoveEvent](https://python.langchain.com/api_reference/google_community/calendar/langchain_google_community.calendar.move_event.CalendarMoveEvent.html)\n",
"- [CalendarDeleteEvent](https://python.langchain.com/api_reference/google_community/calendar/langchain_google_community.calendar.delete_event.CalendarDeleteEvent.html)\n",
"- [GetCurrentDatetime](https://python.langchain.com/api_reference/google_community/calendar/langchain_google_community.calendar.current_datetime.GetCurrentDatetime.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Invocation\n",
"\n",
"### [Invoke directly with args](/docs/concepts/tools/#use-the-tool-directly)\n",
"\n",
"You can invoke the tool directly by passing the required arguments in a dictionary format. Here is an example of creating a new event using the `CalendarCreateEvent` tool."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Event created: https://www.google.com/calendar/event?eid=amoxdjVsM2UzMW51Yjk2czc4ajhvaGdkcGcgam9yZ2VhbmczM0Bt'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain_google_community.calendar.create_event import CalendarCreateEvent\n",
"\n",
"tool = CalendarCreateEvent()\n",
"tool.invoke(\n",
" {\n",
" \"summary\": \"Calculus exam\",\n",
" \"start_datetime\": \"2025-07-11 11:00:00\",\n",
" \"end_datetime\": \"2025-07-11 13:00:00\",\n",
" \"timezone\": \"America/Mexico_City\",\n",
" \"location\": \"UAM Cuajimalpa\",\n",
" \"description\": \"Event created from the LangChain toolkit\",\n",
" \"reminders\": [{\"method\": \"popup\", \"minutes\": 60}],\n",
" \"conference_data\": True,\n",
" \"color_id\": \"5\",\n",
" }\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use within an agent\n",
"\n",
"Below we show how to incorporate the toolkit into an [agent](/docs/tutorials/agents).\n",
"\n",
"We will need a LLM or chat model:\n",
"\n",
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
"\n",
"<ChatModelTabs customVarName=\"llm\" />"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# | output: false\n",
"# | echo: false\n",
"\n",
"from langchain_openai import ChatOpenAI\n",
"\n",
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"agent_executor = create_react_agent(llm, tools)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"\n",
"Create a green event for this afternoon to go for a 30-minute run.\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" get_current_datetime (call_drHRRhm6pdvcAuqagONUEKs5)\n",
" Call ID: call_drHRRhm6pdvcAuqagONUEKs5\n",
" Args:\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: get_current_datetime\n",
"\n",
"Time zone: America/Mexico_City, Date and time: 2025-04-02 19:07:30\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" create_calendar_event (call_p60zSVMmmjTy5Ctezzmlb9zD)\n",
" Call ID: call_p60zSVMmmjTy5Ctezzmlb9zD\n",
" Args:\n",
" summary: Run\n",
" start_datetime: 2025-04-02 19:30:00\n",
" end_datetime: 2025-04-02 20:00:00\n",
" timezone: America/Mexico_City\n",
" color_id: 2\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: create_calendar_event\n",
"\n",
"Event created: https://www.google.com/calendar/event?eid=czZyZHVpcG43ajNiY241dmJmNWwycjE0NWsgam9yZ2VhbmczM0Bt\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"I have created a green event for your run this afternoon. You can view it [here](https://www.google.com/calendar/event?eid=czZyZHVpcG43ajNiY241dmJmNWwycjE0NWsgam9yZ2VhbmczM0Bt). Enjoy your run!\n"
]
}
],
"source": [
"example_query = \"Create a green event for this afternoon to go for a 30-minute run.\"\n",
"\n",
"events = agent_executor.stream(\n",
" {\"messages\": [(\"user\", example_query)]},\n",
" stream_mode=\"values\",\n",
")\n",
"for event in events:\n",
" event[\"messages\"][-1].pretty_print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"- Refer to the [Google Calendar API overview](https://developers.google.com/calendar/api/guides/overview) for more details from Google Calendar API.\n",
"- For detailed documentation of all Google Calendar Toolkit features and configurations head to the [calendar documentation](https://python.langchain.com/api_reference/google_community/calendar.html)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -0,0 +1,392 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"sidebar_label: Hyperbrowser Browser Agent Tools\n",
"---\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Hyperbrowser Browser Agent Tools\n",
"\n",
"[Hyperbrowser](https://hyperbrowser.ai) is a platform for running, running browser agents, and scaling headless browsers. It lets you launch and manage browser sessions at scale and provides easy to use solutions for any webscraping needs, such as scraping a single page or crawling an entire site.\n",
"\n",
"Key Features:\n",
"- Instant Scalability - Spin up hundreds of browser sessions in seconds without infrastructure headaches\n",
"- Simple Integration - Works seamlessly with popular tools like Puppeteer and Playwright\n",
"- Powerful APIs - Easy to use APIs for scraping/crawling any site, and much more\n",
"- Bypass Anti-Bot Measures - Built-in stealth mode, ad blocking, automatic CAPTCHA solving, and rotating proxies\n",
"\n",
"This notebook provides a quick overview for getting started with Hyperbrowser tools.\n",
"\n",
"For more information about Hyperbrowser, please visit the [Hyperbrowser website](https://hyperbrowser.ai) or if you want to check out the docs, you can visit the [Hyperbrowser docs](https://docs.hyperbrowser.ai).\n",
"\n",
"\n",
"## Browser Agents\n",
"\n",
"Hyperbrowser provides powerful browser agent tools that enable AI models to interact with web browsers programmatically. These browser agents can navigate websites, fill forms, click buttons, extract data, and perform complex web automation tasks.\n",
"\n",
"Browser agents are particularly useful for:\n",
"- Web scraping and data extraction from complex websites\n",
"- Automating repetitive web tasks\n",
"- Interacting with web applications that require authentication\n",
"- Performing research across multiple websites\n",
"- Testing web applications\n",
"\n",
"Hyperbrowser offers three types of browser agent tools:\n",
"- **Browser Use Tool**: A general-purpose browser automation tool\n",
"- **OpenAI CUA Tool**: Integration with OpenAI's Computer Use Agent\n",
"- **Claude Computer Use Tool**: Integration with Anthropic's Claude for computer use\n",
"\n",
"\n",
"## Overview\n",
"\n",
"### Integration details\n",
"\n",
"| Tool | Package | Local | Serializable | JS support |\n",
"| :----------------------- | :--------------------- | :---: | :----------: | :--------: |\n",
"| Browser Use Tool | langchain-hyperbrowser | ❌ | ❌ | ❌ |\n",
"| OpenAI CUA Tool | langchain-hyperbrowser | ❌ | ❌ | ❌ |\n",
"| Claude Computer Use Tool | langchain-hyperbrowser | ❌ | ❌ | ❌ |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"To access the Hyperbrowser tools you'll need to install the `langchain-hyperbrowser` integration package, and create a Hyperbrowser account and get an API key.\n",
"\n",
"### Credentials\n",
"\n",
"Head to [Hyperbrowser](https://app.hyperbrowser.ai/) to sign up and generate an API key. Once you've done this set the HYPERBROWSER_API_KEY environment variable:\n",
"\n",
"```bash\n",
"export HYPERBROWSER_API_KEY=<your-api-key>\n",
"```\n",
"\n",
"### Installation\n",
"\n",
"Install **langchain-hyperbrowser**.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-hyperbrowser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"### Browser Use Tool\n",
"\n",
"The `HyperbrowserBrowserUseTool` is a tool to perform web automation tasks using a browser agent, specifically the Browser-Use agent.\n",
"\n",
"```python\n",
"from langchain_hyperbrowser import HyperbrowserBrowserUseTool\n",
"tool = HyperbrowserBrowserUseTool()\n",
"```\n",
"\n",
"### OpenAI CUA Tool\n",
"\n",
"The `HyperbrowserOpenAICUATool` is a specialized tool that leverages OpenAI's Computer Use Agent (CUA) capabilities through Hyperbrowser.\n",
"\n",
"```python\n",
"from langchain_hyperbrowser import HyperbrowserOpenAICUATool\n",
"tool = HyperbrowserOpenAICUATool()\n",
"```\n",
"\n",
"### Claude Computer Use Tool\n",
"\n",
"The `HyperbrowserClaudeComputerUseTool` is a specialized tool that leverages Claude's computer use capabilities through Hyperbrowser.\n",
"\n",
"```python\n",
"from langchain_hyperbrowser import HyperbrowserClaudeComputerUseTool\n",
"tool = HyperbrowserClaudeComputerUseTool()\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Invocation\n",
"\n",
"### Basic Usage\n",
"\n",
"#### Browser Use Tool\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': 'The top 5 posts on Hacker News right now are:\\n1. Stop Syncing Everything - https://sqlsync.dev/posts/stop-syncing-everything/\\n2. Move fast, break things: A review of Abundance by Ezra Klein and Derek Thompson - https://networked.substack.com/p/move-fast-and-break-things\\n3. DEDA Tracking Dots Extraction, Decoding and Anonymisation Toolkit - https://github.com/dfd-tud/deda\\n4. Electron band structure in germanium, my ass (2001) - https://pages.cs.wisc.edu/~kovar/hall.html\\n5. Show HN: I vibecoded a 35k LoC recipe app - https://www.recipeninja.ai', 'error': None}\n"
]
}
],
"source": [
"from langchain_hyperbrowser import HyperbrowserBrowserUseTool\n",
"\n",
"tool = HyperbrowserBrowserUseTool()\n",
"result = tool.run({\"task\": \"Go to Hacker News and summarize the top 5 posts right now\"})\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### OpenAI CUA Tool\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': 'Here are the titles of the top 5 posts on Hacker News right now:\\n\\n1. \"DEDA Tracking Dots Extraction, Decoding and Anonymisation Toolkit\"\\n2. \"A man powers home for eight years using a thousand old laptop batteries\"\\n3. \"Electron band structure in Germanium, my ass\"\\n4. \"Bletchley code breaker Betty Webb dies aged 101\"\\n5. \"Show HN: Zig Topological Sort Library for Parallel Processing\"', 'error': None}\n"
]
}
],
"source": [
"from langchain_hyperbrowser import HyperbrowserOpenAICUATool\n",
"\n",
"tool = HyperbrowserOpenAICUATool()\n",
"result = tool.run(\n",
" {\"task\": \"Go to Hacker News and get me the title of the top 5 posts right now\"}\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Claude Computer Use Tool\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': \"Now I'll summarize the top 5 posts on Hacker News as of April 1, 2025:\\n\\n### Top 5 Hacker News Posts Summary\\n\\n1. **A man powers home for eight years using a thousand old laptop batteries** (techoreon.com)\\n - 267 points, posted 5 hours ago\\n - An innovative DIY project where someone managed to power their home using recycled laptop batteries for an extended period.\\n\\n2. **Electron band structure in germanium, my ass** (wisc.edu)\\n - 611 points, posted 8 hours ago\\n - Academic or technical discussion about electron band structure in germanium, possibly with a controversial or humorous take given the title.\\n\\n3. **Bletchley code breaker Betty Webb dies aged 101** (bbc.com)\\n - 575 points, posted 8 hours ago\\n - Obituary for Betty Webb, who worked as a code breaker at Bletchley Park during WWII, passing away at the age of 101.\\n\\n4. **Show HN: Zig Topological Sort Library for Parallel Processing** (github.com/williamw520)\\n - 55 points, posted 3 hours ago\\n - A developer sharing a library written in Zig programming language for topological sorting that supports parallel processing.\\n\\n5. **The Myst Graph: A New Perspective on Myst** (githr.com)\\n - 107 points, posted 5 hours ago\\n - An article presenting a new analysis or visualization of the classic video game Myst, likely using graph theory.\\n\\nThese are the top 5 posts currently trending on Hacker News as of April 1, 2025.\", 'error': None}\n"
]
}
],
"source": [
"from langchain_hyperbrowser import HyperbrowserClaudeComputerUseTool\n",
"\n",
"tool = HyperbrowserClaudeComputerUseTool()\n",
"result = tool.run({\"task\": \"Go to Hacker News and summarize the top 5 posts right now\"})\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### With Custom Session Options\n",
"\n",
"All tools support custom session options:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': 'I have found that the react package was last published 11 hours ago. This is the most recently updated package I could find.', 'error': None}\n"
]
}
],
"source": [
"result = tool.run(\n",
" {\n",
" \"task\": \"Go to npmjs.com, and tell me when react package was last updated.\",\n",
" \"session_options\": {\n",
" \"session_options\": {\"use_proxy\": True, \"accept_cookies\": True}\n",
" },\n",
" }\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Async Usage\n",
"\n",
"All tools support async usage:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': 'The page displays information about the \"Example Domain,\" stating that it is used for illustrative purposes and can be utilized without permission. There\\'s a link to \"More information...\" but no specific contact details are provided.', 'error': None}\n"
]
}
],
"source": [
"async def browse_website():\n",
" tool = HyperbrowserBrowserUseTool()\n",
" result = await tool.arun(\n",
" {\n",
" \"task\": \"Go to npmjs.com, click the first visible package, and tell me when it was updated\"\n",
" }\n",
" )\n",
" return result\n",
"\n",
"\n",
"result = await browse_website()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use within an agent\n",
"\n",
"Here's how to use any of the Hyperbrowser tools within an agent:\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"\n",
"Go to npmjs.com, and tell me when react package was last updated.\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" hyperbrowser_browser_use (call_pkAaDjn6kKH9yT3rHDb4hmET)\n",
" Call ID: call_pkAaDjn6kKH9yT3rHDb4hmET\n",
" Args:\n",
" task: Go to npmjs.com and find the last updated date of the React package.\n",
" session_options: None\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: hyperbrowser_browser_use\n",
"\n",
"{\"data\": \"The last updated date of the React package is a day ago.\", \"error\": null}\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"The React package was last updated a day ago.\n"
]
}
],
"source": [
"from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
"from langchain_hyperbrowser import browser_use_tool\n",
"from langchain_openai import ChatOpenAI\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"llm = ChatOpenAI(temperature=0)\n",
"\n",
"# You can use any of the three tools here\n",
"browser_use_tool = HyperbrowserBrowserUseTool()\n",
"agent = create_react_agent(llm, [browser_use_tool])\n",
"\n",
"user_input = \"Go to npmjs.com, and tell me when react package was last updated.\"\n",
"for step in agent.stream(\n",
" {\"messages\": user_input},\n",
" stream_mode=\"values\",\n",
"):\n",
" step[\"messages\"][-1].pretty_print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configuration Options\n",
"\n",
"Claude Computer Use, OpenAI CUA, and Browser Use have the following params available:\n",
"\n",
"- `task`: The task to execute using the agent\n",
"- `max_steps`: The maximum number of interaction steps the agent can take to complete the task\n",
"- `session_options`: Browser session configuration\n",
"\n",
"For more details, see the respective API references:\n",
"- [Browser Use API Reference](https://docs.hyperbrowser.ai/reference/api-reference/agents/browser-use)\n",
"- [OpenAI CUA API Reference](https://docs.hyperbrowser.ai/reference/api-reference/agents/openai-cua)\n",
"- [Claude Computer Use API Reference](https://docs.hyperbrowser.ai/reference/api-reference/agents/claude-computer-use)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"- [GitHub](https://github.com/hyperbrowserai/langchain-hyperbrowser/)\n",
"- [PyPi](https://pypi.org/project/langchain-hyperbrowser/)\n",
"- [Hyperbrowser Docs](https://docs.hyperbrowser.ai/)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "langchain-hyperbrowser-AlekOQAq-py3.12",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -0,0 +1,559 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"sidebar_label: Hyperbrowser Web Scraping Tools\n",
"---\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Hyperbrowser Web Scraping Tools\n",
"\n",
"[Hyperbrowser](https://hyperbrowser.ai) is a platform for running and scaling headless browsers. It lets you launch and manage browser sessions at scale and provides easy to use solutions for any webscraping needs, such as scraping a single page or crawling an entire site.\n",
"\n",
"Key Features:\n",
"\n",
"- Instant Scalability - Spin up hundreds of browser sessions in seconds without infrastructure headaches\n",
"- Simple Integration - Works seamlessly with popular tools like Puppeteer and Playwright\n",
"- Powerful APIs - Easy to use APIs for scraping/crawling any site, and much more\n",
"- Bypass Anti-Bot Measures - Built-in stealth mode, ad blocking, automatic CAPTCHA solving, and rotating proxies\n",
"\n",
"This notebook provides a quick overview for getting started with Hyperbrowser web tools.\n",
"\n",
"For more information about Hyperbrowser, please visit the [Hyperbrowser website](https://hyperbrowser.ai) or if you want to check out the docs, you can visit the [Hyperbrowser docs](https://docs.hyperbrowser.ai).\n",
"\n",
"## Key Capabilities\n",
"\n",
"### Scrape\n",
"\n",
"Hyperbrowser provides powerful scraping capabilities that allow you to extract data from any webpage. The scraping tool can convert web content into structured formats like markdown or HTML, making it easy to process and analyze the data.\n",
"\n",
"### Crawl\n",
"\n",
"The crawling functionality enables you to navigate through multiple pages of a website automatically. You can set parameters like page limits to control how extensively the crawler explores the site, collecting data from each page it visits.\n",
"\n",
"### Extract\n",
"\n",
"Hyperbrowser's extraction capabilities use AI to pull specific information from webpages according to your defined schema. This allows you to transform unstructured web content into structured data that matches your exact requirements.\n",
"\n",
"## Overview\n",
"\n",
"### Integration details\n",
"\n",
"| Tool | Package | Local | Serializable | JS support |\n",
"| :----------- | :--------------------- | :---: | :----------: | :--------: |\n",
"| Crawl Tool | langchain-hyperbrowser | ❌ | ❌ | ❌ |\n",
"| Scrape Tool | langchain-hyperbrowser | ❌ | ❌ | ❌ |\n",
"| Extract Tool | langchain-hyperbrowser | ❌ | ❌ | ❌ |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"To access the Hyperbrowser web tools you'll need to install the `langchain-hyperbrowser` integration package, and create a Hyperbrowser account and get an API key.\n",
"\n",
"### Credentials\n",
"\n",
"Head to [Hyperbrowser](https://app.hyperbrowser.ai/) to sign up and generate an API key. Once you've done this set the HYPERBROWSER_API_KEY environment variable:\n",
"\n",
"```bash\n",
"export HYPERBROWSER_API_KEY=<your-api-key>\n",
"```\n",
"\n",
"### Installation\n",
"\n",
"Install **langchain-hyperbrowser**.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-hyperbrowser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instantiation\n",
"\n",
"### Crawl Tool\n",
"\n",
"The `HyperbrowserCrawlTool` is a powerful tool that can crawl entire websites, starting from a given URL. It supports configurable page limits and scraping options.\n",
"\n",
"```python\n",
"from langchain_hyperbrowser import HyperbrowserCrawlTool\n",
"tool = HyperbrowserCrawlTool()\n",
"```\n",
"\n",
"### Scrape Tool\n",
"\n",
"The `HyperbrowserScrapeTool` is a tool that can scrape content from web pages. It supports both markdown and HTML output formats, along with metadata extraction.\n",
"\n",
"```python\n",
"from langchain_hyperbrowser import HyperbrowserScrapeTool\n",
"tool = HyperbrowserScrapeTool()\n",
"```\n",
"\n",
"### Extract Tool\n",
"\n",
"The `HyperbrowserExtractTool` is a powerful tool that uses AI to extract structured data from web pages. It can extract information based predefined schemas.\n",
"\n",
"```python\n",
"from langchain_hyperbrowser import HyperbrowserExtractTool\n",
"tool = HyperbrowserExtractTool()\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Invocation\n",
"\n",
"### Basic Usage\n",
"\n",
"#### Crawl Tool\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': [CrawledPage(metadata={'url': 'https://www.example.com/', 'title': 'Example Domain', 'viewport': 'width=device-width, initial-scale=1', 'sourceURL': 'https://example.com'}, html=None, markdown='Example Domain\\n\\n# Example Domain\\n\\nThis domain is for use in illustrative examples in documents. You may use this\\ndomain in literature without prior coordination or asking for permission.\\n\\n[More information...](https://www.iana.org/domains/example)', links=None, screenshot=None, url='https://example.com', status='completed', error=None)], 'error': None}\n"
]
}
],
"source": [
"from langchain_hyperbrowser import HyperbrowserCrawlTool\n",
"\n",
"result = HyperbrowserCrawlTool().invoke(\n",
" {\n",
" \"url\": \"https://example.com\",\n",
" \"max_pages\": 2,\n",
" \"scrape_options\": {\"formats\": [\"markdown\"]},\n",
" }\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Scrape Tool\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': ScrapeJobData(metadata={'url': 'https://www.example.com/', 'title': 'Example Domain', 'viewport': 'width=device-width, initial-scale=1', 'sourceURL': 'https://example.com'}, html=None, markdown='Example Domain\\n\\n# Example Domain\\n\\nThis domain is for use in illustrative examples in documents. You may use this\\ndomain in literature without prior coordination or asking for permission.\\n\\n[More information...](https://www.iana.org/domains/example)', links=None, screenshot=None), 'error': None}\n"
]
}
],
"source": [
"from langchain_hyperbrowser import HyperbrowserScrapeTool\n",
"\n",
"result = HyperbrowserScrapeTool().invoke(\n",
" {\"url\": \"https://example.com\", \"scrape_options\": {\"formats\": [\"markdown\"]}}\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Extract Tool\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': {'title': 'Example Domain'}, 'error': None}\n"
]
}
],
"source": [
"from langchain_hyperbrowser import HyperbrowserExtractTool\n",
"from pydantic import BaseModel\n",
"\n",
"\n",
"class SimpleExtractionModel(BaseModel):\n",
" title: str\n",
"\n",
"\n",
"result = HyperbrowserExtractTool().invoke(\n",
" {\n",
" \"url\": \"https://example.com\",\n",
" \"schema\": SimpleExtractionModel,\n",
" }\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### With Custom Options\n",
"\n",
"#### Crawl Tool with Custom Options\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': [CrawledPage(metadata={'url': 'https://www.example.com/', 'title': 'Example Domain', 'viewport': 'width=device-width, initial-scale=1', 'sourceURL': 'https://example.com'}, html=None, markdown='Example Domain\\n\\n# Example Domain\\n\\nThis domain is for use in illustrative examples in documents. You may use this\\ndomain in literature without prior coordination or asking for permission.\\n\\n[More information...](https://www.iana.org/domains/example)', links=None, screenshot=None, url='https://example.com', status='completed', error=None)], 'error': None}\n"
]
}
],
"source": [
"result = HyperbrowserCrawlTool().run(\n",
" {\n",
" \"url\": \"https://example.com\",\n",
" \"max_pages\": 2,\n",
" \"scrape_options\": {\n",
" \"formats\": [\"markdown\", \"html\"],\n",
" },\n",
" \"session_options\": {\"use_proxy\": True, \"solve_captchas\": True},\n",
" }\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Scrape Tool with Custom Options\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': ScrapeJobData(metadata={'url': 'https://www.example.com/', 'title': 'Example Domain', 'viewport': 'width=device-width, initial-scale=1', 'sourceURL': 'https://example.com'}, html='<html><head>\\n <title>Example Domain</title>\\n\\n <meta charset=\"utf-8\">\\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\">\\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\\n \\n</head>\\n\\n<body>\\n<div>\\n <h1>Example Domain</h1>\\n <p>This domain is for use in illustrative examples in documents. You may use this\\n domain in literature without prior coordination or asking for permission.</p>\\n <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\\n</div>\\n\\n\\n</body></html>', markdown='Example Domain\\n\\n# Example Domain\\n\\nThis domain is for use in illustrative examples in documents. You may use this\\ndomain in literature without prior coordination or asking for permission.\\n\\n[More information...](https://www.iana.org/domains/example)', links=None, screenshot=None), 'error': None}\n"
]
}
],
"source": [
"result = HyperbrowserScrapeTool().run(\n",
" {\n",
" \"url\": \"https://example.com\",\n",
" \"scrape_options\": {\n",
" \"formats\": [\"markdown\", \"html\"],\n",
" },\n",
" \"session_options\": {\"use_proxy\": True, \"solve_captchas\": True},\n",
" }\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Extract Tool with Custom Schema\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'data': {'products': [{'price': 9.99, 'title': 'Essence Mascara Lash Princess'}, {'price': 19.99, 'title': 'Eyeshadow Palette with Mirror'}, {'price': 14.99, 'title': 'Powder Canister'}, {'price': 12.99, 'title': 'Red Lipstick'}, {'price': 8.99, 'title': 'Red Nail Polish'}, {'price': 49.99, 'title': 'Calvin Klein CK One'}, {'price': 129.99, 'title': 'Chanel Coco Noir Eau De'}, {'price': 89.99, 'title': \"Dior J'adore\"}, {'price': 69.99, 'title': 'Dolce Shine Eau de'}, {'price': 79.99, 'title': 'Gucci Bloom Eau de'}]}, 'error': None}\n"
]
}
],
"source": [
"from typing import List\n",
"\n",
"from pydantic import BaseModel\n",
"\n",
"\n",
"class ProductSchema(BaseModel):\n",
" title: str\n",
" price: float\n",
"\n",
"\n",
"class ProductsSchema(BaseModel):\n",
" products: List[ProductSchema]\n",
"\n",
"\n",
"result = HyperbrowserExtractTool().run(\n",
" {\n",
" \"url\": \"https://dummyjson.com/products?limit=10\",\n",
" \"schema\": ProductsSchema,\n",
" \"session_options\": {\"session_options\": {\"use_proxy\": True}},\n",
" }\n",
")\n",
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Async Usage\n",
"\n",
"All tools support async usage:\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'List' is not defined",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mNameError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[6]\u001b[39m\u001b[32m, line 10\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mlangchain_hyperbrowser\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[32m 2\u001b[39m HyperbrowserCrawlTool,\n\u001b[32m 3\u001b[39m HyperbrowserExtractTool,\n\u001b[32m 4\u001b[39m HyperbrowserScrapeTool,\n\u001b[32m 5\u001b[39m )\n\u001b[32m 7\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mpydantic\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m BaseModel\n\u001b[32m---> \u001b[39m\u001b[32m10\u001b[39m \u001b[38;5;28;43;01mclass\u001b[39;49;00m\u001b[38;5;250;43m \u001b[39;49m\u001b[34;43;01mExtractionSchema\u001b[39;49;00m\u001b[43m(\u001b[49m\u001b[43mBaseModel\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[32m 11\u001b[39m \u001b[43m \u001b[49m\u001b[43mpopular_library_name\u001b[49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mList\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m]\u001b[49m\n\u001b[32m 14\u001b[39m \u001b[38;5;28;01masync\u001b[39;00m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mweb_operations\u001b[39m():\n\u001b[32m 15\u001b[39m \u001b[38;5;66;03m# Crawl\u001b[39;00m\n",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[6]\u001b[39m\u001b[32m, line 11\u001b[39m, in \u001b[36mExtractionSchema\u001b[39m\u001b[34m()\u001b[39m\n\u001b[32m 10\u001b[39m \u001b[38;5;28;01mclass\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mExtractionSchema\u001b[39;00m(BaseModel):\n\u001b[32m---> \u001b[39m\u001b[32m11\u001b[39m popular_library_name: \u001b[43mList\u001b[49m[\u001b[38;5;28mstr\u001b[39m]\n",
"\u001b[31mNameError\u001b[39m: name 'List' is not defined"
]
}
],
"source": [
"from typing import List\n",
"\n",
"from langchain_hyperbrowser import (\n",
" HyperbrowserCrawlTool,\n",
" HyperbrowserExtractTool,\n",
" HyperbrowserScrapeTool,\n",
")\n",
"from pydantic import BaseModel\n",
"\n",
"\n",
"class ExtractionSchema(BaseModel):\n",
" popular_library_name: List[str]\n",
"\n",
"\n",
"async def web_operations():\n",
" # Crawl\n",
" crawl_tool = HyperbrowserCrawlTool()\n",
" crawl_result = await crawl_tool.arun(\n",
" {\n",
" \"url\": \"https://example.com\",\n",
" \"max_pages\": 5,\n",
" \"scrape_options\": {\"formats\": [\"markdown\"]},\n",
" }\n",
" )\n",
"\n",
" # Scrape\n",
" scrape_tool = HyperbrowserScrapeTool()\n",
" scrape_result = await scrape_tool.arun(\n",
" {\"url\": \"https://example.com\", \"scrape_options\": {\"formats\": [\"markdown\"]}}\n",
" )\n",
"\n",
" # Extract\n",
" extract_tool = HyperbrowserExtractTool()\n",
" extract_result = await extract_tool.arun(\n",
" {\n",
" \"url\": \"https://npmjs.com\",\n",
" \"schema\": ExtractionSchema,\n",
" }\n",
" )\n",
"\n",
" return crawl_result, scrape_result, extract_result\n",
"\n",
"\n",
"results = await web_operations()\n",
"print(results)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Use within an agent\n",
"\n",
"Here's how to use any of the web tools within an agent:\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"\n",
"Crawl https://example.com and get content from up to 5 pages\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" hyperbrowser_crawl_data (call_G2ofdHOqjdnJUZu4hhbuga58)\n",
" Call ID: call_G2ofdHOqjdnJUZu4hhbuga58\n",
" Args:\n",
" url: https://example.com\n",
" max_pages: 5\n",
" scrape_options: {'formats': ['markdown']}\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: hyperbrowser_crawl_data\n",
"\n",
"{'data': [CrawledPage(metadata={'url': 'https://www.example.com/', 'title': 'Example Domain', 'viewport': 'width=device-width, initial-scale=1', 'sourceURL': 'https://example.com'}, html=None, markdown='Example Domain\\n\\n# Example Domain\\n\\nThis domain is for use in illustrative examples in documents. You may use this\\ndomain in literature without prior coordination or asking for permission.\\n\\n[More information...](https://www.iana.org/domains/example)', links=None, screenshot=None, url='https://example.com', status='completed', error=None)], 'error': None}\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"I have crawled the website [https://example.com](https://example.com) and retrieved content from the first page. Here is the content in markdown format:\n",
"\n",
"```\n",
"Example Domain\n",
"\n",
"# Example Domain\n",
"\n",
"This domain is for use in illustrative examples in documents. You may use this\n",
"domain in literature without prior coordination or asking for permission.\n",
"\n",
"[More information...](https://www.iana.org/domains/example)\n",
"```\n",
"\n",
"If you would like to crawl more pages or need additional information, please let me know!\n"
]
}
],
"source": [
"from langchain_hyperbrowser import HyperbrowserCrawlTool\n",
"from langchain_openai import ChatOpenAI\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"# Initialize the crawl tool\n",
"crawl_tool = HyperbrowserCrawlTool()\n",
"\n",
"# Create the agent with the crawl tool\n",
"llm = ChatOpenAI(temperature=0)\n",
"\n",
"agent = create_react_agent(llm, [crawl_tool])\n",
"user_input = \"Crawl https://example.com and get content from up to 5 pages\"\n",
"for step in agent.stream(\n",
" {\"messages\": user_input},\n",
" stream_mode=\"values\",\n",
"):\n",
" step[\"messages\"][-1].pretty_print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configuration Options\n",
"\n",
"### Common Options\n",
"\n",
"All tools support these basic configuration options:\n",
"\n",
"- `url`: The URL to process\n",
"- `session_options`: Browser session configuration\n",
" - `use_proxy`: Whether to use a proxy\n",
" - `solve_captchas`: Whether to automatically solve CAPTCHAs\n",
" - `accept_cookies`: Whether to accept cookies\n",
"\n",
"### Tool-Specific Options\n",
"\n",
"#### Crawl Tool\n",
"\n",
"- `max_pages`: Maximum number of pages to crawl\n",
"- `scrape_options`: Options for scraping each page\n",
" - `formats`: List of output formats (markdown, html)\n",
"\n",
"#### Scrape Tool\n",
"\n",
"- `scrape_options`: Options for scraping the page\n",
" - `formats`: List of output formats (markdown, html)\n",
"\n",
"#### Extract Tool\n",
"\n",
"- `schema`: Pydantic model defining the structure to extract\n",
"- `extraction_prompt`: Natural language prompt for extraction\n",
"\n",
"For more details, see the respective API references:\n",
"\n",
"- [Crawl API Reference](https://docs.hyperbrowser.ai/reference/api-reference/crawl)\n",
"- [Scrape API Reference](https://docs.hyperbrowser.ai/reference/api-reference/scrape)\n",
"- [Extract API Reference](https://docs.hyperbrowser.ai/reference/api-reference/extract)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"- [GitHub](https://github.com/hyperbrowserai/langchain-hyperbrowser/)\n",
"- [PyPi](https://pypi.org/project/langchain-hyperbrowser/)\n",
"- [Hyperbrowser Docs](https://docs.hyperbrowser.ai/)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "langchain-hyperbrowser-AlekOQAq-py3.12",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -166,23 +166,21 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[WatsonxTool(name='GoogleSearch', description='Search for online trends, news, current events, real-time information, or research topics.', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Search for online trends, news, current events, real-time information, or research topics.', tool_config_schema={'title': 'config schema for GoogleSearch tool', 'type': 'object', 'properties': {'maxResults': {'title': 'Max number of results to return', 'type': 'integer', 'minimum': 1, 'maximum': 20}}}, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>),\n",
" WatsonxTool(name='WebCrawler', description='Useful for when you need to summarize a webpage. Do not use for Web search.', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Useful for when you need to summarize a webpage. Do not use for Web search.', tool_input_schema={'type': 'object', 'properties': {'url': {'title': 'url', 'description': 'URL for the webpage to be scraped', 'type': 'string', 'pattern': '^(https?:\\\\/\\\\/)?([\\\\da-z\\\\.-]+)\\\\.([a-z\\\\.]{2,6})([\\\\/\\\\w \\\\.-]*)*\\\\/?$'}}, 'required': ['url']}, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>),\n",
" WatsonxTool(name='PythonInterpreter', description='Run Python code generated by the agent model.', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Run Python code and return the console output. Use for isolated calculations, computations or data manipulation. In Python, the following modules are available: Use numpy, pandas, scipy and sympy for working with data. Use matplotlib to plot charts. Other Python libraries are also available -- however, prefer using the ones above. Prefer using qualified imports -- `import library; library.thing()` instead of `import thing from library`. Do not attempt to install libraries manually -- it will not work. Do not use this tool multiple times in a row, always write the full code you want to run in a single invocation. If you get an error running Python code, try to generate a better one that will pass. If the tool returns result that starts with IMAGE(, follow instructions for rendering images.', watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>),\n",
" WatsonxTool(name='SDXLTurbo', description='Generate an image from text using Stability.ai', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Generate an image from text. Not for image refining. Use very precise language about the desired image, including setting, lighting, style, filters and lenses used. Do not ask the tool to refine an image.', watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>),\n",
" WatsonxTool(name='Weather', description='Find the weather for a city.', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Find the weather for a city.', tool_input_schema={'type': 'object', 'properties': {'location': {'title': 'location', 'description': 'Name of the location', 'type': 'string'}, 'country': {'title': 'country', 'description': 'Name of the state or country', 'type': 'string'}}, 'required': ['location']}, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>),\n",
" WatsonxTool(name='RAGQuery', description='Search the documents in a vector index.', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Search information in documents to provide context to a user query. Useful when asked to ground the answer in specific knowledge about {indexName}', tool_config_schema={'title': 'config schema for RAGQuery tool', 'type': 'object', 'properties': {'vectorIndexId': {'title': 'Vector index identifier', 'type': 'string'}, 'projectId': {'title': 'Project identifier', 'type': 'string'}, 'spaceId': {'title': 'Space identifier', 'type': 'string'}}, 'required': ['vectorIndexId'], 'oneOf': [{'required': ['projectId']}, {'required': ['spaceId']}]}, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>)]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"[WatsonxTool(name='GoogleSearch', description='Search for online trends, news, current events, real-time information, or research topics.', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Search for online trends, news, current events, real-time information, or research topics.', tool_config_schema={'title': 'config schema for GoogleSearch tool', 'type': 'object', 'properties': {'maxResults': {'title': 'Max number of results to return', 'type': 'integer', 'minimum': 1, 'maximum': 20}}}, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>),\n",
" WatsonxTool(name='WebCrawler', description='Useful for when you need to summarize a webpage. Do not use for Web search.', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Useful for when you need to summarize a webpage. Do not use for Web search.', tool_input_schema={'type': 'object', 'properties': {'url': {'title': 'url', 'description': 'URL for the webpage to be scraped', 'type': 'string', 'pattern': '^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$'}}, 'required': ['url']}, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>),\n",
" WatsonxTool(name='SDXLTurbo', description='Generate an image from text using Stability.ai', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Generate an image from text. Not for image refining. Use very precise language about the desired image, including setting, lighting, style, filters and lenses used. Do not ask the tool to refine an image.', watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>),\n",
" WatsonxTool(name='Weather', description='Find the weather for a city.', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Find the weather for a city.', tool_input_schema={'type': 'object', 'properties': {'location': {'title': 'location', 'description': 'Name of the location', 'type': 'string'}, 'country': {'title': 'country', 'description': 'Name of the state or country', 'type': 'string'}}, 'required': ['location']}, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>),\n",
" WatsonxTool(name='RAGQuery', description='Search the documents in a vector index.', args_schema=<class 'langchain_ibm.toolkit.ToolArgsSchema'>, agent_description='Search information in documents to provide context to a user query. Useful when asked to ground the answer in specific knowledge about {indexName}', tool_config_schema={'title': 'config schema for RAGQuery tool', 'type': 'object', 'properties': {'vectorIndexId': {'title': 'Vector index identifier', 'type': 'string'}, 'projectId': {'title': 'Project identifier', 'type': 'string'}, 'spaceId': {'title': 'Space identifier', 'type': 'string'}}, 'required': ['vectorIndexId'], 'oneOf': [{'required': ['projectId']}, {'required': ['spaceId']}]}, watsonx_client=<ibm_watsonx_ai.client.APIClient object at 0x127e0f490>)]\n",
"\n"
]
}
],
"source": [
@@ -228,7 +226,7 @@
{
"data": {
"text/plain": [
"{'output': '[{\"title\":\"IBM - United States\",\"description\":\"Technology & Consulting. From next-generation AI to cutting edge hybrid cloud solutions to the deep expertise of IBM Consulting, IBM has what it takes to help\\xa0...\",\"url\":\"https://www.ibm.com/us-en\"},{\"title\":\"IBM - Wikipedia\",\"description\":\"International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American multinational technology company headquartered in\\xa0...\",\"url\":\"https://en.wikipedia.org/wiki/IBM\"},{\"title\":\"IBM Envizi ESG Suite\",\"description\":\"Envizi systemizes the capture, transformation and consolidation of disparate sustainability data into a single source of truth and delivers actionable insights.\",\"url\":\"https://www.ibm.com/products/envizi\"},{\"title\":\"IBM Research\",\"description\":\"Tools + Code · BeeAI Framework. Open-source framework for building, deploying, and serving powerful agentic workflows at scale. · Docling. An open-source tool\\xa0...\",\"url\":\"https://research.ibm.com/\"},{\"title\":\"IBM SkillsBuild: Free Skills-Based Learning From Technology Experts\",\"description\":\"IBM SkillsBuildPower your future in tech with job skills, courses, and credentials—for free. Power your future in tech with job skills, courses, and credentials\\xa0...\",\"url\":\"https://skillsbuild.org/\"},{\"title\":\"IBM | LinkedIn\",\"description\":\"Locations · Primary. International Business Machines Corp. · 590 Madison Ave · 90 Grayston Dr · Plaza Independencia 721 · 388 Phahon Yothin Road · Jalan Prof.\",\"url\":\"https://www.linkedin.com/company/ibm\"},{\"title\":\"International Business Machines Corporation (IBM) Stock Price ...\",\"description\":\"Mar 18, 2025 ... International Business Machines Corporation (IBM) · 1.19% · -2.77% · 11.49% · 12.00% · 29.04% · 138.39% · 3,238.84%. Key Events. Mountain.\",\"url\":\"https://finance.yahoo.com/quote/IBM/\"},{\"title\":\"Zurich - IBM Research\",\"description\":\"The location in Zurich is one of IBM\\'s 12 global research labs. IBM has maintained a research laboratory in Switzerland since 1956.\",\"url\":\"https://research.ibm.com/labs/zurich\"},{\"title\":\"IBM Newsroom\",\"description\":\"News and press releases from around the IBM world. Media contacts. Sources by topic and by region. IBM Media center. Explore IBM\\'s latest and most popular\\xa0...\",\"url\":\"https://newsroom.ibm.com/\"},{\"title\":\"IBM (@ibm) • Instagram photos and videos\",\"description\":\"Science, Technology & Engineering. We partner with developers, data scientists, CTOs and other creators to make the world work better.\",\"url\":\"https://www.instagram.com/ibm/?hl=en\"}]'}"
"{'output': '[{\"title\":\"IBM - United States\",\"description\":\"Technology & Consulting. From next-generation AI to cutting edge hybrid cloud solutions to the deep expertise of IBM Consulting, IBM has what it takes to help\\xa0...\",\"url\":\"https://www.ibm.com/us-en\"},{\"title\":\"IBM - Wikipedia\",\"description\":\"International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American multinational technology company headquartered in\\xa0...\",\"url\":\"https://en.wikipedia.org/wiki/IBM\"},{\"title\":\"IBM Envizi ESG Suite\",\"description\":\"Envizi systemizes the capture, transformation and consolidation of disparate sustainability data into a single source of truth and delivers actionable insights.\",\"url\":\"https://www.ibm.com/products/envizi\"},{\"title\":\"IBM Research\",\"description\":\"Tools + Code · BeeAI Framework. Open-source framework for building, deploying, and serving powerful agentic workflows at scale. · Docling. An open-source tool\\xa0...\",\"url\":\"https://research.ibm.com/\"},{\"title\":\"IBM SkillsBuild: Free Skills-Based Learning From Technology Experts\",\"description\":\"IBM SkillsBuildPower your future in tech with job skills, courses, and credentials—for free. Power your future in tech with job skills, courses, and credentials\\xa0...\",\"url\":\"https://skillsbuild.org/\"},{\"title\":\"IBM | LinkedIn\",\"description\":\"Locations · Primary. International Business Machines Corp. · 590 Madison Ave · 90 Grayston Dr · Plaza Independencia 721 · 388 Phahon Yothin Road · Jalan Prof.\",\"url\":\"https://www.linkedin.com/company/ibm\"},{\"title\":\"International Business Machines Corporation (IBM)\",\"description\":\"PROFITABILITY_AND_INCOME_STATEMENT · 9.60% · (TTM). 3.06% · (TTM). 24.06% · (TTM). 62.75B · (TTM). 6.02B · (TTM). 6.41. BALANCE_SHEET_AND_CASH_FLOW. (MRQ).\",\"url\":\"https://finance.yahoo.com/quote/IBM/\"},{\"title\":\"Zurich - IBM Research\",\"description\":\"The location in Zurich is one of IBM\\'s 12 global research labs. IBM has maintained a research laboratory in Switzerland since 1956.\",\"url\":\"https://research.ibm.com/labs/zurich\"},{\"title\":\"IBM (@ibm) • Instagram photos and videos\",\"description\":\"Science, Technology & Engineering. We partner with developers, data scientists, CTOs and other creators to make the world work better.\",\"url\":\"https://www.instagram.com/ibm/?hl=en\"},{\"title\":\"IBM Newsroom\",\"description\":\"News and press releases from around the IBM world. Media contacts. Sources by topic and by region. IBM Media center. Explore IBM\\'s latest and most popular\\xa0...\",\"url\":\"https://newsroom.ibm.com/\"}]'}"
]
},
"execution_count": 5,
@@ -416,7 +414,7 @@
{
"data": {
"text/plain": [
"{'output': 'Current weather in New York:\\nTemperature: 12.1°C\\nRain: 0mm\\nRelative humidity: 36%\\nWind: 8.1km/h\\n'}"
"{'output': 'Current weather in New York:\\nTemperature: 0°C\\nRain: 0mm\\nRelative humidity: 63%\\nWind: 7.6km/h\\n'}"
]
},
"execution_count": 12,
@@ -450,10 +448,10 @@
"output_type": "stream",
"text": [
"Current weather in New York:\n",
"Temperature: 12.1°C\n",
"Temperature: 0°C\n",
"Rain: 0mm\n",
"Relative humidity: 36%\n",
"Wind: 8.1km/h\n",
"Relative humidity: 63%\n",
"Wind: 7.6km/h\n",
"\n"
]
}
@@ -480,7 +478,7 @@
{
"data": {
"text/plain": [
"ToolMessage(content='{\"output\": \"Current weather in Los Angeles:\\\\nTemperature: 13.3°C\\\\nRain: 0mm\\\\nRelative humidity: 89%\\\\nWind: 3.3km/h\\\\n\"}', name='Weather', tool_call_id='1')"
"ToolMessage(content='{\"output\": \"Current weather in Los Angeles:\\\\nTemperature: 8.6°C\\\\nRain: 0mm\\\\nRelative humidity: 61%\\\\nWind: 8.4km/h\\\\n\"}', name='Weather', tool_call_id='1')"
]
},
"execution_count": 14,
@@ -510,7 +508,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -537,7 +535,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -549,21 +547,17 @@
"What is the weather in Boston?\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" Weather (chatcmpl-tool-fe07ca2f5b5a4fd584eaedc889bea5b7)\n",
" Call ID: chatcmpl-tool-fe07ca2f5b5a4fd584eaedc889bea5b7\n",
" Weather (chatcmpl-tool-6a6c21402c824e43bdd2e8ba390af4a8)\n",
" Call ID: chatcmpl-tool-6a6c21402c824e43bdd2e8ba390af4a8\n",
" Args:\n",
" location: Boston\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: Weather\n",
"\n",
"{\"output\": \"Current weather in Boston:\\nTemperature: 9.1°C\\nRain: 0mm\\nRelative humidity: 41%\\nWind: 24.5km/h\\n\"}\n",
"{\"output\": \"Current weather in Boston:\\nTemperature: -1°C\\nRain: 0mm\\nRelative humidity: 53%\\nWind: 8.3km/h\\n\"}\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"The current weather in Boston is:\n",
"Temperature: 9.1°C\n",
"Rain: 0mm\n",
"Relative humidity: 41%\n",
"Wind: 24.5km/h\n"
"The current weather in Boston is -1°C with 0mm of rain, a relative humidity of 53%, and a wind speed of 8.3km/h.\n"
]
}
],
@@ -590,7 +584,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "langchain-ibm",
"display_name": "langchain_env",
"language": "python",
"name": "python3"
},

View File

@@ -14,10 +14,12 @@
"## Installation and setup\n",
"\n",
"To use this tool, you must first set as environment variables:\n",
" JIRA_API_TOKEN\n",
" JIRA_USERNAME\n",
" JIRA_INSTANCE_URL\n",
" JIRA_CLOUD"
" JIRA_INSTANCE_URL,\n",
" JIRA_CLOUD\n",
"\n",
"You have the choice between two authentication methods:\n",
"- API token authentication: set the JIRA_API_TOKEN (and JIRA_USERNAME if needed) environment variables\n",
"- OAuth2.0 authentication: set the JIRA_OAUTH2 environment variable as a dict having as fields \"client_id\" and \"token\" which is a dict containing at least \"access_token\" and \"token_type\""
]
},
{
@@ -79,6 +81,12 @@
"from langchain_openai import OpenAI"
]
},
{
"cell_type": "markdown",
"id": "3c925f1468696e4c",
"metadata": {},
"source": "For authentication with API token"
},
{
"cell_type": "code",
"execution_count": 3,
@@ -109,6 +117,27 @@
"os.environ[\"JIRA_CLOUD\"] = \"True\""
]
},
{
"cell_type": "markdown",
"id": "325ea81fb416aac6",
"metadata": {},
"source": "For authentication with a Oauth2.0"
},
{
"cell_type": "code",
"execution_count": null,
"id": "917e83e3a764d91a",
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"JIRA_OAUTH2\"] = (\n",
" '{\"client_id\": \"123\", \"token\": {\"access_token\": \"abc\", \"token_type\": \"bearer\"}}'\n",
")\n",
"os.environ[\"JIRA_INSTANCE_URL\"] = \"https://jira.atlassian.com\"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"xyz\"\n",
"os.environ[\"JIRA_CLOUD\"] = \"True\""
]
},
{
"cell_type": "code",
"execution_count": 4,
@@ -215,15 +244,15 @@
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m I need to create an issue in project PW\n",
"\u001B[1m> Entering new AgentExecutor chain...\u001B[0m\n",
"\u001B[32;1m\u001B[1;3m I need to create an issue in project PW\n",
"Action: Create Issue\n",
"Action Input: {\"summary\": \"Make more fried rice\", \"description\": \"Reminder to make more fried rice\", \"issuetype\": {\"name\": \"Task\"}, \"priority\": {\"name\": \"Low\"}, \"project\": {\"key\": \"PW\"}}\u001b[0m\n",
"Observation: \u001b[38;5;200m\u001b[1;3mNone\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
"Final Answer: A new issue has been created in project PW with the summary \"Make more fried rice\" and description \"Reminder to make more fried rice\".\u001b[0m\n",
"Action Input: {\"summary\": \"Make more fried rice\", \"description\": \"Reminder to make more fried rice\", \"issuetype\": {\"name\": \"Task\"}, \"priority\": {\"name\": \"Low\"}, \"project\": {\"key\": \"PW\"}}\u001B[0m\n",
"Observation: \u001B[38;5;200m\u001B[1;3mNone\u001B[0m\n",
"Thought:\u001B[32;1m\u001B[1;3m I now know the final answer\n",
"Final Answer: A new issue has been created in project PW with the summary \"Make more fried rice\" and description \"Reminder to make more fried rice\".\u001B[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
"\u001B[1m> Finished chain.\u001B[0m\n"
]
},
{

View File

@@ -0,0 +1,298 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "65142ccb18a6add5",
"metadata": {},
"source": "# Oxylabs"
},
{
"cell_type": "markdown",
"id": "19976df355f9c461",
"metadata": {},
"source": [
">[Oxylabs](https://oxylabs.io/) is a market-leading web intelligence collection platform, driven by the highest business, ethics, and compliance standards, enabling companies worldwide to unlock data-driven insights.\n",
"\n",
"## Overview\n",
"\n",
"This package contains the LangChain integration with Oxylabs, providing tools to scrape Google search results with Oxylabs Web Scraper API using LangChain's framework.\n",
"\n",
"The following classes are provided by this package:\n",
"- `OxylabsSearchRun` - A tool that returns scraped Google search results in a formatted text\n",
"- `OxylabsSearchResults` - A tool that returns scraped Google search results in a JSON format\n",
"- `OxylabsSearchAPIWrapper` - An API wrapper for initializing Oxylabs API"
]
},
{
"cell_type": "markdown",
"id": "76e6d8030199710d",
"metadata": {},
"source": [
"| Pricing |\n",
"|:-------------------------------:|\n",
"| ✅ Free 5,000 results for 1 week |"
]
},
{
"cell_type": "markdown",
"id": "eb59c5f3051be9d4",
"metadata": {},
"source": "## Setup"
},
{
"cell_type": "markdown",
"id": "45951d881c460419",
"metadata": {},
"source": "Install the required dependencies."
},
{
"cell_type": "code",
"id": "576e08c41f16ceda",
"metadata": {},
"source": [
"%pip install -qU langchain-oxylabs"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "ce19bb3a52a3ab60",
"metadata": {},
"source": "### Credentials"
},
{
"cell_type": "markdown",
"id": "b8330dfd5861482e",
"metadata": {},
"source": "Set up the proper API keys and environment variables. Create your API user credentials: Sign up for a free trial or purchase the product in the [Oxylabs dashboard](https://dashboard.oxylabs.io/en/registration) to create your API user credentials (OXYLABS_USERNAME and OXYLABS_PASSWORD)."
},
{
"cell_type": "code",
"id": "474b6eaf6e35efda",
"metadata": {},
"source": [
"import getpass\n",
"import os\n",
"\n",
"os.environ[\"OXYLABS_USERNAME\"] = getpass.getpass(\"Enter your Oxylabs username: \")\n",
"os.environ[\"OXYLABS_PASSWORD\"] = getpass.getpass(\"Enter your Oxylabs password: \")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "ae310f86b113bd78",
"metadata": {},
"source": "## Instantiation"
},
{
"cell_type": "code",
"id": "69ea8139f6152b48",
"metadata": {},
"source": [
"from langchain_oxylabs import OxylabsSearchAPIWrapper, OxylabsSearchRun\n",
"\n",
"oxylabs_wrapper = OxylabsSearchAPIWrapper()\n",
"tool_ = OxylabsSearchRun(wrapper=oxylabs_wrapper)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "636efff6267b9bc1",
"metadata": {},
"source": "## Invocation"
},
{
"cell_type": "markdown",
"id": "272f5fdaed5fa000",
"metadata": {},
"source": "### Invoke directly with args"
},
{
"cell_type": "markdown",
"id": "3c26724911f6cfdf",
"metadata": {},
"source": "The `OxylabsSearchRun` tool takes a single \"query\" argument, which should be a natural language query and returns combined string format result:"
},
{
"cell_type": "code",
"id": "b5f4d423cb3abf",
"metadata": {},
"source": [
"tool_.invoke({\"query\": \"Restaurants in Paris.\"})"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "784aad18bba31062",
"metadata": {},
"source": "### Invoke with ToolCall"
},
{
"cell_type": "code",
"id": "8afd1652e2c4e7c9",
"metadata": {},
"source": [
"tool_ = OxylabsSearchRun(\n",
" wrapper=oxylabs_wrapper,\n",
" kwargs={\n",
" \"result_categories\": [\n",
" \"local_information\",\n",
" \"combined_search_result\",\n",
" ]\n",
" },\n",
")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"id": "1b903fb2f8272df2",
"metadata": {},
"source": [
"from pprint import pprint\n",
"\n",
"model_generated_tool_call = {\n",
" \"args\": {\n",
" \"query\": \"Visit restaurants in Vilnius.\",\n",
" \"geo_location\": \"Vilnius,Lithuania\",\n",
" },\n",
" \"id\": \"1\",\n",
" \"name\": \"oxylabs_search\",\n",
" \"type\": \"tool_call\",\n",
"}\n",
"tool_call_result = tool_.invoke(model_generated_tool_call)\n",
"\n",
"# The content is a JSON string of results\n",
"pprint(tool_call_result.content)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "a436fc39ce17b6da",
"metadata": {},
"source": [
"## Use within an agent\n",
"Install the required dependencies."
]
},
{
"cell_type": "code",
"id": "a6f23e65092e8d27",
"metadata": {},
"source": "%pip install -qU \"langchain[openai]\" langgraph",
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"id": "21078a0c265759ff",
"metadata": {},
"source": [
"import getpass\n",
"import os\n",
"\n",
"from langchain.chat_models import init_chat_model\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter API key for OpenAI: \")\n",
"llm = init_chat_model(\"gpt-4o-mini\", model_provider=\"openai\")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"id": "396548ee2f08fb30",
"metadata": {},
"source": [
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"# Initialize OxylabsSearchRun tool\n",
"tool_ = OxylabsSearchRun(wrapper=oxylabs_wrapper)\n",
"\n",
"agent = create_react_agent(llm, [tool_])\n",
"\n",
"user_input = \"What happened in the latest Burning Man floods?\"\n",
"\n",
"for step in agent.stream(\n",
" {\"messages\": user_input},\n",
" stream_mode=\"values\",\n",
"):\n",
" step[\"messages\"][-1].pretty_print()"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "b10bbcf5ce299fd5",
"metadata": {},
"source": [
"## JSON results\n",
"`OxylabsSearchResults` tool can be used as an alternative to `OxylabsSearchRun` to retrieve results in a JSON format:"
]
},
{
"cell_type": "code",
"id": "ef0d856f571c4938",
"metadata": {},
"source": [
"import json\n",
"\n",
"from langchain_oxylabs import OxylabsSearchResults\n",
"\n",
"tool_ = OxylabsSearchResults(wrapper=oxylabs_wrapper)\n",
"\n",
"response_results = tool_.invoke({\"query\": \"What are the most famous artists?\"})\n",
"response_results = json.loads(response_results)\n",
"\n",
"for result in response_results:\n",
" for key, value in result.items():\n",
" print(f\"{key}: {value}\")"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"id": "7f16702d224dabb2",
"metadata": {},
"source": [
"## API reference\n",
"More information about this integration package can be found here: https://github.com/oxylabs/langchain-oxylabs\n",
"\n",
"Oxylabs Web Scraper API documentation: https://developers.oxylabs.io/scraper-apis/web-scraper-api\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

View File

@@ -106,9 +106,9 @@
"from langchain_core.tools import Tool\n",
"\n",
"# You can create the tool to pass to an agent\n",
"repl_tool = Tool(\n",
" name=\"python_repl\",\n",
" description=\"A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\",\n",
"custom_tool = Tool(\n",
" name=\"web search\",\n",
" description=\"Search the web for information\",\n",
" func=search.run,\n",
")"
]

View File

@@ -98,7 +98,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "310d21b3",
"metadata": {},
"outputs": [],
@@ -125,7 +125,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "ccfb4159-34ac-4816-a8f0-795c5442c0b2",
"metadata": {},
"outputs": [],
@@ -148,16 +148,16 @@
" \"TABLEAU_JWT_SECRET\"\n",
") # a JWT secret ID (obtained through Tableau's admin UI)\n",
"tableau_api_version = \"3.21\" # the current Tableau REST API Version\n",
"tableau_user = \"joe.constantino@salesforce.com\" # replace with the username querying the target Tableau Data Source\n",
"tableau_user = \"joe.constantino@salesforce.com\" # enter the username querying the target Tableau Data Source\n",
"\n",
"# For this cookbook we are connecting to the Superstore dataset that comes by default with every Tableau server\n",
"datasource_luid = (\n",
" \"0965e61b-a072-43cf-994c-8c6cf526940d\" # the target data source for this Tool\n",
")\n",
"\n",
"model_provider = \"openai\" # the name of the model provider you are using for your Agent\n",
"# Add variables to control LLM models for the Agent and Tools\n",
"os.environ[\"OPENAI_API_KEY\"] # set an your model API key as an environment variable\n",
"tooling_llm_model = \"gpt-4o\""
"tooling_llm_model = \"gpt-4o-mini\""
]
},
{
@@ -178,7 +178,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "72ee3eca",
"metadata": {},
"outputs": [],
@@ -194,6 +194,7 @@
" tableau_user=tableau_user,\n",
" datasource_luid=datasource_luid,\n",
" tooling_llm_model=tooling_llm_model,\n",
" model_provider=model_provider,\n",
")\n",
"\n",
"# load the List of Tools to be used by the Agent. In this case we will just load our data source Q&A tool.\n",
@@ -211,47 +212,14 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"id": "06a1d3f7-79a8-452e-b37e-9070d15445b0",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Here are the results for the states with the highest sales and profits based on the data queried:\n",
"\n",
"### States with the Most Sales\n",
"1. **California**: $457,687.63\n",
"2. **New York**: $310,876.27\n",
"3. **Texas**: $170,188.05\n",
"4. **Washington**: $138,641.27\n",
"5. **Pennsylvania**: $116,511.91\n",
"\n",
"### States with the Most Profit\n",
"1. **California**: $76,381.39\n",
"2. **New York**: $74,038.55\n",
"3. **Washington**: $33,402.65\n",
"4. **Michigan**: $24,463.19\n",
"5. **Virginia**: $18,597.95\n",
"\n",
"### Comparison\n",
"- **California** and **New York** are the only states that appear in both lists, indicating they are the top sellers and also generate the most profit.\n",
"- **Texas**, while having the third highest sales, does not rank in the top five for profit, showing a potential issue with profitability despite high sales.\n",
"\n",
"This analysis suggests that high sales do not always correlate with high profits, as seen with Texas."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"from IPython.display import Markdown, display\n",
"\n",
"model = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
"model = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n",
"\n",
"tableauAgent = create_react_agent(model, tools)\n",
"\n",
@@ -261,13 +229,13 @@
" \"messages\": [\n",
" (\n",
" \"human\",\n",
" \"which states sell the most? Are those the same states with the most profits?\",\n",
" \"what's going on with table sales?\",\n",
" )\n",
" ]\n",
" }\n",
")\n",
"messages\n",
"# display(Markdown(messages['messages'][4].content)) #display a nicely formatted answer for successful generations"
"# display(Markdown(messages['messages'][3].content)) #display a nicely formatted answer for successful generations"
]
},
{
@@ -293,9 +261,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python (package_test_env)",
"display_name": "Python 3",
"language": "python",
"name": "package_test_env"
"name": "python3"
},
"language_info": {
"codemirror_mode": {

View File

@@ -643,9 +643,7 @@
"}\n",
"\n",
"results = vector_search.similarity_search_with_score(\n",
" query=query,\n",
" k=5,\n",
" query_type=CosmosDBQueryType.HYBRID,\n",
" query=query, k=5, query_type=CosmosDBQueryType.HYBRID, pre_filter=pre_filter\n",
")\n",
"\n",
"# Display results\n",

View File

@@ -23,7 +23,7 @@
"metadata": {},
"outputs": [],
"source": [
"! docker run -d -p 8123:8123 -p9000:9000 --name langchain-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server:23.4.2.11"
"! docker run -d -p 8123:8123 -p9000:9000 --name langchain-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server:24.7.6.8"
]
},
{

File diff suppressed because it is too large Load Diff

View File

@@ -20,15 +20,26 @@
"source": [
"## Setup\n",
"\n",
"To access the `CouchbaseVectorStore` you first need to install the `langchain-couchbase` partner package:"
"To access the `CouchbaseSearchVectorStore` you first need to install the `langchain-couchbase` partner package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "bec8d532-fec7-4dc7-9be3-020aa7bdb01f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.1.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0.1\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"pip install -qU langchain-couchbase"
]
@@ -45,10 +56,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "d98e3baa",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the connection string for the Couchbase cluster: ········\n",
"Enter the username for the Couchbase cluster: ········\n",
"Enter the password for the Couchbase cluster: ········\n"
]
}
],
"source": [
"import getpass\n",
"\n",
@@ -104,7 +125,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "9986c6b9",
"metadata": {},
"outputs": [],
@@ -135,14 +156,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "1b1d0a26-e9d4-4823-9800-9549d24d3d16",
"metadata": {},
"outputs": [],
"source": [
"BUCKET_NAME = \"langchain_bucket\"\n",
"SCOPE_NAME = \"_default\"\n",
"COLLECTION_NAME = \"default\"\n",
"COLLECTION_NAME = \"_default\"\n",
"SEARCH_INDEX_NAME = \"langchain-test-index\""
]
},
@@ -174,7 +195,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "6706efdd",
"metadata": {},
"outputs": [],
@@ -188,14 +209,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "33db4670-76c5-49ba-94d6-a8fa35583058",
"metadata": {},
"outputs": [],
"source": [
"from langchain_couchbase.vectorstores import CouchbaseVectorStore\n",
"from langchain_couchbase.vectorstores import CouchbaseSearchVectorStore\n",
"\n",
"vector_store = CouchbaseVectorStore(\n",
"vector_store = CouchbaseSearchVectorStore(\n",
" cluster=cluster,\n",
" bucket_name=BUCKET_NAME,\n",
" scope_name=SCOPE_NAME,\n",
@@ -217,12 +238,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "49c38634",
"metadata": {},
"outputs": [],
"source": [
"vector_store_specific = CouchbaseVectorStore(\n",
"vector_store_specific = CouchbaseSearchVectorStore(\n",
" cluster=cluster,\n",
" bucket_name=BUCKET_NAME,\n",
" scope_name=SCOPE_NAME,\n",
@@ -250,10 +271,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "65a35f00",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['4a6b5252-24ca-4e48-97a9-c33211fc7736',\n",
" '594a413d-761a-44f1-8f0c-6418700b198d',\n",
" 'fdd8461c-f4e3-4c85-af8e-7782ce4d2311',\n",
" '3f6a82b2-7464-4eee-b209-cbca5a236a8a',\n",
" 'df8b87ad-464e-4f83-a007-ccf5a8fa4ff5',\n",
" 'aa18502e-6fb4-4578-9c63-b9a299259b01',\n",
" '8c55a17d-5fa7-4c30-a55d-7ded0d39bf46',\n",
" '41b68c5a-ebf5-4d7a-a079-5e32926ca484',\n",
" '146ac3e0-474a-422a-b0ac-c9fee718396b',\n",
" 'e44941e9-fb3a-4090-88a0-9ffecee3e80e']"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from uuid import uuid4\n",
"\n",
@@ -336,10 +377,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "3a05f294",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vector_store.delete(ids=[uuids[-1]])"
]
@@ -362,10 +414,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "8e00bb23",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Building an exciting new project with LangChain - come check it out! [{'source': 'tweet'}]\n",
"* LangGraph is the best framework for building stateful, agentic applications! [{'source': 'tweet'}]\n"
]
}
],
"source": [
"results = vector_store.similarity_search(\n",
" \"LangChain provides abstractions to make working with LLMs easy\",\n",
@@ -387,10 +448,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "24b146b2-55a2-4fe8-8659-3649032f5dc7",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* [SIM=0.553145] The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees. [{'source': 'news'}]\n"
]
}
],
"source": [
"results = vector_store.similarity_search_with_score(\"Will it be hot tomorrow?\", k=1)\n",
"for res, score in results:\n",
@@ -415,10 +484,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "ffa743dc-4e89-405b-ad71-7390338889e6",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"page_content='I had chocalate chip pancakes and scrambled eggs for breakfast this morning.' metadata={'source': 'tweet'}\n"
]
}
],
"source": [
"query = \"What did I eat for breakfast today?\"\n",
"results = vector_store.similarity_search(query, fields=[\"metadata.source\"])\n",
@@ -452,10 +529,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"id": "7d2e607d-6bbc-4cef-83e3-b6a28bb269ea",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'author': 'John Doe', 'date': '2016-01-01', 'rating': 2, 'source': '../../how_to/state_of_the_union.txt'}\n"
]
}
],
"source": [
"from langchain_community.document_loaders import TextLoader\n",
"from langchain_text_splitters import CharacterTextSplitter\n",
@@ -489,10 +574,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "dc06ba4a-8a6b-4c55-bb69-95cd92db273f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"page_content='One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n",
"\n",
"And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nations top legal minds, who will continue Justice Breyers legacy of excellence.' metadata={'author': 'John Doe'}\n"
]
}
],
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"results = vector_store.similarity_search(\n",
@@ -516,10 +611,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "fd4749e6-ef4f-4cb5-95ff-37c4fa8283d8",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"page_content='A former top litigator in private practice. A former federal public defender. And from a family of public school educators and police officers. A consensus builder. Since shes been nominated, shes received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. \n",
"\n",
"And if we are to advance liberty and justice, we need to secure the Border and fix the immigration system.' metadata={'author': 'Jane Doe'}\n"
]
}
],
"source": [
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
"results = vector_store.similarity_search(\n",
@@ -543,10 +648,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"id": "b7b47e7d-c32f-4999-bce9-3c3c3cebffd0",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"page_content='We are cutting off Russias largest banks from the international financial system. \n",
"\n",
"Preventing Russias central bank from defending the Russian Ruble making Putins $630 Billion “war fund” worthless. \n",
"\n",
"We are choking off Russias access to technology that will sap its economic strength and weaken its military for years to come. \n",
"\n",
"Tonight I say to the Russian oligarchs and corrupt leaders who have bilked billions of dollars off this violent regime no more.' metadata={'author': 'Jane Doe', 'date': '2017-01-01', 'rating': 3, 'source': '../../how_to/state_of_the_union.txt'}\n"
]
}
],
"source": [
"query = \"Any mention about independence?\"\n",
"results = vector_store.similarity_search(\n",
@@ -575,10 +694,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"id": "7e8bf7c5-07d1-4c3f-86d7-1fa3a454dc7f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(Document(id='8616f24425b94a52af3d32d20e6ffb4b', metadata={'author': 'John Doe', 'date': '2014-01-01', 'rating': 5, 'source': '../../how_to/state_of_the_union.txt'}, page_content='In this Capitol, generation after generation, Americans have debated great questions amid great strife, and have done great things. \\n\\nWe have fought for freedom, expanded liberty, defeated totalitarianism and terror. \\n\\nAnd built the strongest, freest, and most prosperous nation the world has ever known. \\n\\nNow is the hour. \\n\\nOur moment of responsibility. \\n\\nOur test of resolve and conscience, of history itself.'), 0.361933544533826)\n"
]
}
],
"source": [
"query = \"Any mention about independence?\"\n",
"results = vector_store.similarity_search_with_score(\n",
@@ -609,10 +736,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "dd0fe7f1-aa40-4c6f-889b-99ad5efcd88b",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(Document(id='d9b36ef70b8942dda4db63563f51cf0f', metadata={'author': 'Jane Doe', 'date': '2017-01-01', 'rating': 3, 'source': '../../how_to/state_of_the_union.txt'}, page_content='We are cutting off Russias largest banks from the international financial system. \\n\\nPreventing Russias central bank from defending the Russian Ruble making Putins $630 Billion “war fund” worthless. \\n\\nWe are choking off Russias access to technology that will sap its economic strength and weaken its military for years to come. \\n\\nTonight I say to the Russian oligarchs and corrupt leaders who have bilked billions of dollars off this violent regime no more.'), 0.7107075545629284)\n"
]
}
],
"source": [
"query = \"Any mention about independence?\"\n",
"results = vector_store.similarity_search_with_score(\n",
@@ -655,13 +790,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"id": "3666265a",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[Document(id='3f6a82b2-7464-4eee-b209-cbca5a236a8a', metadata={'source': 'news'}, page_content='Robbers broke into the city bank and stole $1 million in cash.')]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"retriever = vector_store.as_retriever(\n",
" search_type=\"similarity_score_threshold\",\n",
" search_type=\"similarity\",\n",
" search_kwargs={\"k\": 1, \"score_threshold\": 0.5},\n",
")\n",
"retriever.invoke(\"Stealing from the bank is a crime\", filter={\"source\": \"news\"})"
@@ -740,7 +886,7 @@
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all `CouchbaseVectorStore` features and configurations head to the API reference: https://python.langchain.com/api_reference/couchbase/vectorstores/langchain_couchbase.vectorstores.CouchbaseVectorStore.html"
"For detailed documentation of all `CouchbaseSearchVectorStore` features and configurations head to the API reference: https://couchbase-ecosystem.github.io/langchain-couchbase/langchain_couchbase.html "
]
}
],
@@ -760,7 +906,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.10.13"
}
},
"nbformat": 4,

View File

@@ -0,0 +1,298 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "7679dd7b-7ed4-4755-a499-824deadba708",
"metadata": {},
"source": [
"# MariaDB\n",
"\n",
"LangChain's MariaDB integration (langchain-mariadb) provides vector capabilities for working with MariaDB version 11.7.1 and above, distributed under the MIT license. Users can use the provided implementations as-is or customize them for specific needs.\n",
" Key features include:\n",
"\n",
" * Built-in vector similarity search\n",
" * Support for cosine and euclidean distance metrics\n",
" * Robust metadata filtering options\n",
" * Performance optimization through connection pooling\n",
" * Configurable table and column settings\n",
"\n",
"## Setup\n",
"\n",
"Launch a MariaDB Docker container with:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "92df32f0",
"metadata": {},
"outputs": [],
"source": [
"!docker run --name mariadb-container -e MARIADB_ROOT_PASSWORD=langchain -e MARIADB_DATABASE=langchain -p 3306:3306 -d mariadb:11.7"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Installing the Package\n",
"\n",
"The package uses SQLAlchemy but works best with the MariaDB connector, which requires C/C++ components:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2acbaf9b",
"metadata": {},
"outputs": [],
"source": [
"# Debian, Ubuntu\n",
"!sudo apt install libmariadb3 libmariadb-dev\n",
"\n",
"# CentOS, RHEL, Rocky Linux\n",
"!sudo yum install MariaDB-shared MariaDB-devel\n",
"\n",
"# Install Python connector\n",
"!pip install -U mariadb"
]
},
{
"cell_type": "markdown",
"id": "0dd87fcc",
"metadata": {},
"source": [
"Then install `langchain-mariadb` package\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2acbaf9b",
"metadata": {},
"outputs": [],
"source": [
"pip install -U langchain-mariadb\n"
]
},
{
"cell_type": "markdown",
"id": "0dd87fca",
"metadata": {},
"source": [
"VectorStore works along with an LLM model, here using `langchain-openai` as example.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2acbaf9b",
"metadata": {},
"outputs": [],
"source": [
"pip install langchain-openai\n",
"export OPENAI_API_KEY=...\n"
]
},
{
"cell_type": "markdown",
"id": "0dd87fcc",
"metadata": {},
"source": [
"## Initialization"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "94f5c129",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.documents import Document\n",
"from langchain_mariadb import MariaDBStore\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"# connection string\n",
"url = f\"mariadb+mariadbconnector://myuser:mypassword@localhost/langchain\"\n",
"\n",
"# Initialize vector store\n",
"vectorstore = MariaDBStore(\n",
" embeddings=OpenAIEmbeddings(),\n",
" embedding_length=1536,\n",
" datasource=url,\n",
" collection_name=\"my_docs\",\n",
")"
]
},
{
"cell_type": "markdown",
"id": "61a224a1-d70b-4daf-86ba-ab6e43c08b50",
"metadata": {},
"source": [
"## Manage vector store\n",
"\n",
"### Adding Data\n",
"You can add data as documents with metadata:\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "94f5d129",
"metadata": {},
"outputs": [],
"source": [
"docs = [\n",
" Document(\n",
" page_content=\"there are cats in the pond\",\n",
" metadata={\"id\": 1, \"location\": \"pond\", \"topic\": \"animals\"},\n",
" ),\n",
" Document(\n",
" page_content=\"ducks are also found in the pond\",\n",
" metadata={\"id\": 2, \"location\": \"pond\", \"topic\": \"animals\"},\n",
" ),\n",
" # More documents...\n",
"]\n",
"vectorstore.add_documents(docs)"
]
},
{
"cell_type": "markdown",
"id": "0c712fa3",
"metadata": {},
"source": [
"\n",
"Or as plain text with optional metadata:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "a5b2b71f-49eb-407d-b03a-dea4c0a517d6",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"texts = [\n",
" \"a sculpture exhibit is also at the museum\",\n",
" \"a new coffee shop opened on Main Street\",\n",
"]\n",
"metadatas = [\n",
" {\"id\": 6, \"location\": \"museum\", \"topic\": \"art\"},\n",
" {\"id\": 7, \"location\": \"Main Street\", \"topic\": \"food\"},\n",
"]\n",
"\n",
"vectorstore.add_texts(texts=texts, metadatas=metadatas)"
]
},
{
"cell_type": "markdown",
"id": "59f82250-7903-4279-8300-062542c83416",
"metadata": {},
"source": [
"## Query vector store"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "f15a2359-6dc3-4099-8214-785f167a9ca4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Basic similarity search\n",
"results = vectorstore.similarity_search(\"Hello\", k=2)\n",
"\n",
"# Search with metadata filtering\n",
"results = vectorstore.similarity_search(\"Hello\", filter={\"category\": \"greeting\"})"
]
},
{
"cell_type": "markdown",
"id": "7ecd77a0",
"metadata": {},
"source": [
"\n",
"### Filter Options\n",
"\n",
"The system supports various filtering operations on metadata:\n",
"\n",
"* Equality: $eq\n",
"* Inequality: $ne\n",
"* Comparisons: $lt, $lte, $gt, $gte\n",
"* List operations: $in, $nin\n",
"* Text matching: $like, $nlike\n",
"* Logical operations: $and, $or, $not\n",
"\n",
"Example:\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "f15a2359-6dc3-4099-8214-785f167a9cb4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Search with simple filter\n",
"results = vectorstore.similarity_search(\n",
" \"kitty\", k=10, filter={\"id\": {\"$in\": [1, 5, 2, 9]}}\n",
")\n",
"\n",
"# Search with multiple conditions (AND)\n",
"results = vectorstore.similarity_search(\n",
" \"ducks\",\n",
" k=10,\n",
" filter={\"id\": {\"$in\": [1, 5, 2, 9]}, \"location\": {\"$in\": [\"pond\", \"market\"]}},\n",
")"
]
},
{
"cell_type": "markdown",
"id": "90a65b31",
"metadata": {},
"source": [
"## Usage for retrieval-augmented generation\n",
"\n",
"TODO: document example"
]
},
{
"cell_type": "markdown",
"id": "f08d3a3c",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"See the repo [here](https://github.com/mariadb-corporation/langchain-mariadb) for more detail."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,373 @@
{
"cells": [
{
"cell_type": "raw",
"id": "1957f5cb",
"metadata": {},
"source": [
"---\n",
"sidebar_label: openGauss\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "ef1f0986",
"metadata": {},
"source": [
"# openGauss VectorStore\n",
"\n",
"This notebook covers how to get started with the openGauss VectorStore. [openGauss](https://opengauss.org/en/) is a high-performance relational database with native vector storage and retrieval capabilities. This integration enables ACID-compliant vector operations within LangChain applications, combining traditional SQL functionality with modern AI-driven similarity search.\n",
" vector store."
]
},
{
"cell_type": "markdown",
"id": "36fdc060",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"### Launch openGauss Container"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"```bash\n",
"docker run --name opengauss \\\n",
" -d \\\n",
" -e GS_PASSWORD='MyStrongPass@123' \\\n",
" -p 8888:5432 \\\n",
" opengauss/opengauss-server:latest\n",
"```"
],
"id": "e006fdc593107ef5"
},
{
"cell_type": "markdown",
"id": "a51b3f07b83b8a1d",
"metadata": {},
"source": "### Install langchain-opengauss"
},
{
"cell_type": "raw",
"id": "ad030f666e228cc8",
"metadata": {},
"source": [
"```bash\n",
"pip install langchain-opengauss\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "4d14f2f5f8ab0df7",
"metadata": {},
"source": [
"**System Requirements**:\n",
"- openGauss ≥ 7.0.0\n",
"- Python ≥ 3.8\n",
"- psycopg2-binary"
]
},
{
"cell_type": "markdown",
"id": "9695dee7",
"metadata": {},
"source": [
"### Credentials\n",
"\n",
"Using your openGauss Credentials"
]
},
{
"cell_type": "markdown",
"id": "93df377e",
"metadata": {},
"source": [
"## Initialization\n",
"\n",
"import EmbeddingTabs from \"@theme/EmbeddingTabs\";\n",
"\n",
"<EmbeddingTabs/>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc37144c-208d-4ab3-9f3a-0407a69fe052",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from langchain_opengauss import OpenGauss, OpenGaussSettings\n",
"\n",
"# Configure with schema validation\n",
"config = OpenGaussSettings(\n",
" table_name=\"test_langchain\",\n",
" embedding_dimension=384,\n",
" index_type=\"HNSW\",\n",
" distance_strategy=\"COSINE\",\n",
")\n",
"vector_store = OpenGauss(embedding=embeddings, config=config)"
]
},
{
"cell_type": "markdown",
"id": "ac6071d4",
"metadata": {},
"source": [
"## Manage vector store\n",
"\n",
"### Add items to vector store\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17f5efc0",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.documents import Document\n",
"\n",
"document_1 = Document(page_content=\"foo\", metadata={\"source\": \"https://example.com\"})\n",
"\n",
"document_2 = Document(page_content=\"bar\", metadata={\"source\": \"https://example.com\"})\n",
"\n",
"document_3 = Document(page_content=\"baz\", metadata={\"source\": \"https://example.com\"})\n",
"\n",
"documents = [document_1, document_2, document_3]\n",
"\n",
"vector_store.add_documents(documents=documents, ids=[\"1\", \"2\", \"3\"])"
]
},
{
"cell_type": "markdown",
"id": "c738c3e0",
"metadata": {},
"source": "### Update items in vector store\n"
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0aa8b71",
"metadata": {},
"outputs": [],
"source": [
"updated_document = Document(\n",
" page_content=\"qux\", metadata={\"source\": \"https://another-example.com\"}\n",
")\n",
"\n",
"# If the id is already exist, will update the document\n",
"vector_store.add_documents(document_id=\"1\", document=updated_document)"
]
},
{
"cell_type": "markdown",
"id": "dcf1b905",
"metadata": {},
"source": "### Delete items from vector store\n"
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef61e188",
"metadata": {},
"outputs": [],
"source": [
"vector_store.delete(ids=[\"3\"])"
]
},
{
"cell_type": "markdown",
"id": "c3620501",
"metadata": {},
"source": [
"## Query vector store\n",
"\n",
"Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.\n",
"\n",
"### Query directly\n",
"\n",
"Performing a simple similarity search can be done as follows:\n",
"\n",
"- TODO: Edit and then run code cell to generate output"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa0a16fa",
"metadata": {},
"outputs": [],
"source": [
"results = vector_store.similarity_search(\n",
" query=\"thud\", k=1, filter={\"source\": \"https://another-example.com\"}\n",
")\n",
"for doc in results:\n",
" print(f\"* {doc.page_content} [{doc.metadata}]\")"
]
},
{
"cell_type": "markdown",
"id": "3ed9d733",
"metadata": {},
"source": "If you want to execute a similarity search and receive the corresponding scores you can run:\n"
},
{
"cell_type": "code",
"execution_count": null,
"id": "5efd2eaa",
"metadata": {},
"outputs": [],
"source": [
"results = vector_store.similarity_search_with_score(\n",
" query=\"thud\", k=1, filter={\"source\": \"https://example.com\"}\n",
")\n",
"for doc, score in results:\n",
" print(f\"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]\")"
]
},
{
"cell_type": "markdown",
"id": "0c235cdc",
"metadata": {},
"source": [
"### Query by turning into retriever\n",
"\n",
"You can also transform the vector store into a retriever for easier usage in your chains.\n",
"\n",
"- TODO: Edit and then run code cell to generate output"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3460093",
"metadata": {},
"outputs": [],
"source": [
"retriever = vector_store.as_retriever(search_type=\"mmr\", search_kwargs={\"k\": 1})\n",
"retriever.invoke(\"thud\")"
]
},
{
"cell_type": "markdown",
"id": "901c75dc",
"metadata": {},
"source": [
"## Usage for retrieval-augmented generation\n",
"\n",
"For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:\n",
"\n",
"- [Tutorials](/docs/tutorials/)\n",
"- [How-to: Question and answer with RAG](https://python.langchain.com/docs/how_to/#qa-with-rag)\n",
"- [Retrieval conceptual docs](https://python.langchain.com/docs/concepts/retrieval/)"
]
},
{
"cell_type": "markdown",
"id": "069f1b5f",
"metadata": {},
"source": [
"## Configuration\n",
"\n",
"### Connection Settings\n",
"| Parameter | Default | Description |\n",
"|---------------------|-------------------------|--------------------------------------------------------|\n",
"| `host` | localhost | Database server address |\n",
"| `port` | 8888 | Database connection port |\n",
"| `user` | gaussdb | Database username |\n",
"| `password` | - | Complex password string |\n",
"| `database` | postgres | Default database name |\n",
"| `min_connections` | 1 | Connection pool minimum size |\n",
"| `max_connections` | 5 | Connection pool maximum size |\n",
"| `table_name` | langchain_docs | Name of the table for storing vector data and metadata |\n",
"| `index_type` | IndexType.HNSW |Vector index algorithm type. Options: HNSW or IVFFLAT\\nDefault is HNSW.|\n",
"| `vector_type` | VectorType.vector |Type of vector representation to use. Default is Vector.|\n",
"| `distance_strategy` | DistanceStrategy.COSINE |Vector similarity metric to use for retrieval. Options: euclidean (L2 distance), cosine (angular distance, ideal for text embeddings), manhattan (L1 distance for sparse data), negative_inner_product (dot product for normalized vectors).\\n Default is cosine.|\n",
"|`embedding_dimension`| 1536 |Dimensionality of the vector embeddings.|\n",
"\n",
"### Supported Combinations\n",
"\n",
"| Vector Type | Dimensions | Index Types | Supported Distance Strategies |\n",
"|-------------|------------|--------------|---------------------------------------|\n",
"| vector | ≤2000 | HNSW/IVFFLAT | COSINE/EUCLIDEAN/MANHATTAN/INNER_PROD |\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "6a7b7b7c4f5a03e1",
"metadata": {},
"source": [
"## Performance Optimization\n",
"\n",
"### Index Tuning Guidelines\n",
"**HNSW Parameters**:\n",
"- `m`: 16-100 (balance between recall and memory)\n",
"- `ef_construction`: 64-1000 (must be > 2*m)\n",
"\n",
"**IVFFLAT Recommendations**:\n",
"```python\n",
"import math\n",
"\n",
"lists = min(\n",
" int(math.sqrt(total_rows)) if total_rows > 1e6 else int(total_rows / 1000),\n",
" 2000, # openGauss maximum\n",
")\n",
"```\n",
"\n",
"### Connection Pooling\n",
"```python\n",
"OpenGaussSettings(min_connections=3, max_connections=20)\n",
"```\n"
]
},
{
"cell_type": "markdown",
"id": "6b581b499ffed641",
"metadata": {},
"source": [
"## Limitations\n",
"- `bit` and `sparsevec` vector types currently in development\n",
"- Maximum vector dimensions: 2000 for `vector` type"
]
},
{
"cell_type": "markdown",
"id": "8a27244f",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all __ModuleName__VectorStore features and configurations head to the API reference: https://python.langchain.com/api_reference/en/latest/vectorstores/opengauss.OpenGuass.html"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -120,7 +120,7 @@
"1. `redis://` - Connection to Redis standalone, unencrypted\n",
"2. `rediss://` - Connection to Redis standalone, with TLS encryption\n",
"3. `redis+sentinel://` - Connection to Redis server via Redis Sentinel, unencrypted\n",
"4. `rediss+sentinel://` - Connection to Redis server via Redis Sentinel, booth connections with TLS encryption\n",
"4. `rediss+sentinel://` - Connection to Redis server via Redis Sentinel, both connections with TLS encryption\n",
"\n",
"More information about additional connection parameters can be found in the [redis-py documentation](https://redis-py.readthedocs.io/en/stable/connections.html)."
]
@@ -187,7 +187,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "b1b1eb90-5155-44ca-a8a7-b04b02d5e77c",
"metadata": {},
"outputs": [],
@@ -209,7 +209,7 @@
"# connection to redis standalone at localhost, db 0, no password but with TLS support\n",
"redis_url = \"rediss://localhost:6379\"\n",
"# connection to redis sentinel at localhost and default port, db 0, no password\n",
"# but with TLS support for booth Sentinel and Redis server\n",
"# but with TLS support for both Sentinel and Redis server\n",
"redis_url = \"rediss+sentinel://localhost\""
]
},
@@ -254,7 +254,9 @@
"cell_type": "markdown",
"id": "7f98392b",
"metadata": {},
"source": "To enable automated tracing of your model calls, set your [LangSmith](https://docs.smith.langchain.com/) API key:"
"source": [
"To enable automated tracing of your model calls, set your [LangSmith](https://docs.smith.langchain.com/) API key:"
]
},
{
"cell_type": "code",
@@ -899,7 +901,7 @@
]
},
{
"name": "stdin",
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your OpenAI API key: ········\n"

View File

@@ -0,0 +1,491 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "683953b3",
"metadata": {},
"source": [
"# YDB\n",
"\n",
"> [YDB](https://ydb.tech/) is a versatile open source Distributed SQL Database that combines high availability and scalability with strong consistency and ACID transactions. It accommodates transactional (OLTP), analytical (OLAP), and streaming workloads simultaneously.\n",
"\n",
"This notebook shows how to use functionality related to the `YDB` vector store.\n",
"\n",
"## Setup\n",
"\n",
"First, set up a local YDB with Docker:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da75f17c",
"metadata": {},
"outputs": [],
"source": [
"! docker run -d -p 2136:2136 --name ydb-langchain -e YDB_USE_IN_MEMORY_PDISKS=true -h localhost ydbplatform/local-ydb:trunk"
]
},
{
"cell_type": "markdown",
"id": "0acb2a8d",
"metadata": {},
"source": [
"You'll need to install `langchain-ydb` to use this integration"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d454fb7c",
"metadata": {},
"outputs": [],
"source": [
"! pip install -qU langchain-ydb"
]
},
{
"cell_type": "markdown",
"id": "3df5501b",
"metadata": {},
"source": [
"### Credentials\n",
"\n",
"There are no credentials for this notebook, just make sure you have installed the packages as shown above."
]
},
{
"cell_type": "markdown",
"id": "54d5276f",
"metadata": {},
"source": [
"If you want to get best in-class automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6fd5b03",
"metadata": {},
"outputs": [],
"source": [
"# os.environ[\"LANGSMITH_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")\n",
"# os.environ[\"LANGSMITH_TRACING\"] = \"true\""
]
},
{
"cell_type": "markdown",
"id": "2b87fe34",
"metadata": {},
"source": [
"## Initialization\n",
"\n",
"import EmbeddingTabs from \"@theme/EmbeddingTabs\";\n",
"\n",
"<EmbeddingTabs/>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "60276097",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/ovcharuk/opensource/langchain/.venv/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"# | output: false\n",
"# | echo: false\n",
"from langchain_openai import OpenAIEmbeddings\n",
"\n",
"embeddings = OpenAIEmbeddings(model=\"text-embedding-3-large\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aac9563e",
"metadata": {
"ExecuteTime": {
"end_time": "2023-06-03T08:33:31.554934Z",
"start_time": "2023-06-03T08:33:31.549590Z"
},
"tags": []
},
"outputs": [],
"source": [
"from langchain_ydb.vectorstores import YDB, YDBSearchStrategy, YDBSettings\n",
"\n",
"settings = YDBSettings(\n",
" table=\"ydb_example\",\n",
" strategy=YDBSearchStrategy.COSINE_SIMILARITY,\n",
")\n",
"vector_store = YDB(embeddings, config=settings)"
]
},
{
"cell_type": "markdown",
"id": "32dd3f67",
"metadata": {},
"source": [
"## Manage vector store\n",
"\n",
"Once you have created your vector store, you can interact with it by adding and deleting different items.\n",
"\n",
"### Add items to vector store\n",
"\n",
"Prepare documents to work with:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "944743ee",
"metadata": {},
"outputs": [],
"source": [
"from uuid import uuid4\n",
"\n",
"from langchain_core.documents import Document\n",
"\n",
"document_1 = Document(\n",
" page_content=\"I had chocalate chip pancakes and scrambled eggs for breakfast this morning.\",\n",
" metadata={\"source\": \"tweet\"},\n",
")\n",
"\n",
"document_2 = Document(\n",
" page_content=\"The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees.\",\n",
" metadata={\"source\": \"news\"},\n",
")\n",
"\n",
"document_3 = Document(\n",
" page_content=\"Building an exciting new project with LangChain - come check it out!\",\n",
" metadata={\"source\": \"tweet\"},\n",
")\n",
"\n",
"document_4 = Document(\n",
" page_content=\"Robbers broke into the city bank and stole $1 million in cash.\",\n",
" metadata={\"source\": \"news\"},\n",
")\n",
"\n",
"document_5 = Document(\n",
" page_content=\"Wow! That was an amazing movie. I can't wait to see it again.\",\n",
" metadata={\"source\": \"tweet\"},\n",
")\n",
"\n",
"document_6 = Document(\n",
" page_content=\"Is the new iPhone worth the price? Read this review to find out.\",\n",
" metadata={\"source\": \"website\"},\n",
")\n",
"\n",
"document_7 = Document(\n",
" page_content=\"The top 10 soccer players in the world right now.\",\n",
" metadata={\"source\": \"website\"},\n",
")\n",
"\n",
"document_8 = Document(\n",
" page_content=\"LangGraph is the best framework for building stateful, agentic applications!\",\n",
" metadata={\"source\": \"tweet\"},\n",
")\n",
"\n",
"document_9 = Document(\n",
" page_content=\"The stock market is down 500 points today due to fears of a recession.\",\n",
" metadata={\"source\": \"news\"},\n",
")\n",
"\n",
"document_10 = Document(\n",
" page_content=\"I have a bad feeling I am going to get deleted :(\",\n",
" metadata={\"source\": \"tweet\"},\n",
")\n",
"\n",
"documents = [\n",
" document_1,\n",
" document_2,\n",
" document_3,\n",
" document_4,\n",
" document_5,\n",
" document_6,\n",
" document_7,\n",
" document_8,\n",
" document_9,\n",
" document_10,\n",
"]\n",
"uuids = [str(uuid4()) for _ in range(len(documents))]"
]
},
{
"cell_type": "markdown",
"id": "c616b7f4",
"metadata": {},
"source": [
"You can add items to your vector store by using the `add_documents` function."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3f632996",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Inserting data...: 100%|██████████| 10/10 [00:00<00:00, 14.67it/s]\n"
]
},
{
"data": {
"text/plain": [
"['947be6aa-d489-44c5-910e-62e4d58d2ffb',\n",
" '7a62904d-9db3-412b-83b6-f01b34dd7de3',\n",
" 'e5a49c64-c985-4ed7-ac58-5ffa31ade699',\n",
" '99cf4104-36ab-4bd5-b0da-e210d260e512',\n",
" '5810bcd0-b46e-443e-a663-e888c9e028d1',\n",
" '190c193d-844e-4dbb-9a4b-b8f5f16cfae6',\n",
" 'f8912944-f80a-4178-954e-4595bf59e341',\n",
" '34fc7b09-6000-42c9-95f7-7d49f430b904',\n",
" '0f6b6783-f300-4a4d-bb04-8025c4dfd409',\n",
" '46c37ba9-7cf2-4ac8-9bd1-d84e2cb1155c']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vector_store.add_documents(documents=documents, ids=uuids)"
]
},
{
"cell_type": "markdown",
"id": "18af81cc",
"metadata": {},
"source": [
"### Delete items from vector store\n",
"\n",
"You can delete items from your vector store by ID using the `delete` function."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "12b32762",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vector_store.delete(ids=[uuids[-1]])"
]
},
{
"cell_type": "markdown",
"id": "ada27577",
"metadata": {},
"source": [
"## Query vector store\n",
"\n",
"Once your vector store has been created and relevant documents have been added, you will likely want to query it during the execution of your chain or agent.\n",
"\n",
"### Query directly\n",
"\n",
"#### Similarity search\n",
"\n",
"A simple similarity search can be performed as follows:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "015831a3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Building an exciting new project with LangChain - come check it out! [{'source': 'tweet'}]\n",
"* LangGraph is the best framework for building stateful, agentic applications! [{'source': 'tweet'}]\n"
]
}
],
"source": [
"results = vector_store.similarity_search(\n",
" \"LangChain provides abstractions to make working with LLMs easy\", k=2\n",
")\n",
"for res in results:\n",
" print(f\"* {res.page_content} [{res.metadata}]\")"
]
},
{
"cell_type": "markdown",
"id": "623d3b9d",
"metadata": {},
"source": [
"#### Similarity search with score\n",
"\n",
"You can also perform a search with a score:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e7d43430",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* [SIM=0.595] The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees. [{'source': 'news'}]\n",
"* [SIM=0.212] I had chocalate chip pancakes and scrambled eggs for breakfast this morning. [{'source': 'tweet'}]\n",
"* [SIM=0.118] Wow! That was an amazing movie. I can't wait to see it again. [{'source': 'tweet'}]\n"
]
}
],
"source": [
"results = vector_store.similarity_search_with_score(\"Will it be hot tomorrow?\", k=3)\n",
"for res, score in results:\n",
" print(f\"* [SIM={score:.3f}] {res.page_content} [{res.metadata}]\")"
]
},
{
"cell_type": "markdown",
"id": "f5a90c12",
"metadata": {},
"source": [
"### Filtering\n",
"\n",
"You can search with filters as described below:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "169d01d1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* I had chocalate chip pancakes and scrambled eggs for breakfast this morning. [{'source': 'tweet'}]\n",
"* Wow! That was an amazing movie. I can't wait to see it again. [{'source': 'tweet'}]\n",
"* Building an exciting new project with LangChain - come check it out! [{'source': 'tweet'}]\n",
"* LangGraph is the best framework for building stateful, agentic applications! [{'source': 'tweet'}]\n"
]
}
],
"source": [
"results = vector_store.similarity_search_with_score(\n",
" \"What did I eat for breakfast?\",\n",
" k=4,\n",
" filter={\"source\": \"tweet\"},\n",
")\n",
"for res, _ in results:\n",
" print(f\"* {res.page_content} [{res.metadata}]\")"
]
},
{
"cell_type": "markdown",
"id": "afacfd4e",
"metadata": {},
"source": [
"### Query by turning into retriever\n",
"\n",
"You can also transform the vector store into a retriever for easier usage in your chains.\n",
"\n",
"Here's how to transform your vector store into a retriever and then invoke the retriever with a simple query and filter."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "97187188",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Robbers broke into the city bank and stole $1 million in cash. [{'source': 'news'}]\n",
"* The stock market is down 500 points today due to fears of a recession. [{'source': 'news'}]\n"
]
}
],
"source": [
"retriever = vector_store.as_retriever(\n",
" search_kwargs={\"k\": 2},\n",
")\n",
"results = retriever.invoke(\n",
" \"Stealing from the bank is a crime\", filter={\"source\": \"news\"}\n",
")\n",
"for res in results:\n",
" print(f\"* {res.page_content} [{res.metadata}]\")"
]
},
{
"cell_type": "markdown",
"id": "57fade30",
"metadata": {},
"source": [
"## Usage for retrieval-augmented generation\n",
"\n",
"For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:\n",
"\n",
"- [Tutorials](/docs/tutorials/)\n",
"- [How-to: Question and answer with RAG](https://python.langchain.com/docs/how_to/#qa-with-rag)\n",
"- [Retrieval conceptual docs](https://python.langchain.com/docs/concepts/retrieval)"
]
},
{
"cell_type": "markdown",
"id": "02452d34",
"metadata": {},
"source": [
"## API reference\n",
"\n",
"For detailed documentation of all `YDB` features and configurations head to the API reference:https://python.langchain.com/api_reference/community/vectorstores/langchain_community.vectorstores.ydb.YDB.html"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -413,7 +413,7 @@
"Yellowbrick also supports indexing using the Locality-Sensitive Hashing approach. This is an approximate nearest-neighbor search technique, and allows one to trade off similarity search time at the expense of accuracy. The index introduces two new tunable parameters:\n",
"\n",
"- The number of hyperplanes, which is provided as an argument to `create_lsh_index(num_hyperplanes)`. The more documents, the more hyperplanes are needed. LSH is a form of dimensionality reduction. The original embeddings are transformed into lower dimensional vectors where the number of components is the same as the number of hyperplanes.\n",
"- The Hamming distance, an integer representing the breadth of the search. Smaller Hamming distances result in faster retreival but lower accuracy.\n",
"- The Hamming distance, an integer representing the breadth of the search. Smaller Hamming distances result in faster retrieval but lower accuracy.\n",
"\n",
"Here's how you can create an index on the embeddings we loaded into Yellowbrick. We'll also re-run the previous chat session, but this time the retrieval will use the index. Note that for such a small number of documents, you won't see the benefit of indexing in terms of performance."
]

View File

@@ -38,7 +38,7 @@ LangSmith allows you to closely trace, monitor and evaluate your LLM application
It seamlessly integrates with LangChain, and you can use it to inspect and debug individual steps of your chains as you build.
LangSmith documentation is hosted on a separate site.
You can peruse [LangSmith tutorials here](https://docs.smith.langchain.com/tutorials/).
You can peruse [LangSmith tutorials here](https://docs.smith.langchain.com/).
### Evaluation

File diff suppressed because one or more lines are too long

View File

@@ -13,7 +13,7 @@
"- Map a process to the set of documents, where the process includes generating a score;\n",
"- Rank the results by score and return the maximum.\n",
"\n",
"A common process in this scenario is question-answering using pieces of context from a document. Forcing the model to generate score along with its answer helps to select for answers generated only by relevant context.\n",
"A common process in this scenario is question-answering using pieces of context from a document. Forcing the model to generate a score along with its answer helps to select for answers generated only by relevant context.\n",
"\n",
"An [LangGraph](https://langchain-ai.github.io/langgraph/) implementation allows for the incorporation of [tool calling](/docs/concepts/tool_calling) and other features for this problem. Below we will go through both `MapRerankDocumentsChain` and a corresponding LangGraph implementation on a simple example for illustrative purposes."
]

View File

@@ -152,6 +152,16 @@ WEBBROWSING_TOOL_FEAT_TABLE = {
"interactions": True,
"pricing": "Free trial, with pay-as-you-go and flat rate plans after",
},
"Hyperbrowser Browser Agent Tools": {
"link": "/docs/integrations/tools/hyperbrowser_browser_agent_tools",
"interactions": True,
"pricing": "Free trial, with flat rate plans and pre-paid credits after",
},
"Hyperbrowser Web Scraping Tools": {
"link": "/docs/integrations/tools/hyperbrowser_web_scraping_tools",
"interactions": False,
"pricing": "Free trial, with flat rate plans and pre-paid credits after",
},
}
DATABASE_TOOL_FEAT_TABLE = {

View File

@@ -6,7 +6,7 @@ from langchain_astradb import AstraDBVectorStore
from langchain_chroma import Chroma
from langchain_community import vectorstores
from langchain_core.vectorstores import VectorStore
from langchain_couchbase import CouchbaseVectorStore
from langchain_couchbase import CouchbaseSearchVectorStore
from langchain_milvus import Milvus
from langchain_mongodb import MongoDBAtlasVectorSearch
from langchain_pinecone import PineconeVectorStore
@@ -28,7 +28,7 @@ from_partners = [
("PineconeVectorStore", PineconeVectorStore),
("Milvus", Milvus),
("MongoDBAtlasVectorSearch", MongoDBAtlasVectorSearch),
("CouchbaseVectorStore", CouchbaseVectorStore),
("CouchbaseSearchVectorStore", CouchbaseSearchVectorStore),
]
VECTORSTORE_TEMPLATE = """\
@@ -140,6 +140,17 @@ def get_vectorstore_table():
"Local/Cloud": "Local",
"IDs in add Documents": True,
},
"openGauss": {
"Delete by ID": True,
"Filtering": True,
"similarity_search_by_vector": True,
"similarity_search_with_score": True,
"asearch": False,
"Passes Standard Tests": True,
"Multi Tenancy": False,
"Local/Cloud": "Local",
"IDs in add Documents": True,
},
"QdrantVectorStore": {
"Delete by ID": True,
"Filtering": True,
@@ -184,7 +195,7 @@ def get_vectorstore_table():
"Local/Cloud": "Local",
"IDs in add Documents": True,
},
"CouchbaseVectorStore": {
"CouchbaseSearchVectorStore": {
"Delete by ID": True,
"Filtering": True,
"similarity_search_by_vector": True,

View File

@@ -218,6 +218,13 @@ ${llmVarName} = ChatWatsonx(
apiKeyName: "XAI_API_KEY",
packageName: "langchain-xai",
},
{
value: "perplexity",
label: "Perplexity",
model: "llama-3.1-sonar-small-128k-online",
apiKeyName: "PPLX_API_KEY",
packageName: "langchain-perplexity",
}
].map((item) => ({
...item,
...overrideParams?.[item.value],

View File

@@ -237,6 +237,17 @@ const FEATURE_TABLES = {
"local": false,
"apiLink": "https://python.langchain.com/api_reference/xai/chat_models/langchain_xai.chat_models.ChatXAI.html"
},
{
"name": "ChatPerplexity",
"package": "langchain-perplexity",
"link": "perplexity",
"structured_output": true,
"tool_calling": false,
"json_mode": true,
"multimodal": true,
"local": false,
"apiLink": "https://python.langchain.com/api_reference/perplexity/chat_models/langchain_perplexity.chat_models.ChatPerplexity.html"
}
],
},
llms: {
@@ -1119,6 +1130,19 @@ const FEATURE_TABLES = {
local: true,
idsInAddDocuments: false,
},
{
name: "openGauss",
link: "openGauss",
deleteById: true,
filtering: true,
searchByVector: true,
searchWithScore: true,
async: false,
passesStandardTests: true,
multiTenancy: false,
local: true,
idsInAddDocuments: true,
},
{
name: "PGVector",
link: "pgvector",

View File

@@ -196,7 +196,7 @@ def create_sql_agent(
]
prompt = ChatPromptTemplate.from_messages(messages)
agent = RunnableAgent(
runnable=create_openai_functions_agent(llm, tools, prompt), # type: ignore
runnable=create_openai_functions_agent(llm, tools, prompt), # type: ignore[arg-type]
input_keys_arg=["input"],
return_keys_arg=["output"],
**kwargs,
@@ -211,9 +211,9 @@ def create_sql_agent(
]
prompt = ChatPromptTemplate.from_messages(messages)
if agent_type == "openai-tools":
runnable = create_openai_tools_agent(llm, tools, prompt) # type: ignore
runnable = create_openai_tools_agent(llm, tools, prompt) # type: ignore[arg-type]
else:
runnable = create_tool_calling_agent(llm, tools, prompt) # type: ignore
runnable = create_tool_calling_agent(llm, tools, prompt) # type: ignore[arg-type]
agent = RunnableMultiActionAgent( # type: ignore[assignment]
runnable=runnable,
input_keys_arg=["input"],

View File

@@ -135,7 +135,7 @@ def _get_assistants_tool(
Dict[str, Any]: A dictionary of tools that are converted into OpenAI tools.
"""
if _is_assistants_builtin_tool(tool):
return tool # type: ignore
return tool # type: ignore[return-value]
else:
return convert_to_openai_tool(tool)
@@ -288,7 +288,7 @@ class OpenAIAssistantV2Runnable(OpenAIAssistantRunnable):
assistant = client.beta.assistants.create(
name=name,
instructions=instructions,
tools=[_get_assistants_tool(tool) for tool in tools], # type: ignore
tools=[_get_assistants_tool(tool) for tool in tools],
tool_resources=tool_resources, # type: ignore[arg-type]
model=model,
extra_body=extra_body,
@@ -430,7 +430,7 @@ class OpenAIAssistantV2Runnable(OpenAIAssistantRunnable):
assistant = await async_client.beta.assistants.create(
name=name,
instructions=instructions,
tools=openai_tools, # type: ignore
tools=openai_tools,
tool_resources=tool_resources, # type: ignore[arg-type]
model=model,
)

View File

@@ -238,7 +238,7 @@ class InMemoryCache(BaseCache):
Base = declarative_base()
class FullLLMCache(Base): # type: ignore
class FullLLMCache(Base): # type: ignore[misc,valid-type]
"""SQLite table for full LLM Cache (all generations)."""
__tablename__ = "full_llm_cache"
@@ -261,7 +261,7 @@ class SQLAlchemyCache(BaseCache):
"""Look up based on prompt and llm_string."""
stmt = (
select(self.cache_schema.response)
.where(self.cache_schema.prompt == prompt) # type: ignore
.where(self.cache_schema.prompt == prompt)
.where(self.cache_schema.llm == llm_string)
.order_by(self.cache_schema.idx)
)
@@ -1348,17 +1348,14 @@ class CassandraSemanticCache(BaseCache):
return await self.embedding.aembed_query(text=text)
self._aget_embedding = _acache_embedding
kwargs = {}
embedding_dimension: Union[int, Awaitable[int], None] = None
if setup_mode == CassandraSetupMode.ASYNC:
embedding_dimension = self._aget_embedding_dimension()
kwargs["async_setup"] = True
elif setup_mode == CassandraSetupMode.SYNC:
embedding_dimension = self._get_embedding_dimension()
kwargs = {}
if setup_mode == CassandraSetupMode.ASYNC:
kwargs["async_setup"] = True
self.table = MetadataVectorCassandraTable(
session=self.session,
keyspace=self.keyspace,
@@ -1534,7 +1531,7 @@ class CassandraSemanticCache(BaseCache):
await self.table.aclear()
class FullMd5LLMCache(Base): # type: ignore
class FullMd5LLMCache(Base): # type: ignore[misc,valid-type]
"""SQLite table for full LLM Cache (all generations)."""
__tablename__ = "full_md5_llm_cache"
@@ -1586,7 +1583,7 @@ class SQLAlchemyMd5Cache(BaseCache):
def _delete_previous(self, session: Session, prompt: str, llm_string: str) -> None:
stmt = (
delete(self.cache_schema)
.where(self.cache_schema.prompt_md5 == self.get_md5(prompt)) # type: ignore
.where(self.cache_schema.prompt_md5 == self.get_md5(prompt))
.where(self.cache_schema.llm == llm_string)
.where(self.cache_schema.prompt == prompt)
)
@@ -1596,7 +1593,7 @@ class SQLAlchemyMd5Cache(BaseCache):
prompt_pd5 = self.get_md5(prompt)
stmt = (
select(self.cache_schema.response)
.where(self.cache_schema.prompt_md5 == prompt_pd5) # type: ignore
.where(self.cache_schema.prompt_md5 == prompt_pd5)
.where(self.cache_schema.llm == llm_string)
.where(self.cache_schema.prompt == prompt)
.order_by(self.cache_schema.idx)
@@ -1799,7 +1796,7 @@ class _CachedAwaitable:
def __await__(self) -> Generator:
if self.result is _unset:
self.result = yield from self.awaitable.__await__()
return self.result # type: ignore
return self.result # type: ignore[return-value]
def _reawaitable(func: Callable) -> Callable:

View File

@@ -584,7 +584,7 @@ class CometCallbackHandler(BaseMetadataCallbackHandler, BaseCallbackHandler):
)
_custom_metrics = custom_metrics if custom_metrics else self.custom_metrics
self.__init__( # type: ignore
self.__init__( # type: ignore[misc]
task_type=_task_type,
workspace=_workspace,
project_name=_project_name,

View File

@@ -218,12 +218,17 @@ def standardize_model_name(
or model_name.startswith("gpt-3.5")
or model_name.startswith("gpt-35")
or model_name.startswith("o1-")
or model_name.startswith("o3-")
or ("finetuned" in model_name and "legacy" not in model_name)
):
return model_name + "-completion"
if (
token_type == TokenType.PROMPT_CACHED
and (model_name.startswith("gpt-4o") or model_name.startswith("o1"))
and (
model_name.startswith("gpt-4o")
or model_name.startswith("o1")
or model_name.startswith("o3")
)
and not (model_name.startswith("gpt-4o-2024-05-13"))
):
return model_name + "-cached"

View File

@@ -580,7 +580,7 @@ class WandbCallbackHandler(BaseMetadataCallbackHandler, BaseCallbackHandler):
self.temp_dir.cleanup()
self.reset_callback_meta()
if reset:
self.__init__( # type: ignore
self.__init__( # type: ignore[misc]
job_type=job_type if job_type else self.job_type,
project=project if project else self.project,
entity=entity if entity else self.entity,

View File

@@ -352,7 +352,7 @@ def create_structured_output_runnable(
class _OutputFormatter(BaseModel):
"""Output formatter. Should always be used to format your response to the user.""" # noqa: E501
output: output_schema # type: ignore
output: output_schema # type: ignore[valid-type]
function = _OutputFormatter
output_parser = output_parser or PydanticAttrOutputFunctionsParser(
@@ -537,7 +537,7 @@ def create_structured_output_chain(
class _OutputFormatter(BaseModel):
"""Output formatter. Should always be used to format your response to the user.""" # noqa: E501
output: output_schema # type: ignore
output: output_schema # type: ignore[valid-type]
function = _OutputFormatter
output_parser = output_parser or PydanticAttrOutputFunctionsParser(

View File

@@ -316,7 +316,7 @@ class GraphCypherQAChain(Chain):
MessagesPlaceholder(variable_name="function_response"),
]
)
qa_chain = response_prompt | qa_llm | StrOutputParser() # type: ignore
qa_chain = response_prompt | qa_llm | StrOutputParser() # type: ignore[operator]
except (NotImplementedError, AttributeError):
raise ValueError("Provided LLM does not support native tools/functions")
else:
@@ -404,15 +404,15 @@ class GraphCypherQAChain(Chain):
intermediate_steps.append({"context": context})
if self.use_function_response:
function_response = get_function_response(question, context)
final_result = self.qa_chain.invoke( # type: ignore
final_result = self.qa_chain.invoke( # type: ignore[assignment]
{"question": question, "function_response": function_response},
)
else:
result = self.qa_chain.invoke( # type: ignore
result = self.qa_chain.invoke(
{"question": question, "context": context},
callbacks=callbacks,
)
final_result = result[self.qa_chain.output_key] # type: ignore
final_result = result[self.qa_chain.output_key] # type: ignore[union-attr]
chain_result: Dict[str, Any] = {self.output_key: final_result}
if self.return_intermediate_steps:

View File

@@ -225,11 +225,11 @@ class MemgraphQAChain(Chain):
MessagesPlaceholder(variable_name="function_response"),
]
)
qa_chain = response_prompt | qa_llm | StrOutputParser() # type: ignore
qa_chain = response_prompt | qa_llm | StrOutputParser() # type: ignore[operator]
except (NotImplementedError, AttributeError):
raise ValueError("Provided LLM does not support native tools/functions")
else:
qa_chain = use_qa_llm_kwargs["prompt"] | qa_llm | StrOutputParser() # type: ignore
qa_chain = use_qa_llm_kwargs["prompt"] | qa_llm | StrOutputParser()
prompt = use_cypher_llm_kwargs["prompt"]
llm_to_use = cypher_llm if cypher_llm is not None else llm
@@ -300,11 +300,11 @@ class MemgraphQAChain(Chain):
intermediate_steps.append({"context": context})
if self.use_function_response:
function_response = get_function_response(question, context)
result = self.qa_chain.invoke( # type: ignore
result = self.qa_chain.invoke(
{"question": question, "function_response": function_response},
)
else:
result = self.qa_chain.invoke( # type: ignore
result = self.qa_chain.invoke(
{"question": question, "context": context},
callbacks=callbacks,
)

View File

@@ -67,11 +67,11 @@ def extract_cypher(text: str) -> str:
def use_simple_prompt(llm: BaseLanguageModel) -> bool:
"""Decides whether to use the simple prompt"""
if llm._llm_type and "anthropic" in llm._llm_type: # type: ignore
if llm._llm_type and "anthropic" in llm._llm_type: # type: ignore[attr-defined]
return True
# Bedrock anthropic
if hasattr(llm, "model_id") and "anthropic" in llm.model_id: # type: ignore
if hasattr(llm, "model_id") and "anthropic" in llm.model_id:
return True
return False

View File

@@ -313,8 +313,12 @@ class PebbloRetrievalQA(Chain):
)
@staticmethod
def _get_app_details( # type: ignore
app_name: str, owner: str, description: str, llm: BaseLanguageModel, **kwargs
def _get_app_details(
app_name: str,
owner: str,
description: str,
llm: BaseLanguageModel,
**kwargs: Any,
) -> App:
"""Fetch app details. Internal method.
Returns:

View File

@@ -81,7 +81,7 @@ class CassandraChatMessageHistory(BaseChatMessageHistory):
)
@property
def messages(self) -> List[BaseMessage]: # type: ignore
def messages(self) -> List[BaseMessage]: # type: ignore[override]
"""Retrieve all session messages from DB"""
# The latest are returned, in chronological order
rows = self.table.get_partition(

Some files were not shown because too many files have changed in this diff Show More