Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
baur-krykpayev authored Jun 21, 2024
2 parents c36c4b3 + 4c97a9e commit 57a3a55
Show file tree
Hide file tree
Showing 15 changed files with 745 additions and 104 deletions.
120 changes: 120 additions & 0 deletions docs/scripts/create_chat_model_docstring_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
imperative = [
[
"invoke",
"str | List[dict | tuple | BaseMessage] | PromptValue",
"BaseMessage",
"A single chat model call.",
],
[
"ainvoke",
"'''",
"BaseMessage",
"Defaults to running invoke in an async executor.",
],
[
"stream",
"'''",
"Iterator[BaseMessageChunk]",
"Defaults to yielding output of invoke.",
],
[
"astream",
"'''",
"AsyncIterator[BaseMessageChunk]",
"Defaults to yielding output of ainvoke.",
],
[
"astream_events",
"'''",
"AsyncIterator[StreamEvent]",
"Event types: 'on_chat_model_start', 'on_chat_model_stream', 'on_chat_model_end'.",
],
[
"batch",
"List[''']",
"List[BaseMessage]",
"Defaults to running invoke in concurrent threads.",
],
[
"abatch",
"List[''']",
"List[BaseMessage]",
"Defaults to running ainvoke in concurrent threads.",
],
[
"batch_as_completed",
"List[''']",
"Iterator[Tuple[int, Union[BaseMessage, Exception]]]",
"Defaults to running invoke in concurrent threads.",
],
[
"abatch_as_completed",
"List[''']",
"AsyncIterator[Tuple[int, Union[BaseMessage, Exception]]]",
"Defaults to running ainvoke in concurrent threads.",
],
]
declarative = [
[
"bind_tools",
# "Tools, ...",
# "Runnable with same inputs/outputs as ChatModel",
"Create ChatModel that can call tools.",
],
[
"with_structured_output",
# "An output schema, ...",
# "Runnable that takes ChatModel inputs and returns a dict or Pydantic object",
"Create wrapper that structures model output using schema.",
],
[
"with_retry",
# "Max retries, exceptions to handle, ...",
# "Runnable with same inputs/outputs as ChatModel",
"Create wrapper that retries model calls on failure.",
],
[
"with_fallbacks",
# "List of models to fall back on",
# "Runnable with same inputs/outputs as ChatModel",
"Create wrapper that falls back to other models on failure.",
],
[
"configurable_fields",
# "*ConfigurableField",
# "Runnable with same inputs/outputs as ChatModel",
"Specify init args of the model that can be configured at runtime via the RunnableConfig.",
],
[
"configurable_alternatives",
# "ConfigurableField, ...",
# "Runnable with same inputs/outputs as ChatModel",
"Specify alternative models which can be swapped in at runtime via the RunnableConfig.",
],
]


def create_table(to_build: list) -> str:
for x in to_build:
x[0] = "`" + x[0] + "`"
longest = [max(len(x[i]) for x in to_build) for i in range(len(to_build[0]))]
widths = [int(1.2 * col) for col in longest]
headers = (
["Method", "Input", "Output", "Description"]
if len(widths) == 4
else ["Method", "Description"]
)
rows = [[h + " " * (w - len(h)) for w, h in zip(widths, headers)]]
for x in to_build:
rows.append([y + " " * (w - len(y)) for w, y in zip(widths, x)])

table = [" | ".join(([""] + x + [""])).strip() for x in rows]
lines = [
"+".join(([""] + ["-" * (len(y) + 2) for y in x] + [""])).strip() for x in rows
]
lines[1] = lines[1].replace("-", "=")
lines.append(lines[-1])
rst = lines[0]
for r, li in zip(table, lines[1:]):
rst += "\n" + r + "\n" + li
return rst
17 changes: 5 additions & 12 deletions libs/community/langchain_community/chat_message_histories/sql.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import contextlib
import json
import logging
Expand Down Expand Up @@ -252,17 +251,11 @@ async def aadd_message(self, message: BaseMessage) -> None:
await session.commit()

def add_messages(self, messages: Sequence[BaseMessage]) -> None:
# The method RunnableWithMessageHistory._exit_history() call
# add_message method by mistake and not aadd_message.
# See https://github.com/langchain-ai/langchain/issues/22021
if self.async_mode:
loop = asyncio.get_event_loop()
loop.run_until_complete(self.aadd_messages(messages))
else:
with self._make_sync_session() as session:
for message in messages:
session.add(self.converter.to_sql_model(message, self.session_id))
session.commit()
# Add all messages in one transaction
with self._make_sync_session() as session:
for message in messages:
session.add(self.converter.to_sql_model(message, self.session_id))
session.commit()

async def aadd_messages(self, messages: Sequence[BaseMessage]) -> None:
# Add all messages in one transaction
Expand Down
Loading

0 comments on commit 57a3a55

Please sign in to comment.