Eun Hye Kim
9aae8ef416
core[patch]: Fix utils.json_schema.dereference_refs ( #24335 KeyError: 400 in JSON schema processing) ( #24337 )
...
Description:
This PR fixes a KeyError: 400 that occurs in the JSON schema processing
within the reduce_openapi_spec function. The _retrieve_ref function in
json_schema.py was modified to handle missing components gracefully by
continuing to the next component if the current one is not found. This
ensures that the OpenAPI specification is fully interpreted and the
agent executes without errors.
Issue:
Fixes issue #24335
Dependencies:
No additional dependencies are required for this change.
Twitter handle:
@lunara_x
2024-07-19 13:31:00 -04:00
Erick Friis
ef049769f0
core[patch]: Release 0.2.22 ( #24423 )
...
Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
2024-07-19 09:09:24 -07:00
Bagatur
cd19ba9a07
core[patch]: core lint fix ( #24447 )
2024-07-19 09:01:22 -07:00
Nuno Campos
62b6965d2a
core: In ensure_config don't copy dunder configurable keys to metadata ( #24420 )
2024-07-18 22:28:52 +00:00
Eugene Yurtsev
ef22ebe431
standard-tests[patch]: Add pytest assert rewrites ( #24408 )
...
This will surface nice error messages in subclasses that fail assertions.
2024-07-18 21:41:11 +00:00
Eugene Yurtsev
f62b323108
core[minor]: Support all versions of pydantic base model in argsschema ( #24418 )
...
This adds support to any pydantic base model for tools.
The only potential issue is that `get_input_schema()` will not always
return a v1 base model.
2024-07-18 17:14:23 -04:00
Eugene Yurtsev
570566b858
core[patch]: Update API reference for astream events ( #24359 )
...
Update the API reference for astream events to include information about
custom events.
2024-07-17 21:48:53 -04:00
Bagatur
a4c101ae97
core[patch]: Release 0.2.21 ( #24372 )
2024-07-17 22:44:35 +00:00
William FH
c5a07e2dd8
core[patch]: add InjectedToolArg annotation ( #24279 )
...
```python
from typing_extensions import Annotated
from langchain_core.tools import tool, InjectedToolArg
from langchain_anthropic import ChatAnthropic
@tool
def multiply(x: int, y: int, not_for_model: Annotated[dict, InjectedToolArg]) -> str:
"""multiply."""
return x * y
ChatAnthropic(model='claude-3-sonnet-20240229',).bind_tools([multiply]).invoke('5 times 3').tool_calls
'''
-> [{'name': 'multiply',
'args': {'x': 5, 'y': 3},
'id': 'toolu_01Y1QazYWhu4R8vF4hF4z9no',
'type': 'tool_call'}]
'''
```
---------
Co-authored-by: Bagatur <baskaryan@gmail.com>
Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
2024-07-17 15:28:40 -07:00
Eugene Yurtsev
96bac8e20d
core[patch]: Fix regression requiring input_variables in few chat prompt templates ( #24360 )
...
* Fix regression that requires users passing input_variables=[].
* Regression introduced by my own changes to this PR:
https://github.com/langchain-ai/langchain/pull/22851
2024-07-17 18:14:57 -04:00
Eugene Yurtsev
9e4a0e76f6
core[patch]: Fix one unit test for chat prompt template ( #24362 )
...
Minor change that fixes a unit test that had missing assertions.
2024-07-17 18:56:48 +00:00
Bagatur
80e7cd6cff
core[patch]: Release 0.2.20 ( #24322 )
2024-07-16 15:04:36 -07:00
Eugene Yurtsev
616196c620
Docs: Add how to dispatch custom callback events ( #24278 )
...
* Add how-to guide for dispatching custom callback events.
* Add links from index to the how to guide
* Add link from streaming from within a tool
* Update versionadded to correct release
https://github.com/langchain-ai/langchain/releases/tag/langchain-core%3D%3D0.2.15
2024-07-16 17:38:32 -04:00
Leonid Ganeline
5ccf8ebfac
core: docstrings vectorstores
update ( #24281 )
...
Added missed docstrings. Formatted docstrings to the consistent form.
---------
Co-authored-by: Erick Friis <erick@langchain.dev>
2024-07-16 16:58:11 +00:00
Bagatur
dc42279eb5
core[patch]: fix Typing.cast import ( #24313 )
...
Fixes #24287
2024-07-16 16:53:48 +00:00
Leonid Ganeline
5fcf2ef7ca
core: docstrings documents
( #23506 )
...
Added missed docstrings. Formatted docstrings to the consistent form.
2024-07-16 10:43:54 -04:00
Shenhai Ran
5f2dea2b20
core[patch]: Add encoding options when create prompt template from a file ( #24054 )
...
- Uses default utf-8 encoding for loading prompt templates from file
---------
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
2024-07-16 09:35:09 -04:00
Leonid Ganeline
198b85334f
core[patch]: docstrings langchain_core/
files update ( #24285 )
...
Added missed docstrings. Formatted docstrings to the consistent form.
2024-07-16 09:21:51 -04:00
Tibor Reiss
1c753d1e81
core[patch]: Update typing for template format to include jinja2 as a Literal ( #24144 )
...
Fixes #23929 via adjusting the typing
2024-07-16 09:09:42 -04:00
JP-Ellis
f77659463a
core[patch]: allow message utils to work with lcel ( #23743 )
...
The functions `convert_to_messages` has had an expansion of the
arguments it can take:
1. Previously, it only could take a `Sequence` in order to iterate over
it. This has been broadened slightly to an `Iterable` (which should have
no other impact).
2. Support for `PromptValue` and `BaseChatPromptTemplate` has been
added. These are generated when combining messages using the overloaded
`+` operator.
Functions which rely on `convert_to_messages` (namely `filter_messages`,
`merge_message_runs` and `trim_messages`) have had the type of their
arguments similarly expanded.
Resolves #23706 .
<!--
If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17.
-->
---------
Signed-off-by: JP-Ellis <josh@jpellis.me>
Co-authored-by: Bagatur <baskaryan@gmail.com>
2024-07-15 08:58:05 -07:00
Harold Martin
ccdaf14eff
docs: Spell check fixes ( #24217 )
...
**Description:** Spell check fixes for docs, comments, and a couple of
strings. No code change e.g. variable names.
**Issue:** none
**Dependencies:** none
**Twitter handle:** hmartin
2024-07-15 15:51:43 +00:00
Leonid Ganeline
cacdf96f9c
core docstrings tracers
update ( #24211 )
...
Added missed docstrings. Formatted docstrings to the consistent form.
2024-07-15 11:37:09 -04:00
Leonid Ganeline
36ee083753
core: docstrings utils
update ( #24213 )
...
Added missed docstrings. Formatted docstrings to the consistent form.
2024-07-15 11:36:00 -04:00
Bagatur
620b118c70
core[patch]: Release 0.2.19 ( #24272 )
2024-07-15 07:51:30 -07:00
ccurme
888fbc07b5
core[patch]: support passing args_schema
through as_tool
( #24269 )
...
Note: this allows the schema to be passed in positionally.
```python
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.runnables import RunnableLambda
class Add(BaseModel):
"""Add two integers together."""
a: int = Field(..., description="First integer")
b: int = Field(..., description="Second integer")
def add(input: dict) -> int:
return input["a"] + input["b"]
runnable = RunnableLambda(add)
as_tool = runnable.as_tool(Add)
as_tool.args_schema.schema()
```
```
{'title': 'Add',
'description': 'Add two integers together.',
'type': 'object',
'properties': {'a': {'title': 'A',
'description': 'First integer',
'type': 'integer'},
'b': {'title': 'B', 'description': 'Second integer', 'type': 'integer'}},
'required': ['a', 'b']}
```
2024-07-15 07:51:05 -07:00
Bagatur
0da5078cad
langchain[minor]: Generic configurable model ( #23419 )
...
alternative to
[23244](https://github.com/langchain-ai/langchain/pull/23244 ). allows
you to use chat model declarative methods
![Screenshot 2024-06-25 at 1 07 10
PM](https://github.com/langchain-ai/langchain/assets/22008038/910d1694-9b7b-46bc-bc2e-3792df9321d6 )
2024-07-15 01:11:01 +00:00
Bagatur
d0728b0ba0
core[patch]: add tool name to tool message ( #24243 )
...
Copying current ToolNode behavior
2024-07-15 00:42:40 +00:00
Bagatur
5c3e2612da
core[patch]: Release 0.2.18 ( #24230 )
2024-07-13 09:14:43 -07:00
Bagatur
65321bf975
core[patch]: fix ToolCall "type" when streaming ( #24218 )
2024-07-13 08:59:03 -07:00
Eugene Yurtsev
8d82a0d483
core[patch]: Mark GraphVectorStore as beta ( #24195 )
...
* This PR marks graph vectorstore as beta
2024-07-12 14:28:06 -04:00
Bagatur
0a1e475a30
core[patch]: Release 0.2.17 ( #24189 )
2024-07-12 17:08:29 +00:00
Bagatur
6166ea67a8
core[minor]: rename ToolMessage.raw_output -> artifact ( #24185 )
2024-07-12 09:52:44 -07:00
Leonid Ganeline
aa3e3cfa40
core[patch]: docstrings runnables
update ( #24161 )
...
Added missed docstrings. Formatted docstrings to the consistent form.
2024-07-12 11:27:06 -04:00
Erick Friis
1132fb801b
core: release 0.2.16 ( #24159 )
2024-07-11 23:59:41 +00:00
Nuno Campos
1d37aa8403
core: Remove extra newline ( #24157 )
2024-07-11 23:55:36 +00:00
Bagatur
8d100c58de
core[patch]: Tool accept RunnableConfig ( #24143 )
...
Relies on #24038
---------
Co-authored-by: Erick Friis <erick@langchain.dev>
2024-07-11 22:13:17 +00:00
Bagatur
5fd1e67808
core[minor], integrations...[patch]: Support ToolCall as Tool input and ToolMessage as Tool output ( #24038 )
...
Changes:
- ToolCall, InvalidToolCall and ToolCallChunk can all accept a "type"
parameter now
- LLM integration packages add "type" to all the above
- Tool supports ToolCall inputs that have "type" specified
- Tool outputs ToolMessage when a ToolCall is passed as input
- Tools can separately specify ToolMessage.content and
ToolMessage.raw_output
- Tools emit events for validation errors (using on_tool_error and
on_tool_end)
Example:
```python
@tool("structured_api", response_format="content_and_raw_output")
def _mock_structured_tool_with_raw_output(
arg1: int, arg2: bool, arg3: Optional[dict] = None
) -> Tuple[str, dict]:
"""A Structured Tool"""
return f"{arg1} {arg2}", {"arg1": arg1, "arg2": arg2, "arg3": arg3}
def test_tool_call_input_tool_message_with_raw_output() -> None:
tool_call: Dict = {
"name": "structured_api",
"args": {"arg1": 1, "arg2": True, "arg3": {"img": "base64string..."}},
"id": "123",
"type": "tool_call",
}
expected = ToolMessage("1 True", raw_output=tool_call["args"], tool_call_id="123")
tool = _mock_structured_tool_with_raw_output
actual = tool.invoke(tool_call)
assert actual == expected
tool_call.pop("type")
with pytest.raises(ValidationError):
tool.invoke(tool_call)
actual_content = tool.invoke(tool_call["args"])
assert actual_content == expected.content
```
---------
Co-authored-by: Erick Friis <erick@langchain.dev>
2024-07-11 14:54:02 -07:00
Bagatur
eeb996034b
core[patch]: Release 0.2.15 ( #24149 )
2024-07-11 21:34:25 +00:00
Nuno Campos
03fba07d15
core[patch]: Update styles for mermaid graphs ( #24147 )
2024-07-11 14:19:36 -07:00
ccurme
8ee8ca7c83
core[patch]: propagate parse_docstring
to tool decorator ( #24123 )
...
Disabled by default.
```python
from langchain_core.tools import tool
@tool(parse_docstring=True)
def foo(bar: str, baz: int) -> str:
"""The foo.
Args:
bar: this is the bar
baz: this is the baz
"""
return bar
foo.args_schema.schema()
```
```json
{
"title": "fooSchema",
"description": "The foo.",
"type": "object",
"properties": {
"bar": {
"title": "Bar",
"description": "this is the bar",
"type": "string"
},
"baz": {
"title": "Baz",
"description": "this is the baz",
"type": "integer"
}
},
"required": [
"bar",
"baz"
]
}
```
2024-07-11 20:11:45 +00:00
Eugene Yurtsev
4ba14adec6
core[patch]: Clean up indexing test code ( #24139 )
...
Refactor the code to use the existing InMemroyVectorStore.
This change is needed for another PR that moves some of the imports
around (and messes up the mock.patch in this file)
2024-07-11 18:54:46 +00:00
ccurme
122e80e04d
core[patch]: add versionadded to as_tool
( #24138 )
2024-07-11 18:08:08 +00:00
Erick Friis
c4417ea93c
core: release 0.2.14, remove poetry 1.7 incompatible flag from root ( #24137 )
2024-07-11 17:59:51 +00:00
Nuno Campos
2428984205
core: Add metadata to graph json repr ( #24131 )
...
Thank you for contributing to LangChain!
- [ ] **PR title**: "package: description"
- Where "package" is whichever of langchain, community, core,
experimental, etc. is being modified. Use "docs: ..." for purely docs
changes, "templates: ..." for template 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, efriis, eyurtsev, ccurme, vbarda, hwchase17.
2024-07-11 17:23:52 +00:00
Nuno Campos
3e454d7568
core: fix docstring ( #24129 )
2024-07-11 16:38:14 +00:00
Nuno Campos
ee3fe20af4
core: mermaid: Render metadata key-value pairs when drawing mermaid graph ( #24103 )
...
- if node is runnable binding with metadata attached
2024-07-11 16:22:23 +00:00
Eugene Yurtsev
dc131ac42a
core[minor]: Add dispatching for custom events ( #24080 )
...
This PR allows dispatching adhoc events for a given run.
# Context
This PR allows users to send arbitrary data to the callback system and
to the astream events API from within a given runnable. This can be
extremely useful to surface custom information to end users about
progress etc.
Integration with langsmith tracer will be done separately since the data
cannot be currently visualized. It'll be accommodated using the events
attribute of the Run
# Examples with astream events
```python
from langchain_core.callbacks import adispatch_custom_event
from langchain_core.tools import tool
@tool
async def foo(x: int) -> int:
"""Foo"""
await adispatch_custom_event("event1", {"x": x})
await adispatch_custom_event("event2", {"x": x})
return x + 1
async for event in foo.astream_events({'x': 1}, version='v2'):
print(event)
```
```python
{'event': 'on_tool_start', 'data': {'input': {'x': 1}}, 'name': 'foo', 'tags': [], 'run_id': 'fd6fb7a7-dd37-4191-962c-e43e245909f6', 'metadata': {}, 'parent_ids': []}
{'event': 'on_custom_event', 'run_id': 'fd6fb7a7-dd37-4191-962c-e43e245909f6', 'name': 'event1', 'tags': [], 'metadata': {}, 'data': {'x': 1}, 'parent_ids': []}
{'event': 'on_custom_event', 'run_id': 'fd6fb7a7-dd37-4191-962c-e43e245909f6', 'name': 'event2', 'tags': [], 'metadata': {}, 'data': {'x': 1}, 'parent_ids': []}
{'event': 'on_tool_end', 'data': {'output': 2}, 'run_id': 'fd6fb7a7-dd37-4191-962c-e43e245909f6', 'name': 'foo', 'tags': [], 'metadata': {}, 'parent_ids': []}
```
```python
from langchain_core.callbacks import adispatch_custom_event
from langchain_core.runnables import RunnableLambda
@RunnableLambda
async def foo(x: int) -> int:
"""Foo"""
await adispatch_custom_event("event1", {"x": x})
await adispatch_custom_event("event2", {"x": x})
return x + 1
async for event in foo.astream_events(1, version='v2'):
print(event)
```
```python
{'event': 'on_chain_start', 'data': {'input': 1}, 'name': 'foo', 'tags': [], 'run_id': 'ce2beef2-8608-49ea-8eba-537bdaafb8ec', 'metadata': {}, 'parent_ids': []}
{'event': 'on_custom_event', 'run_id': 'ce2beef2-8608-49ea-8eba-537bdaafb8ec', 'name': 'event1', 'tags': [], 'metadata': {}, 'data': {'x': 1}, 'parent_ids': []}
{'event': 'on_custom_event', 'run_id': 'ce2beef2-8608-49ea-8eba-537bdaafb8ec', 'name': 'event2', 'tags': [], 'metadata': {}, 'data': {'x': 1}, 'parent_ids': []}
{'event': 'on_chain_stream', 'run_id': 'ce2beef2-8608-49ea-8eba-537bdaafb8ec', 'name': 'foo', 'tags': [], 'metadata': {}, 'data': {'chunk': 2}, 'parent_ids': []}
{'event': 'on_chain_end', 'data': {'output': 2}, 'run_id': 'ce2beef2-8608-49ea-8eba-537bdaafb8ec', 'name': 'foo', 'tags': [], 'metadata': {}, 'parent_ids': []}
```
# Examples with handlers
This is copy pasted from unit tests
```python
class CustomCallbackManager(BaseCallbackHandler):
def __init__(self) -> None:
self.events: List[Any] = []
def on_custom_event(
self,
name: str,
data: Any,
*,
run_id: UUID,
tags: Optional[List[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> None:
assert kwargs == {}
self.events.append(
(
name,
data,
run_id,
tags,
metadata,
)
)
callback = CustomCallbackManager()
run_id = uuid.UUID(int=7)
@RunnableLambda
def foo(x: int, config: RunnableConfig) -> int:
dispatch_custom_event("event1", {"x": x})
dispatch_custom_event("event2", {"x": x}, config=config)
return x
foo.invoke(1, {"callbacks": [callback], "run_id": run_id})
assert callback.events == [
("event1", {"x": 1}, UUID("00000000-0000-0000-0000-000000000007"), [], {}),
("event2", {"x": 1}, UUID("00000000-0000-0000-0000-000000000007"), [], {}),
]
```
2024-07-11 02:25:12 +00:00
Erick Friis
6ea6f9f7bc
core: release 0.2.13 ( #24096 )
2024-07-10 16:39:15 -07:00
ccurme
975b6129f6
core[patch]: support conversion of runnables to tools ( #23992 )
...
Open to other thoughts on UX.
string input:
```python
as_tool = retriever.as_tool()
as_tool.invoke("cat") # [Document(...), ...]
```
typed dict input:
```python
class Args(TypedDict):
key: int
def f(x: Args) -> str:
return str(x["key"] * 2)
as_tool = RunnableLambda(f).as_tool(
name="my tool",
description="description", # name, description are inferred if not supplied
)
as_tool.invoke({"key": 3}) # "6"
```
for untyped dict input, allow specification of parameters + types
```python
def g(x: Dict[str, Any]) -> str:
return str(x["key"] * 2)
as_tool = RunnableLambda(g).as_tool(arg_types={"key": int})
result = as_tool.invoke({"key": 3}) # "6"
```
Passing the `arg_types` is slightly awkward but necessary to ensure tool
calls populate parameters correctly:
```python
from typing import Any, Dict
from langchain_core.runnables import RunnableLambda
from langchain_openai import ChatOpenAI
def f(x: Dict[str, Any]) -> str:
return str(x["key"] * 2)
runnable = RunnableLambda(f)
as_tool = runnable.as_tool(arg_types={"key": int})
llm = ChatOpenAI().bind_tools([as_tool])
result = llm.invoke("Use the tool on 3.")
tool_call = result.tool_calls[0]
args = tool_call["args"]
assert args == {"key": 3}
as_tool.run(args)
```
Contrived (?) example with langgraph agent as a tool:
```python
from typing import List, Literal
from typing_extensions import TypedDict
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
llm = ChatOpenAI(temperature=0)
def magic_function(input: int) -> int:
"""Applies a magic function to an input."""
return input + 2
agent_1 = create_react_agent(llm, [magic_function])
class Message(TypedDict):
role: Literal["human"]
content: str
agent_tool = agent_1.as_tool(
arg_types={"messages": List[Message]},
name="Jeeves",
description="Ask Jeeves.",
)
agent_2 = create_react_agent(llm, [agent_tool])
```
---------
Co-authored-by: Erick Friis <erick@langchain.dev>
2024-07-10 19:29:59 -04:00
Bagatur
6928f4c438
core[minor]: Add ToolMessage.raw_output ( #23994 )
...
Decisions to discuss:
1. is a new attr needed or could additional_kwargs be used for this
2. is raw_output a good name for this attr
3. should raw_output default to {} or None
4. should raw_output be included in serialization
5. do we need to update repr/str to exclude raw_output
2024-07-10 20:11:10 +00:00