livekit-plugins-langchain 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of livekit-plugins-langchain might be problematic. Click here for more details.
- livekit/plugins/langchain/__init__.py +42 -0
- livekit/plugins/langchain/langgraph.py +140 -0
- livekit/plugins/langchain/py.typed +0 -0
- livekit/plugins/langchain/version.py +15 -0
- livekit_plugins_langchain-1.1.0.dist-info/METADATA +61 -0
- livekit_plugins_langchain-1.1.0.dist-info/RECORD +7 -0
- livekit_plugins_langchain-1.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Copyright 2025 LiveKit, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""LMNT plugin for LiveKit Agents
|
|
16
|
+
|
|
17
|
+
See https://docs.livekit.io/agents/integrations/llm/langchain/ for more information.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from .langgraph import LangGraphStream, LLMAdapter
|
|
21
|
+
from .version import __version__
|
|
22
|
+
|
|
23
|
+
__all__ = ["__version__", "LLMAdapter", "LangGraphStream"]
|
|
24
|
+
|
|
25
|
+
from livekit.agents import Plugin
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class LangChainPlugin(Plugin):
|
|
29
|
+
def __init__(self) -> None:
|
|
30
|
+
super().__init__(__name__, __version__, __package__)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
Plugin.register_plugin(LangChainPlugin())
|
|
34
|
+
|
|
35
|
+
# Cleanup docs of unexported modules
|
|
36
|
+
_module = dir()
|
|
37
|
+
NOT_IN_ALL = [m for m in _module if m not in __all__]
|
|
38
|
+
|
|
39
|
+
__pdoc__ = {}
|
|
40
|
+
|
|
41
|
+
for n in NOT_IN_ALL:
|
|
42
|
+
__pdoc__[n] = False
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Copyright 2025 LiveKit, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
from langchain_core.messages import AIMessage, BaseMessageChunk, HumanMessage, SystemMessage
|
|
20
|
+
from langchain_core.runnables import RunnableConfig
|
|
21
|
+
from langgraph.pregel.protocol import PregelProtocol
|
|
22
|
+
|
|
23
|
+
from livekit.agents import llm, utils
|
|
24
|
+
from livekit.agents.llm import ToolChoice
|
|
25
|
+
from livekit.agents.llm.chat_context import ChatContext, ChatMessage
|
|
26
|
+
from livekit.agents.llm.tool_context import FunctionTool, RawFunctionTool
|
|
27
|
+
from livekit.agents.types import (
|
|
28
|
+
DEFAULT_API_CONNECT_OPTIONS,
|
|
29
|
+
NOT_GIVEN,
|
|
30
|
+
APIConnectOptions,
|
|
31
|
+
NotGivenOr,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class LLMAdapter(llm.LLM):
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
graph: PregelProtocol,
|
|
39
|
+
*,
|
|
40
|
+
config: RunnableConfig | None = None,
|
|
41
|
+
) -> None:
|
|
42
|
+
super().__init__()
|
|
43
|
+
self._graph = graph
|
|
44
|
+
self._config = config
|
|
45
|
+
|
|
46
|
+
def chat(
|
|
47
|
+
self,
|
|
48
|
+
*,
|
|
49
|
+
chat_ctx: ChatContext,
|
|
50
|
+
tools: list[FunctionTool | RawFunctionTool] | None = None,
|
|
51
|
+
conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
|
|
52
|
+
# these are unused, since tool execution takes place in langgraph
|
|
53
|
+
parallel_tool_calls: NotGivenOr[bool] = NOT_GIVEN,
|
|
54
|
+
tool_choice: NotGivenOr[ToolChoice] = NOT_GIVEN,
|
|
55
|
+
extra_kwargs: NotGivenOr[dict[str, Any]] = NOT_GIVEN,
|
|
56
|
+
) -> LangGraphStream:
|
|
57
|
+
return LangGraphStream(
|
|
58
|
+
self,
|
|
59
|
+
chat_ctx=chat_ctx,
|
|
60
|
+
tools=tools or [],
|
|
61
|
+
graph=self._graph,
|
|
62
|
+
conn_options=conn_options,
|
|
63
|
+
config=self._config,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class LangGraphStream(llm.LLMStream):
|
|
68
|
+
def __init__(
|
|
69
|
+
self,
|
|
70
|
+
llm: LLMAdapter,
|
|
71
|
+
*,
|
|
72
|
+
chat_ctx: ChatContext,
|
|
73
|
+
tools: list[FunctionTool | RawFunctionTool],
|
|
74
|
+
conn_options: APIConnectOptions,
|
|
75
|
+
graph: PregelProtocol,
|
|
76
|
+
config: RunnableConfig | None = None,
|
|
77
|
+
):
|
|
78
|
+
super().__init__(
|
|
79
|
+
llm,
|
|
80
|
+
chat_ctx=chat_ctx,
|
|
81
|
+
tools=tools,
|
|
82
|
+
conn_options=conn_options,
|
|
83
|
+
)
|
|
84
|
+
self._graph = graph
|
|
85
|
+
self._config = config
|
|
86
|
+
|
|
87
|
+
async def _run(self) -> None:
|
|
88
|
+
state = self._chat_ctx_to_state()
|
|
89
|
+
|
|
90
|
+
async for message_chunk, _ in self._graph.astream(
|
|
91
|
+
state,
|
|
92
|
+
self._config,
|
|
93
|
+
stream_mode="messages",
|
|
94
|
+
):
|
|
95
|
+
chat_chunk = _to_chat_chunk(message_chunk)
|
|
96
|
+
if chat_chunk:
|
|
97
|
+
self._event_ch.send_nowait(chat_chunk)
|
|
98
|
+
|
|
99
|
+
def _chat_ctx_to_state(self) -> dict[str, Any]:
|
|
100
|
+
"""Convert chat context to langgraph input"""
|
|
101
|
+
|
|
102
|
+
messages: list[AIMessage | HumanMessage | SystemMessage] = []
|
|
103
|
+
for item in self._chat_ctx.items:
|
|
104
|
+
# only support chat messages, ignoring tool calls
|
|
105
|
+
if isinstance(item, ChatMessage):
|
|
106
|
+
content = item.text_content
|
|
107
|
+
if content:
|
|
108
|
+
if item.role == "assistant":
|
|
109
|
+
messages.append(AIMessage(content=content))
|
|
110
|
+
elif item.role == "user":
|
|
111
|
+
messages.append(HumanMessage(content=content))
|
|
112
|
+
elif item.role in ["system", "developer"]:
|
|
113
|
+
messages.append(SystemMessage(content=content))
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
"messages": messages,
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _to_chat_chunk(msg: str | Any) -> llm.ChatChunk | None:
|
|
121
|
+
message_id = utils.shortuuid("LC_")
|
|
122
|
+
content: str | None = None
|
|
123
|
+
|
|
124
|
+
if isinstance(msg, str):
|
|
125
|
+
content = msg
|
|
126
|
+
elif isinstance(msg, BaseMessageChunk):
|
|
127
|
+
content = msg.text()
|
|
128
|
+
if msg.id:
|
|
129
|
+
message_id = msg.id
|
|
130
|
+
|
|
131
|
+
if not content:
|
|
132
|
+
return None
|
|
133
|
+
|
|
134
|
+
return llm.ChatChunk(
|
|
135
|
+
id=message_id,
|
|
136
|
+
delta=llm.ChoiceDelta(
|
|
137
|
+
role="assistant",
|
|
138
|
+
content=content,
|
|
139
|
+
),
|
|
140
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Copyright 2025 LiveKit, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
__version__ = "1.1.0"
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: livekit-plugins-langchain
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: LangChain/LangGraph plugin for LiveKit agents
|
|
5
|
+
Project-URL: Documentation, https://docs.livekit.io
|
|
6
|
+
Project-URL: Website, https://livekit.io/
|
|
7
|
+
Project-URL: Source, https://github.com/livekit/agents
|
|
8
|
+
Author-email: LiveKit <hello@livekit.io>
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
Keywords: LangChain,LangGraph,agents,livekit
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
19
|
+
Requires-Python: >=3.9.0
|
|
20
|
+
Requires-Dist: langchain-core>=0.3.0
|
|
21
|
+
Requires-Dist: langgraph>=0.3.0
|
|
22
|
+
Requires-Dist: livekit-agents>=1.1.0
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# LangChain plugin for LiveKit Agents
|
|
26
|
+
|
|
27
|
+
This plugin integrates capabilites from LangChain within LiveKit Agents
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install livekit-plugins-langchain
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Using LangGraph workflows
|
|
38
|
+
|
|
39
|
+
You can bring over any existing workflow in LangGraph as an Agents LLM with `langchain.LLMAdapter`. For example:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from langgraph.graph import StateGraph
|
|
43
|
+
from livekit.agents import Agent, AgentSession, JobContext
|
|
44
|
+
from livekit.plugins import langchain
|
|
45
|
+
|
|
46
|
+
...
|
|
47
|
+
|
|
48
|
+
def entrypoint(ctx: JobContext):
|
|
49
|
+
graph = StateGraph(...).build()
|
|
50
|
+
|
|
51
|
+
session = AgentSession(
|
|
52
|
+
vad=...,
|
|
53
|
+
stt=...,
|
|
54
|
+
tts=...,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
await session.start(
|
|
58
|
+
agent=Agent(llm=langchain.LLMAdapter(graph)),
|
|
59
|
+
)
|
|
60
|
+
...
|
|
61
|
+
```
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
livekit/plugins/langchain/__init__.py,sha256=IWe25ou2cWD3Ql7xQlgUGe2-BrvKGr5B6qg-yncrglk,1221
|
|
2
|
+
livekit/plugins/langchain/langgraph.py,sha256=MYC4uiPcBMIIECZhXx1gKXGIYupRhHUQIN3CuvG7Dx8,4372
|
|
3
|
+
livekit/plugins/langchain/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
livekit/plugins/langchain/version.py,sha256=lFVJxRoJYH4JZgg7iJQF0_dlrbfVbRYYWQzbHnv_p-w,600
|
|
5
|
+
livekit_plugins_langchain-1.1.0.dist-info/METADATA,sha256=euWGyAvC9jWgQwF1oxiU8a1RUtli2YOzhtNZzAAjs_0,1733
|
|
6
|
+
livekit_plugins_langchain-1.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
+
livekit_plugins_langchain-1.1.0.dist-info/RECORD,,
|