Compare commits

...

8 Commits

Author SHA1 Message Date
isaac hershenson
d3024f9298 llm fix 2024-07-29 08:16:46 -07:00
isaac hershenson
8395c28dbd Revert "llm fix"
This reverts commit 1f364dcd81.
2024-07-29 08:15:41 -07:00
isaac hershenson
1f364dcd81 llm fix 2024-07-29 08:13:46 -07:00
isaac hershenson
eed9a19c2b first draft 2024-07-26 18:27:07 -07:00
Eugene Yurtsev
9be6b5a20f core[patch]: Correct doc-string for InMemoryRateLimiter (#24730)
Correct the documentaiton string.
2024-07-26 22:17:22 +00:00
Erick Friis
d5b4b7e05c infra: langchain max python 3.11 for resolution (#24729) 2024-07-26 21:17:11 +00:00
Erick Friis
3c3d3e9579 infra: community max python 3.11 for resolution (#24728) 2024-07-26 21:10:14 +00:00
Cristi Burcă
174e7d2ab2 langchain: Make OutputFixingParser.from_llm() create a useable retry chain (#24687)
Description: OutputFixingParser.from_llm() creates a retry chain that
returns a Generation instance, when it should actually just return a
string.
Issue: https://github.com/langchain-ai/langchain/issues/24600
Twitter handle: scribu

---------

Co-authored-by: isaac hershenson <ihershenson@hmc.edu>
2024-07-26 13:55:47 -07:00
7 changed files with 217 additions and 140 deletions

View File

@@ -95,6 +95,11 @@ def _get_configs_for_single_dir(job: str, dir_: str) -> List[Dict[str, str]]:
# declare deps in funny way
max_python = "3.11"
if dir_ in ["libs/community", "libs/langchain"] and job == "extended-tests":
# community extended test resolution in 3.12 is slow
# even in uv
max_python = "3.11"
return [
{"working-directory": dir_, "python-version": min_python},
{"working-directory": dir_, "python-version": max_python},

File diff suppressed because one or more lines are too long

View File

@@ -105,23 +105,31 @@ class InMemoryRateLimiter(BaseRateLimiter):
.. code-block:: python
from langchain_core import InMemoryRateLimiter
import time
from langchain_core.runnables import RunnableLambda, InMemoryRateLimiter
from langchain_core.rate_limiters import InMemoryRateLimiter
rate_limiter = InMemoryRateLimiter(
requests_per_second=100, check_every_n_seconds=0.1, max_bucket_size=10
requests_per_second=0.1, # <-- Can only make a request once every 10 seconds!!
check_every_n_seconds=0.1, # Wake up every 100 ms to check whether allowed to make a request,
max_bucket_size=10, # Controls the maximum burst size.
)
def foo(x: int) -> int:
return x
from langchain_anthropic import ChatAnthropic
model = ChatAnthropic(
model_name="claude-3-opus-20240229",
rate_limiter=rate_limiter
)
for _ in range(5):
tic = time.time()
model.invoke("hello")
toc = time.time()
print(toc - tic)
foo_ = RunnableLambda(foo)
chain = rate_limiter | foo_
assert chain.invoke(1) == 1
.. versionadded:: 0.2.24
"""
""" # noqa: E501
def __init__(
self,

View File

@@ -3,10 +3,9 @@ from __future__ import annotations
from typing import Any, TypeVar, Union
from langchain_core.exceptions import OutputParserException
from langchain_core.language_models import BaseLanguageModel
from langchain_core.output_parsers import BaseOutputParser
from langchain_core.output_parsers import BaseOutputParser, StrOutputParser
from langchain_core.prompts import BasePromptTemplate
from langchain_core.runnables import RunnableSerializable
from langchain_core.runnables import Runnable, RunnableSerializable
from typing_extensions import TypedDict
from langchain.output_parsers.prompts import NAIVE_FIX_PROMPT
@@ -42,7 +41,7 @@ class OutputFixingParser(BaseOutputParser[T]):
@classmethod
def from_llm(
cls,
llm: BaseLanguageModel,
llm: Runnable,
parser: BaseOutputParser[T],
prompt: BasePromptTemplate = NAIVE_FIX_PROMPT,
max_retries: int = 1,
@@ -58,7 +57,7 @@ class OutputFixingParser(BaseOutputParser[T]):
Returns:
OutputFixingParser
"""
chain = prompt | llm
chain = prompt | llm | StrOutputParser()
return cls(parser=parser, retry_chain=chain, max_retries=max_retries)
def parse(self, completion: str) -> T:

View File

@@ -4,7 +4,7 @@ from typing import Any, TypeVar, Union
from langchain_core.exceptions import OutputParserException
from langchain_core.language_models import BaseLanguageModel
from langchain_core.output_parsers import BaseOutputParser
from langchain_core.output_parsers import BaseOutputParser, StrOutputParser
from langchain_core.prompt_values import PromptValue
from langchain_core.prompts import BasePromptTemplate, PromptTemplate
from langchain_core.runnables import RunnableSerializable
@@ -82,7 +82,7 @@ class RetryOutputParser(BaseOutputParser[T]):
Returns:
RetryOutputParser
"""
chain = prompt | llm
chain = prompt | llm | StrOutputParser()
return cls(parser=parser, retry_chain=chain, max_retries=max_retries)
def parse_with_prompt(self, completion: str, prompt_value: PromptValue) -> T:

View File

@@ -3,6 +3,7 @@ from typing import Any, Callable, Dict, Optional, TypeVar
import pytest
from langchain_core.exceptions import OutputParserException
from langchain_core.messages import AIMessage
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.runnables import Runnable, RunnableLambda, RunnablePassthrough
from pytest_mock import MockerFixture
@@ -63,6 +64,22 @@ def test_output_fixing_parser_parse(
# TODO: test whether "instructions" is passed to the retry_chain
def test_output_fixing_parser_from_llm() -> None:
def fake_llm(prompt: str) -> AIMessage:
return AIMessage("2024-07-08T00:00:00.000000Z")
llm = RunnableLambda(fake_llm)
n = 1
parser = OutputFixingParser.from_llm(
llm=llm,
parser=DatetimeOutputParser(),
max_retries=n,
)
assert parser.parse("not a date")
@pytest.mark.parametrize(
"base_parser",
[

View File

@@ -305,9 +305,7 @@ class OllamaLLM(BaseLLM):
if not isinstance(stream_resp, str):
chunk = GenerationChunk(
text=(
stream_resp["message"]["content"]
if "message" in stream_resp
else ""
stream_resp.get("response","")
),
generation_info=(
dict(stream_resp) if stream_resp.get("done") is True else None
@@ -331,9 +329,7 @@ class OllamaLLM(BaseLLM):
if not isinstance(stream_resp, str):
chunk = GenerationChunk(
text=(
stream_resp["message"]["content"]
if "message" in stream_resp
else ""
stream_resp.get("response","")
),
generation_info=(
dict(stream_resp) if stream_resp.get("done") is True else None